|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Copyright Koha-Suomi Oy 2016 |
| 4 |
# |
| 5 |
# This file is part of Koha |
| 6 |
# |
| 7 |
# Koha is free software; you can redistribute it and/or modify it |
| 8 |
# under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# Koha is distributed in the hope that it will be useful, but |
| 13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
| 19 |
|
| 20 |
use Modern::Perl; |
| 21 |
use Test::More tests => 4; |
| 22 |
use t::lib::Mocks; |
| 23 |
use t::lib::TestBuilder; |
| 24 |
|
| 25 |
use C4::Biblio; |
| 26 |
use C4::Circulation; |
| 27 |
use C4::Reserves; |
| 28 |
|
| 29 |
use Koha::Database; |
| 30 |
use Koha::Items; |
| 31 |
use Koha::ItemTypes; |
| 32 |
|
| 33 |
use_ok('Koha::Item::Availabilities'); |
| 34 |
use_ok('Koha::Item::Availability'); |
| 35 |
|
| 36 |
my $schema = Koha::Database->new->schema; |
| 37 |
$schema->storage->txn_begin; |
| 38 |
|
| 39 |
subtest 'Koha::Item::Availability tests' => sub { |
| 40 |
plan tests => 14; |
| 41 |
|
| 42 |
my $availability = Koha::Item::Availability->new->set_available; |
| 43 |
|
| 44 |
is($availability->available, 1, "Available"); |
| 45 |
$availability->set_needs_confirmation; |
| 46 |
is($availability->availability_needs_confirmation, 1, "Needs confirmation"); |
| 47 |
$availability->set_unavailable; |
| 48 |
is($availability->available, 0, "Not available"); |
| 49 |
|
| 50 |
$availability->add_description("such available"); |
| 51 |
$availability->add_description("wow"); |
| 52 |
$availability->add_description("wow"); |
| 53 |
|
| 54 |
ok($availability->has_description("wow"), "Found description 'wow'"); |
| 55 |
ok($availability->has_description(["wow", "such available"]), |
| 56 |
"Found description 'wow' and 'such available'"); |
| 57 |
is($availability->has_description(["wow", "much not found"]), 0, |
| 58 |
"Didn't find 'wow' and 'much not found'"); |
| 59 |
is($availability->description->[0], "such available", |
| 60 |
"Found correct description in correct index 1/4"); |
| 61 |
is($availability->description->[1], "wow", |
| 62 |
"Found correct description in correct index 2/2"); |
| 63 |
|
| 64 |
$availability->add_description(["much description", "very doge"]); |
| 65 |
is($availability->description->[2], "much description", |
| 66 |
"Found correct description in correct index 3/4"); |
| 67 |
is($availability->description->[3], "very doge", |
| 68 |
"Found correct description in correct index 4/4"); |
| 69 |
|
| 70 |
$availability->del_description("wow"); |
| 71 |
is($availability->description->[1], "much description", |
| 72 |
"Found description from correct index after del"); |
| 73 |
$availability->del_description(["very doge", "such available"]); |
| 74 |
is($availability->description->[0], "much description", |
| 75 |
"Found description from correct index after del"); |
| 76 |
|
| 77 |
my $availability_clone = $availability; |
| 78 |
$availability->set_unavailable; |
| 79 |
is($availability_clone->available, $availability->{available}, |
| 80 |
"Availability_clone points to availability"); |
| 81 |
$availability_clone = $availability->clone; |
| 82 |
$availability->set_available; |
| 83 |
isnt($availability_clone->available, $availability->{available}, |
| 84 |
"Availability_clone was cloned and no longer has same availability status"); |
| 85 |
}; |
| 86 |
|
| 87 |
subtest 'Item availability tests' => sub { |
| 88 |
plan tests => 14; |
| 89 |
|
| 90 |
my $builder = t::lib::TestBuilder->new; |
| 91 |
my $library = $builder->build({ source => 'Branch' }); |
| 92 |
my $itemtype_built = $builder->build({ |
| 93 |
source => 'Itemtype', |
| 94 |
value => { |
| 95 |
notforloan => 0, |
| 96 |
} |
| 97 |
}); |
| 98 |
my $biblioitem_built = $builder->build({ |
| 99 |
source => 'Biblioitem', |
| 100 |
value => { |
| 101 |
itemtype => $itemtype_built->{'itemtype'}, |
| 102 |
} |
| 103 |
}); |
| 104 |
my $item_built = $builder->build({ |
| 105 |
source => 'Item', |
| 106 |
value => { |
| 107 |
holding_branch => $library->{branchcode}, |
| 108 |
homebranch => $library->{branchcode}, |
| 109 |
biblioitemnumber => $biblioitem_built->{biblioitemnumber}, |
| 110 |
itype => $itemtype_built->{itemtype}, |
| 111 |
} |
| 112 |
}); |
| 113 |
|
| 114 |
t::lib::Mocks::mock_preference('item-level_itypes', 0); |
| 115 |
t::lib::Mocks::mock_preference('OnSiteCheckouts', 1); |
| 116 |
t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); |
| 117 |
|
| 118 |
my $itemtype = Koha::ItemTypes->find($itemtype_built->{itemtype}); |
| 119 |
my $item = Koha::Items->find($item_built->{itemnumber}); |
| 120 |
$item->set({ |
| 121 |
notforloan => 0, |
| 122 |
damaged => 0, |
| 123 |
itemlost => 0, |
| 124 |
withdrawn => 0, |
| 125 |
onloan => undef, |
| 126 |
restricted => 0, |
| 127 |
})->store; # set available |
| 128 |
|
| 129 |
ok($item->can('availabilities'), "Koha::Item->availabilities exists."); |
| 130 |
my $availabilities = $item->availabilities; |
| 131 |
is(ref($availabilities), 'Koha::Item::Availabilities', '$availabilities is blessed as Koha::Item::Availabilities'); |
| 132 |
|
| 133 |
my $holdability = $availabilities->hold; |
| 134 |
my $issuability = $availabilities->issue; |
| 135 |
my $for_local_use = $availabilities->local_use; |
| 136 |
my $onsite_issuability = $availabilities->onsite_checkout; |
| 137 |
is(ref($holdability), 'Koha::Item::Availability', '1/4 Correct class'); |
| 138 |
is(ref($issuability), 'Koha::Item::Availability', '2/4 Correct class'); |
| 139 |
is(ref($for_local_use), 'Koha::Item::Availability', '3/4 Correct class'); |
| 140 |
is(ref($onsite_issuability), 'Koha::Item::Availability', '4/4 Correct class'); |
| 141 |
|
| 142 |
ok($holdability->available, 'Available for holds'); |
| 143 |
ok($issuability->available, 'Available for checkouts'); |
| 144 |
ok($for_local_use->available, 'Available for local use'); |
| 145 |
ok($onsite_issuability->available, 'Available for onsite checkout'); |
| 146 |
|
| 147 |
# Test plan: |
| 148 |
# Subtest for each availability type in predefined order; |
| 149 |
# hold -> checkout -> local_use -> onsite_checkout |
| 150 |
# Each is dependant on the previous one, no need to run same tests as moving |
| 151 |
# from left to right. |
| 152 |
subtest 'Availability: hold' => sub { |
| 153 |
plan tests => 14; |
| 154 |
|
| 155 |
$item->withdrawn(1)->store; |
| 156 |
ok(!$availabilities->hold->available, "Item withdrawn => not available"); |
| 157 |
is($availabilities->hold->description->[0], 'withdrawn', 'Description: withdrawn'); |
| 158 |
$item->withdrawn(0)->itemlost(1)->store; |
| 159 |
ok(!$availabilities->hold->available, "Item lost => not available"); |
| 160 |
is($availabilities->hold->description->[0], 'itemlost', 'Description: itemlost'); |
| 161 |
$item->itemlost(0)->restricted(1)->store; |
| 162 |
ok(!$availabilities->hold->available, "Item restricted => not available"); |
| 163 |
is($availabilities->hold->description->[0], 'restricted', 'Description: restricted'); |
| 164 |
$item->restricted(0)->store; |
| 165 |
|
| 166 |
subtest 'Hold on damaged item' => sub { |
| 167 |
plan tests => 3; |
| 168 |
|
| 169 |
t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); |
| 170 |
$item->damaged(1)->store; |
| 171 |
ok($item->damaged, "Item is damaged"); |
| 172 |
ok(!$availabilities->hold->available, 'Not available for holds (AllowHoldsOnDamagedItems => 0)'); |
| 173 |
t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1); |
| 174 |
ok($availabilities->hold->available, 'Available for holds (AllowHoldsOnDamagedItems => 1)'); |
| 175 |
$item->damaged(0)->store; |
| 176 |
}; |
| 177 |
|
| 178 |
t::lib::Mocks::mock_preference('item-level_itypes', 1); |
| 179 |
$item->notforloan(1)->store; |
| 180 |
ok(!$availabilities->hold->available, "Item notforloan => not available"); |
| 181 |
is($availabilities->hold->description->[0], 'notforloan', 'Description: notforloan'); |
| 182 |
t::lib::Mocks::mock_preference('item-level_itypes', 0); |
| 183 |
$item->notforloan(0)->store; |
| 184 |
$itemtype->notforloan(1)->store; |
| 185 |
ok(!$availabilities->hold->available, "Itemtype notforloan => not available"); |
| 186 |
is($availabilities->hold->description->[0], 'notforloan', 'Description: notforloan'); |
| 187 |
$itemtype->notforloan(0)->store; |
| 188 |
ok($availabilities->hold->available, "Available"); |
| 189 |
$item->notforloan(-1)->store; |
| 190 |
ok(!$availabilities->hold->available, "Itemtype notforloan -1 => not available"); |
| 191 |
is($availabilities->hold->description->[0], 'ordered', 'Description: ordered'); |
| 192 |
$item->notforloan(0)->store; |
| 193 |
}; |
| 194 |
|
| 195 |
subtest 'Availability: Checkout' => sub { |
| 196 |
plan tests => 7; |
| 197 |
|
| 198 |
my $patron = $builder->build({ source => 'Borrower' }); |
| 199 |
my $biblio = C4::Biblio::GetBiblio($item->biblionumber); |
| 200 |
my $priority= C4::Reserves::CalculatePriority( $item->biblionumber ); |
| 201 |
my $reserve_id = C4::Reserves::AddReserve( |
| 202 |
$item->holdingbranch, |
| 203 |
$patron->{borrowernumber}, |
| 204 |
$item->biblionumber, |
| 205 |
undef, |
| 206 |
$priority, |
| 207 |
undef, undef, undef, |
| 208 |
$$biblio{title}, |
| 209 |
$item->itemnumber, |
| 210 |
undef |
| 211 |
); |
| 212 |
|
| 213 |
ok(!$availabilities->issue->available, "Item reserved => not available"); |
| 214 |
is($availabilities->issue->description->[0], 'reserved', 'Description: reserved'); |
| 215 |
C4::Reserves::CancelReserve({ reserve_id => $reserve_id }); |
| 216 |
ok($availabilities->issue->available, "Reserve cancelled => available"); |
| 217 |
|
| 218 |
my $module = new Test::MockModule('C4::Context'); |
| 219 |
$module->mock( 'userenv', sub { { branch => $patron->{branchcode} } } ); |
| 220 |
my $issue = C4::Circulation::AddIssue($patron, $item->barcode, undef, 1); |
| 221 |
ok(!$availabilities->issue->available, "Item issued => not available"); |
| 222 |
is($availabilities->issue->description->[0], 'onloan', 'Description: onloan'); |
| 223 |
is($availabilities->issue->expected_available, |
| 224 |
$issue->date_due, "Expected to be available '".$issue->date_due."'"); |
| 225 |
C4::Circulation::AddReturn($item->barcode, $item->homebranch); |
| 226 |
ok($availabilities->issue->available, "Checkin => available"); |
| 227 |
}; |
| 228 |
|
| 229 |
subtest 'Availability: Local use' => sub { |
| 230 |
plan tests => 1; |
| 231 |
|
| 232 |
$item->notforloan(1)->store; |
| 233 |
ok($availabilities->local_use->available, "Item notforloan => available"); |
| 234 |
}; |
| 235 |
|
| 236 |
subtest 'Availability: On-site checkout' => sub { |
| 237 |
plan tests => 2; |
| 238 |
|
| 239 |
t::lib::Mocks::mock_preference('OnSiteCheckouts', 0); |
| 240 |
ok(!$availabilities->onsite_checkout->available, 'Not available for onsite checkout ' |
| 241 |
.'(OnSiteCheckouts => 0)'); |
| 242 |
t::lib::Mocks::mock_preference('OnSiteCheckouts', 1); |
| 243 |
ok($availabilities->onsite_checkout->available, 'Available for onsite checkout ' |
| 244 |
.'(OnSiteCheckouts => 1)'); |
| 245 |
}; |
| 246 |
}; |
| 247 |
|
| 248 |
$schema->storage->txn_rollback; |
| 249 |
|
| 250 |
1; |