ManagerTestCase.php 8.95 KB
Newer Older
Alexander Kochetov committed
1 2 3 4 5
<?php

namespace yiiunit\framework\rbac;

use yii\rbac\Item;
6
use yii\rbac\Permission;
7
use yii\rbac\PhpManager;
8
use yii\rbac\Role;
Alexander Kochetov committed
9 10
use yiiunit\TestCase;

11 12 13
/**
 * ManagerTestCase
 */
Alexander Makarov committed
14
abstract class ManagerTestCase extends TestCase
Alexander Kochetov committed
15
{
16 17 18
    /**
     * @var \yii\rbac\ManagerInterface
     */
19 20
    protected $auth;

21 22 23 24 25
    /**
     * @return \yii\rbac\ManagerInterface
     */
    abstract protected function createManager();

26
    public function testCreateRole()
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    {
        $role = $this->auth->createRole('admin');
        $this->assertTrue($role instanceof Role);
        $this->assertEquals(Item::TYPE_ROLE, $role->type);
        $this->assertEquals('admin', $role->name);
    }

    public function testCreatePermission()
    {
        $permission = $this->auth->createPermission('edit post');
        $this->assertTrue($permission instanceof Permission);
        $this->assertEquals(Item::TYPE_PERMISSION, $permission->type);
        $this->assertEquals('edit post', $permission->name);
    }

    public function testAdd()
    {
        $role = $this->auth->createRole('admin');
        $role->description = 'administrator';
        $this->assertTrue($this->auth->add($role));

        $permission = $this->auth->createPermission('edit post');
        $permission->description = 'edit a post';
        $this->assertTrue($this->auth->add($permission));

        $rule = new AuthorRule(['name' => 'is author', 'reallyReally' => true]);
        $this->assertTrue($this->auth->add($rule));

        // todo: check duplication of name
    }
57 58 59 60 61 62 63 64 65 66 67 68

    public function testGetChildren()
    {
        $user = $this->auth->createRole('user');
        $this->auth->add($user);
        $this->assertCount(0, $this->auth->getChildren($user->name));

        $changeName = $this->auth->createPermission('changeName');
        $this->auth->add($changeName);
        $this->auth->addChild($user, $changeName);
        $this->assertCount(1, $this->auth->getChildren($user->name));
    }
69

70
    public function testGetRule()
71
    {
72 73
        $this->prepareData();

74 75 76 77 78 79 80 81
        $rule = $this->auth->getRule('isAuthor');
        $this->assertInstanceOf('yii\rbac\Rule', $rule);
        $this->assertEquals('isAuthor', $rule->name);

        $rule = $this->auth->getRule('nonExisting');
        $this->assertNull($rule);
    }

82
    public function testAddRule()
83
    {
84 85
        $this->prepareData();

86
        $ruleName = 'isReallyReallyAuthor';
87
        $rule = new AuthorRule(['name' => $ruleName, 'reallyReally' => true]);
88
        $this->auth->add($rule);
89 90 91 92

        $rule = $this->auth->getRule($ruleName);
        $this->assertEquals($ruleName, $rule->name);
        $this->assertEquals(true, $rule->reallyReally);
93
    }
94

95 96
    public function testUpdateRule()
    {
97 98
        $this->prepareData();

99 100
        $rule = $this->auth->getRule('isAuthor');
        $rule->name = "newName";
101
        $rule->reallyReally = false;
102
        $this->auth->update('isAuthor', $rule);
103

104 105 106 107 108
        $rule = $this->auth->getRule('isAuthor');
        $this->assertEquals(null, $rule);

        $rule = $this->auth->getRule('newName');
        $this->assertEquals("newName", $rule->name);
109
        $this->assertEquals(false, $rule->reallyReally);
110 111

        $rule->reallyReally = true;
112
        $this->auth->update('newName', $rule);
113 114 115

        $rule = $this->auth->getRule('newName');
        $this->assertEquals(true, $rule->reallyReally);
116 117 118 119
    }

    public function testGetRules()
    {
120 121
        $this->prepareData();

122
        $rule = new AuthorRule(['name' => 'isReallyReallyAuthor', 'reallyReally' => true]);
123
        $this->auth->add($rule);
124 125 126 127 128 129

        $rules = $this->auth->getRules();

        $ruleNames = [];
        foreach ($rules as $rule) {
            $ruleNames[] = $rule->name;
130
        }
131 132 133 134 135 136 137

        $this->assertContains('isReallyReallyAuthor', $ruleNames);
        $this->assertContains('isAuthor', $ruleNames);
    }

    public function testRemoveRule()
    {
138 139 140
        $this->prepareData();

        $this->auth->remove($this->auth->getRule('isAuthor'));
141 142 143 144 145
        $rules = $this->auth->getRules();

        $this->assertEmpty($rules);
    }

146 147
    public function testCheckAccess()
    {
148 149 150
        $this->prepareData();

        $testSuites = [
151 152 153 154
            'reader A' => [
                'createPost' => false,
                'readPost' => true,
                'updatePost' => false,
155
                'updateAnyPost' => false,
156 157 158 159 160
            ],
            'author B' => [
                'createPost' => true,
                'readPost' => true,
                'updatePost' => true,
161
                'updateAnyPost' => false,
162
            ],
163
            'admin C' => [
164 165 166
                'createPost' => true,
                'readPost' => true,
                'updatePost' => false,
167
                'updateAnyPost' => true,
168 169 170 171 172
            ],
        ];

        $params = ['authorID' => 'author B'];

173 174 175
        foreach ($testSuites as $user => $tests) {
            foreach ($tests as $permission => $result) {
                $this->assertEquals($result, $this->auth->checkAccess($user, $permission, $params), "Checking $user can $permission");
176 177 178 179 180 181
            }
        }
    }

    protected function prepareData()
    {
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
        $rule = new AuthorRule;
        $this->auth->add($rule);

        $createPost = $this->auth->createPermission('createPost');
        $createPost->description = 'create a post';
        $this->auth->add($createPost);

        $readPost = $this->auth->createPermission('readPost');
        $readPost->description = 'read a post';
        $this->auth->add($readPost);

        $updatePost = $this->auth->createPermission('updatePost');
        $updatePost->description = 'update a post';
        $updatePost->ruleName = $rule->name;
        $this->auth->add($updatePost);

        $updateAnyPost = $this->auth->createPermission('updateAnyPost');
        $updateAnyPost->description = 'update any post';
        $this->auth->add($updateAnyPost);

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

        $author = $this->auth->createRole('author');
        $this->auth->add($author);
        $this->auth->addChild($author, $createPost);
        $this->auth->addChild($author, $updatePost);
        $this->auth->addChild($author, $reader);

        $admin = $this->auth->createRole('admin');
        $this->auth->add($admin);
        $this->auth->addChild($admin, $author);
        $this->auth->addChild($admin, $updateAnyPost);

        $this->auth->assign($reader, 'reader A');
        $this->auth->assign($author, 'author B');
        $this->auth->assign($admin, 'admin C');
220
    }
tof06 committed
221 222 223 224 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

    public function testGetPermissionsByRole()
    {
        $this->prepareData();
        $roles = $this->auth->getPermissionsByRole('admin');
        $expectedPermissions = ['createPost', 'updatePost', 'readPost', 'updateAnyPost'];
        $this->assertEquals(count($roles), count($expectedPermissions));
        foreach ($expectedPermissions as $permission) {
            $this->assertTrue($roles[$permission] instanceof Permission);
        }
    }

    public function testGetPermissionsByUser()
    {
        $this->prepareData();
        $roles = $this->auth->getPermissionsByUser('author B');
        $expectedPermissions = ['createPost', 'updatePost', 'readPost'];
        $this->assertEquals(count($roles), count($expectedPermissions));
        foreach ($expectedPermissions as $permission) {
            $this->assertTrue($roles[$permission] instanceof Permission);
        }
    }

    public function testGetRolesByUser()
    {
        $this->prepareData();
        $roles = $this->auth->getRolesByUser('reader A');
        $this->assertTrue(reset($roles) instanceof Role);
        $this->assertEquals($roles['reader']->name, 'reader');
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    }

    public function testAssignMultipleRoles()
    {
        $this->prepareData();

        $reader = $this->auth->getRole('reader');
        $author = $this->auth->getRole('author');
        $this->auth->assign($reader, 'readingAuthor');
        $this->auth->assign($author, 'readingAuthor');

        $this->auth = $this->createManager();

        $roles = $this->auth->getRolesByUser('readingAuthor');
        $roleNames = [];
        foreach ($roles as $role) {
            $roleNames[] = $role->name;
        }
tof06 committed
268

269 270
        $this->assertContains('reader', $roleNames, 'Roles should contain reader. Currently it has: ' . implode(', ', $roleNames));
        $this->assertContains('author', $roleNames, 'Roles should contain author. Currently it has: ' . implode(', ', $roleNames));
tof06 committed
271
    }
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

    public function testAssignmentsToIntegerId()
    {
        $this->prepareData();

        $reader = $this->auth->getRole('reader');
        $author = $this->auth->getRole('author');
        $this->auth->assign($reader, 42);
        $this->auth->assign($author, 1337);
        $this->auth->assign($reader, 1337);

        $this->auth = $this->createManager();

        $this->assertEquals(0, count($this->auth->getAssignments(0)));
        $this->assertEquals(1, count($this->auth->getAssignments(42)));
        $this->assertEquals(2, count($this->auth->getAssignments(1337)));
    }
Alexander Kochetov committed
289
}