|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * UserFrosting (http://www.userfrosting.com) |
| 5 | + * |
| 6 | + * @link https://github.com/userfrosting/UserFrosting |
| 7 | + * @copyright Copyright (c) 2019 Alexander Weissman |
| 8 | + * @license https://github.com/userfrosting/UserFrosting/blob/master/LICENSE.md (MIT License) |
| 9 | + */ |
| 10 | + |
| 11 | +namespace UserFrosting\Sprinkle\Admin\Tests\Integration\Controller; |
| 12 | + |
| 13 | +use UserFrosting\Sprinkle\Account\Tests\withTestUser; |
| 14 | +use UserFrosting\Sprinkle\Admin\Controller\PermissionController; |
| 15 | +use UserFrosting\Sprinkle\Core\Tests\RefreshDatabase; |
| 16 | +use UserFrosting\Sprinkle\Core\Tests\TestDatabase; |
| 17 | +use UserFrosting\Sprinkle\Core\Tests\withController; |
| 18 | +use UserFrosting\Support\Exception\ForbiddenException; |
| 19 | +use UserFrosting\Support\Exception\NotFoundException; |
| 20 | +use UserFrosting\Tests\TestCase; |
| 21 | + |
| 22 | +/** |
| 23 | + * Tests CoreController |
| 24 | + */ |
| 25 | +class PermissionControllerGuestTest extends TestCase |
| 26 | +{ |
| 27 | + use TestDatabase; |
| 28 | + use RefreshDatabase; |
| 29 | + use withTestUser; |
| 30 | + use withController; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var bool DB is initialized for normal db |
| 34 | + */ |
| 35 | + protected static $initialized = false; |
| 36 | + |
| 37 | + /** |
| 38 | + * Setup test database for controller tests |
| 39 | + */ |
| 40 | + public function setUp() |
| 41 | + { |
| 42 | + parent::setUp(); |
| 43 | + $this->setupTestDatabase(); |
| 44 | + |
| 45 | + if ($this->usingInMemoryDatabase()) { |
| 46 | + |
| 47 | + // Setup database, then setup User & default role |
| 48 | + $this->refreshDatabase(); |
| 49 | + $this->setupUser(); |
| 50 | + |
| 51 | + } else if (!static::$initialized) { |
| 52 | + |
| 53 | + // Only refresh db once |
| 54 | + $this->refreshDatabase(); |
| 55 | + static::$initialized = true; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + */ |
| 61 | + public function testControllerConstructor() |
| 62 | + { |
| 63 | + $controller = $this->getController(); |
| 64 | + $this->assertInstanceOf(PermissionController::class, $controller); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @depends testControllerConstructor |
| 69 | + * @return PermissionController |
| 70 | + */ |
| 71 | + public function testControllerConstructorWithUser() |
| 72 | + { |
| 73 | + // Skip user setup if using in-memory db |
| 74 | + if (!$this->usingInMemoryDatabase()) { |
| 75 | + $this->setupUser(); |
| 76 | + } |
| 77 | + |
| 78 | + $controller = $this->getController(); |
| 79 | + $this->assertInstanceOf(PermissionController::class, $controller); |
| 80 | + |
| 81 | + return $controller; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @depends testControllerConstructorWithUser |
| 86 | + */ |
| 87 | + public function testGetInfo_GuestUser() |
| 88 | + { |
| 89 | + $controller = $this->getController(); |
| 90 | + $this->expectException(ForbiddenException::class); |
| 91 | + $controller->getInfo($this->getRequest(), $this->getResponse(), []); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @depends testControllerConstructorWithUser |
| 96 | + * @param PermissionController $controller |
| 97 | + */ |
| 98 | + public function testGetInfo_ForbiddenException(PermissionController $controller) |
| 99 | + { |
| 100 | + $this->expectException(ForbiddenException::class); |
| 101 | + $controller->getInfo($this->getRequest(), $this->getResponse(), []); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @depends testControllerConstructorWithUser |
| 106 | + * @param PermissionController $controller |
| 107 | + */ |
| 108 | + public function testGetListWithNoPermission(PermissionController $controller) |
| 109 | + { |
| 110 | + $this->expectException(ForbiddenException::class); |
| 111 | + $controller->getList($this->getRequest(), $this->getResponse(), []); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @depends testControllerConstructorWithUser |
| 116 | + * @param PermissionController $controller |
| 117 | + */ |
| 118 | + public function testGetUsersWithNoPermission(PermissionController $controller) |
| 119 | + { |
| 120 | + $this->expectException(ForbiddenException::class); |
| 121 | + $controller->getUsers($this->getRequest(), $this->getResponse(), []); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * @depends testControllerConstructorWithUser |
| 126 | + * @param PermissionController $controller |
| 127 | + */ |
| 128 | + public function testpageInfoWithNoPermission(PermissionController $controller) |
| 129 | + { |
| 130 | + $this->expectException(ForbiddenException::class); |
| 131 | + $controller->pageInfo($this->getRequest(), $this->getResponse(), []); |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * @depends testControllerConstructorWithUser |
| 136 | + * @param PermissionController $controller |
| 137 | + */ |
| 138 | + public function testpageListWithNoPermission(PermissionController $controller) |
| 139 | + { |
| 140 | + $this->expectException(ForbiddenException::class); |
| 141 | + $controller->pageList($this->getRequest(), $this->getResponse(), []); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * @return PermissionController |
| 146 | + */ |
| 147 | + private function getController() |
| 148 | + { |
| 149 | + return new PermissionController($this->ci); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + */ |
| 154 | + private function setupUser() |
| 155 | + { |
| 156 | + $this->createTestUser(false, true); |
| 157 | + } |
| 158 | +} |
0 commit comments