From 212ccaa4d05b890c831414a692e7864be7e82f33 Mon Sep 17 00:00:00 2001 From: Xinecraft Date: Sun, 2 Feb 2025 19:48:18 +0530 Subject: [PATCH] test for badge module --- database/factories/BadgeFactory.php | 23 +++++ tests/Feature/BadgeAdminTest.php | 143 ++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+) create mode 100644 database/factories/BadgeFactory.php create mode 100644 tests/Feature/BadgeAdminTest.php diff --git a/database/factories/BadgeFactory.php b/database/factories/BadgeFactory.php new file mode 100644 index 000000000..20278a958 --- /dev/null +++ b/database/factories/BadgeFactory.php @@ -0,0 +1,23 @@ + $this->faker->words(2, true), + 'shortname' => $this->faker->unique()->slug(2), + 'is_sticky' => $this->faker->boolean, + 'sort_order' => $this->faker->numberBetween(1, 100), + 'created_by' => User::factory(), + ]; + } +} \ No newline at end of file diff --git a/tests/Feature/BadgeAdminTest.php b/tests/Feature/BadgeAdminTest.php new file mode 100644 index 000000000..a167ac98b --- /dev/null +++ b/tests/Feature/BadgeAdminTest.php @@ -0,0 +1,143 @@ +actingAs(User::whereId(1)->first()); // Super admin + $response = $this->get(route('admin.badge.index')); + + $response->assertStatus(200); + } + + public function test_admin_can_view_create_form() + { + $this->actingAs(User::whereId(1)->first()); // Super admin + $response = $this->get(route('admin.badge.create')); + + $response->assertStatus(200); + } + + public function test_admin_can_create_new_badge() + { + Storage::fake('public'); + + $this->actingAs(User::whereId(1)->first()); // Super admin + $response = $this->post(route('admin.badge.store'), [ + 'name' => 'Test Badge', + 'shortname' => 'test-badge', + 'is_sticky' => true, + 'sort_order' => 1, + 'photo' => UploadedFile::fake()->image('badge.jpg', 100, 100) + ]); + + $this->assertDatabaseHas('badges', [ + 'name' => 'Test Badge', + 'shortname' => 'test-badge', + 'is_sticky' => true, + 'sort_order' => 1 + ]); + + $badge = Badge::where('shortname', 'test-badge')->first(); + $this->assertNotNull($badge->getFirstMedia('badge')); + + $response->assertRedirect(route('admin.badge.index')); + } + + public function test_admin_cannot_create_badge_without_required_fields() + { + $this->actingAs(User::whereId(1)->first()); // Super admin + $response = $this->post(route('admin.badge.store'), [ + 'name' => '', + 'shortname' => '', + 'is_sticky' => true + ]); + + $response->assertSessionHasErrors(['name', 'shortname', 'photo']); + } + + public function test_admin_can_update_badge() + { + Storage::fake('public'); + + $badge = Badge::factory()->create([ + 'name' => 'Old Name', + 'shortname' => 'old-name', + 'is_sticky' => false, + 'sort_order' => 1 + ]); + + $this->actingAs(User::whereId(1)->first()); // Super admin + $response = $this->put(route('admin.badge.update', $badge->id), [ + 'name' => 'Updated Badge', + 'shortname' => 'updated-badge', + 'is_sticky' => true, + 'sort_order' => 2, + 'photo' => UploadedFile::fake()->image('new-badge.jpg', 100, 100) + ]); + + $this->assertDatabaseHas('badges', [ + 'id' => $badge->id, + 'name' => 'Updated Badge', + 'shortname' => 'updated-badge', + 'is_sticky' => true, + 'sort_order' => 2 + ]); + + $badge->refresh(); + $this->assertNotNull($badge->getFirstMedia('badge')); + + $response->assertRedirect(route('admin.badge.index')); + } + + public function test_admin_can_delete_badge() + { + $badge = Badge::factory()->create(); + + $this->actingAs(User::whereId(1)->first()); // Super admin + $response = $this->delete(route('admin.badge.delete', $badge->id)); + + $this->assertDatabaseMissing('badges', ['id' => $badge->id]); + $response->assertRedirect(); + } + + public function test_admin_can_update_badge_without_changing_photo() + { + $badge = Badge::factory()->create([ + 'name' => 'Old Name', + 'shortname' => 'old-name', + 'is_sticky' => false, + 'sort_order' => 1 + ]); + + $this->actingAs(User::whereId(1)->first()); // Super admin + $response = $this->put(route('admin.badge.update', $badge->id), [ + 'name' => 'Updated Badge', + 'shortname' => 'updated-badge', + 'is_sticky' => true, + 'sort_order' => 2 + ]); + + $this->assertDatabaseHas('badges', [ + 'id' => $badge->id, + 'name' => 'Updated Badge', + 'shortname' => 'updated-badge', + 'is_sticky' => true, + 'sort_order' => 2 + ]); + + $response->assertRedirect(route('admin.badge.index')); + } +} \ No newline at end of file