Skip to content

Commit

Permalink
test for badge module
Browse files Browse the repository at this point in the history
  • Loading branch information
Xinecraft committed Feb 2, 2025
1 parent 6bea271 commit 212ccaa
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
23 changes: 23 additions & 0 deletions database/factories/BadgeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Database\Factories;

use App\Models\Badge;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;

class BadgeFactory extends Factory
{
protected $model = Badge::class;

public function definition()
{
return [
'name' => $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(),
];
}
}
143 changes: 143 additions & 0 deletions tests/Feature/BadgeAdminTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace Tests\Feature;

use App\Models\Badge;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class BadgeAdminTest extends TestCase
{
use RefreshDatabase;

public function test_admin_can_list_badges()
{
$this->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'));
}
}

0 comments on commit 212ccaa

Please sign in to comment.