From 23ffe1b912138652a78a5a9347f1afd2f5783768 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 22 Sep 2020 12:56:45 -0300 Subject: [PATCH] Bug 26509: Add Koha::Items->safe_delete This patch adds a handy method that can be used to call safe_delete on a resultset. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Items.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/Items.pm | 47 +++++++++++++++++++++++++++++++++++++---- t/db_dependent/Koha/Items.t | 51 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 93 insertions(+), 5 deletions(-) diff --git a/Koha/Items.pm b/Koha/Items.pm index 057a361f90..7975589b25 100644 --- a/Koha/Items.pm +++ b/Koha/Items.pm @@ -20,6 +20,7 @@ package Koha::Items; use Modern::Perl; use Carp; +use Try::Tiny; use Koha::Database; @@ -33,9 +34,7 @@ Koha::Items - Koha Item object set class =head1 API -=head2 Class Methods - -=cut +=head2 Class methods =head3 filter_by_for_loan @@ -50,7 +49,47 @@ sub filter_by_for_loan { return $self->search( { notforloan => [ 0, undef ] } ); } -=head3 type +=head3 safe_delete + + $items->safe_delete; + +Tries to mark all items in the resultset as deleted. This is done inside a transaction +which will be rolled back if an exception is raised. + +=cut + +sub safe_delete { + my ($self) = @_; + + try { + $self->_resultset->result_source->schema->txn_do( + sub { + while ( my $item = $self->next ) { + my $result = $item->safe_delete; + # FIXME: $item->safe_delete has a weird set of return value options: + # - a string representing the blocking condition + # - the result of calling $self->delete (Koha::Item ref) + unless ( ref($result) eq 'Koha::Item' ) { + Koha::Exceptions::Object::CannotBeDeleted->throw( + object => $item, + reason => $result + ); + } + } + } + ); + + return; + } + catch { + $_->rethrow; + }; +} + + +=head2 Internal methods + +=head3 _type =cut diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t index 5cc3f3702e..ed0e4878e4 100755 --- a/t/db_dependent/Koha/Items.t +++ b/t/db_dependent/Koha/Items.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 12; +use Test::More tests => 13; use Test::Exception; use Time::Fake; @@ -947,3 +947,52 @@ $retrieved_item_1->delete; is( Koha::Items->search->count, $nb_of_items - 1, 'Delete should have deleted the item' ); $schema->storage->txn_rollback; + +subtest 'safe_delete() tests' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $library = $builder->build_object({ class => 'Koha::Libraries' }); + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + my $item_1 = $builder->build_sample_item({ library => $library->branchcode }); + my $item_2 = $builder->build_sample_item({ library => $library->branchcode }); + + my $item_1_id = $item_1->id; + my $item_2_id = $item_2->id; + + t::lib::Mocks::mock_userenv( + { patron => $patron, branchcode => $patron->branchcode } ); + + # book_on_loan + C4::Circulation::AddIssue( $patron->unblessed, $item_1->barcode ); + + is( + $item_1->safe_delete, + 'book_on_loan', + 'item that is on loan cannot be deleted', + ); + + my $items_rs = Koha::Items->search({ itemnumber => { -in => [ $item_1_id, $item_2_id ] } }); + is( $items_rs->count, 2, 'We get the right items count' ); + + throws_ok + { $items_rs->safe_delete } + 'Koha::Exceptions::Object::CannotBeDeleted', + 'Exception thrown'; + + is( $@->reason, 'book_on_loan', 'Exception reason is correct' ); + + $items_rs = Koha::Items->search({ itemnumber => { -in => [ $item_1_id, $item_2_id ] } }); + is( $items_rs->count, 2, 'We get the right items count, rollback happened' ); + + C4::Circulation::AddReturn( $item_1->barcode ); + + my $result = $items_rs->safe_delete; + is( $result, undef, 'Successful call returns undef' ); + $items_rs = Koha::Items->search({ itemnumber => { -in => [ $item_1_id, $item_2_id ] } }); + is( $items_rs->count, 0, 'Successful safe delete, items are not ' ); + + $schema->storage->txn_rollback; +}; -- 2.11.0