feat(basicdata): tambahkan fitur relasi parent-child pada cabang

- Menambahkan kolom `parent_id` pada tabel `branches` dengan migrasi baru.
- Update model `Branch`:
  - Menambahkan relasi `parent()` untuk mendapatkan cabang induk.
  - Menambahkan relasi `children()` untuk mendapatkan anak cabang.
- Update `BranchController`:
  - Menampilkan daftar cabang induk saat membuat atau mengedit cabang.
  - Cek validasi agar cabang tidak bisa menjadi induk dirinya sendiri.
  - Tambahkan larangan hapus cabang jika memiliki anak cabang, baik untuk hapus tunggal maupun multiple.
- Update validation rules pada `BranchRequest` untuk memastikan validitas `parent_id`.
- Update tampilan:
  - Formulir pembuatan/edit cabang: Menampilkan dropdown untuk memilih cabang induk.
  - Daftar cabang: Menampilkan kolom untuk cabang induk.
- Tambahkan test unit:
  - Validasi relasi parent-child pada penyimpanan dan pembaruan cabang.
  - Melarang penghapusan cabang yang memiliki anak.
  - Memastikan perilaku relasi parent-child sesuai ekspektasi.

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-18 15:13:52 +07:00
parent 1998d89f84
commit 4a644c3b5d
7 changed files with 342 additions and 10 deletions

View File

