From d9a2f93b0298ed1818dbcca14ef2d857106fb4a8 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Thu, 11 May 2023 14:43:39 +0000 Subject: [PATCH] Bug 33362: Allow return claims to be resolved even if the issue has since been delete from the database Test Plan: 1) Check out an item 2) Claim return on it, moving it to the old_issues table 3) Delete the old_issue via koha-mysql or Use cleanup_database.pl 4) Attempt to resolve the claim 5) Note the error 6) Apply this patch set 7) Restart all the things! 8) Attempt to resolve the claim 9) No errors! --- Koha/Checkouts/ReturnClaim.pm | 11 +--- t/db_dependent/Koha/Checkouts/ReturnClaim.t | 68 ++++++++++++--------- 2 files changed, 41 insertions(+), 38 deletions(-) diff --git a/Koha/Checkouts/ReturnClaim.pm b/Koha/Checkouts/ReturnClaim.pm index 7dc5a2609c..a91ebd42a6 100644 --- a/Koha/Checkouts/ReturnClaim.pm +++ b/Koha/Checkouts/ReturnClaim.pm @@ -53,15 +53,10 @@ sub store { Koha::Exceptions::Checkouts::ReturnClaims::NoCreatedBy->throw(); } - unless ( !$self->issue_id + # is "fk" is missing, remove it from issue_id + $self->issue_id(undef) unless ( !$self->issue_id || Koha::Checkouts->find( $self->issue_id ) - || Koha::Old::Checkouts->find( $self->issue_id ) ) - { - Koha::Exceptions::Object::FKConstraint->throw( - error => 'Broken FK constraint', - broken_fk => 'issue_id' - ); - } + || Koha::Old::Checkouts->find( $self->issue_id ) ); return $self->SUPER::store; } diff --git a/t/db_dependent/Koha/Checkouts/ReturnClaim.t b/t/db_dependent/Koha/Checkouts/ReturnClaim.t index 4d12bc3751..5d5ebda400 100755 --- a/t/db_dependent/Koha/Checkouts/ReturnClaim.t +++ b/t/db_dependent/Koha/Checkouts/ReturnClaim.t @@ -31,7 +31,7 @@ my $builder = t::lib::TestBuilder->new; subtest "store() tests" => sub { - plan tests => 11; + plan tests => 9; $schema->storage->txn_begin; @@ -109,40 +109,12 @@ subtest "store() tests" => sub { is( ref($claim), 'Koha::Checkouts::ReturnClaim', 'Object type is correct' ); is( Koha::Checkouts::ReturnClaims->search( { issue_id => $checkout->id } )->count, 1, 'Claim stored on the DB'); - { # hide useless warnings - local *STDERR; - open STDERR, '>', '/dev/null'; - - my $another_checkout = $builder->build_object({ class => 'Koha::Checkouts' }); - my $checkout_id = $another_checkout->id; - $another_checkout->delete; - - my $THE_claim; - - throws_ok { - $THE_claim = Koha::Checkouts::ReturnClaim->new( - { - issue_id => $checkout_id, - itemnumber => $checkout->itemnumber, - borrowernumber => $checkout->borrowernumber, - notes => 'Some notes', - created_by => $librarian->borrowernumber - } - )->store; - } - 'Koha::Exceptions::Object::FKConstraint', - 'An exception is thrown on invalid issue_id'; - close STDERR; - - is( $@->broken_fk, 'issue_id', 'Exception field is correct' ); - } - $schema->storage->txn_rollback; }; subtest "resolve() tests" => sub { - plan tests => 9; + plan tests => 10; $schema->storage->txn_begin; @@ -257,6 +229,42 @@ subtest "resolve() tests" => sub { $item->discard_changes; is( $item->itemlost, $new_lost_status, 'Item lost status is updated' ); + # Resolve claim for checkout that has been cleaned from the database + $checkout->delete; + $checkout = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { + borrowernumber => $patron->borrowernumber, + itemnumber => $item->itemnumber, + branchcode => $patron->branchcode + } + } + ); + + $claim = Koha::Checkouts::ReturnClaim->new( + { + issue_id => $checkout->id, + itemnumber => $checkout->itemnumber, + borrowernumber => $checkout->borrowernumber, + notes => 'Some notes', + created_by => $librarian->borrowernumber + } + )->store; + + $checkout->delete; + + $claim->resolve( + { + resolution => "X", + resolved_by => $librarian->id, + resolved_on => $tomorrow, + new_lost_status => $new_lost_status, + } + )->discard_changes; + + is( $claim->issue_id, undef, "Resolved claim cleaned checkout is updated correctly" ); + $schema->storage->txn_rollback; }; -- 2.30.2