Skip to content

Commit d7407d7

Browse files
authored
Merge pull request #17741 from grokability/improved-user-create-tests
Improved user create tests
2 parents 9882046 + 8ccd2e9 commit d7407d7

File tree

2 files changed

+101
-8
lines changed

2 files changed

+101
-8
lines changed

tests/Feature/Users/Api/CreateUserTest.php

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use App\Models\Company;
66
use App\Models\Department;
77
use App\Models\User;
8+
use App\Notifications\WelcomeNotification;
9+
use Illuminate\Support\Facades\Notification;
810
use Illuminate\Testing\Fluent\AssertableJson;
911
use Tests\TestCase;
1012

@@ -58,21 +60,61 @@ public function testDepartmentIdNeedsToBeInteger()
5860
});
5961
}
6062

61-
public function testCanStoreUser()
63+
public function testCanCreateUser()
6264
{
6365
$this->actingAsForApi(User::factory()->createUsers()->create())
6466
->postJson(route('api.users.store'), [
65-
'first_name' => 'Darth',
66-
'username' => 'darthvader',
67-
'password' => 'darth_password',
68-
'password_confirmation' => 'darth_password',
67+
'first_name' => 'Test First Name',
68+
'last_name' => 'Test Last Name',
69+
'username' => 'testuser',
70+
'password' => 'testpassword1235!!',
71+
'password_confirmation' => 'testpassword1235!!',
72+
'send_welcome' => '1',
73+
'activated' => '1',
74+
'notes' => 'Test Note',
6975
])
7076
->assertStatusMessageIs('success')
7177
->assertOk();
7278

7379
$this->assertDatabaseHas('users', [
74-
'first_name' => 'Darth',
75-
'username' => 'darthvader',
80+
'first_name' => 'Test First Name',
81+
'last_name' => 'Test Last Name',
82+
'username' => 'testuser',
83+
'activated' => '1',
84+
'notes' => 'Test Note',
85+
86+
]);
87+
}
88+
89+
public function testCanCreateAndNotifyUser()
90+
{
91+
Notification::fake();
92+
93+
$this->actingAsForApi(User::factory()->createUsers()->create())
94+
->postJson(route('api.users.store'), [
95+
'first_name' => 'Test First Name',
96+
'last_name' => 'Test Last Name',
97+
'username' => 'testuser',
98+
'password' => 'testpassword1235!!',
99+
'password_confirmation' => 'testpassword1235!!',
100+
'send_welcome' => '1',
101+
'activated' => '1',
102+
'email' => '[email protected]',
103+
'notes' => 'Test Note',
104+
])
105+
->assertStatusMessageIs('success')
106+
->assertOk();
107+
108+
$this->assertDatabaseHas('users', [
109+
'first_name' => 'Test First Name',
110+
'last_name' => 'Test Last Name',
111+
'username' => 'testuser',
112+
'activated' => '1',
113+
'email' => '[email protected]',
114+
'notes' => 'Test Note',
76115
]);
116+
117+
$user = User::where('username', 'testuser')->first();
118+
Notification::assertSentTo($user, WelcomeNotification::class);
77119
}
78120
}

tests/Feature/Users/Ui/CreateUserTest.php

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Tests\Feature\Users\Ui;
44

55
use App\Models\User;
6+
use App\Notifications\WelcomeNotification;
7+
use Illuminate\Support\Facades\Notification;
68
use Tests\TestCase;
79

810
class CreateUserTest extends TestCase
@@ -25,6 +27,39 @@ public function testPageRenders()
2527

2628
public function testCanCreateUser()
2729
{
30+
$response = $this->actingAs(User::factory()->createUsers()->viewUsers()->create())
31+
->from(route('users.index'))
32+
->post(route('users.store'), [
33+
'first_name' => 'Test First Name',
34+
'last_name' => 'Test Last Name',
35+
'username' => 'testuser',
36+
'password' => 'testpassword1235!!',
37+
'password_confirmation' => 'testpassword1235!!',
38+
'send_welcome' => '1',
39+
'activated' => '1',
40+
'notes' => 'Test Note',
41+
])
42+
->assertSessionHasNoErrors()
43+
->assertStatus(302)
44+
->assertRedirect(route('users.index'));
45+
46+
$this->assertDatabaseHas('users', [
47+
'first_name' => 'Test First Name',
48+
'last_name' => 'Test Last Name',
49+
'username' => 'testuser',
50+
'activated' => '1',
51+
'notes' => 'Test Note',
52+
53+
]);
54+
55+
$this->followRedirects($response)->assertSee('Success');
56+
57+
}
58+
59+
public function testCanCreateAndNotifyUser()
60+
{
61+
62+
Notification::fake();
2863

2964
$response = $this->actingAs(User::factory()->createUsers()->viewUsers()->create())
3065
->from(route('users.index'))
@@ -33,11 +68,27 @@ public function testCanCreateUser()
3368
'last_name' => 'Test Last Name',
3469
'username' => 'testuser',
3570
'password' => 'testpassword1235!!',
36-
//'notes' => 'Test Note',
71+
'password_confirmation' => 'testpassword1235!!',
72+
'send_welcome' => '1',
73+
'activated' => '1',
74+
'email' => '[email protected]',
75+
'notes' => 'Test Note',
3776
])
77+
->assertSessionHasNoErrors()
3878
->assertStatus(302)
3979
->assertRedirect(route('users.index'));
4080

81+
$this->assertDatabaseHas('users', [
82+
'first_name' => 'Test First Name',
83+
'last_name' => 'Test Last Name',
84+
'username' => 'testuser',
85+
'activated' => '1',
86+
'email' => '[email protected]',
87+
'notes' => 'Test Note',
88+
]);
89+
90+
$user = User::where('username', 'testuser')->first();
91+
Notification::assertSentTo($user, WelcomeNotification::class);
4192
$this->followRedirects($response)->assertSee('Success');
4293

4394
}

0 commit comments

Comments
 (0)