From aadaf1d0aca981616713578cd91d7d1747ec6b89 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 3 Jan 2022 15:06:23 -0300 Subject: [PATCH] Bug 29525: Add Koha::Old::Holds->filter_by_anonymizable This patch adds the 'filter_by_anonymizable' method, and tests for it. A new DBIC relationship is added as well to the OldReserve schema file. To test: 1. Apply this patch 2. Run: $ kshell k$ t/db_dependent/Koha/Old/Holds.t => SUCCESS: Tests pass! 3. Sign off :-D --- Koha/Old/Holds.pm | 44 ++++++++++++++++++ Koha/Schema/Result/OldReserve.pm | 12 +++++ t/db_dependent/Koha/Old/Holds.t | 78 +++++++++++++++++++++++++++++++- 3 files changed, 133 insertions(+), 1 deletion(-) diff --git a/Koha/Old/Holds.pm b/Koha/Old/Holds.pm index 40bf2dcdf2..3a032b823e 100644 --- a/Koha/Old/Holds.pm +++ b/Koha/Old/Holds.pm @@ -22,6 +22,7 @@ use Modern::Perl; use Koha::Database; +use Koha::DateUtils qw(dt_from_string); use Koha::Old::Hold; use base qw(Koha::Holds); @@ -36,6 +37,49 @@ This object represents a set of holds that have been filled or canceled =head2 Class methods +=head3 filter_by_anonymizable + + my $holds = $patron->old_holds; + my $anonymizable_holds = $holds->filter_by_anonymizable( { [ before => $older_than_date, ] } ); + +This method filters a I resultset, so it only contains holds that can be +anonimized given the patron privacy settings. It accepts the following parameters: + +=over 4 + +=item I: a I object. Holds older than this date are eligible for anonymization. If omitted, the current date is used. + +=back + +=cut + +sub filter_by_anonymizable { + my ( $self, $params ) = @_; + my $before = $params->{before}; + + $before //= dt_from_string(); + + my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef; + + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + return $self->search( + { + timestamp => { '<' => $dtf->format_datetime($before) }, + 'me.borrowernumber' => { 'not' => undef }, + 'patron.privacy' => { '<>' => 0 }, + ( + $anonymous_patron + ? ( 'me.borrowernumber' => { '!=' => $anonymous_patron } ) + : () + ), + }, + { + join => ['patron'], + order_by => ['me.reserve_id'], + } + ); +} + =head3 anonymize $patron->old_holds->anonymize(); diff --git a/Koha/Schema/Result/OldReserve.pm b/Koha/Schema/Result/OldReserve.pm index 05174e1566..cd19cbe101 100644 --- a/Koha/Schema/Result/OldReserve.pm +++ b/Koha/Schema/Result/OldReserve.pm @@ -394,6 +394,18 @@ __PACKAGE__->belongs_to( }, ); +__PACKAGE__->belongs_to( + "patron", + "Koha::Schema::Result::Borrower", + { borrowernumber => "borrowernumber" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "SET NULL", + on_update => "SET NULL", + }, +); + __PACKAGE__->add_columns( '+item_level_hold' => { is_boolean => 1 }, '+lowestPriority' => { is_boolean => 1 }, diff --git a/t/db_dependent/Koha/Old/Holds.t b/t/db_dependent/Koha/Old/Holds.t index fc1f5592cd..abf349ada5 100755 --- a/t/db_dependent/Koha/Old/Holds.t +++ b/t/db_dependent/Koha/Old/Holds.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; use Koha::Database; use Koha::DateUtils qw(dt_from_string); @@ -85,3 +85,79 @@ subtest 'anonymize() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'filter_by_anonymizable() tests' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + # patron_1 => keep records forever + my $patron_1 = $builder->build_object( + { class => 'Koha::Patrons', value => { privacy => 0 } } ); + + # patron_2 => never keep records + my $patron_2 = $builder->build_object( + { class => 'Koha::Patrons', value => { privacy => 1 } } ); + + is( $patron_1->old_holds->count, 0, 'patron_1 has no old holds' ); + is( $patron_2->old_holds->count, 0, 'patron_2 has no old holds' ); + + my $hold_1 = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron_1->id, + } + } + )->_move_to_old; + my $hold_2 = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron_2->id, + } + } + )->_move_to_old; + my $hold_3 = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron_1->id, + } + } + )->_move_to_old; + my $hold_4 = $builder->build_object( + { + class => 'Koha::Holds', + value => { + borrowernumber => $patron_2->id, + } + } + )->_move_to_old; + + $hold_1 = Koha::Old::Holds->find( $hold_1->id ) + ->set( { timestamp => dt_from_string() } )->store; + $hold_2 = Koha::Old::Holds->find( $hold_2->id ) + ->set( { timestamp => dt_from_string()->subtract( days => 1 ) } )->store; + $hold_3 = Koha::Old::Holds->find( $hold_3->id ) + ->set( { timestamp => dt_from_string()->subtract( days => 2 ) } )->store; + $hold_4 = Koha::Old::Holds->find( $hold_4->id ) + ->set( { timestamp => dt_from_string()->subtract( days => 3 ) } )->store; + + is( $patron_1->old_holds->count, 2, 'patron_1 has 2 completed holds' ); + is( $patron_2->old_holds->count, 2, 'patron_2 has 2 completed holds' ); + + # filter them so only the older two are part of the resultset + my $holds = Koha::Old::Holds->search( + { 'me.borrowernumber' => [ $patron_1->id, $patron_2->id ] } ); + is( $holds->count, 4, 'Total of 4 holds returned correctly' ); + my $rs = $holds->filter_by_anonymizable; + is( $rs->count, 2, 'Only 2 can be anonymized' ); + + $rs = $holds->filter_by_anonymizable( + { before => dt_from_string()->subtract( days => 1 ) } ); + is( $rs->count, 1, 'Only 1 can be anonymized with passed before date' ); + + $schema->storage->txn_rollback; +}; -- 2.32.0