4 # This file is part of Koha.
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.
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.
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>.
21 use Test::More tests => 4;
30 use t::lib::TestBuilder;
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $builder = t::lib::TestBuilder->new;
37 # TODO create a subroutine in t::lib::Mocks
38 my $branch = $builder->build({ source => 'Branch' });
39 my $userenv_patron = $builder->build({
41 value => { branchcode => $branch->{branchcode} },
43 C4::Context->_new_userenv('DUMMY SESSION');
44 C4::Context->set_userenv(
45 $userenv_patron->{borrowernumber},
46 $userenv_patron->{userid},
47 'usercnum', 'First name', 'Surname',
48 $branch->{branchcode},
49 $branch->{branchname},
53 my $anonymous = $builder->build( { source => 'Borrower', }, );
55 t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} );
57 subtest 'patron privacy is 1 (default)' => sub {
59 my $patron = $builder->build(
60 { source => 'Borrower',
61 value => { privacy => 1, }
64 my $item = $builder->build(
72 my $issue = $builder->build(
75 borrowernumber => $patron->{borrowernumber},
76 itemnumber => $item->{itemnumber},
81 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
82 is( $returned, 1, 'The item should have been returned' );
83 my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
84 ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
85 is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
87 my $dbh = C4::Context->dbh;
88 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
89 SELECT borrowernumber FROM old_issues where itemnumber = ?
90 |, undef, $item->{itemnumber});
91 is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' );
95 subtest 'patron privacy is 0 (forever)' => sub {
98 my $patron = $builder->build(
99 { source => 'Borrower',
100 value => { privacy => 0, }
103 my $item = $builder->build(
111 my $issue = $builder->build(
114 borrowernumber => $patron->{borrowernumber},
115 itemnumber => $item->{itemnumber},
120 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
121 is( $returned, 1, 'The item should have been returned' );
122 my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
123 is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
125 my $dbh = C4::Context->dbh;
126 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
127 SELECT borrowernumber FROM old_issues where itemnumber = ?
128 |, undef, $item->{itemnumber});
129 is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'With privacy=0, the issue should not be anonymised' );
132 t::lib::Mocks::mock_preference( 'AnonymousPatron', '' );
134 subtest 'AnonymousPatron is not defined' => sub {
136 my $patron = $builder->build(
137 { source => 'Borrower',
138 value => { privacy => 1, }
141 my $item = $builder->build(
149 my $issue = $builder->build(
152 borrowernumber => $patron->{borrowernumber},
153 itemnumber => $item->{itemnumber},
158 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
159 is( $returned, 1, 'The item should have been returned' );
160 my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11');
161 ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' );
162 is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' );
164 my $dbh = C4::Context->dbh;
165 my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q|
166 SELECT borrowernumber FROM old_issues where itemnumber = ?
167 |, undef, $item->{itemnumber});
168 is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' );
171 subtest 'Test StoreLastBorrower' => sub {
174 t::lib::Mocks::mock_preference( 'StoreLastBorrower', '1' );
176 my $patron = $builder->build(
178 source => 'Borrower',
179 value => { privacy => 1, }
183 my $item = $builder->build(
193 my $issue = $builder->build(
197 borrowernumber => $patron->{borrowernumber},
198 itemnumber => $item->{itemnumber},
203 my $item_object = Koha::Items->find( $item->{itemnumber} );
204 my $patron_object = $item_object->last_returned_by();
205 is( $patron_object, undef, 'Koha::Item::last_returned_by returned undef' );
207 my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
209 $item_object = Koha::Items->find( $item->{itemnumber} );
210 $patron_object = $item_object->last_returned_by();
211 is( ref($patron_object), 'Koha::Patron', 'Koha::Item::last_returned_by returned Koha::Patron' );
213 $patron = $builder->build(
215 source => 'Borrower',
216 value => { privacy => 1, }
220 $issue = $builder->build(
224 borrowernumber => $patron->{borrowernumber},
225 itemnumber => $item->{itemnumber},
230 ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
232 $item_object = Koha::Items->find( $item->{itemnumber} );
233 $patron_object = $item_object->last_returned_by();
234 is( $patron_object->id, $patron->{borrowernumber}, 'Second patron to return item replaces the first' );
236 $patron = $builder->build(
238 source => 'Borrower',
239 value => { privacy => 1, }
242 $patron_object = Koha::Patrons->find( $patron->{borrowernumber} );
244 $item_object->last_returned_by($patron_object);
245 $item_object = Koha::Items->find( $item->{itemnumber} );
246 my $patron_object2 = $item_object->last_returned_by();
247 is( $patron_object->id, $patron_object2->id,
248 'Calling last_returned_by with Borrower object sets last_returned_by to that borrower' );
250 $patron_object->delete;
251 $item_object = Koha::Items->find( $item->{itemnumber} );
252 is( $item_object->last_returned_by, undef, 'last_returned_by should return undef if the last patron to return the item has been deleted' );
254 t::lib::Mocks::mock_preference( 'StoreLastBorrower', '0' );
255 $patron = $builder->build(
257 source => 'Borrower',
258 value => { privacy => 1, }
262 $issue = $builder->build(
266 borrowernumber => $patron->{borrowernumber},
267 itemnumber => $item->{itemnumber},
271 ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' );
273 $item_object = Koha::Items->find( $item->{itemnumber} );
274 is( $item_object->last_returned_by, undef, 'Last patron to return item should not be stored if StoreLastBorrower if off' );
277 $schema->storage->txn_rollback;