ManagerTestCase.php 8.06 KB
Newer Older
Alexander Kochetov committed
1 2 3 4 5 6 7 8
<?php

namespace yiiunit\framework\rbac;

use yii\rbac\Assignment;
use yii\rbac\Item;
use yiiunit\TestCase;

Alexander Makarov committed
9
abstract class ManagerTestCase extends TestCase
Alexander Kochetov committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
{
	/** @var \yii\rbac\PhpManager|\yii\rbac\DbManager */
	protected $auth;

	public function testCreateItem()
	{
		$type = Item::TYPE_TASK;
		$name = 'editUser';
		$description = 'edit a user';
		$bizRule = 'checkUserIdentity()';
		$data = array(1, 2, 3);
		$item = $this->auth->createItem($name, $type, $description, $bizRule, $data);
		$this->assertTrue($item instanceof Item);
		$this->assertEquals($item->type, $type);
		$this->assertEquals($item->name, $name);
		$this->assertEquals($item->description, $description);
		$this->assertEquals($item->bizRule, $bizRule);
		$this->assertEquals($item->data, $data);

		// test shortcut
		$name2 = 'createUser';
		$item2 = $this->auth->createRole($name2, $description, $bizRule, $data);
		$this->assertEquals($item2->type, Item::TYPE_ROLE);

		// test adding an item with the same name
35
		$this->setExpectedException('\yii\base\Exception');
Alexander Kochetov committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
		$this->auth->createItem($name, $type, $description, $bizRule, $data);
	}

	public function testGetItem()
	{
		$this->assertTrue($this->auth->getItem('readPost') instanceof Item);
		$this->assertTrue($this->auth->getItem('reader') instanceof Item);
		$this->assertNull($this->auth->getItem('unknown'));
	}

	public function testRemoveAuthItem()
	{
		$this->assertTrue($this->auth->getItem('updatePost') instanceof Item);
		$this->assertTrue($this->auth->removeItem('updatePost'));
		$this->assertNull($this->auth->getItem('updatePost'));
		$this->assertFalse($this->auth->removeItem('updatePost'));
	}

	public function testChangeItemName()
	{
		$item = $this->auth->getItem('readPost');
		$this->assertTrue($item instanceof Item);
		$this->assertTrue($this->auth->hasItemChild('reader', 'readPost'));
		$item->name = 'readPost2';
60
		$item->save();
Alexander Kochetov committed
61 62 63 64 65 66 67 68 69 70 71
		$this->assertNull($this->auth->getItem('readPost'));
		$this->assertEquals($this->auth->getItem('readPost2'), $item);
		$this->assertFalse($this->auth->hasItemChild('reader', 'readPost'));
		$this->assertTrue($this->auth->hasItemChild('reader', 'readPost2'));
	}

	public function testAddItemChild()
	{
		$this->auth->addItemChild('createPost', 'updatePost');

		// test adding upper level item to lower one
72
		$this->setExpectedException('\yii\base\Exception');
Alexander Kochetov committed
73 74 75 76 77 78
		$this->auth->addItemChild('readPost', 'reader');
	}

	public function testAddItemChild2()
	{
		// test adding inexistent items
79
		$this->setExpectedException('\yii\base\Exception');
Alexander Kochetov committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
		$this->assertFalse($this->auth->addItemChild('createPost2', 'updatePost'));
	}

	public function testRemoveItemChild()
	{
		$this->assertTrue($this->auth->hasItemChild('reader', 'readPost'));
		$this->assertTrue($this->auth->removeItemChild('reader', 'readPost'));
		$this->assertFalse($this->auth->hasItemChild('reader', 'readPost'));
		$this->assertFalse($this->auth->removeItemChild('reader', 'readPost'));
	}

	public function testGetItemChildren()
	{
		$this->assertEquals(array(), $this->auth->getItemChildren('readPost'));
		$children = $this->auth->getItemChildren('author');
		$this->assertEquals(3, count($children));
		$this->assertTrue(reset($children) instanceof Item);
	}

	public function testAssign()
	{
		$auth = $this->auth->assign('new user', 'createPost', 'rule', 'data');
		$this->assertTrue($auth instanceof Assignment);
		$this->assertEquals($auth->userId, 'new user');
		$this->assertEquals($auth->itemName, 'createPost');
		$this->assertEquals($auth->bizRule, 'rule');
		$this->assertEquals($auth->data, 'data');

108
		$this->setExpectedException('\yii\base\Exception');
Alexander Kochetov committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
		$this->auth->assign('new user', 'createPost2', 'rule', 'data');
	}

	public function testRevoke()
	{
		$this->assertTrue($this->auth->isAssigned('author B', 'author'));
		$auth = $this->auth->getAssignment('author B', 'author');
		$this->assertTrue($auth instanceof Assignment);
		$this->assertTrue($this->auth->revoke('author B', 'author'));
		$this->assertFalse($this->auth->isAssigned('author B', 'author'));
		$this->assertFalse($this->auth->revoke('author B', 'author'));
	}

	public function testGetAssignments()
	{
		$this->auth->assign('author B', 'deletePost');
		$auths = $this->auth->getAssignments('author B');
		$this->assertEquals(2, count($auths));
		$this->assertTrue(reset($auths) instanceof Assignment);
	}

	public function testGetItems()
	{
		$this->assertEquals(count($this->auth->getRoles()), 4);
		$this->assertEquals(count($this->auth->getOperations()), 4);
		$this->assertEquals(count($this->auth->getTasks()), 1);
		$this->assertEquals(count($this->auth->getItems()), 9);

		$this->assertEquals(count($this->auth->getItems('author B', null)), 1);
		$this->assertEquals(count($this->auth->getItems('author C', null)), 0);
		$this->assertEquals(count($this->auth->getItems('author B', Item::TYPE_ROLE)), 1);
		$this->assertEquals(count($this->auth->getItems('author B', Item::TYPE_OPERATION)), 0);
	}

	public function testClearAll()
	{
		$this->auth->clearAll();
		$this->assertEquals(count($this->auth->getRoles()), 0);
		$this->assertEquals(count($this->auth->getOperations()), 0);
		$this->assertEquals(count($this->auth->getTasks()), 0);
		$this->assertEquals(count($this->auth->getItems()), 0);
		$this->assertEquals(count($this->auth->getAssignments('author B')), 0);
	}

	public function testClearAssignments()
	{
		$this->auth->clearAssignments();
		$this->assertEquals(count($this->auth->getAssignments('author B')), 0);
	}

	public function testDetectLoop()
	{
161
		$this->setExpectedException('\yii\base\Exception');
Alexander Kochetov committed
162 163 164 165 166 167
		$this->auth->addItemChild('readPost', 'readPost');
	}

	public function testExecuteBizRule()
	{
		$this->assertTrue($this->auth->executeBizRule(null, array(), null));
resurtm committed
168 169
		$this->assertTrue($this->auth->executeBizRule('return 1 == true;', array(), null));
		$this->assertTrue($this->auth->executeBizRule('return $params[0] == $params[1];', array(1, '1'), null));
170
		$this->assertFalse($this->auth->executeBizRule('invalid;', array(), null));
Alexander Kochetov committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
	}

	public function testCheckAccess()
	{
		$results = array(
			'reader A' => array(
				'createPost' => false,
				'readPost' => true,
				'updatePost' => false,
				'updateOwnPost' => false,
				'deletePost' => false,
			),
			'author B' => array(
				'createPost' => true,
				'readPost' => true,
				'updatePost' => true,
				'updateOwnPost' => true,
				'deletePost' => false,
			),
			'editor C' => array(
				'createPost' => false,
				'readPost' => true,
				'updatePost' => true,
				'updateOwnPost' => false,
				'deletePost' => false,
			),
			'admin D' => array(
				'createPost' => true,
				'readPost' => true,
				'updatePost' => true,
				'updateOwnPost' => false,
				'deletePost' => true,
			),
		);

		$params = array('authorID' => 'author B');

		foreach (array('reader A', 'author B', 'editor C', 'admin D') as $user) {
			$params['userID'] = $user;
			foreach (array('createPost', 'readPost', 'updatePost', 'updateOwnPost', 'deletePost') as $operation) {
				$result = $this->auth->checkAccess($user, $operation, $params);
				$this->assertEquals($results[$user][$operation], $result);
			}
		}
	}

	protected function prepareData()
	{
		$this->auth->createOperation('createPost', 'create a post');
		$this->auth->createOperation('readPost', 'read a post');
		$this->auth->createOperation('updatePost', 'update a post');
		$this->auth->createOperation('deletePost', 'delete a post');

resurtm committed
224
		$task = $this->auth->createTask('updateOwnPost', 'update a post by author himself', 'return $params["authorID"] == $params["userID"];');
Alexander Kochetov committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
		$task->addChild('updatePost');

		$role = $this->auth->createRole('reader');
		$role->addChild('readPost');

		$role = $this->auth->createRole('author');
		$role->addChild('reader');
		$role->addChild('createPost');
		$role->addChild('updateOwnPost');

		$role = $this->auth->createRole('editor');
		$role->addChild('reader');
		$role->addChild('updatePost');

		$role = $this->auth->createRole('admin');
		$role->addChild('editor');
		$role->addChild('author');
		$role->addChild('deletePost');

		$this->auth->assign('reader A', 'reader');
		$this->auth->assign('author B', 'author');
		$this->auth->assign('editor C', 'editor');
		$this->auth->assign('admin D', 'admin');
	}
}