|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# This file is part of Koha |
| 4 |
# |
| 5 |
# Koha is free software; you can redistribute it and/or modify it |
| 6 |
# under the terms of the GNU General Public License as published by |
| 7 |
# the Free Software Foundation; either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# Koha is distributed in the hope that it will be useful, but |
| 11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU General Public License |
| 16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 17 |
|
| 18 |
use Modern::Perl; |
| 19 |
|
| 20 |
use Test::More tests => 1; |
| 21 |
|
| 22 |
use Koha::Database; |
| 23 |
use Koha::DateUtils qw(dt_from_string); |
| 24 |
use Koha::Old::Holds; |
| 25 |
|
| 26 |
use t::lib::TestBuilder; |
| 27 |
|
| 28 |
my $schema = Koha::Database->new->schema; |
| 29 |
my $builder = t::lib::TestBuilder->new; |
| 30 |
|
| 31 |
subtest 'anonymize() tests' => sub { |
| 32 |
|
| 33 |
plan tests => 5; |
| 34 |
|
| 35 |
$schema->storage->txn_begin; |
| 36 |
|
| 37 |
my $patron = $builder->build_object( { class => 'Koha::Patrons' } ); |
| 38 |
|
| 39 |
is( $patron->old_holds->count, 0, 'Patron has no old holds' ); |
| 40 |
is( $patron->old_holds->anonymize + 0, 0, 'Anonymizing an empty resultset returns 0' ); |
| 41 |
|
| 42 |
my $hold_1 = $builder->build_object( |
| 43 |
{ |
| 44 |
class => 'Koha::Old::Holds', |
| 45 |
value => |
| 46 |
{ borrowernumber => $patron->id, timestamp => dt_from_string() } |
| 47 |
} |
| 48 |
); |
| 49 |
my $hold_2 = $builder->build_object( |
| 50 |
{ |
| 51 |
class => 'Koha::Old::Holds', |
| 52 |
value => { |
| 53 |
borrowernumber => $patron->id, |
| 54 |
timestamp => dt_from_string()->subtract( days => 1 ) |
| 55 |
} |
| 56 |
} |
| 57 |
); |
| 58 |
my $hold_3 = $builder->build_object( |
| 59 |
{ |
| 60 |
class => 'Koha::Old::Holds', |
| 61 |
value => { |
| 62 |
borrowernumber => $patron->id, |
| 63 |
timestamp => dt_from_string()->subtract( days => 2 ) |
| 64 |
} |
| 65 |
} |
| 66 |
); |
| 67 |
my $hold_4 = $builder->build_object( |
| 68 |
{ |
| 69 |
class => 'Koha::Old::Holds', |
| 70 |
value => { |
| 71 |
borrowernumber => $patron->id, |
| 72 |
timestamp => dt_from_string()->subtract( days => 3 ) |
| 73 |
} |
| 74 |
} |
| 75 |
); |
| 76 |
|
| 77 |
is( $patron->old_holds->count, 4, 'Patron has 4 completed holds' ); |
| 78 |
# filter them so only the older two are part of the resultset |
| 79 |
my $holds = $patron->old_holds->search({ timestamp => { '<=' => dt_from_string()->subtract( days => 2 ) } }); |
| 80 |
# Anonymize them |
| 81 |
my $anonymized_count = $holds->anonymize(); |
| 82 |
is( $anonymized_count, 2, 'update() tells 2 rows were updated' ); |
| 83 |
|
| 84 |
is( $patron->old_holds->count, 2, 'Patron has 2 completed holds' ); |
| 85 |
|
| 86 |
$schema->storage->txn_rollback; |
| 87 |
}; |