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 (-1 / +116 lines)
Lines 1199-1204 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
1199
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1199
    t::lib::Mocks::mock_preference('IndependentBranches', 0);
1200
};
1200
};
1201
1201
1202
subtest 'anonymise items_last_borrower' => sub {
1203
    plan tests => 1;
1204
1205
    # TODO create a subroutine in t::lib::Mocks
1206
    my $anonymous = $builder->build( { source => 'Borrower', }, );
1207
1208
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
1209
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1210
1211
    subtest 'withrawn, lost and damaged items' => sub {
1212
        plan tests => 5;
1213
1214
        my $patron = $builder->build(
1215
            {   source => 'Borrower',
1216
                value  => { privacy => 1, }
1217
            }
1218
        );
1219
        my $item_1 = $builder->build_object(
1220
            {   class => 'Koha::Items',
1221
                value  => {
1222
                    itemlost  => 0,
1223
                    withdrawn => 0,
1224
                    damaged => 0,
1225
                },
1226
            }
1227
        );
1228
        my $issue_1 = $builder->build(
1229
            {   source => 'Issue',
1230
                value  => {
1231
                    borrowernumber => $patron->{borrowernumber},
1232
                    itemnumber     => $item_1->itemnumber,
1233
                },
1234
            }
1235
        );
1236
        my $item_2 = $builder->build_object(
1237
            {   class => 'Koha::Items',
1238
                value  => {
1239
                    itemlost  => 0,
1240
                    withdrawn => 0,
1241
                    damaged => 0,
1242
                },
1243
            }
1244
        );
1245
        my $issue_2 = $builder->build(
1246
            {   source => 'Issue',
1247
                value  => {
1248
                    borrowernumber => $patron->{borrowernumber},
1249
                    itemnumber     => $item_2->itemnumber,
1250
                },
1251
            }
1252
        );
1253
        my $item_3 = $builder->build_object(
1254
            {   class => 'Koha::Items',
1255
                value  => {
1256
                    itemlost  => 0,
1257
                    withdrawn => 0,
1258
                    damaged => 0,
1259
                },
1260
            }
1261
        );
1262
        my $issue_3 = $builder->build(
1263
            {   source => 'Issue',
1264
                value  => {
1265
                    borrowernumber => $patron->{borrowernumber},
1266
                    itemnumber     => $item_3->itemnumber,
1267
                },
1268
            }
1269
        );
1270
        my $item_4 = $builder->build_object(
1271
            {   class => 'Koha::Items',
1272
                value  => {
1273
                    itemlost  => 0,
1274
                    withdrawn => 0,
1275
                    damaged => 0,
1276
                },
1277
            }
1278
        );
1279
        my $issue_4 = $builder->build(
1280
            {   source => 'Issue',
1281
                value  => {
1282
                    borrowernumber => $patron->{borrowernumber},
1283
                    itemnumber     => $item_4->itemnumber,
1284
                },
1285
            }
1286
        );
1287
1288
        my ( $returned_1, undef, undef ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string('2010-10-11') );
1289
        my ( $returned_2, undef, undef ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string('2010-10-11') );
1290
        my ( $returned_3, undef, undef ) = C4::Circulation::AddReturn( $item_3->barcode, undef, undef, dt_from_string('2010-10-11') );
1291
        my ( $returned_4, undef, undef ) = C4::Circulation::AddReturn( $item_4->barcode, undef, undef, dt_from_string('2010-10-11') );
1292
        is( $returned_1 && $returned_2 && $returned_3 && $returned_4, 1, 'The items should have been returned' );
1293
        $item_1->withdrawn(1)->store;
1294
        $item_2->itemlost(1)->store;
1295
        $item_3->damaged(1)->store;
1296
1297
        Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-12' } )->anonymise_issue_history();
1298
1299
        my $dbh = C4::Context->dbh;
1300
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1301
        $sth->execute($item_1->itemnumber);
1302
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1303
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'withrawn items should not be anonymised' );
1304
        $sth->execute($item_2->itemnumber);
1305
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1306
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'lost items should not be anonymised' );
1307
        $sth->execute($item_3->itemnumber);
1308
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1309
        is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'damaged items should not be anonymised' );
1310
        $sth->execute($item_4->itemnumber);
1311
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1312
        is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'not withdrawn, lost or damaged items are anonymised' );
1313
1314
    };
1315
1316
};
1317
1202
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
1318
subtest 'libraries_where_can_see_patrons + can_see_patron_infos + search_limited' => sub {
1203
    plan tests => 3;
1319
    plan tests => 3;
1204
1320
1205
- 

Return to bug 23260