Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[koha.git] / t / db_dependent / Koha / Old / Checkouts.t
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 => 2;
21 use Test::Exception;
22
23 use Koha::Database;
24 use Koha::DateUtils qw(dt_from_string);
25 use Koha::Old::Checkouts;
26
27 use t::lib::Mocks;
28 use t::lib::TestBuilder;
29
30 my $schema  = Koha::Database->new->schema;
31 my $builder = t::lib::TestBuilder->new;
32
33 subtest 'anonymize() tests' => sub {
34
35     plan tests => 10;
36
37     $schema->storage->txn_begin;
38
39     my $patron = $builder->build_object( { class => 'Koha::Patrons' } );
40     my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
41
42     is( $patron->old_checkouts->count, 0, 'Patron has no old checkouts' );
43
44     t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
45
46     throws_ok
47         { $patron->old_checkouts->anonymize; }
48         'Koha::Exceptions::SysPref::NotSet',
49         'Exception thrown because AnonymousPatron not set';
50
51     is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
52
53     t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
54
55     is( $patron->old_checkouts->anonymize + 0,
56         0, 'Anonymizing an empty resultset returns 0' );
57
58     my $checkout_1 = $builder->build_object(
59         {
60             class => 'Koha::Old::Checkouts',
61             value =>
62               { borrowernumber => $patron->id, timestamp => dt_from_string() }
63         }
64     );
65     my $checkout_2 = $builder->build_object(
66         {
67             class => 'Koha::Old::Checkouts',
68             value => {
69                 borrowernumber => $patron->id,
70                 timestamp      => dt_from_string()->subtract( days => 1 )
71             }
72         }
73     );
74     my $checkout_3 = $builder->build_object(
75         {
76             class => 'Koha::Old::Checkouts',
77             value => {
78                 borrowernumber => $patron->id,
79                 timestamp      => dt_from_string()->subtract( days => 2 )
80             }
81         }
82     );
83     my $checkout_4 = $builder->build_object(
84         {
85             class => 'Koha::Old::Checkouts',
86             value => {
87                 borrowernumber => $patron->id,
88                 timestamp      => dt_from_string()->subtract( days => 3 )
89             }
90         }
91     );
92
93     is( $patron->old_checkouts->count, 4, 'Patron has 4 completed checkouts' );
94
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 } );
98
99     t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
100     throws_ok
101         { $checkouts->anonymize; }
102         'Koha::Exceptions::SysPref::NotSet',
103         'Exception thrown because AnonymousPatron not set';
104
105     is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
106     is( $patron->old_checkouts->count, 4, 'Patron has 4 completed checkouts' );
107
108     t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
109
110     # Anonymize them
111     my $anonymized_count = $checkouts->anonymize();
112     is( $anonymized_count, 2, 'update() tells 2 rows were updated' );
113
114     is( $patron->old_checkouts->count, 2, 'Patron has 2 completed checkouts' );
115
116     $schema->storage->txn_rollback;
117 };
118
119 subtest 'filter_by_anonymizable() tests' => sub {
120
121     plan tests => 7;
122
123     $schema->storage->txn_begin;
124
125     my $anonymous_patron = $builder->build_object({ class => 'Koha::Patrons' });
126     t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
127
128     # patron_1 => keep records forever
129     my $patron_1 = $builder->build_object(
130         { class => 'Koha::Patrons', value => { privacy => 0 } } );
131
132     # patron_2 => never keep records
133     my $patron_2 = $builder->build_object(
134         { class => 'Koha::Patrons', value => { privacy => 1 } } );
135
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' );
138
139     my $checkout_1 = $builder->build_object(
140         {
141             class => 'Koha::Old::Checkouts',
142             value => {
143                 borrowernumber => $patron_1->id,
144             }
145         }
146     );
147     my $checkout_2 = $builder->build_object(
148         {
149             class => 'Koha::Old::Checkouts',
150             value => {
151                 borrowernumber => $patron_2->id,
152             }
153         }
154     );
155     my $checkout_3 = $builder->build_object(
156         {
157             class => 'Koha::Old::Checkouts',
158             value => {
159                 borrowernumber => $patron_1->id,
160             }
161         }
162     );
163     my $checkout_4 = $builder->build_object(
164         {
165             class => 'Koha::Old::Checkouts',
166             value => {
167                 borrowernumber => $patron_2->id,
168             }
169         }
170     );
171     # borrowernumber == undef => never listed as anonymizable
172     my $checkout_5 = $builder->build_object(
173         {
174             class => 'Koha::Old::Checkouts',
175             value => {
176                 borrowernumber => undef,
177             }
178         }
179     );
180     # borrowernumber == anonymous patron => never listed as anonymizable
181     my $checkout_6 = $builder->build_object(
182         {
183             class => 'Koha::Old::Checkouts',
184             value => {
185                 borrowernumber => $anonymous_patron->id,
186             }
187         }
188     );
189
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;
193
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' );
196
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' );
203
204     $rs = $checkouts->filter_by_anonymizable->filter_by_last_update(
205         { days => 1 } );
206
207     is( $rs->count, 1, 'Only 1 can be anonymized with date filter applied' );
208
209     $schema->storage->txn_rollback;
210 };