diff --git a/tests/Feature/Studio/Catalog/Bottle/AuditTest.php b/tests/Feature/Studio/Catalog/Bottle/AuditTest.php new file mode 100644 index 0000000..6ad3da2 --- /dev/null +++ b/tests/Feature/Studio/Catalog/Bottle/AuditTest.php @@ -0,0 +1,38 @@ +setupUser(); + + $role = Role::firstOrCreate(['name' => 'Owner']); + Permission::firstOrCreate(['name' => 'view bottle']); + Permission::firstOrCreate(['name' => 'audit bottle']); + $role->givePermissionTo(['view bottle', 'audit bottle']); + $this->user->assignRole($role); +}); + +it('renders the bottle audit page correctly', function () { + $outlet = Outlet::factory()->create(); + $bottle = Bottle::factory()->create(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.bottle.audit', ['outlet' => $outlet, 'bottle' => $bottle])) + ->assertOk() + ->assertSeeLivewire(Audit::class); +}); + +it('cannot access bottle audit without permission', function () { + $outlet = Outlet::factory()->create(); + $bottle = Bottle::factory()->create(); + $this->user->roles()->detach(); + $this->user->permissions()->detach(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.bottle.audit', ['outlet' => $outlet, 'bottle' => $bottle])) + ->assertForbidden(); +}); diff --git a/tests/Feature/Studio/Catalog/Bottle/CreateTest.php b/tests/Feature/Studio/Catalog/Bottle/CreateTest.php new file mode 100644 index 0000000..3988b80 --- /dev/null +++ b/tests/Feature/Studio/Catalog/Bottle/CreateTest.php @@ -0,0 +1,68 @@ +setupUser(); + + $role = Role::firstOrCreate(['name' => 'Owner']); + Permission::firstOrCreate(['name' => 'view bottle']); + Permission::firstOrCreate(['name' => 'create bottle']); + $role->givePermissionTo(['view bottle', 'create bottle']); + $this->user->assignRole($role); +}); + +it('renders the bottle create page correctly', function () { + $this->actingAs($this->user) + ->get(route('studio.catalog.bottle.create')) + ->assertOk() + ->assertSeeLivewire(Create::class); +}); + +it('shows validation errors when fields are empty', function () { + Livewire::actingAs($this->user) + ->test(Create::class) + ->call('save') + ->assertHasErrors([ + 'form.name' => 'required', + 'form.size' => 'required', + 'form.cost_price' => 'required', + 'form.sale_price' => 'required', + 'form.outlet_ids' => 'required', + ]); +}); + +it('can store a new bottle', function () { + $outlet = Outlet::factory()->create(); + + Livewire::actingAs($this->user) + ->test(Create::class) + ->set('form.name', 'Bottle C') + ->set('form.size', '100') + ->set('form.cost_price', '5000') + ->set('form.sale_price', '15000') + ->set('form.outlet_ids', [$outlet->id]) + ->call('save') + ->assertHasNoErrors() + ->assertRedirect(route('studio.catalog.bottle.index', ['notification' => 'Botol berhasil ditambahkan.'])); + + $this->assertDatabaseHas('bottles', [ + 'name' => 'Bottle C', + 'size' => 100, + 'cost_price' => 5000, + 'sale_price' => 15000, + ]); +}); + +it('cannot access bottle create without permission', function () { + $this->user->roles()->detach(); + $this->user->permissions()->detach(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.bottle.create')) + ->assertForbidden(); +}); diff --git a/tests/Feature/Studio/Catalog/Bottle/EditTest.php b/tests/Feature/Studio/Catalog/Bottle/EditTest.php new file mode 100644 index 0000000..ed6125a --- /dev/null +++ b/tests/Feature/Studio/Catalog/Bottle/EditTest.php @@ -0,0 +1,64 @@ +setupUser(); + + $role = Role::firstOrCreate(['name' => 'Owner']); + Permission::firstOrCreate(['name' => 'view bottle']); + Permission::firstOrCreate(['name' => 'update bottle']); + $role->givePermissionTo(['view bottle', 'update bottle']); + $this->user->assignRole($role); +}); + +it('renders the bottle edit page correctly', function () { + $bottle = Bottle::factory()->create(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.bottle.edit', ['bottle' => $bottle])) + ->assertOk() + ->assertSeeLivewire(Edit::class); +}); + +it('can load data correctly into edit form', function () { + $bottle = Bottle::factory()->create(['name' => 'Old Bottle Name']); + + Livewire::actingAs($this->user) + ->test(Edit::class, ['bottle' => $bottle]) + ->assertSet('form.name', 'Old Bottle Name') + ->assertSet('form.bottle.id', $bottle->id); +}); + +it('can update a bottle', function () { + $bottle = Bottle::factory()->create(); + $outlet = Outlet::factory()->create(); + + Livewire::actingAs($this->user) + ->test(Edit::class, ['bottle' => $bottle]) + ->set('form.name', 'Updated Bottle Name') + ->set('form.outlet_ids', [$outlet->id]) + ->call('save') + ->assertHasNoErrors() + ->assertRedirect(route('studio.catalog.bottle.index', ['notification' => 'Botol berhasil diperbarui.'])); + + $this->assertDatabaseHas('bottles', [ + 'id' => $bottle->id, + 'name' => 'Updated Bottle Name', + ]); +}); + +it('cannot access bottle edit without permission', function () { + $bottle = Bottle::factory()->create(); + $this->user->roles()->detach(); + $this->user->permissions()->detach(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.bottle.edit', ['bottle' => $bottle])) + ->assertForbidden(); +}); diff --git a/tests/Feature/Studio/Catalog/Bottle/IndexTest.php b/tests/Feature/Studio/Catalog/Bottle/IndexTest.php new file mode 100644 index 0000000..5beed47 --- /dev/null +++ b/tests/Feature/Studio/Catalog/Bottle/IndexTest.php @@ -0,0 +1,45 @@ +setupUser(); + + $role = Role::firstOrCreate(['name' => 'Owner']); + Permission::firstOrCreate(['name' => 'view bottle']); + Permission::firstOrCreate(['name' => 'delete bottle']); + $role->givePermissionTo(['view bottle', 'delete bottle']); + $this->user->assignRole($role); +}); + +it('renders the bottle index page correctly', function () { + $this->actingAs($this->user) + ->get(route('studio.catalog.bottle.index')) + ->assertOk() + ->assertSeeLivewire(Index::class); +}); + +it('can delete a bottle', function () { + $bottle = Bottle::factory()->create(); + + Livewire::actingAs($this->user) + ->test(Index::class) + ->call('delete', $bottle->id) + ->assertHasNoErrors() + ->assertDispatched('refreshDatatable'); + + $this->assertSoftDeleted('bottles', ['id' => $bottle->id]); +}); + +it('cannot access bottle index without permission', function () { + $this->user->roles()->detach(); + $this->user->permissions()->detach(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.bottle.index')) + ->assertForbidden(); +}); diff --git a/tests/Feature/Studio/Catalog/Product/AuditTest.php b/tests/Feature/Studio/Catalog/Product/AuditTest.php new file mode 100644 index 0000000..f1a7c05 --- /dev/null +++ b/tests/Feature/Studio/Catalog/Product/AuditTest.php @@ -0,0 +1,38 @@ +setupUser(); + + $role = Role::firstOrCreate(['name' => 'Owner']); + Permission::firstOrCreate(['name' => 'view product']); + Permission::firstOrCreate(['name' => 'audit product']); + $role->givePermissionTo(['view product', 'audit product']); + $this->user->assignRole($role); +}); + +it('renders the product audit page correctly', function () { + $outlet = Outlet::factory()->create(); + $product = Product::factory()->create(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.product.audit', ['outlet' => $outlet, 'product' => $product])) + ->assertOk() + ->assertSeeLivewire(Audit::class); +}); + +it('cannot access product audit without permission', function () { + $outlet = Outlet::factory()->create(); + $product = Product::factory()->create(); + $this->user->roles()->detach(); + $this->user->permissions()->detach(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.product.audit', ['outlet' => $outlet, 'product' => $product])) + ->assertForbidden(); +}); diff --git a/tests/Feature/Studio/Catalog/Product/CreateTest.php b/tests/Feature/Studio/Catalog/Product/CreateTest.php new file mode 100644 index 0000000..4b5dc2f --- /dev/null +++ b/tests/Feature/Studio/Catalog/Product/CreateTest.php @@ -0,0 +1,68 @@ +setupUser(); + + $role = Role::firstOrCreate(['name' => 'Owner']); + Permission::firstOrCreate(['name' => 'view product']); + Permission::firstOrCreate(['name' => 'create product']); + $role->givePermissionTo(['view product', 'create product']); + $this->user->assignRole($role); +}); + +it('renders the product create page correctly', function () { + $this->actingAs($this->user) + ->get(route('studio.catalog.product.create')) + ->assertOk() + ->assertSeeLivewire(Create::class); +}); + +it('shows validation errors when fields are empty', function () { + Livewire::actingAs($this->user) + ->test(Create::class) + ->call('save') + ->assertHasErrors([ + 'form.name' => 'required', + 'form.sku' => 'required', + 'form.cost_price' => 'required', + 'form.sale_price' => 'required', + 'form.outlet_ids' => 'required', + ]); +}); + +it('can store a new product', function () { + $outlet = Outlet::factory()->create(); + + Livewire::actingAs($this->user) + ->test(Create::class) + ->set('form.name', 'Product B') + ->set('form.sku', 'SKU-Product-B') + ->set('form.cost_price', '20000') + ->set('form.sale_price', '40000') + ->set('form.outlet_ids', [$outlet->id]) + ->call('save') + ->assertHasNoErrors() + ->assertRedirect(route('studio.catalog.product.index', ['notification' => 'Produk berhasil ditambahkan.'])); + + $this->assertDatabaseHas('products', [ + 'name' => 'Product B', + 'sku' => 'SKU-Product-B', + 'cost_price' => 20000, + 'sale_price' => 40000, + ]); +}); + +it('cannot access product create without permission', function () { + $this->user->roles()->detach(); + $this->user->permissions()->detach(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.product.create')) + ->assertForbidden(); +}); diff --git a/tests/Feature/Studio/Catalog/Product/EditTest.php b/tests/Feature/Studio/Catalog/Product/EditTest.php new file mode 100644 index 0000000..97430e6 --- /dev/null +++ b/tests/Feature/Studio/Catalog/Product/EditTest.php @@ -0,0 +1,64 @@ +setupUser(); + + $role = Role::firstOrCreate(['name' => 'Owner']); + Permission::firstOrCreate(['name' => 'view product']); + Permission::firstOrCreate(['name' => 'update product']); + $role->givePermissionTo(['view product', 'update product']); + $this->user->assignRole($role); +}); + +it('renders the product edit page correctly', function () { + $product = Product::factory()->create(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.product.edit', ['product' => $product])) + ->assertOk() + ->assertSeeLivewire(Edit::class); +}); + +it('can load data correctly into edit form', function () { + $product = Product::factory()->create(['name' => 'Old Product Name']); + + Livewire::actingAs($this->user) + ->test(Edit::class, ['product' => $product]) + ->assertSet('form.name', 'Old Product Name') + ->assertSet('form.product.id', $product->id); +}); + +it('can update a product', function () { + $product = Product::factory()->create(); + $outlet = Outlet::factory()->create(); + + Livewire::actingAs($this->user) + ->test(Edit::class, ['product' => $product]) + ->set('form.name', 'Updated Product Name') + ->set('form.outlet_ids', [$outlet->id]) + ->call('save') + ->assertHasNoErrors() + ->assertRedirect(route('studio.catalog.product.index', ['notification' => 'Produk berhasil diperbarui.'])); + + $this->assertDatabaseHas('products', [ + 'id' => $product->id, + 'name' => 'Updated Product Name', + ]); +}); + +it('cannot access product edit without permission', function () { + $product = Product::factory()->create(); + $this->user->roles()->detach(); + $this->user->permissions()->detach(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.product.edit', ['product' => $product])) + ->assertForbidden(); +}); diff --git a/tests/Feature/Studio/Catalog/Product/IndexTest.php b/tests/Feature/Studio/Catalog/Product/IndexTest.php new file mode 100644 index 0000000..e7f0d0e --- /dev/null +++ b/tests/Feature/Studio/Catalog/Product/IndexTest.php @@ -0,0 +1,45 @@ +setupUser(); + + $role = Role::firstOrCreate(['name' => 'Owner']); + Permission::firstOrCreate(['name' => 'view product']); + Permission::firstOrCreate(['name' => 'delete product']); + $role->givePermissionTo(['view product', 'delete product']); + $this->user->assignRole($role); +}); + +it('renders the product index page correctly', function () { + $this->actingAs($this->user) + ->get(route('studio.catalog.product.index')) + ->assertOk() + ->assertSeeLivewire(Index::class); +}); + +it('can delete a product', function () { + $product = Product::factory()->create(); + + Livewire::actingAs($this->user) + ->test(Index::class) + ->call('delete', $product->id) + ->assertHasNoErrors() + ->assertDispatched('refreshDatatable'); + + $this->assertSoftDeleted('products', ['id' => $product->id]); +}); + +it('cannot access product index without permission', function () { + $this->user->roles()->detach(); + $this->user->permissions()->detach(); + + $this->actingAs($this->user) + ->get(route('studio.catalog.product.index')) + ->assertForbidden(); +});