diff --git a/src/Http/Controllers/NotificationController.php b/src/Http/Controllers/NotificationController.php index a66dd8f3..a06c123d 100644 --- a/src/Http/Controllers/NotificationController.php +++ b/src/Http/Controllers/NotificationController.php @@ -53,6 +53,6 @@ public function destroy(Request $request, string $id): JsonResponse $notification->delete(); - return new JsonResponse($notification); + return new JsonResponse(['deleted' => true]); } } diff --git a/src/Widgets/Widget.php b/src/Widgets/Widget.php index f82e36a6..f9e260f2 100644 --- a/src/Widgets/Widget.php +++ b/src/Widgets/Widget.php @@ -64,6 +64,14 @@ public function getUriKey(): string return $this->getKey(); } + /** + * Get the blade template. + */ + public function getTemplate(): string + { + return $this->template; + } + /** * Get the view data. */ diff --git a/tests/Fields/RepeaterTest.php b/tests/Fields/RepeaterTest.php index 7f3ce4f0..fca1a89a 100644 --- a/tests/Fields/RepeaterTest.php +++ b/tests/Fields/RepeaterTest.php @@ -27,7 +27,7 @@ public function test_a_repeater_field_has_repeater_template(): void $this->assertSame('root::fields.repeater', $this->field->getTemplate()); } - public function test_a_repeater_field_register_routes(): void + public function test_a_repeater_field_registers_routes(): void { $this->app['router']->prefix('posts/fields')->group(function ($router) { $this->field->registerRoutes($this->app['request'], $router); diff --git a/tests/Http/ActionControllerTest.php b/tests/Http/ActionControllerTest.php index 830297af..8c2dec3e 100644 --- a/tests/Http/ActionControllerTest.php +++ b/tests/Http/ActionControllerTest.php @@ -16,7 +16,7 @@ public function setUp(): void $this->admin = User::factory()->create(); } - public function test_an_action_controller_handles_action_request(): void + public function test_action_controller_handles_action_request(): void { $this->actingAs($this->admin) ->post('/root/users/actions/send-password-reset-notification') diff --git a/tests/Http/BelongsToManyControllerTest.php b/tests/Http/BelongsToManyControllerTest.php index 8a9eed41..14810637 100644 --- a/tests/Http/BelongsToManyControllerTest.php +++ b/tests/Http/BelongsToManyControllerTest.php @@ -37,7 +37,7 @@ public function setUp(): void }); } - public function test_a_belongs_to_many_controller_handles_index(): void + public function test_belongs_to_many_controller_handles_index(): void { $this->actingAs($this->admin) ->get('/root/users/'.$this->admin->getKey().'/fields/teams') @@ -46,7 +46,7 @@ public function test_a_belongs_to_many_controller_handles_index(): void ->assertViewHas($this->field->toIndex($this->app['request'], $this->admin)); } - public function test_a_belongs_to_many_controller_handles_store(): void + public function test_belongs_to_many_controller_handles_store(): void { $team = Team::factory()->create(); @@ -65,7 +65,7 @@ public function test_a_belongs_to_many_controller_handles_store(): void ]); } - public function test_a_belongs_to_many_controller_handles_update(): void + public function test_belongs_to_many_controller_handles_update(): void { $team = $this->admin->teams->first(); @@ -82,7 +82,7 @@ public function test_a_belongs_to_many_controller_handles_update(): void $this->assertSame('member', $team->pivot->refresh()->role); } - public function test_a_belongs_to_many_controller_handles_destroy(): void + public function test_belongs_to_many_controller_handles_destroy(): void { $team = $this->admin->teams->first(); diff --git a/tests/Http/DashboardControllerTest.php b/tests/Http/DashboardControllerTest.php index d608dc03..68512862 100644 --- a/tests/Http/DashboardControllerTest.php +++ b/tests/Http/DashboardControllerTest.php @@ -17,7 +17,7 @@ public function setUp(): void $this->admin = User::factory()->create(); } - public function test_a_dashboard_controller_handles_request(): void + public function test_dashboard_controller_handles_request(): void { $this->actingAs($this->admin) ->get('/root') diff --git a/tests/Http/DownloadControllerTest.php b/tests/Http/DownloadControllerTest.php index efb1faa8..f7c8e363 100644 --- a/tests/Http/DownloadControllerTest.php +++ b/tests/Http/DownloadControllerTest.php @@ -19,7 +19,7 @@ public function setUp(): void $this->admin = User::factory()->create(); } - public function test_a_download_controller_handles_request(): void + public function test_download_controller_handles_request(): void { $medium = Medium::factory()->create(); diff --git a/tests/Http/ForgotPasswordControllerTest.php b/tests/Http/ForgotPasswordControllerTest.php deleted file mode 100644 index fbe82a64..00000000 --- a/tests/Http/ForgotPasswordControllerTest.php +++ /dev/null @@ -1,36 +0,0 @@ -get('/root/password/reset') - ->assertOk() - ->assertViewIs('root::auth.forgot-password'); - } - - public function test_forgot_password_controller_handles_password_reset_request(): void - { - Notification::fake([ResetPassword::class]); - - $this->post('/root/password/email') - ->assertRedirect() - ->assertSessionHasErrors(['email']); - - $user = User::factory()->create(); - - $this->post('/root/password/email', [ - 'email' => $user->email, - ])->assertRedirect() - ->assertSessionDoesntHaveErrors(); - - Notification::assertSentTo($user, ResetPassword::class); - } -} diff --git a/tests/Http/LoginControllerTest.php b/tests/Http/LoginControllerTest.php deleted file mode 100644 index 133f8c45..00000000 --- a/tests/Http/LoginControllerTest.php +++ /dev/null @@ -1,56 +0,0 @@ -get('/root/login') - ->assertOk() - ->assertViewIs('root::auth.login'); - } - - public function test_login_controller_handles_login(): void - { - Event::fake([Login::class]); - - $this->post('/root/login') - ->assertRedirect() - ->assertSessionHasErrors(['email', 'password']); - - $user = User::factory()->create(); - - $this->post('/root/login', [ - 'email' => $user->email, - 'password' => 'password', - ])->assertRedirect() - ->assertSessionDoesntHaveErrors(); - - Event::assertDispatched(Login::class, function ($event) use ($user) { - return $event->user->id === $user->id; - }); - } - - public function test_login_controller_handles_logout(): void - { - Event::fake([Logout::class]); - - $user = User::factory()->create(); - - $this->app['auth']->login($user); - - $this->post('/root/logout') - ->assertRedirect(); - - Event::assertDispatched(Logout::class, function ($event) use ($user) { - return $event->user->id === $user->id; - }); - } -} diff --git a/tests/Http/MediaControllerTest.php b/tests/Http/MediaControllerTest.php index 6dccb010..04c8b9cb 100644 --- a/tests/Http/MediaControllerTest.php +++ b/tests/Http/MediaControllerTest.php @@ -33,7 +33,7 @@ public function setUp(): void }); } - public function test_a_media_controller_handles_index(): void + public function test_media_controller_handles_index(): void { $this->actingAs($this->admin) ->get('/root/users/'.$this->admin->getKey().'/fields/media') @@ -41,7 +41,7 @@ public function test_a_media_controller_handles_index(): void ->assertJson($this->field->paginateRelatable($this->app['request'], $this->admin)->toArray()); } - public function test_a_media_controller_handles_store(): void + public function test_media_controller_handles_store(): void { Queue::fake(); @@ -59,7 +59,7 @@ public function test_a_media_controller_handles_store(): void Queue::assertPushedWithChain(MoveFile::class, [PerformConversions::class]); } - public function test_a_media_controller_handles_destroy(): void + public function test_media_controller_handles_destroy(): void { $medium = Medium::factory()->create(); diff --git a/tests/Http/MorphToControllerTest.php b/tests/Http/MorphToControllerTest.php index c403843f..a38dfe68 100644 --- a/tests/Http/MorphToControllerTest.php +++ b/tests/Http/MorphToControllerTest.php @@ -28,7 +28,7 @@ public function setUp(): void $this->admin = User::factory()->create(); } - public function test_a_morph_to_controller_handles_request(): void + public function test_morph_to_controller_handles_request(): void { $this->actingAs($this->admin) ->get('/root/users/'.$this->admin->getKey().'/fields/employer') diff --git a/tests/Http/NotificationControllerTest.php b/tests/Http/NotificationControllerTest.php new file mode 100644 index 00000000..8f036393 --- /dev/null +++ b/tests/Http/NotificationControllerTest.php @@ -0,0 +1,61 @@ +admin = User::factory()->create(); + + $this->notification = Notification::factory()->unread()->for($this->admin, 'notifiable')->create(); + } + + public function test_notification_controller_handles_index(): void + { + $this->actingAs($this->admin) + ->get('/root/api/notifications') + ->assertOk() + ->assertJson($this->admin->rootNotifications()->paginate()->toArray()); + } + + public function test_notification_controller_handles_show(): void + { + $this->actingAs($this->admin) + ->get('/root/api/notifications/'.$this->notification->getKey()) + ->assertOk() + ->assertJson($this->notification->toArray()); + } + + public function test_notification_controller_handles_update(): void + { + $this->assertTrue($this->notification->unread()); + + $this->actingAs($this->admin) + ->patch('/root/api/notifications/'.$this->notification->getKey()) + ->assertOk() + ->assertJson($this->notification->refresh()->toArray()); + + $this->assertTrue($this->notification->read()); + } + + public function test_notification_controller_handles_destroy(): void + { + $this->actingAs($this->admin) + ->delete('/root/api/notifications/'.$this->notification->getKey()) + ->assertOk() + ->assertJson(['deleted' => true]); + + $this->assertDatabaseMissing('root_notifications', ['id' => $this->notification->getKey()]); + } +} diff --git a/tests/Http/RelationControllerTest.php b/tests/Http/RelationControllerTest.php index f415e6dd..d4b1ff0e 100644 --- a/tests/Http/RelationControllerTest.php +++ b/tests/Http/RelationControllerTest.php @@ -35,7 +35,7 @@ public function setUp(): void ); } - public function test_a_relation_controller_handles_index(): void + public function test_relation_controller_handles_index(): void { $this->actingAs($this->admin) ->get('/root/users/'.$this->admin->getKey().'/fields/uploads') @@ -44,7 +44,7 @@ public function test_a_relation_controller_handles_index(): void ->assertViewHas($this->field->toIndex($this->app['request'], $this->admin)); } - public function test_a_relation_controller_handles_create(): void + public function test_relation_controller_handles_create(): void { $this->actingAs($this->admin) ->get('/root/users/'.$this->admin->getKey().'/fields/uploads/create') @@ -53,7 +53,7 @@ public function test_a_relation_controller_handles_create(): void ->assertViewHas($this->field->toCreate($this->app['request'], $this->admin)); } - public function test_a_relation_controller_handles_store(): void + public function test_relation_controller_handles_store(): void { $this->actingAs($this->admin) ->post('/root/users/'.$this->admin->getKey().'/fields/uploads', $data = Medium::factory()->make()->toArray()) @@ -65,7 +65,7 @@ public function test_a_relation_controller_handles_store(): void ); } - public function test_a_relation_controller_handles_show(): void + public function test_relation_controller_handles_show(): void { $this->app['request']->setRouteResolver(function () { return $this->app['router']->getRoutes()->get('GET')['root/users/{resourceModel}/fields/uploads/{usersUpload}']; @@ -78,7 +78,7 @@ public function test_a_relation_controller_handles_show(): void ->assertViewHas($this->field->toShow($this->app['request'], $this->admin, $this->medium)); } - public function test_a_relation_controller_handles_edit(): void + public function test_relation_controller_handles_edit(): void { $this->app['request']->setRouteResolver(function () { return $this->app['router']->getRoutes()->get('GET')['root/users/{resourceModel}/fields/uploads/{usersUpload}/edit']; @@ -91,7 +91,7 @@ public function test_a_relation_controller_handles_edit(): void ->assertViewHas($this->field->toEdit($this->app['request'], $this->admin, $this->medium)); } - public function test_a_relation_controller_handles_update(): void + public function test_relation_controller_handles_update(): void { $this->app['request']->setRouteResolver(function () { return $this->app['router']->getRoutes()->get('PATCH')['root/users/{resourceModel}/fields/uploads/{usersUpload}']; @@ -108,7 +108,7 @@ public function test_a_relation_controller_handles_update(): void $this->assertSame('updated.png', $this->medium->refresh()->file_name); } - public function test_a_relation_controller_handles_destroy(): void + public function test_relation_controller_handles_destroy(): void { $this->app['request']->setRouteResolver(function () { return $this->app['router']->getRoutes()->get('DELETE')['root/users/{resourceModel}/fields/uploads/{usersUpload}']; diff --git a/tests/Http/RepeaterControllerTest.php b/tests/Http/RepeaterControllerTest.php index 612d084d..2ef67c5f 100644 --- a/tests/Http/RepeaterControllerTest.php +++ b/tests/Http/RepeaterControllerTest.php @@ -28,7 +28,7 @@ public function setUp(): void $this->admin = User::factory()->create(); } - public function test_a_repeater_controller_handles_request(): void + public function test_repeater_controller_handles_request(): void { $this->actingAs($this->admin) ->post('/root/users/'.$this->admin->getKey().'/fields/settings') diff --git a/tests/Http/ResetPasswordControllerTest.php b/tests/Http/ResetPasswordControllerTest.php deleted file mode 100644 index ddad81a7..00000000 --- a/tests/Http/ResetPasswordControllerTest.php +++ /dev/null @@ -1,59 +0,0 @@ -user = User::factory()->unverified()->create(); - - Password::broker()->sendResetLink(['email' => $this->user->email], function ($user, $token) { - $this->token = $token; - }); - } - - public function test_reset_password_controller_shows_form(): void - { - $this->get('/root/password/reset/'.$this->token.'/'.$this->user->email) - ->assertOk() - ->assertViewIs('root::auth.reset-password'); - } - - public function test_reset_password_controller_handles_password_reset_request(): void - { - Event::fake([PasswordReset::class]); - - $this->assertFalse($this->user->hasVerifiedEmail()); - - $this->post('/root/password/reset') - ->assertRedirect() - ->assertSessionHasErrors(['email', 'password']); - - $this->post('/root/password/reset', [ - 'token' => $this->token, - 'email' => $this->user->email, - 'password' => 'password', - 'password_confirmation' => 'password', - ])->assertRedirect() - ->assertSessionDoesntHaveErrors(); - - $this->assertTrue($this->user->refresh()->hasVerifiedEmail()); - - Event::assertDispatched(PasswordReset::class, function ($event) { - return $event->user->id === $this->user->id; - }); - } -} diff --git a/tests/Http/ResourceControllerTest.php b/tests/Http/ResourceControllerTest.php index 40fd52a8..7e27eeec 100644 --- a/tests/Http/ResourceControllerTest.php +++ b/tests/Http/ResourceControllerTest.php @@ -22,7 +22,7 @@ public function setUp(): void $this->admin = User::factory()->create(['name' => 'Admin']); } - public function test_a_resource_controller_handles_index(): void + public function test_resource_controller_handles_index(): void { $this->actingAs($this->admin) ->get($this->resource->getUri()) @@ -31,7 +31,7 @@ public function test_a_resource_controller_handles_index(): void ->assertViewHas($this->resource->toIndex($this->app['request'])); } - public function test_a_resource_controller_handles_create(): void + public function test_resource_controller_handles_create(): void { $this->actingAs($this->admin) ->get($this->resource->getUri().'/create') @@ -40,7 +40,7 @@ public function test_a_resource_controller_handles_create(): void ->assertViewHas($this->resource->toCreate($this->app['request'])); } - public function test_a_resource_controller_handles_show(): void + public function test_resource_controller_handles_show(): void { $this->actingAs($this->admin) ->get($this->resource->getUri().'/'.$this->admin->getKey()) @@ -49,7 +49,7 @@ public function test_a_resource_controller_handles_show(): void ->assertViewHas($this->resource->toShow($this->app['request'], $this->admin)); } - public function test_a_resource_controller_handles_edit(): void + public function test_resource_controller_handles_edit(): void { $this->actingAs($this->admin) ->get($this->resource->getUri().'/'.$this->admin->getKey().'/edit') @@ -58,7 +58,7 @@ public function test_a_resource_controller_handles_edit(): void ->assertViewHas($this->resource->toEdit($this->app['request'], $this->admin)); } - public function test_a_resource_controller_handles_store(): void + public function test_resource_controller_handles_store(): void { $this->actingAs($this->admin) ->post($this->resource->getUri(), [ @@ -71,7 +71,7 @@ public function test_a_resource_controller_handles_store(): void $this->assertDatabaseHas('users', ['email' => 'test@root.local', 'name' => 'Test User']); } - public function test_a_resource_controller_handles_update(): void + public function test_resource_controller_handles_update(): void { $this->actingAs($this->admin) ->patch($this->resource->getUri().'/'.$this->admin->getKey(), array_merge($this->admin->toArray(), ['name' => 'Test Admin'])) @@ -80,7 +80,7 @@ public function test_a_resource_controller_handles_update(): void $this->assertSame('Test Admin', $this->admin->refresh()->name); } - public function test_a_resource_controller_handles_destroy(): void + public function test_resource_controller_handles_destroy(): void { $user = User::factory()->create(); diff --git a/tests/Http/WidgetControllerTest.php b/tests/Http/WidgetControllerTest.php new file mode 100644 index 00000000..06c43c57 --- /dev/null +++ b/tests/Http/WidgetControllerTest.php @@ -0,0 +1,39 @@ +admin = User::factory()->create(); + + $this->widget = Root::instance() + ->resources + ->resolve('users') + ->resolveWidgets($this->app['request']) + ->first(function ($widget) { + return $widget->getKey() === 'users-count'; + }); + } + + public function test_widget_controller_handles_request(): void + { + $this->actingAs($this->admin) + ->get('/root/users/widgets/users-count') + ->assertOk() + ->assertViewIs($this->widget->getTemplate()) + ->assertViewHas($this->widget->data($this->app['request'])); + } +} diff --git a/tests/Widgets/WidgetTest.php b/tests/Widgets/WidgetTest.php index fe5bba18..d3566561 100644 --- a/tests/Widgets/WidgetTest.php +++ b/tests/Widgets/WidgetTest.php @@ -3,18 +3,39 @@ namespace Cone\Root\Tests\Widgets; use Cone\Root\Tests\TestCase; +use Cone\Root\Widgets\Welcome; +use Cone\Root\Widgets\Widget; class WidgetTest extends TestCase { + protected Widget $widget; + public function setUp(): void { parent::setUp(); - // + $this->widget = new class extends Welcome + { + protected bool $async = true; + + public function getKey(): string + { + return 'welcome'; + } + }; } - public function test_a_widget_has_template(): void + public function test_a_widget_registers_routes(): void { - $this->assertTrue(true); + $this->app['router']->prefix('/root/widgets')->group(function ($router) { + $this->widget->registerRoutes($this->app['request'], $router); + }); + + $this->assertSame('/root/widgets/welcome', $this->widget->getUri()); + + $this->assertArrayHasKey( + trim($this->widget->getUri(), '/'), + $this->app['router']->getRoutes()->get('GET') + ); } }