|
10 | 10 | use MyCLabs\ACL\Doctrine\ACLSetup; |
11 | 11 | use MyCLabs\ACL\Model\Actions; |
12 | 12 | use MyCLabs\ACL\Model\Authorization; |
| 13 | +use MyCLabs\ACL\Model\ClassResource; |
13 | 14 | use MyCLabs\ACL\Repository\AuthorizationRepository; |
14 | 15 | use Tests\MyCLabs\ACL\Unit\Repository\Model\File; |
15 | 16 | use Tests\MyCLabs\ACL\Unit\Repository\Model\FileOwnerRole; |
@@ -68,7 +69,6 @@ public function testInsertBulk() |
68 | 69 | $this->em->persist($resource); |
69 | 70 | $role = new FileOwnerRole($user, $resource); |
70 | 71 | $this->em->persist($role); |
71 | | - |
72 | 72 | $this->em->flush(); |
73 | 73 |
|
74 | 74 | $authorizations = [ |
@@ -96,4 +96,102 @@ public function testInsertBulk() |
96 | 96 | $this->assertEquals(0, count($authorization->getChildAuthorizations())); |
97 | 97 | $this->assertTrue($authorization->isCascadable()); |
98 | 98 | } |
| 99 | + |
| 100 | + /** |
| 101 | + * @depends testInsertBulk |
| 102 | + */ |
| 103 | + public function testFindCascadableAuthorizations() |
| 104 | + { |
| 105 | + $user = new User(); |
| 106 | + $this->em->persist($user); |
| 107 | + $resource = new File(); |
| 108 | + $this->em->persist($resource); |
| 109 | + $role = new FileOwnerRole($user, $resource); |
| 110 | + $this->em->persist($role); |
| 111 | + $this->em->flush(); |
| 112 | + |
| 113 | + $classResource = new ClassResource('\Tests\MyCLabs\ACL\Unit\Repository\Model\File'); |
| 114 | + |
| 115 | + $authorizations = [ |
| 116 | + // VIEW cascades (entity resource) |
| 117 | + Authorization::create($role, new Actions([ Actions::VIEW ]), $resource, true), |
| 118 | + // EDIT doesn't cascade (entity resource) |
| 119 | + Authorization::create($role, new Actions([ Actions::EDIT ]), $resource, false), |
| 120 | + |
| 121 | + // VIEW cascades (class resource) |
| 122 | + Authorization::create($role, new Actions([ Actions::VIEW ]), $classResource, true), |
| 123 | + // EDIT doesn't cascade (class resource) |
| 124 | + Authorization::create($role, new Actions([ Actions::EDIT ]), $classResource, false), |
| 125 | + ]; |
| 126 | + |
| 127 | + /** @var AuthorizationRepository $repository */ |
| 128 | + $repository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization'); |
| 129 | + |
| 130 | + $repository->insertBulk($authorizations); |
| 131 | + |
| 132 | + // Test for entity resource |
| 133 | + $result = $repository->findCascadableAuthorizationsForResource($resource); |
| 134 | + $this->assertCount(1, $result); |
| 135 | + $this->assertTrue($result[0]->getActions()->view); |
| 136 | + $this->assertFalse($result[0]->getActions()->edit); |
| 137 | + |
| 138 | + // Test for class resource |
| 139 | + $result = $repository->findCascadableAuthorizationsForResource($classResource); |
| 140 | + $this->assertCount(1, $result); |
| 141 | + $this->assertTrue($result[0]->getActions()->view); |
| 142 | + $this->assertFalse($result[0]->getActions()->edit); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * @depends testInsertBulk |
| 147 | + */ |
| 148 | + public function testIsAllowedOnEntity() |
| 149 | + { |
| 150 | + $user = new User(); |
| 151 | + $this->em->persist($user); |
| 152 | + $resource = new File(); |
| 153 | + $this->em->persist($resource); |
| 154 | + $role = new FileOwnerRole($user, $resource); |
| 155 | + $this->em->persist($role); |
| 156 | + $this->em->flush(); |
| 157 | + |
| 158 | + $authorizations = [ |
| 159 | + Authorization::create($role, new Actions([ Actions::VIEW ]), $resource), |
| 160 | + ]; |
| 161 | + |
| 162 | + /** @var AuthorizationRepository $repository */ |
| 163 | + $repository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization'); |
| 164 | + $repository->insertBulk($authorizations); |
| 165 | + |
| 166 | + $this->assertTrue($repository->isAllowedOnEntity($user, Actions::VIEW, $resource)); |
| 167 | + $this->assertFalse($repository->isAllowedOnEntity($user, Actions::EDIT, $resource)); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * @depends testInsertBulk |
| 172 | + */ |
| 173 | + public function testIsAllowedOnEntityClass() |
| 174 | + { |
| 175 | + $user = new User(); |
| 176 | + $this->em->persist($user); |
| 177 | + $resource = new File(); |
| 178 | + $this->em->persist($resource); |
| 179 | + $role = new FileOwnerRole($user, $resource); |
| 180 | + $this->em->persist($role); |
| 181 | + $this->em->flush(); |
| 182 | + |
| 183 | + $class = 'Tests\MyCLabs\ACL\Unit\Repository\Model\File'; |
| 184 | + $classResource = new ClassResource($class); |
| 185 | + |
| 186 | + $authorizations = [ |
| 187 | + Authorization::create($role, new Actions([ Actions::VIEW ]), $classResource), |
| 188 | + ]; |
| 189 | + |
| 190 | + /** @var AuthorizationRepository $repository */ |
| 191 | + $repository = $this->em->getRepository('MyCLabs\ACL\Model\Authorization'); |
| 192 | + $repository->insertBulk($authorizations); |
| 193 | + |
| 194 | + $this->assertTrue($repository->isAllowedOnEntityClass($user, Actions::VIEW, $class)); |
| 195 | + $this->assertFalse($repository->isAllowedOnEntityClass($user, Actions::EDIT, $class)); |
| 196 | + } |
99 | 197 | } |
0 commit comments