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

Return to bug 23260