Lines 20-26
Link Here
|
20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
21 |
use utf8; |
21 |
use utf8; |
22 |
|
22 |
|
23 |
use Test::More tests => 35; |
23 |
use Test::More tests => 36; |
24 |
use Test::Exception; |
24 |
use Test::Exception; |
25 |
use Test::MockModule; |
25 |
use Test::MockModule; |
26 |
|
26 |
|
Lines 1225-1232
subtest 'Tests for itemtype|item_type' => sub {
Link Here
|
1225 |
$schema->storage->txn_rollback; |
1225 |
$schema->storage->txn_rollback; |
1226 |
}; |
1226 |
}; |
1227 |
|
1227 |
|
|
|
1228 |
subtest 'get_transfer' => sub { |
1229 |
plan tests => 8; |
1230 |
$schema->storage->txn_begin; |
1231 |
|
1232 |
my $item = $builder->build_sample_item(); |
1233 |
|
1234 |
my $transfer = $item->get_transfer(); |
1235 |
is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item has no queued transfers' ); |
1236 |
|
1237 |
my $library_to = $builder->build( { source => 'Branch' } ); |
1238 |
|
1239 |
my $transfer_1 = $builder->build_object( |
1240 |
{ |
1241 |
class => 'Koha::Item::Transfers', |
1242 |
value => { |
1243 |
itemnumber => $item->itemnumber, |
1244 |
frombranch => $item->holdingbranch, |
1245 |
tobranch => $library_to->{branchcode}, |
1246 |
reason => 'Manual', |
1247 |
datesent => undef, |
1248 |
datearrived => undef, |
1249 |
datecancelled => undef, |
1250 |
daterequested => \'NOW()' |
1251 |
} |
1252 |
} |
1253 |
); |
1254 |
|
1255 |
$transfer = $item->get_transfer(); |
1256 |
is( |
1257 |
ref($transfer), 'Koha::Item::Transfer', |
1258 |
'Koha::Item->get_transfer should return a Koha::Item::Transfer object' |
1259 |
); |
1260 |
|
1261 |
my $transfer_2 = $builder->build_object( |
1262 |
{ |
1263 |
class => 'Koha::Item::Transfers', |
1264 |
value => { |
1265 |
itemnumber => $item->itemnumber, |
1266 |
frombranch => $item->holdingbranch, |
1267 |
tobranch => $library_to->{branchcode}, |
1268 |
reason => 'Manual', |
1269 |
datesent => undef, |
1270 |
datearrived => undef, |
1271 |
datecancelled => undef, |
1272 |
daterequested => \'NOW()' |
1273 |
} |
1274 |
} |
1275 |
); |
1276 |
|
1277 |
$transfer = $item->get_transfer(); |
1278 |
is( |
1279 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1280 |
'Koha::Item->get_transfer returns the oldest transfer request' |
1281 |
); |
1282 |
|
1283 |
$transfer_2->datesent( \'NOW()' )->store; |
1284 |
$transfer = $item->get_transfer(); |
1285 |
is( |
1286 |
$transfer->branchtransfer_id, $transfer_2->branchtransfer_id, |
1287 |
'Koha::Item->get_transfer returns the in_transit transfer' |
1288 |
); |
1289 |
|
1290 |
my $transfer_3 = $builder->build_object( |
1291 |
{ |
1292 |
class => 'Koha::Item::Transfers', |
1293 |
value => { |
1294 |
itemnumber => $item->itemnumber, |
1295 |
frombranch => $item->holdingbranch, |
1296 |
tobranch => $library_to->{branchcode}, |
1297 |
reason => 'Manual', |
1298 |
datesent => undef, |
1299 |
datearrived => undef, |
1300 |
datecancelled => undef, |
1301 |
daterequested => \'NOW()' |
1302 |
} |
1303 |
} |
1304 |
); |
1305 |
|
1306 |
$transfer_2->datearrived( \'NOW()' )->store; |
1307 |
$transfer = $item->get_transfer(); |
1308 |
is( |
1309 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1310 |
'Koha::Item->get_transfer returns the next queued transfer' |
1311 |
); |
1312 |
is( $transfer->itemnumber, $item->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' ); |
1313 |
|
1314 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1315 |
$transfer = $item->get_transfer(); |
1316 |
is( |
1317 |
$transfer->branchtransfer_id, $transfer_3->branchtransfer_id, |
1318 |
'Koha::Item->get_transfer ignores cancelled transfers' |
1319 |
); |
1320 |
|
1321 |
subtest "Ensure prefetches don't affect which transfer if returned" => sub { |
1322 |
plan tests => 5; |
1323 |
|
1324 |
# Reset test data |
1325 |
$transfer_1->datecancelled(undef)->store; |
1326 |
$transfer_2->set( { datesent => undef, datearrived => undef } )->store; |
1327 |
|
1328 |
my $prefetched_item = Koha::Items->search( |
1329 |
{ 'me.itemnumber' => $item->itemnumber }, |
1330 |
{ prefetch => ['current_branchtransfers'] } |
1331 |
)->next; |
1332 |
|
1333 |
$transfer = $prefetched_item->get_transfer(); |
1334 |
is( |
1335 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1336 |
'Koha::Item->get_transfer returns the oldest transfer request' |
1337 |
); |
1338 |
|
1339 |
$transfer_2->datesent( \'NOW()' )->store; |
1340 |
$prefetched_item = Koha::Items->search( |
1341 |
{ 'me.itemnumber' => $item->itemnumber }, |
1342 |
{ prefetch => ['current_branchtransfers'] } |
1343 |
)->next; |
1344 |
|
1345 |
$transfer = $prefetched_item->get_transfer(); |
1346 |
is( |
1347 |
$transfer->branchtransfer_id, $transfer_2->branchtransfer_id, |
1348 |
'Koha::Item->get_transfer returns the in_transit transfer' |
1349 |
); |
1350 |
|
1351 |
$transfer_2->datearrived( \'NOW()' )->store; |
1352 |
$prefetched_item = Koha::Items->search( |
1353 |
{ 'me.itemnumber' => $item->itemnumber }, |
1354 |
{ prefetch => ['current_branchtransfers'] } |
1355 |
)->next; |
1356 |
$transfer = $prefetched_item->get_transfer(); |
1357 |
is( |
1358 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1359 |
'Koha::Item->get_transfer returns the next queued transfer' |
1360 |
); |
1361 |
is( $transfer->itemnumber, $item->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' ); |
1362 |
|
1363 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1364 |
$prefetched_item = Koha::Items->search( |
1365 |
{ 'me.itemnumber' => $item->itemnumber }, |
1366 |
{ prefetch => ['current_branchtransfers'] } |
1367 |
)->next; |
1368 |
$transfer = $prefetched_item->get_transfer(); |
1369 |
is( |
1370 |
$transfer->branchtransfer_id, $transfer_3->branchtransfer_id, |
1371 |
'Koha::Item->get_transfer ignores cancelled transfers' |
1372 |
); |
1373 |
}; |
1374 |
$schema->storage->txn_rollback; |
1375 |
}; |
1376 |
|
1228 |
subtest 'get_transfers' => sub { |
1377 |
subtest 'get_transfers' => sub { |
1229 |
plan tests => 16; |
1378 |
plan tests => 17; |
1230 |
$schema->storage->txn_begin; |
1379 |
$schema->storage->txn_begin; |
1231 |
|
1380 |
|
1232 |
my $item = $builder->build_sample_item(); |
1381 |
my $item = $builder->build_sample_item(); |
Lines 1321-1326
subtest 'get_transfers' => sub {
Link Here
|
1321 |
$result_1 = $transfers->next; |
1470 |
$result_1 = $transfers->next; |
1322 |
is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains'); |
1471 |
is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains'); |
1323 |
|
1472 |
|
|
|
1473 |
subtest "Ensure prefetches don't affect the return order" => sub { |
1474 |
plan tests => 13; |
1475 |
|
1476 |
# Reset test data |
1477 |
$transfer_2->datesent(undef)->store; |
1478 |
$transfer_2->datearrived(undef)->store; |
1479 |
$transfer_1->datecancelled(undef)->store; |
1480 |
|
1481 |
my $prefetched_item = Koha::Items->search( |
1482 |
{ 'me.itemnumber' => $item->itemnumber }, |
1483 |
{ prefetch => ['current_branchtransfers'] } |
1484 |
)->next; |
1485 |
|
1486 |
$transfers = $prefetched_item->get_transfers(); |
1487 |
is( |
1488 |
$transfers->count, 3, |
1489 |
'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all' |
1490 |
); |
1491 |
$result_1 = $transfers->next; |
1492 |
$result_2 = $transfers->next; |
1493 |
$result_3 = $transfers->next; |
1494 |
is( |
1495 |
$result_1->branchtransfer_id, $transfer_1->branchtransfer_id, |
1496 |
'Koha::Item->get_transfers returns the oldest transfer request first' |
1497 |
); |
1498 |
is( |
1499 |
$result_2->branchtransfer_id, $transfer_2->branchtransfer_id, |
1500 |
'Koha::Item->get_transfers returns the newer transfer request second' |
1501 |
); |
1502 |
is( |
1503 |
$result_3->branchtransfer_id, $transfer_3->branchtransfer_id, |
1504 |
'Koha::Item->get_transfers returns the newest transfer request last' |
1505 |
); |
1506 |
|
1507 |
$transfer_2->datesent( \'NOW()' )->store; |
1508 |
$prefetched_item = Koha::Items->search( |
1509 |
{ 'me.itemnumber' => $item->itemnumber }, |
1510 |
{ prefetch => ['current_branchtransfers'] } |
1511 |
)->next; |
1512 |
$transfers = $prefetched_item->get_transfers(); |
1513 |
is( |
1514 |
$transfers->count, 3, |
1515 |
'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all' |
1516 |
); |
1517 |
$result_1 = $transfers->next; |
1518 |
$result_2 = $transfers->next; |
1519 |
$result_3 = $transfers->next; |
1520 |
is( |
1521 |
$result_1->branchtransfer_id, $transfer_2->branchtransfer_id, |
1522 |
'Koha::Item->get_transfers returns the active transfer request first' |
1523 |
); |
1524 |
is( |
1525 |
$result_2->branchtransfer_id, $transfer_1->branchtransfer_id, |
1526 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1527 |
); |
1528 |
is( |
1529 |
$result_3->branchtransfer_id, $transfer_3->branchtransfer_id, |
1530 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1531 |
); |
1532 |
|
1533 |
$transfer_2->datearrived( \'NOW()' )->store; |
1534 |
$prefetched_item = Koha::Items->search( |
1535 |
{ 'me.itemnumber' => $item->itemnumber }, |
1536 |
{ prefetch => ['current_branchtransfers'] } |
1537 |
)->next; |
1538 |
$transfers = $prefetched_item->get_transfers(); |
1539 |
is( |
1540 |
$transfers->count, 2, |
1541 |
'Once a transfer is received, it no longer appears in the list from ->get_transfers()' |
1542 |
); |
1543 |
$result_1 = $transfers->next; |
1544 |
$result_2 = $transfers->next; |
1545 |
is( |
1546 |
$result_1->branchtransfer_id, $transfer_1->branchtransfer_id, |
1547 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1548 |
); |
1549 |
is( |
1550 |
$result_2->branchtransfer_id, $transfer_3->branchtransfer_id, |
1551 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1552 |
); |
1553 |
|
1554 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1555 |
$prefetched_item = Koha::Items->search( |
1556 |
{ 'me.itemnumber' => $item->itemnumber }, |
1557 |
{ prefetch => ['current_branchtransfers'] } |
1558 |
)->next; |
1559 |
$transfers = $prefetched_item->get_transfers(); |
1560 |
is( |
1561 |
$transfers->count, 1, |
1562 |
'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()' |
1563 |
); |
1564 |
$result_1 = $transfers->next; |
1565 |
is( |
1566 |
$result_1->branchtransfer_id, $transfer_3->branchtransfer_id, |
1567 |
'Koha::Item->get_transfers returns the only transfer that remains' |
1568 |
); |
1569 |
}; |
1570 |
|
1324 |
$schema->storage->txn_rollback; |
1571 |
$schema->storage->txn_rollback; |
1325 |
}; |
1572 |
}; |
1326 |
|
1573 |
|
1327 |
- |
|
|