Lines 20-30
Link Here
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
|
21 |
|
22 |
use DateTime; |
22 |
use DateTime; |
|
|
23 |
use DateTime::Duration; |
23 |
use Koha::Database; |
24 |
use Koha::Database; |
24 |
use Koha::Item::Transfer; |
25 |
use Koha::Item::Transfer; |
25 |
use t::lib::TestBuilder; |
26 |
use t::lib::TestBuilder; |
26 |
|
27 |
|
27 |
use Test::More tests => 7; |
28 |
use Test::More tests => 8; |
28 |
|
29 |
|
29 |
my $schema = Koha::Database->new->schema; |
30 |
my $schema = Koha::Database->new->schema; |
30 |
|
31 |
|
Lines 146-157
subtest "Tests for repatriate." => sub {
Link Here
|
146 |
}; |
147 |
}; |
147 |
|
148 |
|
148 |
subtest "Tests for needs_advancing." => sub { |
149 |
subtest "Tests for needs_advancing." => sub { |
149 |
plan tests => 3; |
150 |
plan tests => 6; |
150 |
$schema->storage->txn_begin; |
151 |
$schema->storage->txn_begin; |
151 |
|
152 |
|
152 |
# Setup a pristine stockrotation context. |
153 |
# Test behaviour of item freshly added to rota. |
153 |
my $sritem = $builder->build({ source => 'Stockrotationitem' }); |
154 |
my $sritem = $builder->build({ |
|
|
155 |
source => 'Stockrotationitem', |
156 |
value => { 'fresh' => 1, }, |
157 |
}); |
154 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
158 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
|
|
159 |
is($dbitem->needs_advancing, 1, "An item that is fresh will always need advancing."); |
160 |
|
161 |
# Setup a pristine stockrotation context. |
162 |
$sritem = $builder->build({ |
163 |
source => 'Stockrotationitem', |
164 |
value => { 'fresh' => 0,} |
165 |
}); |
166 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
155 |
$dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id); |
167 |
$dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id); |
156 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
168 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
157 |
$dbitem->stage->position(1); |
169 |
$dbitem->stage->position(1); |
Lines 163-177
subtest "Tests for needs_advancing." => sub {
Link Here
|
163 |
'tobranch' => $dbitem->stage->branchcode_id, |
175 |
'tobranch' => $dbitem->stage->branchcode_id, |
164 |
'datesent' => DateTime->now, |
176 |
'datesent' => DateTime->now, |
165 |
'datearrived' => undef, |
177 |
'datearrived' => undef, |
166 |
'comments' => "Test item", |
178 |
'comments' => "StockrotationAdvance", |
167 |
})->store; |
179 |
})->store; |
168 |
|
180 |
|
169 |
# Test item will not be advanced if in transit. |
181 |
# Test item will not be advanced if in transit. |
170 |
is($dbitem->needs_advancing, 0, "Not ready to advance: in transfer."); |
182 |
is($dbitem->needs_advancing, 0, "Not ready to advance: in transfer."); |
|
|
183 |
# Test item will not be advanced if in transit even if fresh. |
184 |
$dbitem->fresh(1)->store; |
185 |
is($dbitem->needs_advancing, 0, "Not ready to advance: in transfer (fresh)."); |
186 |
$dbitem->fresh(0)->store; |
171 |
|
187 |
|
172 |
# Test item will not be advanced if it has not spent enough time. |
188 |
# Test item will not be advanced if it has not spent enough time. |
173 |
$dbtransfer->datearrived(DateTime->now)->store; |
189 |
$dbtransfer->datearrived(DateTime->now)->store; |
174 |
is($dbitem->needs_advancing, 0, "Not ready to advance: Not spent enough time."); |
190 |
is($dbitem->needs_advancing, 0, "Not ready to advance: Not spent enough time."); |
|
|
191 |
# Test item will be advanced if it has not spent enough time, but is fresh. |
192 |
$dbitem->fresh(1)->store; |
193 |
is($dbitem->needs_advancing, 1, "Advance: Not spent enough time, but fresh."); |
194 |
$dbitem->fresh(0)->store; |
175 |
|
195 |
|
176 |
# Test item will be advanced if it has spent enough time. |
196 |
# Test item will be advanced if it has spent enough time. |
177 |
$dbtransfer->datesent( # Item was sent 100 days ago... |
197 |
$dbtransfer->datesent( # Item was sent 100 days ago... |
Lines 186-195
subtest "Tests for needs_advancing." => sub {
Link Here
|
186 |
}; |
206 |
}; |
187 |
|
207 |
|
188 |
subtest "Tests for advance." => sub { |
208 |
subtest "Tests for advance." => sub { |
189 |
plan tests => 12; |
209 |
plan tests => 15; |
190 |
$schema->storage->txn_begin; |
210 |
$schema->storage->txn_begin; |
191 |
|
211 |
|
192 |
my $sritem = $builder->build({ source => 'Stockrotationitem' }); |
212 |
my $sritem = $builder->build({ |
|
|
213 |
source => 'Stockrotationitem', |
214 |
value => { 'fresh' => 1 } |
215 |
}); |
193 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
216 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
194 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
217 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id); |
195 |
my $dbstage = $dbitem->stage; |
218 |
my $dbstage = $dbitem->stage; |
Lines 200-205
subtest "Tests for advance." => sub {
Link Here
|
200 |
# Sanity check |
223 |
# Sanity check |
201 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage sanity check."); |
224 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage sanity check."); |
202 |
|
225 |
|
|
|
226 |
# Test if an item is fresh, always move to first stage. |
227 |
is($dbitem->fresh, 1, "Fresh is correct."); |
228 |
$dbitem->advance; |
229 |
is($dbitem->stage->stage_id, $dbstage->stage_id, "Stage is first stage after fresh advance."); |
230 |
is($dbitem->fresh, 0, "Fresh reset after advance."); |
231 |
|
203 |
# Test cases of single stage |
232 |
# Test cases of single stage |
204 |
$dbstage->rota->cyclical(1)->store; # Set Rota to cyclical. |
233 |
$dbstage->rota->cyclical(1)->store; # Set Rota to cyclical. |
205 |
ok($dbitem->advance, "Single stage cyclical advance done."); |
234 |
ok($dbitem->advance, "Single stage cyclical advance done."); |
Lines 250-253
subtest "Tests for advance." => sub {
Link Here
|
250 |
$schema->storage->txn_rollback; |
279 |
$schema->storage->txn_rollback; |
251 |
}; |
280 |
}; |
252 |
|
281 |
|
|
|
282 |
subtest "Tests for investigate (singular)." => sub { |
283 |
plan tests => 7; |
284 |
$schema->storage->txn_begin; |
285 |
|
286 |
# Test brand new item's investigation ['initiation'] |
287 |
my $sritem = $builder->build({ source => 'Stockrotationitem', value => { fresh => 1 } }); |
288 |
my $dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
289 |
is($dbitem->investigate->{reason}, 'initiation', "fresh item initiates."); |
290 |
|
291 |
# Test brand new item at stagebranch ['initiation'] |
292 |
$sritem = $builder->build({ source => 'Stockrotationitem', value => { fresh => 1 } }); |
293 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
294 |
$dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store; |
295 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id)->store; |
296 |
is($dbitem->investigate->{reason}, 'initiation', "fresh item at stagebranch initiates."); |
297 |
|
298 |
# Test item not at stagebranch with branchtransfer history ['repatriation'] |
299 |
$sritem = $builder->build({ |
300 |
source => 'Stockrotationitem', |
301 |
value => { 'fresh' => 0,} |
302 |
}); |
303 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
304 |
my $dbtransfer = Koha::Item::Transfer->new({ |
305 |
'itemnumber' => $dbitem->itemnumber_id, |
306 |
'frombranch' => $dbitem->itemnumber->homebranch, |
307 |
'tobranch' => $dbitem->itemnumber->homebranch, |
308 |
'datesent' => DateTime->now, |
309 |
'datearrived' => DateTime->now, |
310 |
'comments' => "StockrotationAdvance", |
311 |
})->store; |
312 |
is($dbitem->investigate->{reason}, 'repatriation', "older item repatriates."); |
313 |
|
314 |
# Test item at stagebranch with branchtransfer history ['not-ready'] |
315 |
$sritem = $builder->build({ |
316 |
source => 'Stockrotationitem', |
317 |
value => { 'fresh' => 0,} |
318 |
}); |
319 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
320 |
$dbtransfer = Koha::Item::Transfer->new({ |
321 |
'itemnumber' => $dbitem->itemnumber_id, |
322 |
'frombranch' => $dbitem->itemnumber->homebranch, |
323 |
'tobranch' => $dbitem->stage->branchcode_id, |
324 |
'datesent' => DateTime->now, |
325 |
'datearrived' => DateTime->now, |
326 |
'comments' => "StockrotationAdvance", |
327 |
})->store; |
328 |
$dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store; |
329 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id)->store; |
330 |
is($dbitem->investigate->{reason}, 'not-ready', "older item at stagebranch not-ready."); |
331 |
|
332 |
# Test item due for advancement ['advancement'] |
333 |
$sritem = $builder->build({ source => 'Stockrotationitem', value => { fresh => 0 } }); |
334 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
335 |
$dbitem->indemand(0)->store; |
336 |
$dbitem->stage->duration(50)->store; |
337 |
my $sent_duration = DateTime::Duration->new( days => 55); |
338 |
my $arrived_duration = DateTime::Duration->new( days => 52); |
339 |
$dbtransfer = Koha::Item::Transfer->new({ |
340 |
'itemnumber' => $dbitem->itemnumber_id, |
341 |
'frombranch' => $dbitem->itemnumber->homebranch, |
342 |
'tobranch' => $dbitem->stage->branchcode_id, |
343 |
'datesent' => DateTime->now - $sent_duration, |
344 |
'datearrived' => DateTime->now - $arrived_duration, |
345 |
'comments' => "StockrotationAdvance", |
346 |
})->store; |
347 |
$dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store; |
348 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id)->store; |
349 |
is($dbitem->investigate->{reason}, 'advancement', |
350 |
"Item ready for advancement."); |
351 |
|
352 |
# Test item due for advancement but in-demand ['in-demand'] |
353 |
$sritem = $builder->build({ source => 'Stockrotationitem', value => { fresh => 0 } }); |
354 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
355 |
$dbitem->indemand(1)->store; |
356 |
$dbitem->stage->duration(50)->store; |
357 |
$sent_duration = DateTime::Duration->new( days => 55); |
358 |
$arrived_duration = DateTime::Duration->new( days => 52); |
359 |
$dbtransfer = Koha::Item::Transfer->new({ |
360 |
'itemnumber' => $dbitem->itemnumber_id, |
361 |
'frombranch' => $dbitem->itemnumber->homebranch, |
362 |
'tobranch' => $dbitem->stage->branchcode_id, |
363 |
'datesent' => DateTime->now - $sent_duration, |
364 |
'datearrived' => DateTime->now - $arrived_duration, |
365 |
'comments' => "StockrotationAdvance", |
366 |
})->store; |
367 |
$dbitem->itemnumber->homebranch($dbitem->stage->branchcode_id)->store; |
368 |
$dbitem->itemnumber->holdingbranch($dbitem->stage->branchcode_id)->store; |
369 |
is($dbitem->investigate->{reason}, 'in-demand', |
370 |
"Item advances, but in-demand."); |
371 |
|
372 |
# Test item ready for advancement, but at wrong library ['repatriation'] |
373 |
$sritem = $builder->build({ source => 'Stockrotationitem', value => { fresh => 0 } }); |
374 |
$dbitem = Koha::Stockrotationitems->find($sritem->{itemnumber_id}); |
375 |
$dbitem->indemand(0)->store; |
376 |
$dbitem->stage->duration(50)->store; |
377 |
$sent_duration = DateTime::Duration->new( days => 55); |
378 |
$arrived_duration = DateTime::Duration->new( days => 52); |
379 |
$dbtransfer = Koha::Item::Transfer->new({ |
380 |
'itemnumber' => $dbitem->itemnumber_id, |
381 |
'frombranch' => $dbitem->itemnumber->homebranch, |
382 |
'tobranch' => $dbitem->stage->branchcode_id, |
383 |
'datesent' => DateTime->now - $sent_duration, |
384 |
'datearrived' => DateTime->now - $arrived_duration, |
385 |
'comments' => "StockrotationAdvance", |
386 |
})->store; |
387 |
is($dbitem->investigate->{reason}, 'repatriation', |
388 |
"Item advances, but not at stage branch."); |
389 |
|
390 |
$schema->storage->txn_rollback; |
391 |
}; |
392 |
|
253 |
1; |
393 |
1; |