@@ -18,6 +18,7 @@ class BranchControllerTest extends TestCase
protected $user;
protected $adminRole;
protected $branch;
protected $parentBranch;
protected function setUp(): void
{
@@ -64,6 +65,12 @@ class BranchControllerTest extends TestCase
$this->user = User::factory()->create();
$this->user->assignRole($this->adminRole);
// Create a parent branch for testing
$this->parentBranch = Branch::create([
'code' => 'PARENT',
'name' => 'Parent Branch'
]);
// Create a branch for testing
$this->branch = Branch::create([
'code' => 'TEST',
@@ -137,6 +144,28 @@ class BranchControllerTest extends TestCase
$this->assertDatabaseHas('branches', $branchData);
}
#[Test]
public function user_with_permission_can_store_branch_with_parent()
{
$branchData = [
'code' => 'CHILD',
'name' => 'Child Branch',
'parent_id' => $this->parentBranch->id
];
$response = $this->actingAs($this->user)
->post(route('basicdata.branch.store'), $branchData);
$response->assertRedirect(route('basicdata.branch.index'));
$this->assertDatabaseHas('branches', $branchData);
// Verify the relationship
$childBranch = Branch::where('code', 'CHILD')->first();
$this->assertEquals($this->parentBranch->id, $childBranch->parent_id);
$this->assertTrue($childBranch->parent()->exists());
$this->assertEquals($this->parentBranch->id, $childBranch->parent->id);
}
#[Test]
public function user_without_permission_cannot_store_branch()
{
@@ -201,6 +230,53 @@ class BranchControllerTest extends TestCase
$this->assertDatabaseHas('branches', $updatedData);
}
#[Test]
public function user_with_permission_can_update_branch_with_parent()
{
$updatedData = [
'code' => 'UPD',
'name' => 'Updated Branch',
'parent_id' => $this->parentBranch->id
];
$response = $this->actingAs($this->user)
->put(route('basicdata.branch.update', $this->branch->id), $updatedData);
$response->assertRedirect(route('basicdata.branch.index'));
$this->assertDatabaseHas('branches', $updatedData);
// Verify the relationship
$this->branch->refresh();
$this->assertEquals($this->parentBranch->id, $this->branch->parent_id);
$this->assertTrue($this->branch->parent()->exists());
$this->assertEquals($this->parentBranch->id, $this->branch->parent->id);
}
#[Test]
public function user_with_permission_can_remove_parent_from_branch()
{
// First set a parent
$this->branch->update(['parent_id' => $this->parentBranch->id]);
$this->assertEquals($this->parentBranch->id, $this->branch->parent_id);
// Then remove it
$updatedData = [
'code' => 'UPD',
'name' => 'Updated Branch',
'parent_id' => null
];
$response = $this->actingAs($this->user)
->put(route('basicdata.branch.update', $this->branch->id), $updatedData);
$response->assertRedirect(route('basicdata.branch.index'));
// Verify the relationship is removed
$this->branch->refresh();
$this->assertNull($this->branch->parent_id);
$this->assertFalse($this->branch->parent()->exists());
}
#[Test]
public function user_without_permission_cannot_update_branch()
{
@@ -277,4 +353,126 @@ class BranchControllerTest extends TestCase
$response->assertStatus(403);
}
#[Test]
public function branch_cannot_be_its_own_parent()
{
$updatedData = [
'code' => 'SELF',
'name' => 'Self-Referencing Branch',
'parent_id' => $this->branch->id
];
$response = $this->actingAs($this->user)
->from(route('basicdata.branch.edit', $this->branch->id)) // Tambahkan ini untuk mengetahui URL sebelumnya
->put(route('basicdata.branch.update', $this->branch->id), $updatedData);
// Pastikan redirect back dengan kesalahan
$response->assertRedirect();
$response->assertSessionHasErrors('parent_id');
// Periksa bahwa parent_id tidak berubah
$this->branch->refresh();
$this->assertNull($this->branch->parent_id);
}
#[Test]
public function cannot_delete_branch_with_children()
{
// Create a child branch
$childBranch = Branch::create([
'code' => 'CHILD',
'name' => 'Child Branch',
'parent_id' => $this->parentBranch->id
]);
// Verify the relationship is established
$this->assertEquals($this->parentBranch->id, $childBranch->parent_id);
$this->assertTrue($this->parentBranch->children()->exists());
// Try to delete the parent branch
$response = $this->actingAs($this->user)
->delete(route('basicdata.branch.destroy', $this->parentBranch->id));
// Assert that the request fails with the expected message
$response->assertJson([
'success' => false,
'message' => 'Cabang dengan anak cabang tidak dapat dihapus.'
]);
$response->assertStatus(422); // Unprocessable Entity
// Verify the parent branch was not deleted
$this->assertDatabaseHas('branches', [
'id' => $this->parentBranch->id,
'deleted_at' => null
]);
}
#[Test]
public function cannot_delete_multiple_branches_if_any_has_children()
{
// Create a child branch
$childBranch = Branch::create([
'code' => 'CHILD',
'name' => 'Child Branch',
'parent_id' => $this->parentBranch->id
]);
// Create another branch without children
$anotherBranch = Branch::create([
'code' => 'ANOTHER',
'name' => 'Another Branch'
]);
// Try to delete both the parent branch and another branch
$response = $this->actingAs($this->user)
->post(route('basicdata.branch.deleteMultiple'), [
'ids' => [$this->parentBranch->id, $anotherBranch->id]
]);
// Assert that the request fails with the expected message
$response->assertJson([
'success' => false,
'message' => 'Beberapa cabang memiliki anak cabang dan tidak dapat dihapus.'
]);
$response->assertStatus(422); // Unprocessable Entity
// Verify neither branch was deleted
$this->assertDatabaseHas('branches', [
'id' => $this->parentBranch->id,
'deleted_at' => null
]);
$this->assertDatabaseHas('branches', [
'id' => $anotherBranch->id,
'deleted_at' => null
]);
}
#[Test]
public function branch_has_correct_children_relationship()
{
// Create a child branch
$childBranch = Branch::create([
'code' => 'CHILD1',
'name' => 'Child Branch 1',
'parent_id' => $this->parentBranch->id
]);
// Create another child branch
$anotherChildBranch = Branch::create([
'code' => 'CHILD2',
'name' => 'Child Branch 2',
'parent_id' => $this->parentBranch->id
]);
// Refresh parent branch
$this->parentBranch->refresh();
// Assert that the parent has two children
$this->assertEquals(2, $this->parentBranch->children()->count());
// Assert that the children collection contains the two child branches
$this->assertTrue($this->parentBranch->children->contains($childBranch));
$this->assertTrue($this->parentBranch->children->contains($anotherChildBranch));
}
}