|
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 => 5; |
| 22 |
use t::lib::Mocks; |
| 23 |
use t::lib::TestBuilder; |
| 24 |
require t::db_dependent::Koha::Availability::Helpers; |
| 25 |
use Benchmark; |
| 26 |
|
| 27 |
use Koha::Database; |
| 28 |
use Koha::IssuingRules; |
| 29 |
use Koha::Items; |
| 30 |
use Koha::ItemTypes; |
| 31 |
|
| 32 |
use Koha::Biblio::Availability::Hold; |
| 33 |
|
| 34 |
my $schema = Koha::Database->new->schema; |
| 35 |
$schema->storage->txn_begin; |
| 36 |
|
| 37 |
my $builder = t::lib::TestBuilder->new; |
| 38 |
|
| 39 |
set_default_system_preferences(); |
| 40 |
set_default_circulation_rules(); |
| 41 |
|
| 42 |
subtest 'Biblio with zero available items in OPAC' => \&t_no_available_items_opac; |
| 43 |
sub t_no_available_items_opac { |
| 44 |
plan tests => 6; |
| 45 |
|
| 46 |
my $item1 = build_a_test_item(); |
| 47 |
my $biblio = Koha::Biblios->find($item1->biblionumber); |
| 48 |
my $item2 = build_a_test_item(); |
| 49 |
$item2->biblionumber($biblio->biblionumber)->store; |
| 50 |
$item2->biblioitemnumber($item1->biblioitemnumber)->store; |
| 51 |
|
| 52 |
my $patron = build_a_test_patron(); |
| 53 |
Koha::IssuingRules->search->delete; |
| 54 |
my $rule = Koha::IssuingRule->new({ |
| 55 |
branchcode => '*', |
| 56 |
itemtype => $item2->effective_itemtype, |
| 57 |
categorycode => '*', |
| 58 |
holds_per_record => 0, |
| 59 |
reservesallowed => 0, |
| 60 |
opacitemholds => 'Y', |
| 61 |
})->store; |
| 62 |
|
| 63 |
t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1); |
| 64 |
t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype'); |
| 65 |
|
| 66 |
my $branch2 = Koha::Libraries->find($builder->build({ source => 'Branch' })->{branchcode}); |
| 67 |
C4::Circulation::CreateBranchTransferLimit( |
| 68 |
$branch2->branchcode, |
| 69 |
$item1->holdingbranch, |
| 70 |
$item1->effective_itemtype |
| 71 |
); |
| 72 |
|
| 73 |
my $availability = Koha::Biblio::Availability::Hold->new({ |
| 74 |
biblio => $biblio, |
| 75 |
patron => $patron, |
| 76 |
to_branch => $branch2->branchcode |
| 77 |
})->in_opac; |
| 78 |
my $expecting = 'Koha::Exceptions::Biblio::NoAvailableItems'; |
| 79 |
|
| 80 |
ok(!Koha::Item::Availability::Hold->new({ |
| 81 |
item => $item1, patron => $patron, to_branch => $branch2->branchcode, |
| 82 |
})->in_opac->available, |
| 83 |
'When I look at the first item of two in this biblio, it is not available.'); |
| 84 |
ok(!Koha::Item::Availability::Hold->new({ |
| 85 |
item => $item2, patron => $patron |
| 86 |
})->in_opac->available, |
| 87 |
'When I look at the second item of two in this biblio, it is not available.'); |
| 88 |
ok(!$availability->available, 'Then, the biblio is not available.'); |
| 89 |
is($availability->unavailable, 1, 'Then, there is exactly one reason for unavailability.'); |
| 90 |
is(ref($availability->unavailabilities->{$expecting}), $expecting, 'The reason says there are no' |
| 91 |
.' available items in this biblio.'); |
| 92 |
is(@{$availability->item_unavailabilities}, 2, 'There seems to be two items that are unavailable.'); |
| 93 |
}; |
| 94 |
|
| 95 |
subtest 'Biblio with zero available items in intranet' => \&t_no_available_items_intranet; |
| 96 |
sub t_no_available_items_intranet { |
| 97 |
plan tests => 2; |
| 98 |
|
| 99 |
my $item1 = build_a_test_item(); |
| 100 |
my $biblio = Koha::Biblios->find($item1->biblionumber); |
| 101 |
my $item2 = build_a_test_item(); |
| 102 |
$item2->biblionumber($biblio->biblionumber)->store; |
| 103 |
$item2->biblioitemnumber($item1->biblioitemnumber)->store; |
| 104 |
|
| 105 |
my $patron = build_a_test_patron(); |
| 106 |
Koha::IssuingRules->search->delete; |
| 107 |
my $rule = Koha::IssuingRule->new({ |
| 108 |
branchcode => '*', |
| 109 |
itemtype => $item2->effective_itemtype, |
| 110 |
categorycode => '*', |
| 111 |
holds_per_record => 0, |
| 112 |
reservesallowed => 0, |
| 113 |
opacitemholds => 'Y', |
| 114 |
})->store; |
| 115 |
$item1->withdrawn('1')->store; |
| 116 |
|
| 117 |
subtest 'While AllowHoldPolicyOverride is disabled' => sub { |
| 118 |
plan tests => 3; |
| 119 |
|
| 120 |
t::lib::Mocks::mock_preference('AllowHoldPolicyOverride', 0); |
| 121 |
my $availability = Koha::Biblio::Availability::Hold->new({ |
| 122 |
biblio => $biblio, patron => $patron})->in_intranet; |
| 123 |
my $i1_avail = Koha::Item::Availability::Hold->new({ |
| 124 |
item => $item1, patron => $patron })->in_intranet; |
| 125 |
my $i2_avail = Koha::Item::Availability::Hold->new({ |
| 126 |
item => $item2, patron => $patron })->in_intranet; |
| 127 |
ok(!$i1_avail->available, 'When I look at the first item of two in this' |
| 128 |
.' biblio, it is not available.'); |
| 129 |
ok(!$i2_avail->available, 'When I look at the second item of two in this' |
| 130 |
.' biblio, it is not available.'); |
| 131 |
ok(!$availability->available, 'Then, the biblio is not available.'); |
| 132 |
}; |
| 133 |
|
| 134 |
subtest 'Given AllowHoldPolicyOverride is enabled' => sub { |
| 135 |
plan tests => 5; |
| 136 |
|
| 137 |
t::lib::Mocks::mock_preference('AllowHoldPolicyOverride', 1); |
| 138 |
my $availability = Koha::Biblio::Availability::Hold->new({ |
| 139 |
biblio => $biblio, patron => $patron})->in_intranet; |
| 140 |
my $i1_avail = Koha::Item::Availability::Hold->new({ |
| 141 |
item => $item1, patron => $patron })->in_intranet; |
| 142 |
my $i2_avail = Koha::Item::Availability::Hold->new({ |
| 143 |
item => $item2, patron => $patron })->in_intranet; |
| 144 |
ok($i1_avail->available, 'When I look at the first item of two in this' |
| 145 |
.' biblio, it is available.'); |
| 146 |
is($i1_avail->confirm, 1, 'Then the first item availability seems to have' |
| 147 |
.' one reason to ask for confirmation.'); |
| 148 |
ok($i2_avail->available, 'When I look at the second item of two in this' |
| 149 |
.' biblio, it is available.'); |
| 150 |
is($i2_avail->confirm, 1, 'Then the second item availability seems to' |
| 151 |
.' have one reason to ask for confirmation.'); |
| 152 |
ok($availability->available, 'Then, the biblio is available.'); |
| 153 |
}; |
| 154 |
|
| 155 |
}; |
| 156 |
|
| 157 |
subtest 'Biblio with one available items out of two' => \&t_one_out_of_two_items_available; |
| 158 |
sub t_one_out_of_two_items_available { |
| 159 |
plan tests => 5; |
| 160 |
|
| 161 |
my $item1 = build_a_test_item(); |
| 162 |
my $biblio = Koha::Biblios->find($item1->biblionumber); |
| 163 |
my $item2 = build_a_test_item(); |
| 164 |
$item2->biblionumber($biblio->biblionumber)->store; |
| 165 |
$item2->biblioitemnumber($item1->biblioitemnumber)->store; |
| 166 |
|
| 167 |
my $patron = build_a_test_patron(); |
| 168 |
$item1->withdrawn('1')->store; |
| 169 |
|
| 170 |
my $availability = Koha::Biblio::Availability::Hold->new({biblio => $biblio, patron => $patron})->in_opac; |
| 171 |
my $item_availabilities = $availability->item_availabilities; |
| 172 |
ok(!Koha::Item::Availability::Hold->new({ item => $item1, patron => $patron })->in_opac->available, |
| 173 |
'When I look at the first item of two in this biblio, it is not available.'); |
| 174 |
ok(Koha::Item::Availability::Hold->new({ item => $item2, patron => $patron })->in_opac->available, |
| 175 |
'When I look at the second item of two in this biblio, it seems to be available.'); |
| 176 |
ok($availability->available, 'Then, the biblio is available.'); |
| 177 |
is(@{$item_availabilities}, 1, 'There seems to be one available item in this biblio.'); |
| 178 |
is($item_availabilities->[0]->item->itemnumber, $item2->itemnumber, 'Then the only available item' |
| 179 |
.'is the second item of this biblio.'); |
| 180 |
}; |
| 181 |
|
| 182 |
subtest 'Biblio with two items out of two available' => \&t_all_items_available; |
| 183 |
sub t_all_items_available { |
| 184 |
plan tests => 4; |
| 185 |
|
| 186 |
my $item1 = build_a_test_item(); |
| 187 |
my $biblio = Koha::Biblios->find($item1->biblionumber); |
| 188 |
my $item2 = build_a_test_item(); |
| 189 |
$item2->biblionumber($biblio->biblionumber)->store; |
| 190 |
$item2->biblioitemnumber($item1->biblioitemnumber)->store; |
| 191 |
|
| 192 |
my $patron = build_a_test_patron(); |
| 193 |
|
| 194 |
my $availability = Koha::Biblio::Availability::Hold->new({biblio => $biblio, patron => $patron})->in_opac; |
| 195 |
my $item_availabilities = $availability->item_availabilities; |
| 196 |
ok(Koha::Item::Availability::Hold->new({ item => $item1, patron => $patron })->in_opac->available, |
| 197 |
'When I look at the first item of two in this biblio, it seems to be available.'); |
| 198 |
ok(Koha::Item::Availability::Hold->new({ item => $item2, patron => $patron })->in_opac->available, |
| 199 |
'When I look at the second item of two in this biblio, it seems to be available.'); |
| 200 |
ok($availability->available, 'Then, the biblio is available.'); |
| 201 |
is(@{$item_availabilities}, 2, 'There seems to be two available items in this biblio.'); |
| 202 |
}; |
| 203 |
|
| 204 |
subtest 'Performance test' => \&t_performance_test; |
| 205 |
sub t_performance_test { |
| 206 |
plan tests => 2; |
| 207 |
|
| 208 |
set_default_circulation_rules(); |
| 209 |
my $item = build_a_test_item(); |
| 210 |
my $patron = build_a_test_patron(); |
| 211 |
my $biblio = Koha::Biblios->find($item->biblionumber); |
| 212 |
my $biblioitem = Koha::Biblioitems->find($item->biblioitemnumber); |
| 213 |
|
| 214 |
# add some items to biblio |
| 215 |
my $bib_iterations = 10; |
| 216 |
my $count = 10; |
| 217 |
for my $i (1..($count-1)) { # one already built earlier |
| 218 |
my $t_item = build_a_test_item($biblio, $biblioitem); |
| 219 |
$t_item->itype($item->itype)->store; |
| 220 |
$t_item->homebranch($item->homebranch)->store; |
| 221 |
} |
| 222 |
|
| 223 |
my @items = $biblio->items; |
| 224 |
is(@items, $count, "We will get availability for $count items."); |
| 225 |
my $res1 = timethis($bib_iterations, sub { |
| 226 |
Koha::Biblio::Availability::Hold->new({ biblio => $biblio, patron => $patron })->in_opac; |
| 227 |
}); |
| 228 |
ok($res1, "Calculated search availability $bib_iterations times."); |
| 229 |
}; |
| 230 |
|
| 231 |
$schema->storage->txn_rollback; |
| 232 |
|
| 233 |
1; |