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

(-)a/Koha/Patrons.pm (-39 lines)
Lines 158-202 sub search_patrons_to_anonymise { Link Here
158
    return Koha::Patrons->_new_from_dbic($rs);
158
    return Koha::Patrons->_new_from_dbic($rs);
159
}
159
}
160
160
161
=head3 anonymise_issue_history
162
163
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
164
165
Anonymise issue history (old_issues) for all patrons older than the given date (optional).
166
To make sure all the conditions are met, the caller has the responsibility to
167
call search_patrons_to_anonymise to filter the Koha::Patrons set
168
169
=cut
170
171
sub anonymise_issue_history {
172
    my ( $self, $params ) = @_;
173
174
    my $older_than_date = $params->{before};
175
176
    $older_than_date = dt_from_string $older_than_date if $older_than_date;
177
178
    # The default of 0 does not work due to foreign key constraints
179
    # The anonymisation should not fail quietly if AnonymousPatron is not a valid entry
180
    # Set it to undef (NULL)
181
    my $dtf = Koha::Database->new->schema->storage->datetime_parser;
182
    my $nb_rows = 0;
183
    while ( my $patron = $self->next ) {
184
        my $old_issues_to_anonymise = $patron->old_checkouts->search(
185
        {
186
            (
187
                $older_than_date
188
                ? ( returndate =>
189
                      { '<' => $dtf->format_datetime($older_than_date) } )
190
                : ()
191
            )
192
        }
193
        );
194
        my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
195
        $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
196
    }
197
    return $nb_rows;
198
}
199
200
=head3 delete
161
=head3 delete
201
162
202
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
163
    Koha::Patrons->search({ some filters here })->delete({ move => 1 });
(-)a/t/db_dependent/Koha/Patrons.t (-6 / +39 lines)
Lines 1022-1028 subtest 'notice_email_address' => sub { Link Here
1022
    $patron->delete;
1022
    $patron->delete;
1023
};
1023
};
1024
1024
1025
subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub {
1025
subtest 'search_patrons_to_anonymise' => sub {
1026
1026
    plan tests => 5;
1027
    plan tests => 5;
1027
1028
1028
    # TODO create a subroutine in t::lib::Mocks
1029
    # TODO create a subroutine in t::lib::Mocks
Lines 1083-1089 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
1083
        my $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber} } );
1084
        my $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->search( { 'me.borrowernumber' => $patron->{borrowernumber} } );
1084
        is( ref($patrons_to_anonymise), 'Koha::Patrons', 'search_patrons_to_anonymise should return Koha::Patrons' );
1085
        is( ref($patrons_to_anonymise), 'Koha::Patrons', 'search_patrons_to_anonymise should return Koha::Patrons' );
1085
1086
1086
        my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history( { before => '2010-10-11' } );
1087
        my $rows_affected = Koha::Old::Checkouts->search(
1088
            {
1089
                borrowernumber => [
1090
                    Koha::Patrons->search_patrons_to_anonymise(
1091
                        { before => '2011-11-12' }
1092
                    )->get_column('borrowernumber')
1093
                ],
1094
                returndate => { '<' => '2011-10-11', }
1095
            }
1096
        )->anonymize;
1087
        ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
1097
        ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
1088
1098
1089
        $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } );
1099
        $patrons_to_anonymise = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } );
Lines 1098-1104 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
1098
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1108
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1099
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'The issue should not have been anonymised, the returned date is later' );
1109
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'The issue should not have been anonymised, the returned date is later' );
1100
1110
1101
        $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2011-11-12' } )->anonymise_issue_history;
1111
        $rows_affected = Koha::Old::Checkouts->search(
1112
            {
1113
                borrowernumber => [
1114
                    Koha::Patrons->search_patrons_to_anonymise(
1115
                        { before => '2011-11-12' }
1116
                    )->get_column('borrowernumber')
1117
                ],
1118
                returndate => { '<' => '2011-11-12', }
1119
            }
1120
        )->anonymize;
1102
        $sth->execute($item_2->itemnumber);
1121
        $sth->execute($item_2->itemnumber);
1103
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1122
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1104
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue should have been anonymised, the returned date is before' );
1123
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue should have been anonymised, the returned date is before' );
Lines 1106-1112 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
1106
        my $sth_reset = $dbh->prepare(q|UPDATE old_issues SET borrowernumber = ? WHERE itemnumber = ?|);
1125
        my $sth_reset = $dbh->prepare(q|UPDATE old_issues SET borrowernumber = ? WHERE itemnumber = ?|);
1107
        $sth_reset->execute( $patron->{borrowernumber}, $item_1->itemnumber );
1126
        $sth_reset->execute( $patron->{borrowernumber}, $item_1->itemnumber );
1108
        $sth_reset->execute( $patron->{borrowernumber}, $item_2->itemnumber );
1127
        $sth_reset->execute( $patron->{borrowernumber}, $item_2->itemnumber );
1109
        $rows_affected = Koha::Patrons->search_patrons_to_anonymise->anonymise_issue_history;
1128
        $rows_affected = Koha::Old::Checkouts->search(
1129
            {
1130
                borrowernumber => [
1131
                    Koha::Patrons->search_patrons_to_anonymise->get_column(
1132
                        'borrowernumber')
1133
                ]
1134
            }
1135
        )->anonymize;
1110
        $sth->execute($item_1->itemnumber);
1136
        $sth->execute($item_1->itemnumber);
1111
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1137
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1112
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue 1 should have been anonymised, before parameter was not passed' );
1138
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'The issue 1 should have been anonymised, before parameter was not passed' );
Lines 1170-1176 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
1170
1196
1171
        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, undef, undef, dt_from_string('2010-10-10') );
1197
        my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->barcode, undef, undef, dt_from_string('2010-10-10') );
1172
        is( $returned, 1, 'The item should have been returned' );
1198
        is( $returned, 1, 'The item should have been returned' );
1173
        my $rows_affected = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-11' } )->anonymise_issue_history( { before => '2010-10-11' } );
1199
        my $rows_affected = Koha::Old::Checkouts->search(
1200
            {
1201
                borrowernumber => [
1202
                    Koha::Patrons->search_patrons_to_anonymise(
1203
                        { before => '2010-10-11' }
1204
                    )->get_column('borrowernumber')
1205
                ]
1206
            }
1207
        )->anonymize;
1174
        ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
1208
        ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
1175
1209
1176
        my $dbh = C4::Context->dbh;
1210
        my $dbh = C4::Context->dbh;
1177
- 

Return to bug 29984