View | Details | Raw Unified | Return to bug 18169
Collapse All | Expand All

(-)a/Koha/Patrons.pm (-5 / +14 lines)
Lines 216-224 sub search_patrons_to_anonymise { Link Here
216
216
217
=head3 anonymise_issue_history
217
=head3 anonymise_issue_history
218
218
219
    Koha::Patrons->search->anonymise_issue_history( { before => $older_than_date } );
219
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
220
220
221
Anonymise issue history (old_issues) for all patrons older than the given date.
221
Anonymise issue history (old_issues) for all patrons older than the given date (optional).
222
To make sure all the conditions are met, the caller has the responsability to
222
To make sure all the conditions are met, the caller has the responsability to
223
call search_patrons_to_anonymise to filter the Koha::Patrons set
223
call search_patrons_to_anonymise to filter the Koha::Patrons set
224
224
Lines 229-242 sub anonymise_issue_history { Link Here
229
229
230
    my $older_than_date = $params->{before};
230
    my $older_than_date = $params->{before};
231
231
232
    return unless $older_than_date;
232
    $older_than_date = dt_from_string $older_than_date if $older_than_date;
233
    $older_than_date = dt_from_string $older_than_date;
234
233
235
    # The default of 0 does not work due to foreign key constraints
234
    # The default of 0 does not work due to foreign key constraints
236
    # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
235
    # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
237
    # Set it to undef (NULL)
236
    # Set it to undef (NULL)
238
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
237
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
239
    my $old_issues_to_anonymise = $self->search_related( 'old_issues', { returndate => { '<' => $dtf->format_datetime($older_than_date) } } );
238
    my $old_issues_to_anonymise = $self->search_related(
239
        'old_issues',
240
        {
241
            (
242
                $older_than_date
243
                ? ( returndate =>
244
                      { '<' => $dtf->format_datetime($older_than_date) } )
245
                : ()
246
            )
247
        }
248
    );
240
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
249
    my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
241
    $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
250
    $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
242
}
251
}
(-)a/opac/opac-privacy.pl (-3 / +1 lines)
Lines 60-69 if ( $op eq "update_privacy" ) { Link Here
60
elsif ( $op eq "delete_record" ) {
60
elsif ( $op eq "delete_record" ) {
61
61
62
    # delete all reading records for items returned
62
    # delete all reading records for items returned
63
    # uses a hardcoded date ridiculously far in the future
64
65
    my $rows = eval {
63
    my $rows = eval {
66
        Koha::Patrons->search({ 'me.borrowernumber' => $borrowernumber })->anonymise_issue_history( { before => '2999-12-12' } );
64
        Koha::Patrons->search({ 'me.borrowernumber' => $borrowernumber })->anonymise_issue_history;
67
    };
65
    };
68
    $rows = $@ ? 0 : int($rows);
66
    $rows = $@ ? 0 : int($rows);
69
    $template->param( 'deleted' => $rows );
67
    $template->param( 'deleted' => $rows );
(-)a/t/db_dependent/Koha/Patrons.t (-11 / +36 lines)
Lines 688-694 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
688
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
688
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
689
689
690
    subtest 'patron privacy is 1 (default)' => sub {
690
    subtest 'patron privacy is 1 (default)' => sub {
691
        plan tests => 4;
691
        plan tests => 6;
692
692
693
        t::lib::Mocks::mock_preference('IndependentBranches', 0);
693
        t::lib::Mocks::mock_preference('IndependentBranches', 0);
694
        my $patron = $builder->build(
694
        my $patron = $builder->build(
Lines 696-702 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
696
                value  => { privacy => 1, }
696
                value  => { privacy => 1, }
697
            }
697
            }
698
        );
698
        );
699
        my $item = $builder->build(
699
        my $item_1 = $builder->build(
700
            {   source => 'Item',
700
            {   source => 'Item',
701
                value  => {
701
                value  => {
702
                    itemlost  => 0,
702
                    itemlost  => 0,
Lines 704-732 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
704
                },
704
                },
705
            }
705
            }
706
        );
706
        );
707
        my $issue = $builder->build(
707
        my $issue_1 = $builder->build(
708
            {   source => 'Issue',
708
            {   source => 'Issue',
709
                value  => {
709
                value  => {
710
                    borrowernumber => $patron->{borrowernumber},
710
                    borrowernumber => $patron->{borrowernumber},
711
                    itemnumber     => $item->{itemnumber},
711
                    itemnumber     => $item_1->{itemnumber},
712
                },
713
            }
714
        );
715
        my $item_2 = $builder->build(
716
            {   source => 'Item',
717
                value  => {
718
                    itemlost  => 0,
719
                    withdrawn => 0,
720
                },
721
            }
722
        );
723
        my $issue_2 = $builder->build(
724
            {   source => 'Issue',
725
                value  => {
726
                    borrowernumber => $patron->{borrowernumber},
727
                    itemnumber     => $item_2->{itemnumber},
712
                },
728
                },
713
            }
729
            }
714
        );
730
        );
715
731
716
        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
732
        my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->{barcode}, undef, undef, undef, '2010-10-10' );
717
        is( $returned, 1, 'The item should have been returned' );
733
        my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->{barcode}, undef, undef, undef, '2011-11-11' );
734
        is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
718
735
719
        my $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber} } );
736
        my $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber} } );
720
        is( ref($patrons_to_anonymise), 'Koha::Patrons', 'search_patrons_to_anonymise should return Koha::Patrons' );
737
        is( ref($patrons_to_anonymise), 'Koha::Patrons', 'search_patrons_to_anonymise should return Koha::Patrons' );
721
738
722
        my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
739
        my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history( { before => '2010-10-11' } );
723
        ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
740
        ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
724
741
725
        my $dbh = C4::Context->dbh;
742
        my $dbh = C4::Context->dbh;
726
        my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
743
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM old_issues where itemnumber = ?|);
727
            SELECT borrowernumber FROM old_issues where itemnumber = ?
744
        $sth->execute($item_1->{itemnumber});
728
        |, undef, $item->{itemnumber});
745
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
729
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
746
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
747
        $sth->execute($item_2->{itemnumber});
748
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
749
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'The issue should not have been anonymised, the returned date is later' );
750
751
        $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history;
752
        $sth->execute($item_2->{itemnumber});
753
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
754
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue should have been anonymised, the returned date is before' );
755
730
        Koha::Patrons->find( $patron->{borrowernumber})->delete;
756
        Koha::Patrons->find( $patron->{borrowernumber})->delete;
731
    };
757
    };
732
758
733
- 

Return to bug 18169