| Conditions | 1 |
| Paths | 1 |
| Total Lines | 58 |
| Code Lines | 46 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 18 | public function testIsCurrentUserAllowed(): void |
||
| 19 | { |
||
| 20 | $acl = new Acl(); |
||
| 21 | $card = new Card(); |
||
| 22 | $card->setSite(Site::Dilps); |
||
| 23 | |||
| 24 | $ownerStudent = new User(); |
||
| 25 | $ownerStudent->setSite(Site::Dilps); |
||
| 26 | $ownerStudent->setLogin('Sarah'); |
||
| 27 | User::setCurrent($ownerStudent); |
||
| 28 | $card->timestampCreation(); |
||
| 29 | |||
| 30 | User::setCurrent(null); |
||
| 31 | self::assertFalse($acl->isCurrentUserAllowed($card, 'update'), 'anonymous cannot update'); |
||
| 32 | self::assertSame('Non-logged user with role anonymous is not allowed on resource "Card#null" with privilege "update"', $acl->getLastDenialMessage()); |
||
| 33 | |||
| 34 | User::setCurrent($ownerStudent); |
||
| 35 | self::assertFalse($acl->isCurrentUserAllowed($card, 'update'), 'student cannot update even if owner'); |
||
| 36 | self::assertSame('User "Sarah" with role student is not allowed on resource "Card#null" with privilege "update"', $acl->getLastDenialMessage()); |
||
| 37 | |||
| 38 | $ownerJunior = new User(User::ROLE_JUNIOR); |
||
| 39 | $ownerJunior->setSite(Site::Dilps); |
||
| 40 | $ownerJunior->setLogin('Kyle'); |
||
| 41 | User::setCurrent($ownerJunior); |
||
| 42 | $card->timestampCreation(); |
||
| 43 | |||
| 44 | self::assertTrue($acl->isCurrentUserAllowed($card, 'update'), 'only junior owner can update'); |
||
| 45 | self::assertNull($acl->getLastDenialMessage()); |
||
| 46 | self::assertTrue($acl->isCurrentUserAllowed($card, 'delete'), 'junior can delete his card'); |
||
| 47 | self::assertNull($acl->getLastDenialMessage()); |
||
| 48 | |||
| 49 | $change = new Change(); |
||
| 50 | $change->setSuggestion($card); |
||
| 51 | self::assertFalse($acl->isCurrentUserAllowed($card, 'delete'), 'junior cannot delete his card if it is a suggestion'); |
||
| 52 | self::assertSame('User "Kyle" with role junior is not allowed on resource "Card#null" with privilege "delete"', $acl->getLastDenialMessage()); |
||
| 53 | |||
| 54 | $otherStudent = new User(); |
||
| 55 | $otherStudent->setSite(Site::Dilps); |
||
| 56 | $otherStudent->setLogin('John'); |
||
| 57 | User::setCurrent($otherStudent); |
||
| 58 | self::assertFalse($acl->isCurrentUserAllowed($card, 'update'), 'other user cannot update'); |
||
| 59 | self::assertSame('User "John" with role student is not allowed on resource "Card#null" with privilege "update" because it is not the owner, nor one of the responsible', $acl->getLastDenialMessage()); |
||
| 60 | |||
| 61 | $administrator = new User(User::ROLE_ADMINISTRATOR); |
||
| 62 | $administrator->setSite(Site::Dilps); |
||
| 63 | $administrator->setLogin('Jane'); |
||
| 64 | User::setCurrent($administrator); |
||
| 65 | self::assertTrue($acl->isCurrentUserAllowed($card, 'update'), 'admin can do anything'); |
||
| 66 | self::assertNull($acl->getLastDenialMessage()); |
||
| 67 | |||
| 68 | $collection = new Collection(); |
||
| 69 | $collection->setSite(Site::Dilps); |
||
| 70 | self::assertFalse($acl->isCurrentUserAllowed($collection, 'read'), 'admin cannot read non-admin collection'); |
||
| 71 | self::assertSame('User "Jane" with role administrator is not allowed on resource "Collection#null" with privilege "read" because it is not the owner, nor one of the responsible', $acl->getLastDenialMessage()); |
||
| 72 | |||
| 73 | $collection->setVisibility(CollectionVisibility::Administrator); |
||
| 74 | self::assertTrue($acl->isCurrentUserAllowed($collection, 'read'), 'admin can do anything'); |
||
| 75 | self::assertNull($acl->getLastDenialMessage()); |
||
| 76 | } |
||
| 78 |