3 # This file is part of Koha
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.
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.
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>.
20 use Test::More tests => 2;
24 use Koha::DateUtils qw(dt_from_string);
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
33 subtest 'anonymize() tests' => sub {
37 $schema->storage->txn_begin;
39 my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
40 my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
42 is( $patron->old_holds->count, 0, 'Patron has no old holds' );
44 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
47 { $patron->old_holds->anonymize; }
48 'Koha::Exceptions::SysPref::NotSet',
49 'Exception thrown because AnonymousPatron not set';
51 is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
53 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
55 is( $patron->old_holds->anonymize + 0, 0, 'Anonymizing an empty resultset returns 0' );
57 my $hold_1 = $builder->build_object(
59 class => 'Koha::Old::Holds',
61 { borrowernumber => $patron->id, timestamp => dt_from_string() }
64 my $hold_2 = $builder->build_object(
66 class => 'Koha::Old::Holds',
68 borrowernumber => $patron->id,
69 timestamp => dt_from_string()->subtract( days => 1 )
73 my $hold_3 = $builder->build_object(
75 class => 'Koha::Old::Holds',
77 borrowernumber => $patron->id,
78 timestamp => dt_from_string()->subtract( days => 2 )
82 my $hold_4 = $builder->build_object(
84 class => 'Koha::Old::Holds',
86 borrowernumber => $patron->id,
87 timestamp => dt_from_string()->subtract( days => 3 )
92 is( $patron->old_holds->count, 4, 'Patron has 4 completed holds' );
93 # filter them so only the older two are part of the resultset
94 my $holds = $patron->old_holds->search({ timestamp => { '<=' => dt_from_string()->subtract( days => 2 ) } });
97 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
99 { $holds->anonymize; }
100 'Koha::Exceptions::SysPref::NotSet',
101 'Exception thrown because AnonymousPatron not set';
103 is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
104 is( $patron->old_holds->count, 4, 'Patron has 4 completed holds' );
106 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
108 my $anonymized_count = $holds->anonymize();
109 is( $anonymized_count, 2, 'update() tells 2 rows were updated' );
111 is( $patron->old_holds->count, 2, 'Patron has 2 completed holds' );
113 $schema->storage->txn_rollback;
116 subtest 'filter_by_anonymizable() tests' => sub {
120 $schema->storage->txn_begin;
122 # patron_1 => keep records forever
123 my $patron_1 = $builder->build_object(
124 { class => 'Koha::Patrons', value => { privacy => 0 } } );
126 # patron_2 => never keep records
127 my $patron_2 = $builder->build_object(
128 { class => 'Koha::Patrons', value => { privacy => 1 } } );
130 is( $patron_1->old_holds->count, 0, 'patron_1 has no old holds' );
131 is( $patron_2->old_holds->count, 0, 'patron_2 has no old holds' );
133 my $hold_1 = $builder->build_object(
135 class => 'Koha::Holds',
137 borrowernumber => $patron_1->id,
141 my $hold_2 = $builder->build_object(
143 class => 'Koha::Holds',
145 borrowernumber => $patron_2->id,
149 my $hold_3 = $builder->build_object(
151 class => 'Koha::Holds',
153 borrowernumber => $patron_1->id,
157 my $hold_4 = $builder->build_object(
159 class => 'Koha::Holds',
161 borrowernumber => $patron_2->id,
166 $hold_1 = Koha::Old::Holds->find( $hold_1->id )
167 ->set( { timestamp => dt_from_string() } )->store;
168 $hold_2 = Koha::Old::Holds->find( $hold_2->id )
169 ->set( { timestamp => dt_from_string()->subtract( days => 1 ) } )->store;
170 $hold_3 = Koha::Old::Holds->find( $hold_3->id )
171 ->set( { timestamp => dt_from_string()->subtract( days => 2 ) } )->store;
172 $hold_4 = Koha::Old::Holds->find( $hold_4->id )
173 ->set( { timestamp => dt_from_string()->subtract( days => 3 ) } )->store;
175 is( $patron_1->old_holds->count, 2, 'patron_1 has 2 completed holds' );
176 is( $patron_2->old_holds->count, 2, 'patron_2 has 2 completed holds' );
178 # filter them so only the older two are part of the resultset
179 my $holds = Koha::Old::Holds->search(
180 { 'me.borrowernumber' => [ $patron_1->id, $patron_2->id ] } );
181 is( $holds->count, 4, 'Total of 4 holds returned correctly' );
182 my $rs = $holds->filter_by_anonymizable;
183 is( $rs->count, 2, 'Only 2 can be anonymized' );
186 ->filter_by_anonymizable
187 ->filter_by_last_update( { days => 1 } );
189 is( $rs->count, 1, 'Only 1 can be anonymized with date filter applied' );
191 $schema->storage->txn_rollback;