ManagerTestCase.php 8.36 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
{
	/** @var \yii\rbac\PhpManager|\yii\rbac\DbManager */
	protected $auth;

	public function testCreateItem()
	{
		$type = Item::TYPE_TASK;
		$name = 'editUser';
		$description = 'edit a user';
		$bizRule = 'checkUserIdentity()';
Alexander Makarov committed
20
		$data = [1, 2, 3];
Alexander Kochetov committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34
		$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
		$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()
	{
Alexander Makarov committed
93
		$this->assertEquals([], $this->auth->getItemChildren('readPost'));
Alexander Kochetov committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107
		$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
		$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'));
	}

122 123 124 125 126
	public function testRevokeAll()
	{
		$this->assertTrue($this->auth->revokeAll('reader E'));
		$this->assertFalse($this->auth->isAssigned('reader E', 'reader'));
	}
127

Alexander Kochetov committed
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 161 162 163 164 165 166
	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()
	{
167
		$this->setExpectedException('\yii\base\Exception');
Alexander Kochetov committed
168 169 170 171 172
		$this->auth->addItemChild('readPost', 'readPost');
	}

	public function testExecuteBizRule()
	{
Alexander Makarov committed
173 174 175 176
		$this->assertTrue($this->auth->executeBizRule(null, [], null));
		$this->assertTrue($this->auth->executeBizRule('return 1 == true;', [], null));
		$this->assertTrue($this->auth->executeBizRule('return $params[0] == $params[1];', [1, '1'], null));
		$this->assertFalse($this->auth->executeBizRule('invalid;', [], null));
Alexander Kochetov committed
177 178 179 180
	}

	public function testCheckAccess()
	{
Alexander Makarov committed
181 182
		$results = [
			'reader A' => [
Alexander Kochetov committed
183 184 185 186 187
				'createPost' => false,
				'readPost' => true,
				'updatePost' => false,
				'updateOwnPost' => false,
				'deletePost' => false,
Alexander Makarov committed
188 189
			],
			'author B' => [
Alexander Kochetov committed
190 191 192 193 194
				'createPost' => true,
				'readPost' => true,
				'updatePost' => true,
				'updateOwnPost' => true,
				'deletePost' => false,
Alexander Makarov committed
195 196
			],
			'editor C' => [
Alexander Kochetov committed
197 198 199 200 201
				'createPost' => false,
				'readPost' => true,
				'updatePost' => true,
				'updateOwnPost' => false,
				'deletePost' => false,
Alexander Makarov committed
202 203
			],
			'admin D' => [
Alexander Kochetov committed
204 205 206 207 208
				'createPost' => true,
				'readPost' => true,
				'updatePost' => true,
				'updateOwnPost' => false,
				'deletePost' => true,
Alexander Makarov committed
209
			],
210 211 212 213 214 215 216
			'reader E' => [
				'createPost' => false,
				'readPost' => false,
				'updatePost' => false,
				'updateOwnPost' => false,
				'deletePost' => false,
			],
Alexander Makarov committed
217
		];
Alexander Kochetov committed
218

Alexander Makarov committed
219
		$params = ['authorID' => 'author B'];
Alexander Kochetov committed
220

Alexander Makarov committed
221
		foreach (['reader A', 'author B', 'editor C', 'admin D'] as $user) {
Alexander Kochetov committed
222
			$params['userID'] = $user;
Alexander Makarov committed
223
			foreach (['createPost', 'readPost', 'updatePost', 'updateOwnPost', 'deletePost'] as $operation) {
Alexander Kochetov committed
224 225 226 227 228 229 230 231 232 233 234 235 236
				$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
237
		$task = $this->auth->createTask('updateOwnPost', 'update a post by author himself', 'return $params["authorID"] == $params["userID"];');
Alexander Kochetov committed
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
		$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');
261
		$this->auth->assign('reader E', 'reader');
Alexander Kochetov committed
262 263
	}
}