|
| 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\UserController; |
| 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\Tests\TestCase; |
| 20 | + |
| 21 | +/** |
| 22 | + * Tests UserController |
| 23 | + */ |
| 24 | +class UserControllerGuestTest extends TestCase |
| 25 | +{ |
| 26 | + use TestDatabase; |
| 27 | + use RefreshDatabase; |
| 28 | + use withTestUser; |
| 29 | + use withController; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var bool DB is initialized for normal db |
| 33 | + */ |
| 34 | + protected static $initialized = false; |
| 35 | + |
| 36 | + /** |
| 37 | + * Setup test database for controller tests |
| 38 | + */ |
| 39 | + public function setUp() |
| 40 | + { |
| 41 | + parent::setUp(); |
| 42 | + $this->setupTestDatabase(); |
| 43 | + |
| 44 | + if ($this->usingInMemoryDatabase()) { |
| 45 | + |
| 46 | + // Setup database, then setup User & default role |
| 47 | + $this->refreshDatabase(); |
| 48 | + $this->setupUser(); |
| 49 | + |
| 50 | + } else if (!static::$initialized) { |
| 51 | + |
| 52 | + // Only refresh db once |
| 53 | + $this->refreshDatabase(); |
| 54 | + static::$initialized = true; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + */ |
| 60 | + public function testControllerConstructor() |
| 61 | + { |
| 62 | + $controller = $this->getController(); |
| 63 | + $this->assertInstanceOf(UserController::class, $controller); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @depends testControllerConstructor |
| 68 | + * @return UserController |
| 69 | + */ |
| 70 | + public function testControllerConstructorWithUser() |
| 71 | + { |
| 72 | + // Skip user setup if using in-memory db |
| 73 | + if (!$this->usingInMemoryDatabase()) { |
| 74 | + $this->setupUser(); |
| 75 | + } |
| 76 | + |
| 77 | + $controller = $this->getController(); |
| 78 | + $this->assertInstanceOf(UserController::class, $controller); |
| 79 | + |
| 80 | + return $controller; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @depends testControllerConstructorWithUser |
| 85 | + * @param UserController $controller |
| 86 | + */ |
| 87 | + public function testCreateWithNoPermissions(UserController $controller) |
| 88 | + { |
| 89 | + $this->expectException(ForbiddenException::class); |
| 90 | + $controller->create($this->getRequest(), $this->getResponse(), []); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @depends testControllerConstructorWithUser |
| 95 | + * @param UserController $controller |
| 96 | + */ |
| 97 | + public function testDeleteWithNoPermission(UserController $controller) |
| 98 | + { |
| 99 | + $this->expectException(ForbiddenException::class); |
| 100 | + $controller->delete($this->getRequest(), $this->getResponse(), ['user_name' => 'userfoo']); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @depends testControllerConstructorWithUser |
| 105 | + * @param UserController $controller |
| 106 | + */ |
| 107 | + public function testGetListWithNoPermission(UserController $controller) |
| 108 | + { |
| 109 | + $this->expectException(ForbiddenException::class); |
| 110 | + $controller->getList($this->getRequest(), $this->getResponse(), []); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @return UserController |
| 115 | + */ |
| 116 | + private function getController() |
| 117 | + { |
| 118 | + return new UserController($this->ci); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + */ |
| 123 | + private function setupUser() |
| 124 | + { |
| 125 | + // Admin user, WILL have access |
| 126 | + $testUser = $this->createTestUser(false, true); |
| 127 | + |
| 128 | + // Create test user |
| 129 | + $fm = $this->ci->factory; |
| 130 | + $user = $fm->create('UserFrosting\Sprinkle\Account\Database\Models\User', [ |
| 131 | + 'id' => '9999', |
| 132 | + 'user_name' => 'userfoo', |
| 133 | + |
| 134 | + ]); |
| 135 | + } |
| 136 | +} |
0 commit comments