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

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Nick Clemens 2022-03-08 12:31:12 +00:00 committed by Tomas Cohen Arazi
parent f83c7f8655
commit 6361638407
Signed by: tomascohen
GPG key ID: 0A272EA1B2F3C15F
2 changed files with 14 additions and 1 deletions

View file

@ -230,8 +230,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};

View file

@ -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;
};