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 => 41;
22
use Test::More tests => 42;
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 1211-1226 subtest 'search_patrons_to_anonymise & anonymise_issue_history' => sub { Link Here
1211
};
1211
};
1212
1212
1213
subtest 'anonymise items_last_borrower' => sub {
1213
subtest 'anonymise items_last_borrower' => sub {
1214
    plan tests => 1;
1214
    plan tests => 2;
1215
1215
1216
    my $anonymous = $builder->build_object( { class => 'Koha::Patrons', }, );
1216
    my $anonymous = $builder->build_object( { class => 'Koha::Patrons', }, );
1217
1217
1218
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->borrowernumber );
1218
    t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->borrowernumber );
1219
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1219
    t::lib::Mocks::mock_preference('StoreLastBorrower', 1);
1220
    t::lib::Mocks::mock_preference('AnonymiseLastBorrower', 1);
1221
1222
    subtest 'anonymise items_last_borrower by days' => sub {
1223
        plan tests => 4;
1224
1225
        t::lib::Mocks::mock_preference('AnonymiseLastBorrowerDays', 5);
1226
1227
        my $patron = $builder->build_object(
1228
            {
1229
                class => 'Koha::Patrons',
1230
                value => { privacy => 1, }
1231
            }
1232
        );
1233
        my $item_1 = $builder->build_sample_item;
1234
        my $issue_1 = $builder->build_object(
1235
            {
1236
                class => 'Koha::Checkouts',
1237
                value => {
1238
                    borrowernumber => $patron->borrowernumber,
1239
                    itemnumber     => $item_1->itemnumber,
1240
                },
1241
            }
1242
        );
1243
        my $item_2 = $builder->build_sample_item;
1244
        my $issue_2 = $builder->build_object(
1245
            {
1246
                class => 'Koha::Checkouts',
1247
                value => {
1248
                    borrowernumber => $patron->borrowernumber,
1249
                    itemnumber     => $item_2->itemnumber,
1250
                },
1251
            }
1252
        );
1253
1254
        my $dbh = C4::Context->dbh;
1255
        my ( $returned_1 ) = C4::Circulation::AddReturn( $item_1->barcode, undef, undef, dt_from_string );
1256
        my ( $returned_2 ) = C4::Circulation::AddReturn( $item_2->barcode, undef, undef, dt_from_string );
1257
        is( $returned_1 && $returned_2, 1, 'The items should have been returned' );
1258
        my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber = ?|);
1259
        $stu->execute(DateTime->today(time_zone => C4::Context->tz())->add( days => -6 ), $item_2->itemnumber);
1260
1261
        my $nb_rows = Koha::Patrons->anonymise_last_borrowers();
1262
        is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised');
1263
1264
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1265
        $sth->execute($item_1->itemnumber);
1266
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1267
        is( $borrowernumber_used_to_anonymised, $patron->borrowernumber, 'Last borrower younger than 5 days are not anonymised' );
1268
        $sth->execute($item_2->itemnumber);
1269
        ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1270
        is( $borrowernumber_used_to_anonymised, $anonymous->borrowernumber, 'Last borrower older than 5 days are anonymised' );
1271
1272
    };
1220
1273
1221
    subtest 'withrawn, lost and damaged items' => sub {
1274
    subtest 'withrawn, lost and damaged items' => sub {
1222
        plan tests => 6;
1275
        plan tests => 6;
1223
1276
1277
        t::lib::Mocks::mock_preference('AnonymiseLastBorrowerDays', 0);
1278
1224
        my $patron = $builder->build_object(
1279
        my $patron = $builder->build_object(
1225
            {
1280
            {
1226
                class => 'Koha::Patrons',
1281
                class => 'Koha::Patrons',
Lines 1277-1286 subtest 'anonymise items_last_borrower' => sub { Link Here
1277
        $item_2->itemlost(1)->store;
1332
        $item_2->itemlost(1)->store;
1278
        $item_3->damaged(1)->store;
1333
        $item_3->damaged(1)->store;
1279
1334
1280
        my $nb_rows = Koha::Patrons->search_patrons_to_anonymise( { before => '2010-10-12' } )->anonymise_issue_history();
1281
        is( $nb_rows, 4, 'anonymise_issue_history should have returned the number of checkouts anonymised');
1282
1283
        my $dbh = C4::Context->dbh;
1335
        my $dbh = C4::Context->dbh;
1336
        my $stu = $dbh->prepare(q|UPDATE items_last_borrower set created_on = ? where itemnumber in (?, ?, ?, ?)|);
1337
        $stu->execute(DateTime->today(time_zone => C4::Context->tz())->add( days => -1 ), $item_1->itemnumber,$item_2->itemnumber,$item_3->itemnumber,$item_4->itemnumber);
1338
1339
        my $nb_rows = Koha::Patrons->anonymise_last_borrowers();
1340
        is( $nb_rows, 1, 'anonymise_issue_history should have returned the number of last borrowers anonymised');
1341
1284
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1342
        my $sth = $dbh->prepare(q|SELECT borrowernumber FROM items_last_borrower where itemnumber = ?|);
1285
        $sth->execute($item_1->itemnumber);
1343
        $sth->execute($item_1->itemnumber);
1286
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1344
        my ($borrowernumber_used_to_anonymised) = $sth->fetchrow_array;
1287
- 

Return to bug 23260