From 35423b7a4c843cc715102cf45469a885b8e42843 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Tue, 8 Mar 2022 12:31:12 +0000 Subject: [PATCH] Bug 24857: Delete item group when last item is deleted To test: 1 - Find a record with an item gorup, or add a group 2 - Add an item to this group, ensure it is the only item in the group 3 - Delete the item 4 - Confirm the gorup was also deleted 5 - prove t/db_dependent/Koha/Biblio/ItemGroups.t --- Koha/Item.pm | 6 ++++++ t/db_dependent/Koha/Biblio/ItemGroups.t | 9 ++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Koha/Item.pm b/Koha/Item.pm index 4e79c39eff..2eadf79aec 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -234,8 +234,14 @@ sub delete { # FIXME check the item has no current issues # i.e. raise the appropriate exception + # Get the item group so we can delete it later if it has no items left + my $item_group = C4::Context->preference('EnableItemGroups') ? $self->item_group : undef; + my $result = $self->SUPER::delete; + # Delete the item gorup if it has no items left + $item_group->delete if ( $item_group && $item_group->items->count == 0 ); + my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); $indexer->index_records( $self->biblionumber, "specialUpdate", "biblioserver" ) unless $params->{skip_record_index}; diff --git a/t/db_dependent/Koha/Biblio/ItemGroups.t b/t/db_dependent/Koha/Biblio/ItemGroups.t index 321e778842..f0cbe5baf2 100755 --- a/t/db_dependent/Koha/Biblio/ItemGroups.t +++ b/t/db_dependent/Koha/Biblio/ItemGroups.t @@ -36,7 +36,7 @@ t::lib::Mocks::mock_preference('EnableItemGroups', 1); subtest 'add_item() and items() tests' => sub { - plan tests => 8; + plan tests => 10; $schema->storage->txn_begin; @@ -66,5 +66,12 @@ subtest 'add_item() and items() tests' => sub { is( scalar(@items), 1, 'Item group now has only one item'); is( $items[0]->id, $item_2->id, 'Item 2 is correct' ); + # Remove last item + $item_2->delete; + @items = $item_group->items->as_list(); + is( scalar(@items), 0, "Item group now has no items"); + $item_group = Koha::Biblio::ItemGroups->find( $item_group->id ); + is( $item_group, undef, 'ItemGroup is deleted when last item is deleted' ); + $schema->storage->txn_rollback; }; -- 2.30.2