From 42181dfce7bec6983092ca76365211e06a42ad8c 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 Signed-off-by: David Nind Signed-off-by: Martin Renvoize Signed-off-by: Fridolin Somers --- 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 b55509cb37..bbddb00fb9 100644 --- a/C4/Circulation.pm +++ b/C4/Circulation.pm @@ -2341,17 +2341,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 @@ -2372,7 +2361,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.39.5