From 46a504092f6cdf868cef2df3b32a04aaea75d0a2 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 4 Feb 2022 08:47:05 -0300 Subject: [PATCH] Bug 30023: Make MarkIssueReturned use ->anonymize Koha::Old::Checkout->anonymize takes care of checking the syspref and raises an exception if not set. So no we can now leverage on it, instead of checking manually and then tweaking the checkout object manually as well. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Circulation/MarkIssueReturned.t => SUCCESS: Tests pass! 3. Sign off :-D --- C4/Circulation.pm | 13 +------------ t/db_dependent/Circulation/MarkIssueReturned.t | 11 ++++++++--- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/C4/Circulation.pm b/C4/Circulation.pm index d59fbf6380..0e380d93d6 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2359,17 +2359,6 @@ sub MarkIssueReturned { my $issue_id = $issue->issue_id; - my $anonymouspatron; - if ( $privacy && $privacy == 2 ) { - # The default of 0 will not work due to foreign key constraints - # The anonymisation will fail if AnonymousPatron is not a valid entry - # We need to check if the anonymous patron exist, Koha will fail loudly if it does not - # Note that a warning should appear on the about page (System information tab). - $anonymouspatron = C4::Context->preference('AnonymousPatron'); - die "Fatal error: the patron ($borrowernumber) has requested their circulation history be anonymized on check-in, but the AnonymousPatron system preference is empty or not set correctly." - unless Koha::Patrons->find( $anonymouspatron ); - } - my $schema = Koha::Database->schema; # FIXME Improve the return value and handle it from callers @@ -2390,7 +2379,7 @@ sub MarkIssueReturned { # anonymise patron checkout immediately if $privacy set to 2 and AnonymousPatron is set to a valid borrowernumber if ( $privacy && $privacy == 2) { - $old_checkout->borrowernumber($anonymouspatron)->store; + $old_checkout->anonymize; } # And finally delete the issue diff --git a/t/db_dependent/Circulation/MarkIssueReturned.t b/t/db_dependent/Circulation/MarkIssueReturned.t index 094276ffb4..654ac37451 100755 --- a/t/db_dependent/Circulation/MarkIssueReturned.t +++ b/t/db_dependent/Circulation/MarkIssueReturned.t @@ -76,7 +76,7 @@ subtest 'Failure tests' => sub { subtest 'Anonymous patron tests' => sub { - plan tests => 2; + plan tests => 3; $schema->storage->txn_begin; @@ -99,8 +99,13 @@ subtest 'Anonymous patron tests' => sub { t::lib::Mocks::mock_preference( 'AnonymousPatron', '' ); my $issue = C4::Circulation::AddIssue( $patron->unblessed, $item->barcode ); - eval { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ) }; - like ( $@, qr, 'AnonymousPatron is not set - Fatal error on anonymization' ); + + throws_ok + { C4::Circulation::MarkIssueReturned( $patron->borrowernumber, $item->itemnumber, undef, 2 ); } + 'Koha::Exceptions::SysPref::NotSet', + 'AnonymousPatron not set causes an exception'; + + is( $@->syspref, 'AnonymousPatron', 'AnonymousPatron is not set - Fatal error on anonymization' ); Koha::Checkouts->find( $issue->issue_id )->delete; # Create a valid anonymous user -- 2.32.0