Line 0
Link Here
|
|
|
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 => 14; |
22 |
use t::lib::TestBuilder; |
23 |
|
24 |
use Koha::Database; |
25 |
use Koha::Biblios; |
26 |
|
27 |
use Koha::Exceptions; |
28 |
|
29 |
use_ok('Koha::Biblio::Availability'); |
30 |
|
31 |
my $schema = Koha::Database->new->schema; |
32 |
$schema->storage->txn_begin; |
33 |
|
34 |
my $builder = t::lib::TestBuilder->new; |
35 |
my $biblio = Koha::Biblios->find($builder->build({source => 'Biblio'})->{'biblionumber'}); |
36 |
my $patron = Koha::Patrons->find($builder->build({source => 'Borrower'})->{'borrowernumber'}); |
37 |
|
38 |
subtest 'Check Koha::Biblio::Availability -object default values' => \&check_default_values; |
39 |
|
40 |
subtest 'Attempt to instantiate holdability class with valid biblio' => sub { |
41 |
plan tests => 1; |
42 |
|
43 |
my $availability = Koha::Biblio::Availability->new({ biblio => $biblio }); |
44 |
ok($availability->available, 'When I instantiate class with a valid biblio, then it is available.'); |
45 |
}; |
46 |
|
47 |
subtest 'Attempt to instantiate holdability class with valid biblionumber' => sub { |
48 |
plan tests => 1; |
49 |
|
50 |
my $availability = Koha::Biblio::Availability->new({ biblionumber => $biblio->biblionumber }); |
51 |
ok($availability->available, 'When I instantiate class with a valid biblionumber, then it is available.'); |
52 |
}; |
53 |
|
54 |
subtest 'Attempt to instantiate holdability class without specifying an biblio' => sub { |
55 |
plan tests => 2; |
56 |
|
57 |
my $availability = eval{Koha::Biblio::Availability->new}; |
58 |
ok($@, 'When I instantiate class without giving an biblio,' |
59 |
.' an exception is thrown.'); |
60 |
is(ref($@), 'Koha::Exceptions::MissingParameter', |
61 |
'Then I see that Koha::Exceptions::MissingParameter is thrown.'); |
62 |
}; |
63 |
|
64 |
subtest 'Attempt to instantiate holdability class with notfound biblionumber' => sub { |
65 |
plan tests => 2; |
66 |
|
67 |
my $availability = eval{Koha::Biblio::Availability->new({ biblionumber => -9001 })}; |
68 |
ok($@, 'When I instantiate class with valid biblionumber that does not' |
69 |
.' refer to any stored biblio, an exception is thrown.'); |
70 |
is(ref($@), 'Koha::Exceptions::Biblio::NotFound', |
71 |
'Then I see that Koha::Exceptions::Biblio::NotFound is thrown.'); |
72 |
}; |
73 |
|
74 |
subtest 'Attempt to instantiate holdability class with invalid biblionumber' => sub { |
75 |
plan tests => 2; |
76 |
|
77 |
my $availability = eval{Koha::Biblio::Availability->new({ biblionumber => 'lol' })}; |
78 |
ok($@, 'When I instantiate class with invalid biblionumber that is not' |
79 |
.' a number, an exception is thrown.'); |
80 |
is(ref($@), 'Koha::Exceptions::BadParameter', |
81 |
'Then I see that Koha::Exceptions::BadParameter is thrown.'); |
82 |
}; |
83 |
|
84 |
subtest 'Attempt to instantiate holdability class with invalid biblio' => sub { |
85 |
plan tests => 2; |
86 |
|
87 |
my $availability = eval{Koha::Biblio::Availability->new({ biblio => {'nonsense'=>'yeah'} })}; |
88 |
ok($@, 'When I instantiate class with invalid biblio,' |
89 |
.' an exception is thrown.'); |
90 |
is(ref($@), 'Koha::Exceptions::BadParameter', |
91 |
'Then I see that Koha::Exceptions::BadParameter is thrown.'); |
92 |
}; |
93 |
|
94 |
subtest 'Attempt to instantiate holdability class with valid patron' => sub { |
95 |
plan tests => 1; |
96 |
|
97 |
my $availability = Koha::Biblio::Availability->new({ biblio => $biblio, patron => $patron }); |
98 |
ok($availability->available, 'When I instantiate class with a valid patron, then it is available.'); |
99 |
}; |
100 |
|
101 |
subtest 'Attempt to instantiate holdability class with valid borrowernumber' => sub { |
102 |
plan tests => 1; |
103 |
|
104 |
my $availability = Koha::Biblio::Availability->new({ biblio => $biblio, borrowernumber => $patron->borrowernumber }); |
105 |
ok($availability->available, 'When I instantiate class with a valid borrowernumber, then it is available.'); |
106 |
}; |
107 |
|
108 |
subtest 'Attempt to instantiate holdability class without specifying a patron' => sub { |
109 |
plan tests => 2; |
110 |
|
111 |
my $availability = eval{Koha::Biblio::Availability->new({ biblio => $biblio })}; |
112 |
is($availability->available, 1, 'When I request availability without specifying patron,' |
113 |
.' then the biblio is available.'); |
114 |
is(ref($@), '', 'Then holdability can be checked without giving a patron.'); |
115 |
}; |
116 |
|
117 |
subtest 'Attempt to instantiate holdability class with notfound borrowernumber' => sub { |
118 |
plan tests => 2; |
119 |
|
120 |
my $availability = eval{Koha::Biblio::Availability->new({ biblio => $biblio, borrowernumber => -9001 })}; |
121 |
ok($@, 'When I instantiate class with valid borrowernumber that does not' |
122 |
.' refer to any stored biblio, an exception is thrown.'); |
123 |
is(ref($@), 'Koha::Exceptions::Patron::NotFound', |
124 |
'Then I see that Koha::Exceptions::Patron::NotFound is thrown.'); |
125 |
}; |
126 |
|
127 |
subtest 'Attempt to instantiate holdability class with invalid borrowernumber' => sub { |
128 |
plan tests => 2; |
129 |
|
130 |
my $availability = eval{Koha::Biblio::Availability->new({ biblio => $biblio, borrowernumber => 'lol' })}; |
131 |
ok($@, 'When I instantiate class with invalid borrowernumber that is not' |
132 |
.' a number, an exception is thrown.'); |
133 |
is(ref($@), 'Koha::Exceptions::BadParameter', |
134 |
'Then I see that Koha::Exceptions::BadParameter is thrown.'); |
135 |
}; |
136 |
|
137 |
subtest 'Attempt to instantiate holdability class with invalid patron' => sub { |
138 |
plan tests => 1; |
139 |
|
140 |
my $availability = eval{Koha::Biblio::Availability->new({ biblio => $biblio, patron => $biblio })}; |
141 |
is(ref($@), 'Koha::Exceptions::BadParameter', 'Patron not found'); |
142 |
}; |
143 |
|
144 |
$schema->storage->txn_rollback; |
145 |
|
146 |
sub check_default_values { |
147 |
plan tests => 7; |
148 |
|
149 |
my $availability = Koha::Biblio::Availability->new({ biblio => $biblio}); |
150 |
is($availability->available, 1, 'Koha::Biblio::Availability -object is available.'); |
151 |
is(keys %{$availability->unavailabilities}, 0, 'There are no unavailabilities.'); |
152 |
is(keys %{$availability->confirmations}, 0, 'Nothing needs to be confirmed.'); |
153 |
is(keys %{$availability->notes}, 0, 'There are no additional notes.'); |
154 |
is(ref($availability->biblio), 'Koha::Biblio', 'This availability is related to an biblio.'); |
155 |
is($availability->patron, undef, 'This availability is not related to any patron.'); |
156 |
is($availability->expected_available, undef, 'There is no expectation of future availability'); |
157 |
} |
158 |
|
159 |
1; |