Bug 33360: Add Koha::Notice::Util for mail domain limits
[koha.git] / t / db_dependent / Koha / Old / Holds.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::Holds;
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_holds->count, 0, 'Patron has no old holds' );
43
44     t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
45
46     throws_ok
47         { $patron->old_holds->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_holds->anonymize + 0, 0, 'Anonymizing an empty resultset returns 0' );
56
57     my $hold_1 = $builder->build_object(
58         {
59             class => 'Koha::Old::Holds',
60             value =>
61               { borrowernumber => $patron->id, timestamp => dt_from_string() }
62         }
63     );
64     my $hold_2 = $builder->build_object(
65         {
66             class => 'Koha::Old::Holds',
67             value => {
68                 borrowernumber => $patron->id,
69                 timestamp      => dt_from_string()->subtract( days => 1 )
70             }
71         }
72     );
73     my $hold_3 = $builder->build_object(
74         {
75             class => 'Koha::Old::Holds',
76             value => {
77                 borrowernumber => $patron->id,
78                 timestamp      => dt_from_string()->subtract( days => 2 )
79             }
80         }
81     );
82     my $hold_4 = $builder->build_object(
83         {
84             class => 'Koha::Old::Holds',
85             value => {
86                 borrowernumber => $patron->id,
87                 timestamp      => dt_from_string()->subtract( days => 3 )
88             }
89         }
90     );
91
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 ) } });
95     # Anonymize them
96
97     t::lib::Mocks::mock_preference( 'AnonymousPatron', undef );
98     throws_ok
99         { $holds->anonymize; }
100         'Koha::Exceptions::SysPref::NotSet',
101         'Exception thrown because AnonymousPatron not set';
102
103     is( $@->syspref, 'AnonymousPatron', 'syspref parameter is correctly passed' );
104     is( $patron->old_holds->count, 4, 'Patron has 4 completed holds' );
105
106     t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous_patron->id );
107
108     my $anonymized_count = $holds->anonymize();
109     is( $anonymized_count, 2, 'update() tells 2 rows were updated' );
110
111     is( $patron->old_holds->count, 2, 'Patron has 2 completed holds' );
112
113     $schema->storage->txn_rollback;
114 };
115
116 subtest 'filter_by_anonymizable() tests' => sub {
117
118     plan tests => 7;
119
120     $schema->storage->txn_begin;
121
122     # patron_1 => keep records forever
123     my $patron_1 = $builder->build_object(
124         { class => 'Koha::Patrons', value => { privacy => 0 } } );
125
126     # patron_2 => never keep records
127     my $patron_2 = $builder->build_object(
128         { class => 'Koha::Patrons', value => { privacy => 1 } } );
129
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' );
132
133     my $hold_1 = $builder->build_object(
134         {
135             class => 'Koha::Holds',
136             value => {
137                 borrowernumber => $patron_1->id,
138             }
139         }
140     )->_move_to_old;
141     my $hold_2 = $builder->build_object(
142         {
143             class => 'Koha::Holds',
144             value => {
145                 borrowernumber => $patron_2->id,
146             }
147         }
148     )->_move_to_old;
149     my $hold_3 = $builder->build_object(
150         {
151             class => 'Koha::Holds',
152             value => {
153                 borrowernumber => $patron_1->id,
154             }
155         }
156     )->_move_to_old;
157     my $hold_4 = $builder->build_object(
158         {
159             class => 'Koha::Holds',
160             value => {
161                 borrowernumber => $patron_2->id,
162             }
163         }
164     )->_move_to_old;
165
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;
174
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' );
177
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' );
184
185     $rs = $holds
186             ->filter_by_anonymizable
187             ->filter_by_last_update( { days => 1 } );
188
189     is( $rs->count, 1, 'Only 1 can be anonymized with date filter applied' );
190
191     $schema->storage->txn_rollback;
192 };