|
| 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 Mockery as m; |
| 14 | +use League\FactoryMuffin\Faker\Facade as Faker; |
| 15 | +use UserFrosting\Sprinkle\Account\Database\Models\User; |
| 16 | +use UserFrosting\Sprinkle\Account\Tests\withTestUser; |
| 17 | +use UserFrosting\Sprinkle\Admin\Controller\UserController; |
| 18 | +use UserFrosting\Sprinkle\Core\Mail\Mailer; |
| 19 | +use UserFrosting\Sprinkle\Core\Mail\TwigMailMessage; |
| 20 | +use UserFrosting\Sprinkle\Core\Tests\RefreshDatabase; |
| 21 | +use UserFrosting\Sprinkle\Core\Tests\TestDatabase; |
| 22 | +use UserFrosting\Sprinkle\Core\Tests\withController; |
| 23 | +use UserFrosting\Support\Exception\BadRequestException; |
| 24 | +use UserFrosting\Support\Exception\NotFoundException; |
| 25 | +use UserFrosting\Tests\TestCase; |
| 26 | + |
| 27 | +/** |
| 28 | + * Tests UserController |
| 29 | + * Specific tests for createUser, which fails on pgsql because of a bug when |
| 30 | + * creating a user with specific id before calling create |
| 31 | + */ |
| 32 | +class UserControllerCreateTest extends TestCase |
| 33 | +{ |
| 34 | + use TestDatabase; |
| 35 | + use RefreshDatabase; |
| 36 | + use withTestUser; |
| 37 | + use withController; |
| 38 | + |
| 39 | + /** |
| 40 | + * Setup test database for controller tests |
| 41 | + */ |
| 42 | + public function setUp() |
| 43 | + { |
| 44 | + parent::setUp(); |
| 45 | + $this->setupTestDatabase(); |
| 46 | + $this->refreshDatabase(); |
| 47 | + } |
| 48 | + |
| 49 | + public function tearDown() |
| 50 | + { |
| 51 | + parent::tearDown(); |
| 52 | + m::close(); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + */ |
| 57 | + public function testCreate() |
| 58 | + { |
| 59 | + $fm = $this->ci->factory; |
| 60 | + |
| 61 | + // Create fake mailer |
| 62 | + $mailer = m::mock(Mailer::class); |
| 63 | + $mailer->shouldReceive('send')->once()->with(\Mockery::type(TwigMailMessage::class)); |
| 64 | + $this->ci->mailer = $mailer; |
| 65 | + |
| 66 | + // Create will fail on PGSQL if a user is created with forced id |
| 67 | + // because it mess the auto_increment |
| 68 | + // @see https://stackoverflow.com/questions/36157029/laravel-5-2-eloquent-save-auto-increment-pgsql-exception-on-same-id |
| 69 | + $user = $fm->create(User::class); |
| 70 | + $this->giveUserTestPermission($user, 'create_user'); |
| 71 | + $this->giveUserTestPermission($user, 'create_user_field'); |
| 72 | + $this->loginUser($user); |
| 73 | + |
| 74 | + // Recreate controller to use the fake mailer |
| 75 | + $controller = $this->getController(); |
| 76 | + |
| 77 | + // Create a fake group |
| 78 | + $group = $fm->create('UserFrosting\Sprinkle\Account\Database\Models\Group'); |
| 79 | + |
| 80 | + // Set post data |
| 81 | + $data = [ |
| 82 | + 'user_name' => 'foo', |
| 83 | + 'first_name' => 'foo name', |
| 84 | + 'last_name' => 'foo last', |
| 85 | + |
| 86 | + 'group_id' => $group->id |
| 87 | + ]; |
| 88 | + $request = $this->getRequest()->withParsedBody($data); |
| 89 | + |
| 90 | + // Get controller stuff |
| 91 | + $result = $controller->create($request, $this->getResponse(), []); |
| 92 | + $this->assertSame($result->getStatusCode(), 200); |
| 93 | + $this->assertJson((string) $result->getBody()); |
| 94 | + $this->assertSame('[]', (string) $result->getBody()); |
| 95 | + |
| 96 | + // Make sure role was created |
| 97 | + $user = User::where('user_name', 'foo')->first(); |
| 98 | + $this->assertSame('foo name', $user->first_name); |
| 99 | + $this->assertSame($group->id, $user->group->id); |
| 100 | + |
| 101 | + // Test message |
| 102 | + $ms = $this->ci->alerts; |
| 103 | + $messages = $ms->getAndClearMessages(); |
| 104 | + $this->assertSame('success', end($messages)['type']); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @depends testCreate |
| 109 | + * @param UserController $controller |
| 110 | + */ |
| 111 | + public function testCreateWithNoLocale() |
| 112 | + { |
| 113 | + $this->ci->config['site.locales.available'] = []; |
| 114 | + $this->testCreate(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * @depends testCreate |
| 119 | + * @param UserController $controller |
| 120 | + */ |
| 121 | + public function testCreateWithNoGroupId() |
| 122 | + { |
| 123 | + $fm = $this->ci->factory; |
| 124 | + |
| 125 | + // Create fake mailer |
| 126 | + $mailer = m::mock(Mailer::class); |
| 127 | + $mailer->shouldReceive('send')->once()->with(\Mockery::type(TwigMailMessage::class)); |
| 128 | + $this->ci->mailer = $mailer; |
| 129 | + |
| 130 | + // Also create a group |
| 131 | + $group = $fm->create('UserFrosting\Sprinkle\Account\Database\Models\Group'); |
| 132 | + |
| 133 | + // Create will fail on PGSQL if a user is created with forced id |
| 134 | + // because it mess the auto_increment |
| 135 | + // @see https://stackoverflow.com/questions/36157029/laravel-5-2-eloquent-save-auto-increment-pgsql-exception-on-same-id |
| 136 | + $user = $fm->create(User::class, [ |
| 137 | + 'group_id' => $group->id, |
| 138 | + ]); |
| 139 | + $this->giveUserTestPermission($user, 'create_user'); |
| 140 | + $this->loginUser($user); |
| 141 | + |
| 142 | + // Recreate controller to use the fake mailer |
| 143 | + $controller = $this->getController(); |
| 144 | + |
| 145 | + // Set post data |
| 146 | + $data = [ |
| 147 | + 'user_name' => 'foo', |
| 148 | + 'first_name' => 'foo name', |
| 149 | + 'last_name' => 'foo last', |
| 150 | + |
| 151 | + ]; |
| 152 | + $request = $this->getRequest()->withParsedBody($data); |
| 153 | + |
| 154 | + // Get controller stuff |
| 155 | + $result = $controller->create($request, $this->getResponse(), []); |
| 156 | + $this->assertSame($result->getStatusCode(), 200); |
| 157 | + $this->assertJson((string) $result->getBody()); |
| 158 | + $this->assertSame('[]', (string) $result->getBody()); |
| 159 | + |
| 160 | + // Make sure role was created |
| 161 | + $user = User::where('user_name', 'foo')->first(); |
| 162 | + $this->assertSame('foo name', $user->first_name); |
| 163 | + $this->assertSame($group->id, $user->group->id); |
| 164 | + |
| 165 | + // Test message |
| 166 | + $ms = $this->ci->alerts; |
| 167 | + $messages = $ms->getAndClearMessages(); |
| 168 | + $this->assertSame('success', end($messages)['type']); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @return UserController |
| 173 | + */ |
| 174 | + private function getController() |
| 175 | + { |
| 176 | + return new UserController($this->ci); |
| 177 | + } |
| 178 | +} |
0 commit comments