Bug 15548: Move new patron related code to Patron*
[koha.git] / t / db_dependent / Circulation / AnonymiseIssueHistory.t
1
2 #!/usr/bin/perl
3
4 # This file is part of Koha.
5 #
6 # Koha is free software; you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
10 #
11 # Koha is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use Test::More tests => 4;
22
23 use C4::Context;
24 use C4::Circulation;
25
26 use Koha::Database;
27 use Koha::Items;
28
29 use t::lib::Mocks;
30 use t::lib::TestBuilder;
31
32 my $schema  = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
34
35 my $builder = t::lib::TestBuilder->new;
36
37 # TODO create a subroutine in t::lib::Mocks
38 my $userenv_patron = $builder->build( { source => 'Borrower', }, );
39 C4::Context->_new_userenv('DUMMY SESSION');
40 C4::Context->set_userenv(
41     $userenv_patron->{borrowernumber},
42     $userenv_patron->{userid},
43     'usercnum', 'First name', 'Surname',
44     $userenv_patron->{_fk}{branchcode}{branchcode},
45     $userenv_patron->{_fk}{branchcode}{branchname}, 0
46 );
47
48 my $anonymous = $builder->build( { source => 'Borrower', }, );
49
50 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
51
52 subtest 'patron privacy is 1 (default)' => sub {
53     plan tests => 4;
54     my $patron = $builder->build(
55         {   source => 'Borrower',
56             value  => { privacy => 1, }
57         }
58     );
59     my $item = $builder->build(
60         {   source => 'Item',
61             value  => {
62                 itemlost  => 0,
63                 withdrawn => 0,
64             },
65         }
66     );
67     my $issue = $builder->build(
68         {   source => 'Issue',
69             value  => {
70                 borrowernumber => $patron->{borrowernumber},
71                 itemnumber     => $item->{itemnumber},
72             },
73         }
74     );
75
76     my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
77     is( $returned, 1, 'The item should have been returned' );
78     my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
79     ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
80     is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
81
82     my $dbh = C4::Context->dbh;
83     my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
84         SELECT borrowernumber FROM old_issues where itemnumber = ?
85     |, undef, $item->{itemnumber});
86     is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
87
88 };
89
90 subtest 'patron privacy is 0 (forever)' => sub {
91     plan tests => 3;
92
93     my $patron = $builder->build(
94         {   source => 'Borrower',
95             value  => { privacy => 0, }
96         }
97     );
98     my $item = $builder->build(
99         {   source => 'Item',
100             value  => {
101                 itemlost  => 0,
102                 withdrawn => 0,
103             },
104         }
105     );
106     my $issue = $builder->build(
107         {   source => 'Issue',
108             value  => {
109                 borrowernumber => $patron->{borrowernumber},
110                 itemnumber     => $item->{itemnumber},
111             },
112         }
113     );
114
115     my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
116     is( $returned, 1, 'The item should have been returned' );
117     my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
118     is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
119
120     my $dbh = C4::Context->dbh;
121     my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
122         SELECT borrowernumber FROM old_issues where itemnumber = ?
123     |, undef, $item->{itemnumber});
124     is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'With privacy=0, the issue should not be anonymised' );
125 };
126
127 t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
128
129 subtest 'AnonymousPatron is not defined' => sub {
130     plan tests => 4;
131     my $patron = $builder->build(
132         {   source => 'Borrower',
133             value  => { privacy => 1, }
134         }
135     );
136     my $item = $builder->build(
137         {   source => 'Item',
138             value  => {
139                 itemlost  => 0,
140                 withdrawn => 0,
141             },
142         }
143     );
144     my $issue = $builder->build(
145         {   source => 'Issue',
146             value  => {
147                 borrowernumber => $patron->{borrowernumber},
148                 itemnumber     => $item->{itemnumber},
149             },
150         }
151     );
152
153     my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
154     is( $returned, 1, 'The item should have been returned' );
155     my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
156     ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
157     is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
158
159     my $dbh = C4::Context->dbh;
160     my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
161         SELECT borrowernumber FROM old_issues where itemnumber = ?
162     |, undef, $item->{itemnumber});
163     is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
164 };
165
166 subtest 'Test StoreLastBorrower' => sub {
167     plan tests => 6;
168
169     t::lib::Mocks::mock_preference( 'StoreLastBorrower', '1' );
170
171     my $patron = $builder->build(
172         {
173             source => 'Borrower',
174             value  => { privacy => 1, }
175         }
176     );
177
178     my $item = $builder->build(
179         {
180             source => 'Item',
181             value  => {
182                 itemlost  => 0,
183                 withdrawn => 0,
184             },
185         }
186     );
187
188     my $issue = $builder->build(
189         {
190             source => 'Issue',
191             value  => {
192                 borrowernumber => $patron->{borrowernumber},
193                 itemnumber     => $item->{itemnumber},
194             },
195         }
196     );
197
198     my $item_object   = Koha::Items->find( $item->{itemnumber} );
199     my $patron_object = $item_object->last_returned_by();
200     is( $patron_object, undef, 'Koha::Item::last_returned_by returned undef' );
201
202     my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
203
204     $item_object   = Koha::Items->find( $item->{itemnumber} );
205     $patron_object = $item_object->last_returned_by();
206     is( ref($patron_object), 'Koha::Patron', 'Koha::Item::last_returned_by returned Koha::Patron' );
207
208     $patron = $builder->build(
209         {
210             source => 'Borrower',
211             value  => { privacy => 1, }
212         }
213     );
214
215     $issue = $builder->build(
216         {
217             source => 'Issue',
218             value  => {
219                 borrowernumber => $patron->{borrowernumber},
220                 itemnumber     => $item->{itemnumber},
221             },
222         }
223     );
224
225     ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
226
227     $item_object   = Koha::Items->find( $item->{itemnumber} );
228     $patron_object = $item_object->last_returned_by();
229     is( $patron_object->id, $patron->{borrowernumber}, 'Second patron to return item replaces the first' );
230
231     $patron = $builder->build(
232         {
233             source => 'Borrower',
234             value  => { privacy => 1, }
235         }
236     );
237     $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
238
239     $item_object->last_returned_by($patron_object);
240     $item_object = Koha::Items->find( $item->{itemnumber} );
241     my $patron_object2 = $item_object->last_returned_by();
242     is( $patron_object->id, $patron_object2->id,
243         'Calling last_returned_by with Borrower object sets last_returned_by to that borrower' );
244
245     $patron_object->delete;
246     $item_object = Koha::Items->find( $item->{itemnumber} );
247     is( $item_object->last_returned_by, undef, 'last_returned_by should return undef if the last patron to return the item has been deleted' );
248
249     t::lib::Mocks::mock_preference( 'StoreLastBorrower', '0' );
250     $patron = $builder->build(
251         {
252             source => 'Borrower',
253             value  => { privacy => 1, }
254         }
255     );
256
257     $issue = $builder->build(
258         {
259             source => 'Issue',
260             value  => {
261                 borrowernumber => $patron->{borrowernumber},
262                 itemnumber     => $item->{itemnumber},
263             },
264         }
265     );
266     ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
267
268     $item_object   = Koha::Items->find( $item->{itemnumber} );
269     is( $item_object->last_returned_by, undef, 'Last patron to return item should not be stored if StoreLastBorrower if off' );
270 };
271
272 $schema->storage->txn_rollback;
273
274 1;