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);
25 use Koha::Old::Checkouts;
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_checkouts->count, 0, 'Patron has no old checkouts' );
44 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
47 { $patron->old_checkouts->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_checkouts->anonymize + 0,
56 0, 'Anonymizing an empty resultset returns 0' );
58 my $checkout_1 = $builder->build_object(
60 class => 'Koha::Old::Checkouts',
62 { borrowernumber => $patron->id, timestamp => dt_from_string() }
65 my $checkout_2 = $builder->build_object(
67 class => 'Koha::Old::Checkouts',
69 borrowernumber => $patron->id,
70 timestamp => dt_from_string()->subtract( days => 1 )
74 my $checkout_3 = $builder->build_object(
76 class => 'Koha::Old::Checkouts',
78 borrowernumber => $patron->id,
79 timestamp => dt_from_string()->subtract( days => 2 )
83 my $checkout_4 = $builder->build_object(
85 class => 'Koha::Old::Checkouts',
87 borrowernumber => $patron->id,
88 timestamp => dt_from_string()->subtract( days => 3 )
93 is( $patron->old_checkouts->count, 4, 'Patron has 4 completed checkouts' );
95 # filter them so only the older two are part of the resultset
96 my $checkouts = $patron->old_checkouts->filter_by_last_update(
97 { days => 1, days_inclusive => 1 } );
99 t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
101 { $checkouts->anonymize; }
102 'Koha::Exceptions::SysPref::NotSet',
103 'Exception thrown because AnonymousPatron not set';
105 is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
106 is( $patron->old_checkouts->count, 4, 'Patron has 4 completed checkouts' );
108 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
111 my $anonymized_count = $checkouts->anonymize();
112 is( $anonymized_count, 2, 'update() tells 2 rows were updated' );
114 is( $patron->old_checkouts->count, 2, 'Patron has 2 completed checkouts' );
116 $schema->storage->txn_rollback;
119 subtest 'filter_by_anonymizable() tests' => sub {
123 $schema->storage->txn_begin;
125 my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
126 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
128 # patron_1 => keep records forever
129 my $patron_1 = $builder->build_object(
130 { class => 'Koha::Patrons', value => { privacy => 0 } } );
132 # patron_2 => never keep records
133 my $patron_2 = $builder->build_object(
134 { class => 'Koha::Patrons', value => { privacy => 1 } } );
136 is( $patron_1->old_checkouts->count, 0, 'patron_1 has no old checkouts' );
137 is( $patron_2->old_checkouts->count, 0, 'patron_2 has no old checkouts' );
139 my $checkout_1 = $builder->build_object(
141 class => 'Koha::Old::Checkouts',
143 borrowernumber => $patron_1->id,
147 my $checkout_2 = $builder->build_object(
149 class => 'Koha::Old::Checkouts',
151 borrowernumber => $patron_2->id,
155 my $checkout_3 = $builder->build_object(
157 class => 'Koha::Old::Checkouts',
159 borrowernumber => $patron_1->id,
163 my $checkout_4 = $builder->build_object(
165 class => 'Koha::Old::Checkouts',
167 borrowernumber => $patron_2->id,
171 # borrowernumber == undef => never listed as anonymizable
172 my $checkout_5 = $builder->build_object(
174 class => 'Koha::Old::Checkouts',
176 borrowernumber => undef,
180 # borrowernumber == anonymous patron => never listed as anonymizable
181 my $checkout_6 = $builder->build_object(
183 class => 'Koha::Old::Checkouts',
185 borrowernumber => $anonymous_patron->id,
190 $checkout_2->set( { timestamp => dt_from_string()->subtract( days => 1 ) } )->store;
191 $checkout_3->set( { timestamp => dt_from_string()->subtract( days => 2 ) } )->store;
192 $checkout_4->set( { timestamp => dt_from_string()->subtract( days => 3 ) } )->store;
194 is( $patron_1->old_checkouts->count, 2, 'patron_1 has 2 completed checkouts' );
195 is( $patron_2->old_checkouts->count, 2, 'patron_2 has 2 completed checkouts' );
197 # filter them so only the older two are part of the resultset
198 my $checkouts = Koha::Old::Checkouts->search(
199 { 'me.borrowernumber' => [ $patron_1->id, $patron_2->id ] } );
200 is( $checkouts->count, 4, 'Total of 4 checkouts returned correctly' );
201 my $rs = $checkouts->filter_by_anonymizable;
202 is( $rs->count, 2, 'Only 2 can be anonymized' );
204 $rs = $checkouts->filter_by_anonymizable->filter_by_last_update(
207 is( $rs->count, 1, 'Only 1 can be anonymized with date filter applied' );
209 $schema->storage->txn_rollback;