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

(-)a/Koha/Patrons.pm (-2 / +24 lines)
Lines 165-173 sub search_patrons_to_anonymise { Link Here
165
165
166
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
166
    Koha::Patrons->search->anonymise_issue_history( { [ before => $older_than_date ] } );
167
167
168
Anonymise issue history (old_issues) for all patrons older than the given date (optional).
168
Anonymise issue history (old_issues and items_last_borrowers) for all issues older
169
than the given date (optional).
170
169
To make sure all the conditions are met, the caller has the responsibility to
171
To make sure all the conditions are met, the caller has the responsibility to
170
call search_patrons_to_anonymise to filter the Koha::Patrons set
172
call search_patrons_to_anonymise to filter the Koha::Patrons set.
171
173
172
=cut
174
=cut
173
175
Lines 194-201 sub anonymise_issue_history { Link Here
194
            )
196
            )
195
        }
197
        }
196
        );
198
        );
199
200
        my $last_borrowers_to_anonymise =
201
          $patron->_result->items_last_borrowers->search(
202
            {
203
                (
204
                    $older_than_date
205
                    ? ( created_on =>
206
                          { '<' => $dtf->format_datetime($older_than_date) } )
207
                    : (),
208
                    "itemnumber.damaged" => 0,
209
                    "itemnumber.itemlost" => 0,
210
                    "itemnumber.withdrawn" => 0,
211
                )
212
            },
213
            {
214
                join => ['itemnumber']
215
            }
216
          );
217
197
        my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
218
        my $anonymous_patron = C4::Context->preference('AnonymousPatron') || undef;
198
        $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
219
        $nb_rows += $old_issues_to_anonymise->update( { 'old_issues.borrowernumber' => $anonymous_patron } );
220
        $nb_rows += $last_borrowers_to_anonymise->update( { 'items_last_borrower.borrowernumber' => $anonymous_patron } );
199
    }
221
    }
200
    return $nb_rows;
222
    return $nb_rows;
201
}
223
}
(-)a/t/db_dependent/Koha/Patrons.t (-2 / +117 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 40;
22
use Test::More tests => 41;
23
use Test::Warn;
23
use Test::Warn;
24
use Test::Exception;
24
use Test::Exception;
25
use Test::MockModule;
25
use Test::MockModule;
Lines 1171-1176 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
1171
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1171
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1172
};
1172
};
1173
1173
1174
subtest 'anonymise items_last_borrower' => sub {
1175
    plan tests => 1;
1176
1177
    # TODO create a subroutine in t::lib::Mocks
1178
    my $anonymous = $builder->build( { source => 'Borrower', }, );
1179
1180
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
1181
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1182
1183
    subtest 'withrawn, lost and damaged items' => sub {
1184
        plan tests => 5;
1185
1186
        my $patron = $builder->build(
1187
            {   source => 'Borrower',
1188
                value  => { privacy => 1, }
1189
            }
1190
        );
1191
        my $item_1 = $builder->build_object(
1192
            {   class => 'Koha::Items',
1193
                value  => {
1194
                    itemlost  => 0,
1195
                    withdrawn => 0,
1196
                    damaged => 0,
1197
                },
1198
            }
1199
        );
1200
        my $issue_1 = $builder->build(
1201
            {   source => 'Issue',
1202
                value  => {
1203
                    borrowernumber => $patron->{borrowernumber},
1204
                    itemnumber     => $item_1->itemnumber,
1205
                },
1206
            }
1207
        );
1208
        my $item_2 = $builder->build_object(
1209
            {   class => 'Koha::Items',
1210
                value  => {
1211
                    itemlost  => 0,
1212
                    withdrawn => 0,
1213
                    damaged => 0,
1214
                },
1215
            }
1216
        );
1217
        my $issue_2 = $builder->build(
1218
            {   source => 'Issue',
1219
                value  => {
1220
                    borrowernumber => $patron->{borrowernumber},
1221
                    itemnumber     => $item_2->itemnumber,
1222
                },
1223
            }
1224
        );
1225
        my $item_3 = $builder->build_object(
1226
            {   class => 'Koha::Items',
1227
                value  => {
1228
                    itemlost  => 0,
1229
                    withdrawn => 0,
1230
                    damaged => 0,
1231
                },
1232
            }
1233
        );
1234
        my $issue_3 = $builder->build(
1235
            {   source => 'Issue',
1236
                value  => {
1237
                    borrowernumber => $patron->{borrowernumber},
1238
                    itemnumber     => $item_3->itemnumber,
1239
                },
1240
            }
1241
        );
1242
        my $item_4 = $builder->build_object(
1243
            {   class => 'Koha::Items',
1244
                value  => {
1245
                    itemlost  => 0,
1246
                    withdrawn => 0,
1247
                    damaged => 0,
1248
                },
1249
            }
1250
        );
1251
        my $issue_4 = $builder->build(
1252
            {   source => 'Issue',
1253
                value  => {
1254
                    borrowernumber => $patron->{borrowernumber},
1255
                    itemnumber     => $item_4->itemnumber,
1256
                },
1257
            }
1258
        );
1259
1260
        my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string('2010-10-11') );
1261
        my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string('2010-10-11') );
1262
        my ( $returned_3, undef, undef ) = C4::Circulation::AddReturn( $item_3->barcode, undef, undef, dt_from_string('2010-10-11') );
1263
        my ( $returned_4, undef, undef ) = C4::Circulation::AddReturn( $item_4->barcode, undef, undef, dt_from_string('2010-10-11') );
1264
        is( $returned_1 && $returned_2 && $returned_3 && $returned_4, 1, 'The items should have been returned' );
1265
        $item_1->withdrawn(1)->store;
1266
        $item_2->itemlost(1)->store;
1267
        $item_3->damaged(1)->store;
1268
1269
        Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-12' } )->anonymise_issue_history();
1270
1271
        my $dbh = C4::Context->dbh;
1272
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1273
        $sth->execute($item_1->itemnumber);
1274
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1275
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'withrawn items should not be anonymised' );
1276
        $sth->execute($item_2->itemnumber);
1277
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1278
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'lost items should not be anonymised' );
1279
        $sth->execute($item_3->itemnumber);
1280
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1281
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'damaged items should not be anonymised' );
1282
        $sth->execute($item_4->itemnumber);
1283
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1284
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'not withdrawn, lost or damaged items are anonymised' );
1285
1286
    };
1287
1288
};
1289
1174
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
1290
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
1175
    plan tests => 3;
1291
    plan tests => 3;
1176
1292
1177
- 

Return to bug 23260