Line 0
Link Here
|
0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 3; |
21 |
use Test::Exception; |
22 |
|
23 |
use Koha::Database; |
24 |
|
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
my $schema = Koha::Database->new->schema; |
28 |
my $builder = t::lib::TestBuilder->new; |
29 |
|
30 |
subtest 'filter_by_current() tests' => sub { |
31 |
|
32 |
plan tests => 3; |
33 |
|
34 |
$schema->storage->txn_begin; |
35 |
|
36 |
my $requested = $builder->build_object( |
37 |
{ |
38 |
class => 'Koha::Hold::CancellationRequests', |
39 |
value => { status => 'requested' } |
40 |
} |
41 |
); |
42 |
my $accepted = $builder->build_object( |
43 |
{ |
44 |
class => 'Koha::Hold::CancellationRequests', |
45 |
value => { status => 'accepted' } |
46 |
} |
47 |
); |
48 |
my $rejected = $builder->build_object( |
49 |
{ |
50 |
class => 'Koha::Hold::CancellationRequests', |
51 |
value => { status => 'rejected' } |
52 |
} |
53 |
); |
54 |
|
55 |
my $rs = Koha::Hold::CancellationRequests->search( |
56 |
{ |
57 |
hold_cancellation_request_id => [ |
58 |
$requested->id, |
59 |
$accepted->id, |
60 |
$rejected->id, |
61 |
] |
62 |
} |
63 |
); |
64 |
|
65 |
is( $rs->count, 3 ); |
66 |
|
67 |
my $rs_current = $rs->filter_by_current; |
68 |
|
69 |
is( $rs_current->count, 1 ); |
70 |
is( $rs_current->next->status, 'requested' ); |
71 |
|
72 |
$schema->storage->txn_rollback; |
73 |
}; |
74 |
|
75 |
subtest 'reject() tests' => sub { |
76 |
|
77 |
plan tests => 12; |
78 |
|
79 |
$schema->storage->txn_begin; |
80 |
|
81 |
my $requested_1 = $builder->build_object( |
82 |
{ |
83 |
class => 'Koha::Hold::CancellationRequests', |
84 |
value => { status => 'requested' } |
85 |
} |
86 |
); |
87 |
my $requested_2 = $builder->build_object( |
88 |
{ |
89 |
class => 'Koha::Hold::CancellationRequests', |
90 |
value => { status => 'requested' } |
91 |
} |
92 |
); |
93 |
my $accepted = $builder->build_object( |
94 |
{ |
95 |
class => 'Koha::Hold::CancellationRequests', |
96 |
value => { status => 'accepted' } |
97 |
} |
98 |
); |
99 |
my $rejected = $builder->build_object( |
100 |
{ |
101 |
class => 'Koha::Hold::CancellationRequests', |
102 |
value => { status => 'rejected' } |
103 |
} |
104 |
); |
105 |
|
106 |
my $requests = [ $requested_1, $requested_2, $accepted ]; |
107 |
|
108 |
my $rs = Koha::Hold::CancellationRequests->search( |
109 |
{ |
110 |
hold_cancellation_request_id => [ map { $_->id } @{ $requests } ] |
111 |
} |
112 |
); |
113 |
|
114 |
throws_ok |
115 |
{ $rs->reject; } |
116 |
'Koha::Exceptions::InvalidStatus', |
117 |
'Exception thrown for accepted request'; |
118 |
|
119 |
is( $@->invalid_status, 'accepted', "'invalid_status' parameter set to the conflicting value" ); |
120 |
|
121 |
# reset iterator |
122 |
$rs->reset; |
123 |
|
124 |
my $i = 0; |
125 |
while ( my $req = $rs->next ) { |
126 |
is( $req->status, $requests->[$i]->status, "After exception, status is kept (" . $req->status . ")" ); |
127 |
$i++; |
128 |
} |
129 |
|
130 |
$requests = [ $requested_1, $requested_2, $rejected ]; |
131 |
|
132 |
$rs = Koha::Hold::CancellationRequests->search( |
133 |
{ |
134 |
hold_cancellation_request_id => [ map { $_->id } @{ $requests } ] |
135 |
} |
136 |
); |
137 |
|
138 |
throws_ok |
139 |
{ $rs->reject; } |
140 |
'Koha::Exceptions::InvalidStatus', |
141 |
'Exception thrown for accepted request'; |
142 |
|
143 |
is( $@->invalid_status, 'rejected', "'invalid_status' parameter set to the conflicting value" ); |
144 |
|
145 |
# reset iterator |
146 |
$rs->reset; |
147 |
|
148 |
$i = 0; |
149 |
while ( my $req = $rs->next ) { |
150 |
is( $req->status, $requests->[$i]->status, "After exception, status is kept (" . $req->status . ")" ); |
151 |
$i++; |
152 |
} |
153 |
|
154 |
$requests = [ $requested_1, $requested_2 ]; |
155 |
|
156 |
$rs = Koha::Hold::CancellationRequests->search( |
157 |
{ |
158 |
hold_cancellation_request_id => [ map { $_->id } @{ $requests } ] |
159 |
} |
160 |
); |
161 |
|
162 |
$rs = $rs->reject; |
163 |
|
164 |
$rs->reset; |
165 |
|
166 |
$i = 0; |
167 |
while ( my $req = $rs->next ) { |
168 |
is( $req->status, 'rejected', "status is 'rejected'" ); |
169 |
$i++; |
170 |
} |
171 |
|
172 |
$schema->storage->txn_rollback; |
173 |
}; |
174 |
|
175 |
subtest 'accept() tests' => sub { |
176 |
|
177 |
plan tests => 12; |
178 |
|
179 |
$schema->storage->txn_begin; |
180 |
|
181 |
my $requested_1 = $builder->build_object( |
182 |
{ |
183 |
class => 'Koha::Hold::CancellationRequests', |
184 |
value => { status => 'requested' } |
185 |
} |
186 |
); |
187 |
my $requested_2 = $builder->build_object( |
188 |
{ |
189 |
class => 'Koha::Hold::CancellationRequests', |
190 |
value => { status => 'requested' } |
191 |
} |
192 |
); |
193 |
my $accepted = $builder->build_object( |
194 |
{ |
195 |
class => 'Koha::Hold::CancellationRequests', |
196 |
value => { status => 'accepted' } |
197 |
} |
198 |
); |
199 |
my $rejected = $builder->build_object( |
200 |
{ |
201 |
class => 'Koha::Hold::CancellationRequests', |
202 |
value => { status => 'rejected' } |
203 |
} |
204 |
); |
205 |
|
206 |
my $requests = [ $requested_1, $requested_2, $accepted ]; |
207 |
|
208 |
my $rs = Koha::Hold::CancellationRequests->search( |
209 |
{ |
210 |
hold_cancellation_request_id => [ map { $_->id } @{ $requests } ] |
211 |
} |
212 |
); |
213 |
|
214 |
throws_ok |
215 |
{ $rs->accept; } |
216 |
'Koha::Exceptions::InvalidStatus', |
217 |
'Exception thrown for accepted request'; |
218 |
|
219 |
is( $@->invalid_status, 'accepted', "'invalid_status' parameter set to the conflicting value" ); |
220 |
|
221 |
# reset iterator |
222 |
$rs->reset; |
223 |
|
224 |
my $i = 0; |
225 |
while ( my $req = $rs->next ) { |
226 |
is( $req->status, $requests->[$i]->status, "After exception, status is kept (" . $req->status . ")" ); |
227 |
$i++; |
228 |
} |
229 |
|
230 |
$requests = [ $requested_1, $requested_2, $rejected ]; |
231 |
|
232 |
$rs = Koha::Hold::CancellationRequests->search( |
233 |
{ |
234 |
hold_cancellation_request_id => [ map { $_->id } @{ $requests } ] |
235 |
} |
236 |
); |
237 |
|
238 |
throws_ok |
239 |
{ $rs->accept; } |
240 |
'Koha::Exceptions::InvalidStatus', |
241 |
'Exception thrown for accepted request'; |
242 |
|
243 |
is( $@->invalid_status, 'rejected', "'invalid_status' parameter set to the conflicting value" ); |
244 |
|
245 |
# reset iterator |
246 |
$rs->reset; |
247 |
|
248 |
$i = 0; |
249 |
while ( my $req = $rs->next ) { |
250 |
is( $req->status, $requests->[$i]->status, "After exception, status is kept (" . $req->status . ")" ); |
251 |
$i++; |
252 |
} |
253 |
|
254 |
$requests = [ $requested_1, $requested_2 ]; |
255 |
|
256 |
$rs = Koha::Hold::CancellationRequests->search( |
257 |
{ |
258 |
hold_cancellation_request_id => [ map { $_->id } @{ $requests } ] |
259 |
} |
260 |
); |
261 |
|
262 |
$rs = $rs->accept; |
263 |
|
264 |
$rs->reset; |
265 |
|
266 |
$i = 0; |
267 |
while ( my $req = $rs->next ) { |
268 |
is( $req->status, 'accepted', "status is 'accepted'" ); |
269 |
$i++; |
270 |
} |
271 |
|
272 |
$schema->storage->txn_rollback; |
273 |
}; |