- Added CategoryController for handling category operations. - Created CategoryRequest for validation of category data. - Introduced CategoryService for business logic related to categories. - Implemented sluggable functionality in the Category model for automatic slug generation. - Developed UI components for category management, including a data table and dialogs for creating and editing categories. - Updated routes to include resourceful routes for categories.
161 lines
4.4 KiB
PHP
161 lines
4.4 KiB
PHP
<?php
|
|
|
|
use App\Models\Category;
|
|
use App\Models\User;
|
|
use Inertia\Testing\AssertableInertia as Assert;
|
|
|
|
test('guests are redirected to the login page', function () {
|
|
$response = $this->get(route('admin.master.categories.index'));
|
|
$response->assertRedirect(route('login'));
|
|
});
|
|
|
|
test('authenticated users can visit the category index page', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->get(route('admin.master.categories.index'));
|
|
$response->assertOk();
|
|
});
|
|
|
|
test('category index page displays categories', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$categories = Category::factory()->count(3)->create();
|
|
|
|
$response = $this->get(route('admin.master.categories.index'));
|
|
$response->assertOk();
|
|
$response->assertInertia(fn (Assert $page) => $page
|
|
->component('admin/master/category/index')
|
|
->has('categories', 3)
|
|
);
|
|
});
|
|
|
|
test('category can be created', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->post(route('admin.master.categories.store'), [
|
|
'name' => 'Kategori Test',
|
|
]);
|
|
|
|
$response
|
|
->assertSessionHasNoErrors()
|
|
->assertRedirect(route('admin.master.categories.index'));
|
|
|
|
$this->assertDatabaseHas('categories', [
|
|
'name' => 'Kategori Test',
|
|
'slug' => 'kategori-test',
|
|
]);
|
|
});
|
|
|
|
test('category name is required', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->post(route('admin.master.categories.store'), [
|
|
'name' => '',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors('name');
|
|
});
|
|
|
|
test('category name must not exceed 100 characters', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$response = $this->post(route('admin.master.categories.store'), [
|
|
'name' => str_repeat('a', 101),
|
|
]);
|
|
|
|
$response->assertSessionHasErrors('name');
|
|
});
|
|
|
|
test('category name must be unique', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
Category::factory()->create(['name' => 'Existing Category']);
|
|
|
|
$response = $this->post(route('admin.master.categories.store'), [
|
|
'name' => 'Existing Category',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors('name');
|
|
});
|
|
|
|
test('category slug is automatically generated', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$this->post(route('admin.master.categories.store'), [
|
|
'name' => 'Kategori Otomatis',
|
|
]);
|
|
|
|
$this->assertDatabaseHas('categories', [
|
|
'name' => 'Kategori Otomatis',
|
|
'slug' => 'kategori-otomatis',
|
|
]);
|
|
});
|
|
|
|
test('category can be updated', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$category = Category::factory()->create();
|
|
|
|
$response = $this->put(route('admin.master.categories.update', $category), [
|
|
'name' => 'Kategori Updated',
|
|
]);
|
|
|
|
$response
|
|
->assertSessionHasNoErrors()
|
|
->assertRedirect(route('admin.master.categories.index'));
|
|
|
|
$category->refresh();
|
|
expect($category->name)->toBe('Kategori Updated');
|
|
expect($category->slug)->toBe('kategori-updated');
|
|
});
|
|
|
|
test('category name can be updated to itself', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$category = Category::factory()->create(['name' => 'My Category']);
|
|
|
|
$response = $this->put(route('admin.master.categories.update', $category), [
|
|
'name' => 'My Category',
|
|
]);
|
|
|
|
$response->assertSessionHasNoErrors();
|
|
});
|
|
|
|
test('category update name must be unique excluding itself', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$category = Category::factory()->create(['name' => 'First']);
|
|
Category::factory()->create(['name' => 'Second']);
|
|
|
|
$response = $this->put(route('admin.master.categories.update', $category), [
|
|
'name' => 'Second',
|
|
]);
|
|
|
|
$response->assertSessionHasErrors('name');
|
|
});
|
|
|
|
test('category can be deleted', function () {
|
|
$user = User::factory()->create();
|
|
$this->actingAs($user);
|
|
|
|
$category = Category::factory()->create();
|
|
|
|
$response = $this->delete(route('admin.master.categories.destroy', $category));
|
|
|
|
$response
|
|
->assertSessionHasNoErrors()
|
|
->assertRedirect(route('admin.master.categories.index'));
|
|
|
|
$this->assertSoftDeleted('categories', ['id' => $category->id]);
|
|
});
|