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 => 34; |
23 |
use Test::More tests => 35; |
24 |
use Test::Exception; |
24 |
use Test::Exception; |
25 |
use Test::MockModule; |
25 |
use Test::MockModule; |
26 |
|
26 |
|
Lines 1191-1198
subtest 'Tests for itemtype' => sub {
Link Here
|
1191 |
$schema->storage->txn_rollback; |
1191 |
$schema->storage->txn_rollback; |
1192 |
}; |
1192 |
}; |
1193 |
|
1193 |
|
|
|
1194 |
subtest 'get_transfer' => sub { |
1195 |
plan tests => 8; |
1196 |
$schema->storage->txn_begin; |
1197 |
|
1198 |
my $item = $builder->build_sample_item(); |
1199 |
|
1200 |
my $transfer = $item->get_transfer(); |
1201 |
is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item has no queued transfers' ); |
1202 |
|
1203 |
my $library_to = $builder->build( { source => 'Branch' } ); |
1204 |
|
1205 |
my $transfer_1 = $builder->build_object( |
1206 |
{ |
1207 |
class => 'Koha::Item::Transfers', |
1208 |
value => { |
1209 |
itemnumber => $item->itemnumber, |
1210 |
frombranch => $item->holdingbranch, |
1211 |
tobranch => $library_to->{branchcode}, |
1212 |
reason => 'Manual', |
1213 |
datesent => undef, |
1214 |
datearrived => undef, |
1215 |
datecancelled => undef, |
1216 |
daterequested => \'NOW()' |
1217 |
} |
1218 |
} |
1219 |
); |
1220 |
|
1221 |
$transfer = $item->get_transfer(); |
1222 |
is( |
1223 |
ref($transfer), 'Koha::Item::Transfer', |
1224 |
'Koha::Item->get_transfer should return a Koha::Item::Transfer object' |
1225 |
); |
1226 |
|
1227 |
my $transfer_2 = $builder->build_object( |
1228 |
{ |
1229 |
class => 'Koha::Item::Transfers', |
1230 |
value => { |
1231 |
itemnumber => $item->itemnumber, |
1232 |
frombranch => $item->holdingbranch, |
1233 |
tobranch => $library_to->{branchcode}, |
1234 |
reason => 'Manual', |
1235 |
datesent => undef, |
1236 |
datearrived => undef, |
1237 |
datecancelled => undef, |
1238 |
daterequested => \'NOW()' |
1239 |
} |
1240 |
} |
1241 |
); |
1242 |
|
1243 |
$transfer = $item->get_transfer(); |
1244 |
is( |
1245 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1246 |
'Koha::Item->get_transfer returns the oldest transfer request' |
1247 |
); |
1248 |
|
1249 |
$transfer_2->datesent( \'NOW()' )->store; |
1250 |
$transfer = $item->get_transfer(); |
1251 |
is( |
1252 |
$transfer->branchtransfer_id, $transfer_2->branchtransfer_id, |
1253 |
'Koha::Item->get_transfer returns the in_transit transfer' |
1254 |
); |
1255 |
|
1256 |
my $transfer_3 = $builder->build_object( |
1257 |
{ |
1258 |
class => 'Koha::Item::Transfers', |
1259 |
value => { |
1260 |
itemnumber => $item->itemnumber, |
1261 |
frombranch => $item->holdingbranch, |
1262 |
tobranch => $library_to->{branchcode}, |
1263 |
reason => 'Manual', |
1264 |
datesent => undef, |
1265 |
datearrived => undef, |
1266 |
datecancelled => undef, |
1267 |
daterequested => \'NOW()' |
1268 |
} |
1269 |
} |
1270 |
); |
1271 |
|
1272 |
$transfer_2->datearrived( \'NOW()' )->store; |
1273 |
$transfer = $item->get_transfer(); |
1274 |
is( |
1275 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1276 |
'Koha::Item->get_transfer returns the next queued transfer' |
1277 |
); |
1278 |
is( $transfer->itemnumber, $item->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' ); |
1279 |
|
1280 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1281 |
$transfer = $item->get_transfer(); |
1282 |
is( |
1283 |
$transfer->branchtransfer_id, $transfer_3->branchtransfer_id, |
1284 |
'Koha::Item->get_transfer ignores cancelled transfers' |
1285 |
); |
1286 |
|
1287 |
subtest "Ensure prefetches don't affect which transfer if returned" => sub { |
1288 |
plan tests => 5; |
1289 |
|
1290 |
# Reset test data |
1291 |
$transfer_1->datecancelled(undef)->store; |
1292 |
$transfer_2->set( { datesent => undef, datearrived => undef } )->store; |
1293 |
|
1294 |
my $prefetched_item = Koha::Items->search( |
1295 |
{ 'me.itemnumber' => $item->itemnumber }, |
1296 |
{ prefetch => ['current_branchtransfers'] } |
1297 |
)->next; |
1298 |
|
1299 |
$transfer = $prefetched_item->get_transfer(); |
1300 |
is( |
1301 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1302 |
'Koha::Item->get_transfer returns the oldest transfer request' |
1303 |
); |
1304 |
|
1305 |
$transfer_2->datesent( \'NOW()' )->store; |
1306 |
$prefetched_item = Koha::Items->search( |
1307 |
{ 'me.itemnumber' => $item->itemnumber }, |
1308 |
{ prefetch => ['current_branchtransfers'] } |
1309 |
)->next; |
1310 |
|
1311 |
$transfer = $prefetched_item->get_transfer(); |
1312 |
is( |
1313 |
$transfer->branchtransfer_id, $transfer_2->branchtransfer_id, |
1314 |
'Koha::Item->get_transfer returns the in_transit transfer' |
1315 |
); |
1316 |
|
1317 |
$transfer_2->datearrived( \'NOW()' )->store; |
1318 |
$prefetched_item = Koha::Items->search( |
1319 |
{ 'me.itemnumber' => $item->itemnumber }, |
1320 |
{ prefetch => ['current_branchtransfers'] } |
1321 |
)->next; |
1322 |
$transfer = $prefetched_item->get_transfer(); |
1323 |
is( |
1324 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1325 |
'Koha::Item->get_transfer returns the next queued transfer' |
1326 |
); |
1327 |
is( $transfer->itemnumber, $item->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' ); |
1328 |
|
1329 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1330 |
$prefetched_item = Koha::Items->search( |
1331 |
{ 'me.itemnumber' => $item->itemnumber }, |
1332 |
{ prefetch => ['current_branchtransfers'] } |
1333 |
)->next; |
1334 |
$transfer = $prefetched_item->get_transfer(); |
1335 |
is( |
1336 |
$transfer->branchtransfer_id, $transfer_3->branchtransfer_id, |
1337 |
'Koha::Item->get_transfer ignores cancelled transfers' |
1338 |
); |
1339 |
}; |
1340 |
$schema->storage->txn_rollback; |
1341 |
}; |
1342 |
|
1194 |
subtest 'get_transfers' => sub { |
1343 |
subtest 'get_transfers' => sub { |
1195 |
plan tests => 16; |
1344 |
plan tests => 17; |
1196 |
$schema->storage->txn_begin; |
1345 |
$schema->storage->txn_begin; |
1197 |
|
1346 |
|
1198 |
my $item = $builder->build_sample_item(); |
1347 |
my $item = $builder->build_sample_item(); |
Lines 1287-1292
subtest 'get_transfers' => sub {
Link Here
|
1287 |
$result_1 = $transfers->next; |
1436 |
$result_1 = $transfers->next; |
1288 |
is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains'); |
1437 |
is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains'); |
1289 |
|
1438 |
|
|
|
1439 |
subtest "Ensure prefetches don't affect the return order" => sub { |
1440 |
plan tests => 13; |
1441 |
|
1442 |
# Reset test data |
1443 |
$transfer_2->datesent(undef)->store; |
1444 |
$transfer_2->datearrived(undef)->store; |
1445 |
$transfer_1->datecancelled(undef)->store; |
1446 |
|
1447 |
my $prefetched_item = Koha::Items->search( |
1448 |
{ 'me.itemnumber' => $item->itemnumber }, |
1449 |
{ prefetch => ['current_branchtransfers'] } |
1450 |
)->next; |
1451 |
|
1452 |
$transfers = $prefetched_item->get_transfers(); |
1453 |
is( |
1454 |
$transfers->count, 3, |
1455 |
'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all' |
1456 |
); |
1457 |
$result_1 = $transfers->next; |
1458 |
$result_2 = $transfers->next; |
1459 |
$result_3 = $transfers->next; |
1460 |
is( |
1461 |
$result_1->branchtransfer_id, $transfer_1->branchtransfer_id, |
1462 |
'Koha::Item->get_transfers returns the oldest transfer request first' |
1463 |
); |
1464 |
is( |
1465 |
$result_2->branchtransfer_id, $transfer_2->branchtransfer_id, |
1466 |
'Koha::Item->get_transfers returns the newer transfer request second' |
1467 |
); |
1468 |
is( |
1469 |
$result_3->branchtransfer_id, $transfer_3->branchtransfer_id, |
1470 |
'Koha::Item->get_transfers returns the newest transfer request last' |
1471 |
); |
1472 |
|
1473 |
$transfer_2->datesent( \'NOW()' )->store; |
1474 |
$prefetched_item = Koha::Items->search( |
1475 |
{ 'me.itemnumber' => $item->itemnumber }, |
1476 |
{ prefetch => ['current_branchtransfers'] } |
1477 |
)->next; |
1478 |
$transfers = $prefetched_item->get_transfers(); |
1479 |
is( |
1480 |
$transfers->count, 3, |
1481 |
'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all' |
1482 |
); |
1483 |
$result_1 = $transfers->next; |
1484 |
$result_2 = $transfers->next; |
1485 |
$result_3 = $transfers->next; |
1486 |
is( |
1487 |
$result_1->branchtransfer_id, $transfer_2->branchtransfer_id, |
1488 |
'Koha::Item->get_transfers returns the active transfer request first' |
1489 |
); |
1490 |
is( |
1491 |
$result_2->branchtransfer_id, $transfer_1->branchtransfer_id, |
1492 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1493 |
); |
1494 |
is( |
1495 |
$result_3->branchtransfer_id, $transfer_3->branchtransfer_id, |
1496 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1497 |
); |
1498 |
|
1499 |
$transfer_2->datearrived( \'NOW()' )->store; |
1500 |
$prefetched_item = Koha::Items->search( |
1501 |
{ 'me.itemnumber' => $item->itemnumber }, |
1502 |
{ prefetch => ['current_branchtransfers'] } |
1503 |
)->next; |
1504 |
$transfers = $prefetched_item->get_transfers(); |
1505 |
is( |
1506 |
$transfers->count, 2, |
1507 |
'Once a transfer is received, it no longer appears in the list from ->get_transfers()' |
1508 |
); |
1509 |
$result_1 = $transfers->next; |
1510 |
$result_2 = $transfers->next; |
1511 |
is( |
1512 |
$result_1->branchtransfer_id, $transfer_1->branchtransfer_id, |
1513 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1514 |
); |
1515 |
is( |
1516 |
$result_2->branchtransfer_id, $transfer_3->branchtransfer_id, |
1517 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1518 |
); |
1519 |
|
1520 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1521 |
$prefetched_item = Koha::Items->search( |
1522 |
{ 'me.itemnumber' => $item->itemnumber }, |
1523 |
{ prefetch => ['current_branchtransfers'] } |
1524 |
)->next; |
1525 |
$transfers = $prefetched_item->get_transfers(); |
1526 |
is( |
1527 |
$transfers->count, 1, |
1528 |
'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()' |
1529 |
); |
1530 |
$result_1 = $transfers->next; |
1531 |
is( |
1532 |
$result_1->branchtransfer_id, $transfer_3->branchtransfer_id, |
1533 |
'Koha::Item->get_transfers returns the only transfer that remains' |
1534 |
); |
1535 |
}; |
1536 |
|
1290 |
$schema->storage->txn_rollback; |
1537 |
$schema->storage->txn_rollback; |
1291 |
}; |
1538 |
}; |
1292 |
|
1539 |
|