|
Lines 1-313
Link Here
|
| 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 => 5; |
| 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 $branch = $builder->build({ source => 'Branch' }); |
| 39 |
my $userenv_patron = $builder->build({ |
| 40 |
source => 'Borrower', |
| 41 |
value => { branchcode => $branch->{branchcode} }, |
| 42 |
}); |
| 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}, |
| 50 |
0, |
| 51 |
); |
| 52 |
|
| 53 |
my $anonymous = $builder->build( { source => 'Borrower', }, ); |
| 54 |
|
| 55 |
t::lib::Mocks::mock_preference( 'AnonymousPatron', $anonymous->{borrowernumber} ); |
| 56 |
|
| 57 |
subtest 'patron privacy is 1 (default)' => sub { |
| 58 |
plan tests => 4; |
| 59 |
t::lib::Mocks::mock_preference('IndependentBranches', 0); |
| 60 |
my $patron = $builder->build( |
| 61 |
{ source => 'Borrower', |
| 62 |
value => { privacy => 1, branchcode => $userenv_patron->{branchcode} } |
| 63 |
} |
| 64 |
); |
| 65 |
my $item = $builder->build( |
| 66 |
{ source => 'Item', |
| 67 |
value => { |
| 68 |
itemlost => 0, |
| 69 |
withdrawn => 0, |
| 70 |
}, |
| 71 |
} |
| 72 |
); |
| 73 |
my $issue = $builder->build( |
| 74 |
{ source => 'Issue', |
| 75 |
value => { |
| 76 |
borrowernumber => $patron->{borrowernumber}, |
| 77 |
itemnumber => $item->{itemnumber}, |
| 78 |
}, |
| 79 |
} |
| 80 |
); |
| 81 |
|
| 82 |
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' ); |
| 83 |
is( $returned, 1, 'The item should have been returned' ); |
| 84 |
my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11'); |
| 85 |
ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' ); |
| 86 |
is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' ); |
| 87 |
|
| 88 |
my $dbh = C4::Context->dbh; |
| 89 |
my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q| |
| 90 |
SELECT borrowernumber FROM old_issues where itemnumber = ? |
| 91 |
|, undef, $item->{itemnumber}); |
| 92 |
is( $borrowernumber_used_to_anonymised, $anonymous->{borrowernumber}, 'With privacy=1, the issue should have been anonymised' ); |
| 93 |
|
| 94 |
}; |
| 95 |
|
| 96 |
subtest 'patron privacy is 0 (forever)' => sub { |
| 97 |
plan tests => 3; |
| 98 |
t::lib::Mocks::mock_preference('IndependentBranches', 0); |
| 99 |
|
| 100 |
my $patron = $builder->build( |
| 101 |
{ source => 'Borrower', |
| 102 |
value => { privacy => 0, branchcode => $userenv_patron->{branchcode} } |
| 103 |
} |
| 104 |
); |
| 105 |
my $item = $builder->build( |
| 106 |
{ source => 'Item', |
| 107 |
value => { |
| 108 |
itemlost => 0, |
| 109 |
withdrawn => 0, |
| 110 |
}, |
| 111 |
} |
| 112 |
); |
| 113 |
my $issue = $builder->build( |
| 114 |
{ source => 'Issue', |
| 115 |
value => { |
| 116 |
borrowernumber => $patron->{borrowernumber}, |
| 117 |
itemnumber => $item->{itemnumber}, |
| 118 |
}, |
| 119 |
} |
| 120 |
); |
| 121 |
|
| 122 |
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' ); |
| 123 |
is( $returned, 1, 'The item should have been returned' ); |
| 124 |
my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11'); |
| 125 |
is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' ); |
| 126 |
|
| 127 |
my $dbh = C4::Context->dbh; |
| 128 |
my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q| |
| 129 |
SELECT borrowernumber FROM old_issues where itemnumber = ? |
| 130 |
|, undef, $item->{itemnumber}); |
| 131 |
is( $borrowernumber_used_to_anonymised, $patron->{borrowernumber}, 'With privacy=0, the issue should not be anonymised' ); |
| 132 |
}; |
| 133 |
|
| 134 |
t::lib::Mocks::mock_preference( 'AnonymousPatron', '' ); |
| 135 |
|
| 136 |
subtest 'AnonymousPatron is not defined' => sub { |
| 137 |
plan tests => 4; |
| 138 |
t::lib::Mocks::mock_preference('IndependentBranches', 0); |
| 139 |
my $patron = $builder->build( |
| 140 |
{ source => 'Borrower', |
| 141 |
value => { privacy => 1, branchcode => $userenv_patron->{branchcode} } |
| 142 |
} |
| 143 |
); |
| 144 |
my $item = $builder->build( |
| 145 |
{ source => 'Item', |
| 146 |
value => { |
| 147 |
itemlost => 0, |
| 148 |
withdrawn => 0, |
| 149 |
}, |
| 150 |
} |
| 151 |
); |
| 152 |
my $issue = $builder->build( |
| 153 |
{ source => 'Issue', |
| 154 |
value => { |
| 155 |
borrowernumber => $patron->{borrowernumber}, |
| 156 |
itemnumber => $item->{itemnumber}, |
| 157 |
}, |
| 158 |
} |
| 159 |
); |
| 160 |
|
| 161 |
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' ); |
| 162 |
is( $returned, 1, 'The item should have been returned' ); |
| 163 |
my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11'); |
| 164 |
ok( $rows_affected > 0, 'AnonymiseIssueHistory should affect at least 1 row' ); |
| 165 |
is( $err, undef, 'AnonymiseIssueHistory should not return any error if success' ); |
| 166 |
|
| 167 |
my $dbh = C4::Context->dbh; |
| 168 |
my ($borrowernumber_used_to_anonymised) = $dbh->selectrow_array(q| |
| 169 |
SELECT borrowernumber FROM old_issues where itemnumber = ? |
| 170 |
|, undef, $item->{itemnumber}); |
| 171 |
is( $borrowernumber_used_to_anonymised, undef, 'With AnonymousPatron is not defined, the issue should have been anonymised anyway' ); |
| 172 |
}; |
| 173 |
|
| 174 |
subtest 'Logged in librarian is not superlibrarian & IndependentBranches' => sub { |
| 175 |
plan tests => 1; |
| 176 |
t::lib::Mocks::mock_preference('IndependentBranches', 1); |
| 177 |
my $patron = $builder->build( |
| 178 |
{ source => 'Borrower', |
| 179 |
value => { privacy => 1 } # Another branchcode than the logged in librarian |
| 180 |
} |
| 181 |
); |
| 182 |
my $item = $builder->build( |
| 183 |
{ source => 'Item', |
| 184 |
value => { |
| 185 |
itemlost => 0, |
| 186 |
withdrawn => 0, |
| 187 |
}, |
| 188 |
} |
| 189 |
); |
| 190 |
my $issue = $builder->build( |
| 191 |
{ source => 'Issue', |
| 192 |
value => { |
| 193 |
borrowernumber => $patron->{borrowernumber}, |
| 194 |
itemnumber => $item->{itemnumber}, |
| 195 |
}, |
| 196 |
} |
| 197 |
); |
| 198 |
|
| 199 |
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' ); |
| 200 |
my $patrons_to_anonymise = C4::Members::GetBorrowersWithIssuesHistoryOlderThan( '2010-10-11' ); |
| 201 |
my ( $rows_affected, $err ) = C4::Circulation::AnonymiseIssueHistory('2010-10-11'); |
| 202 |
is( scalar(@$patrons_to_anonymise), $rows_affected, , 'AnonymiseIssueHistory should affect at least 1 row' ); |
| 203 |
}; |
| 204 |
|
| 205 |
subtest 'Test StoreLastBorrower' => sub { |
| 206 |
plan tests => 6; |
| 207 |
|
| 208 |
t::lib::Mocks::mock_preference( 'StoreLastBorrower', '1' ); |
| 209 |
|
| 210 |
my $patron = $builder->build( |
| 211 |
{ |
| 212 |
source => 'Borrower', |
| 213 |
value => { privacy => 1, } |
| 214 |
} |
| 215 |
); |
| 216 |
|
| 217 |
my $item = $builder->build( |
| 218 |
{ |
| 219 |
source => 'Item', |
| 220 |
value => { |
| 221 |
itemlost => 0, |
| 222 |
withdrawn => 0, |
| 223 |
}, |
| 224 |
} |
| 225 |
); |
| 226 |
|
| 227 |
my $issue = $builder->build( |
| 228 |
{ |
| 229 |
source => 'Issue', |
| 230 |
value => { |
| 231 |
borrowernumber => $patron->{borrowernumber}, |
| 232 |
itemnumber => $item->{itemnumber}, |
| 233 |
}, |
| 234 |
} |
| 235 |
); |
| 236 |
|
| 237 |
my $item_object = Koha::Items->find( $item->{itemnumber} ); |
| 238 |
my $patron_object = $item_object->last_returned_by(); |
| 239 |
is( $patron_object, undef, 'Koha::Item::last_returned_by returned undef' ); |
| 240 |
|
| 241 |
my ( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' ); |
| 242 |
|
| 243 |
$item_object = Koha::Items->find( $item->{itemnumber} ); |
| 244 |
$patron_object = $item_object->last_returned_by(); |
| 245 |
is( ref($patron_object), 'Koha::Patron', 'Koha::Item::last_returned_by returned Koha::Patron' ); |
| 246 |
|
| 247 |
$patron = $builder->build( |
| 248 |
{ |
| 249 |
source => 'Borrower', |
| 250 |
value => { privacy => 1, } |
| 251 |
} |
| 252 |
); |
| 253 |
|
| 254 |
$issue = $builder->build( |
| 255 |
{ |
| 256 |
source => 'Issue', |
| 257 |
value => { |
| 258 |
borrowernumber => $patron->{borrowernumber}, |
| 259 |
itemnumber => $item->{itemnumber}, |
| 260 |
}, |
| 261 |
} |
| 262 |
); |
| 263 |
|
| 264 |
( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' ); |
| 265 |
|
| 266 |
$item_object = Koha::Items->find( $item->{itemnumber} ); |
| 267 |
$patron_object = $item_object->last_returned_by(); |
| 268 |
is( $patron_object->id, $patron->{borrowernumber}, 'Second patron to return item replaces the first' ); |
| 269 |
|
| 270 |
$patron = $builder->build( |
| 271 |
{ |
| 272 |
source => 'Borrower', |
| 273 |
value => { privacy => 1, } |
| 274 |
} |
| 275 |
); |
| 276 |
$patron_object = Koha::Patrons->find( $patron->{borrowernumber} ); |
| 277 |
|
| 278 |
$item_object->last_returned_by($patron_object); |
| 279 |
$item_object = Koha::Items->find( $item->{itemnumber} ); |
| 280 |
my $patron_object2 = $item_object->last_returned_by(); |
| 281 |
is( $patron_object->id, $patron_object2->id, |
| 282 |
'Calling last_returned_by with Borrower object sets last_returned_by to that borrower' ); |
| 283 |
|
| 284 |
$patron_object->delete; |
| 285 |
$item_object = Koha::Items->find( $item->{itemnumber} ); |
| 286 |
is( $item_object->last_returned_by, undef, 'last_returned_by should return undef if the last patron to return the item has been deleted' ); |
| 287 |
|
| 288 |
t::lib::Mocks::mock_preference( 'StoreLastBorrower', '0' ); |
| 289 |
$patron = $builder->build( |
| 290 |
{ |
| 291 |
source => 'Borrower', |
| 292 |
value => { privacy => 1, } |
| 293 |
} |
| 294 |
); |
| 295 |
|
| 296 |
$issue = $builder->build( |
| 297 |
{ |
| 298 |
source => 'Issue', |
| 299 |
value => { |
| 300 |
borrowernumber => $patron->{borrowernumber}, |
| 301 |
itemnumber => $item->{itemnumber}, |
| 302 |
}, |
| 303 |
} |
| 304 |
); |
| 305 |
( $returned, undef, undef ) = C4::Circulation::AddReturn( $item->{barcode}, undef, undef, undef, '2010-10-10' ); |
| 306 |
|
| 307 |
$item_object = Koha::Items->find( $item->{itemnumber} ); |
| 308 |
is( $item_object->last_returned_by, undef, 'Last patron to return item should not be stored if StoreLastBorrower if off' ); |
| 309 |
}; |
| 310 |
|
| 311 |
$schema->storage->txn_rollback; |
| 312 |
|
| 313 |
1; |