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

(-)a/t/db_dependent/Koha/Patrons.t (-6 / +63 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 44;
22
use Test::More tests => 45;
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 1287-1302 subtest 'search_patrons_to_anonymise' => sub { Link Here
1287
};
1287
};
1288
1288
1289
subtest 'anonymise items_last_borrower' => sub {
1289
subtest 'anonymise items_last_borrower' => sub {
1290
    plan tests => 1;
1290
    plan tests => 2;
1291
1291
1292
    my $anonymous = $builder->build_object( { class => 'Koha::Patrons', }, );
1292
    my $anonymous = $builder->build_object( { class => 'Koha::Patrons', }, );
1293
1293
1294
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->borrowernumber );
1294
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->borrowernumber );
1295
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1295
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1296
    t::lib::Mocks::mock_preference('AnonymiseLastBorrower', 1);
1297
1298
    subtest 'anonymise items_last_borrower by days' => sub {
1299
        plan tests => 4;
1300
1301
        t::lib::Mocks::mock_preference('AnonymiseLastBorrowerDays', 5);
1302
1303
        my $patron = $builder->build_object(
1304
            {
1305
                class => 'Koha::Patrons',
1306
                value => { privacy => 1, }
1307
            }
1308
        );
1309
        my $item_1 = $builder->build_sample_item;
1310
        my $issue_1 = $builder->build_object(
1311
            {
1312
                class => 'Koha::Checkouts',
1313
                value => {
1314
                    borrowernumber => $patron->borrowernumber,
1315
                    itemnumber     => $item_1->itemnumber,
1316
                },
1317
            }
1318
        );
1319
        my $item_2 = $builder->build_sample_item;
1320
        my $issue_2 = $builder->build_object(
1321
            {
1322
                class => 'Koha::Checkouts',
1323
                value => {
1324
                    borrowernumber => $patron->borrowernumber,
1325
                    itemnumber     => $item_2->itemnumber,
1326
                },
1327
            }
1328
        );
1329
1330
        my $dbh = C4::Context->dbh;
1331
        my ( $returned_1 ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string );
1332
        my ( $returned_2 ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string );
1333
        is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
1334
        my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber = ?|);
1335
        $stu->execute(DateTime->today(time_zone => C4::Context->tz())->add( days => -6 ), $item_2->itemnumber);
1336
1337
        my $nb_rows = Koha::Patrons->anonymise_last_borrowers();
1338
        is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised');
1339
1340
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1341
        $sth->execute($item_1->itemnumber);
1342
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1343
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'Last borrower younger than 5 days are not anonymised' );
1344
        $sth->execute($item_2->itemnumber);
1345
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1346
        is( $borrowernumber_used_to_anonymised, $anonymous->borrowernumber, 'Last borrower older than 5 days are anonymised' );
1347
1348
    };
1296
1349
1297
    subtest 'withrawn, lost and damaged items' => sub {
1350
    subtest 'withrawn, lost and damaged items' => sub {
1298
        plan tests => 6;
1351
        plan tests => 6;
1299
1352
1353
        t::lib::Mocks::mock_preference('AnonymiseLastBorrowerDays', 0);
1354
1300
        my $patron = $builder->build_object(
1355
        my $patron = $builder->build_object(
1301
            {
1356
            {
1302
                class => 'Koha::Patrons',
1357
                class => 'Koha::Patrons',
Lines 1353-1362 subtest 'anonymise items_last_borrower' => sub { Link Here
1353
        $item_2->itemlost(1)->store;
1408
        $item_2->itemlost(1)->store;
1354
        $item_3->damaged(1)->store;
1409
        $item_3->damaged(1)->store;
1355
1410
1356
        my $nb_rows = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-12' } )->anonymise_issue_history();
1357
        is( $nb_rows, 4, 'anonymise_issue_history should have returned the number of checkouts anonymised');
1358
1359
        my $dbh = C4::Context->dbh;
1411
        my $dbh = C4::Context->dbh;
1412
        my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber in (?, ?, ?, ?)|);
1413
        $stu->execute(DateTime->today(time_zone => C4::Context->tz())->add( days => -1 ), $item_1->itemnumber,$item_2->itemnumber,$item_3->itemnumber,$item_4->itemnumber);
1414
1415
        my $nb_rows = Koha::Patrons->anonymise_last_borrowers();
1416
        is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised');
1417
1360
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1418
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1361
        $sth->execute($item_1->itemnumber);
1419
        $sth->execute($item_1->itemnumber);
1362
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1420
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1363
- 

Return to bug 23260