From c79d69610ec83c3f35b9b087da8d8d656ef94d4b Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 5 Nov 2020 14:48:41 -0500 Subject: [PATCH] Bug 24857: (QA follow-up) Fix unit tests and code for changes in Koha::Item::delete --- Koha/Biblio/Volume.pm | 2 +- Koha/Item.pm | 21 ++++++++------------- t/db_dependent/Koha/Volumes.t | 13 +++++++++---- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/Koha/Biblio/Volume.pm b/Koha/Biblio/Volume.pm index 70633085fa..a5eda853dc 100644 --- a/Koha/Biblio/Volume.pm +++ b/Koha/Biblio/Volume.pm @@ -68,7 +68,7 @@ sub items { my $items_rs = $self->_result->volume_items; my @item_numbers = $items_rs->get_column('itemnumber')->all; - return unless @item_numbers; + return Koha::Items->new->empty unless @item_numbers; return Koha::Items->search( { diff --git a/Koha/Item.pm b/Koha/Item.pm index 015c5000db..64cb72a1ea 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -216,24 +216,18 @@ sub delete { # FIXME check the item has no current issues # i.e. raise the appropriate exception + # Get the volume so we can delete it later if it has no items left + my $volume = C4::Context->preference('EnableVolumes') ? $self->volume : undef; + my $result = $self->SUPER::delete; + # Delete the volume if it has no items left + $volume->delete if ( $volume && $volume->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}; - if ( C4::Context->preference('EnableVolumes') ) { - my $volume_item = - Koha::Biblio::Volume::Items->find( { itemnumber => $self->itemnumber } ); - my $volume_id = $volume_item ? $volume_item->volume_id : undef; - - # If this item is the last item on a volume, delete the volume as well - if ($volume_id) { - my $volume = Koha::Biblio::Volumes->find($volume_id); - $volume->delete unless $volume->items->count > 1; - } - } - $self->_after_item_action_hooks({ action => 'delete' }); logaction( "CATALOGUING", "DELETE", $self->itemnumber, "item" ) @@ -416,7 +410,8 @@ sub volume { my $volume_rs = $volume_item->volume; return unless $volume_rs; - return Koha::Biblio::Volume->_new_from_dbic( $volume_rs ); + my $volume = Koha::Biblio::Volume->_new_from_dbic( $volume_rs ); + return $volume; } =head3 holds diff --git a/t/db_dependent/Koha/Volumes.t b/t/db_dependent/Koha/Volumes.t index 2930b83530..1d4b17f9d2 100644 --- a/t/db_dependent/Koha/Volumes.t +++ b/t/db_dependent/Koha/Volumes.t @@ -73,11 +73,11 @@ subtest 'Test Koha::Biblio::Volume::add_item & Koha::Biblio::Volume::items' => s my $volume = Koha::Biblio::Volume->new( { biblionumber => $biblio->id } )->store(); - my @items = $volume->items; - is( scalar(@items), 0, 'Volume has no items'); + my $items = $volume->items; + is( $items->count, 0, 'Volume has no items'); $volume->add_item({ item_id => $item_1->id }); - @items = $volume->items->as_list; + my @items = $volume->items->as_list; is( scalar(@items), 1, 'Volume has one item'); is( $items[0]->id, $item_1->id, 'Item 1 is correct' ); @@ -117,7 +117,7 @@ subtest 'Test Koha::Item::volume' => sub { subtest 'Koha::Item::delete should delete volume if no other items are using the volume' => sub { - plan tests => 8; + plan tests => 11; $schema->storage->txn_begin; @@ -137,11 +137,16 @@ subtest 'Koha::Item::delete should delete volume if no other items are using the my $volume = Koha::Biblio::Volumes->find( $volume_1->id ); is( $volume->id, $volume_1->id, 'Found the correct volume'); + is( $item_1->volume->id, $volume_1->id, 'Item 1 has correct volume'); + is( $item_2->volume->id, $volume_1->id, 'Item 2 has correct volume'); + $item_1->delete(); is( $biblio->items->count, 1, 'Bib has 2 item'); $volume = Koha::Biblio::Volumes->find( $volume_1->id ); is( $volume->id, $volume_1->id, 'Volume still exists after deleting and item, but other items remain'); + is( $item_2->volume->id, $volume_1->id, 'Item 2 still has correct volume'); + $item_2->delete(); is( $biblio->items->count, 0, 'Bib has 0 items'); $volume = Koha::Biblio::Volumes->find( $volume_1->id ); -- 2.24.1 (Apple Git-126)