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 => 31; |
23 |
use Test::More tests => 32; |
24 |
use Test::Exception; |
24 |
use Test::Exception; |
25 |
use Test::MockModule; |
25 |
use Test::MockModule; |
26 |
|
26 |
|
Lines 1100-1107
subtest 'Tests for itemtype' => sub {
Link Here
|
1100 |
$schema->storage->txn_rollback; |
1100 |
$schema->storage->txn_rollback; |
1101 |
}; |
1101 |
}; |
1102 |
|
1102 |
|
|
|
1103 |
subtest 'get_transfer' => sub { |
1104 |
plan tests => 8; |
1105 |
$schema->storage->txn_begin; |
1106 |
|
1107 |
my $item = $builder->build_sample_item(); |
1108 |
|
1109 |
my $transfer = $item->get_transfer(); |
1110 |
is( $transfer, undef, 'Koha::Item->get_transfer should return undef if the item has no queued transfers' ); |
1111 |
|
1112 |
my $library_to = $builder->build( { source => 'Branch' } ); |
1113 |
|
1114 |
my $transfer_1 = $builder->build_object( |
1115 |
{ |
1116 |
class => 'Koha::Item::Transfers', |
1117 |
value => { |
1118 |
itemnumber => $item->itemnumber, |
1119 |
frombranch => $item->holdingbranch, |
1120 |
tobranch => $library_to->{branchcode}, |
1121 |
reason => 'Manual', |
1122 |
datesent => undef, |
1123 |
datearrived => undef, |
1124 |
datecancelled => undef, |
1125 |
daterequested => \'NOW()' |
1126 |
} |
1127 |
} |
1128 |
); |
1129 |
|
1130 |
$transfer = $item->get_transfer(); |
1131 |
is( |
1132 |
ref($transfer), 'Koha::Item::Transfer', |
1133 |
'Koha::Item->get_transfer should return a Koha::Item::Transfer object' |
1134 |
); |
1135 |
|
1136 |
my $transfer_2 = $builder->build_object( |
1137 |
{ |
1138 |
class => 'Koha::Item::Transfers', |
1139 |
value => { |
1140 |
itemnumber => $item->itemnumber, |
1141 |
frombranch => $item->holdingbranch, |
1142 |
tobranch => $library_to->{branchcode}, |
1143 |
reason => 'Manual', |
1144 |
datesent => undef, |
1145 |
datearrived => undef, |
1146 |
datecancelled => undef, |
1147 |
daterequested => \'NOW()' |
1148 |
} |
1149 |
} |
1150 |
); |
1151 |
|
1152 |
$transfer = $item->get_transfer(); |
1153 |
is( |
1154 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1155 |
'Koha::Item->get_transfer returns the oldest transfer request' |
1156 |
); |
1157 |
|
1158 |
$transfer_2->datesent( \'NOW()' )->store; |
1159 |
$transfer = $item->get_transfer(); |
1160 |
is( |
1161 |
$transfer->branchtransfer_id, $transfer_2->branchtransfer_id, |
1162 |
'Koha::Item->get_transfer returns the in_transit transfer' |
1163 |
); |
1164 |
|
1165 |
my $transfer_3 = $builder->build_object( |
1166 |
{ |
1167 |
class => 'Koha::Item::Transfers', |
1168 |
value => { |
1169 |
itemnumber => $item->itemnumber, |
1170 |
frombranch => $item->holdingbranch, |
1171 |
tobranch => $library_to->{branchcode}, |
1172 |
reason => 'Manual', |
1173 |
datesent => undef, |
1174 |
datearrived => undef, |
1175 |
datecancelled => undef, |
1176 |
daterequested => \'NOW()' |
1177 |
} |
1178 |
} |
1179 |
); |
1180 |
|
1181 |
$transfer_2->datearrived( \'NOW()' )->store; |
1182 |
$transfer = $item->get_transfer(); |
1183 |
is( |
1184 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1185 |
'Koha::Item->get_transfer returns the next queued transfer' |
1186 |
); |
1187 |
is( $transfer->itemnumber, $item->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' ); |
1188 |
|
1189 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1190 |
$transfer = $item->get_transfer(); |
1191 |
is( |
1192 |
$transfer->branchtransfer_id, $transfer_3->branchtransfer_id, |
1193 |
'Koha::Item->get_transfer ignores cancelled transfers' |
1194 |
); |
1195 |
|
1196 |
subtest "Ensure prefetches don't affect which transfer if returned" => sub { |
1197 |
plan tests => 5; |
1198 |
|
1199 |
# Reset test data |
1200 |
$transfer_1->datecancelled(undef)->store; |
1201 |
$transfer_2->set( { datesent => undef, datearrived => undef } )->store; |
1202 |
|
1203 |
my $prefetched_item = Koha::Items->search( |
1204 |
{ 'me.itemnumber' => $item->itemnumber }, |
1205 |
{ prefetch => ['current_branchtransfers'] } |
1206 |
)->next; |
1207 |
|
1208 |
$transfer = $prefetched_item->get_transfer(); |
1209 |
is( |
1210 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1211 |
'Koha::Item->get_transfer returns the oldest transfer request' |
1212 |
); |
1213 |
|
1214 |
$transfer_2->datesent( \'NOW()' )->store; |
1215 |
$prefetched_item = Koha::Items->search( |
1216 |
{ 'me.itemnumber' => $item->itemnumber }, |
1217 |
{ prefetch => ['current_branchtransfers'] } |
1218 |
)->next; |
1219 |
|
1220 |
$transfer = $prefetched_item->get_transfer(); |
1221 |
is( |
1222 |
$transfer->branchtransfer_id, $transfer_2->branchtransfer_id, |
1223 |
'Koha::Item->get_transfer returns the in_transit transfer' |
1224 |
); |
1225 |
|
1226 |
$transfer_2->datearrived( \'NOW()' )->store; |
1227 |
$prefetched_item = Koha::Items->search( |
1228 |
{ 'me.itemnumber' => $item->itemnumber }, |
1229 |
{ prefetch => ['current_branchtransfers'] } |
1230 |
)->next; |
1231 |
$transfer = $prefetched_item->get_transfer(); |
1232 |
is( |
1233 |
$transfer->branchtransfer_id, $transfer_1->branchtransfer_id, |
1234 |
'Koha::Item->get_transfer returns the next queued transfer' |
1235 |
); |
1236 |
is( $transfer->itemnumber, $item->itemnumber, 'Koha::Item->get_transfer returns the right items transfer' ); |
1237 |
|
1238 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1239 |
$prefetched_item = Koha::Items->search( |
1240 |
{ 'me.itemnumber' => $item->itemnumber }, |
1241 |
{ prefetch => ['current_branchtransfers'] } |
1242 |
)->next; |
1243 |
$transfer = $prefetched_item->get_transfer(); |
1244 |
is( |
1245 |
$transfer->branchtransfer_id, $transfer_3->branchtransfer_id, |
1246 |
'Koha::Item->get_transfer ignores cancelled transfers' |
1247 |
); |
1248 |
}; |
1249 |
$schema->storage->txn_rollback; |
1250 |
}; |
1251 |
|
1103 |
subtest 'get_transfers' => sub { |
1252 |
subtest 'get_transfers' => sub { |
1104 |
plan tests => 16; |
1253 |
plan tests => 17; |
1105 |
$schema->storage->txn_begin; |
1254 |
$schema->storage->txn_begin; |
1106 |
|
1255 |
|
1107 |
my $item = $builder->build_sample_item(); |
1256 |
my $item = $builder->build_sample_item(); |
Lines 1196-1201
subtest 'get_transfers' => sub {
Link Here
|
1196 |
$result_1 = $transfers->next; |
1345 |
$result_1 = $transfers->next; |
1197 |
is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains'); |
1346 |
is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains'); |
1198 |
|
1347 |
|
|
|
1348 |
subtest "Ensure prefetches don't affect the return order" => sub { |
1349 |
plan tests => 13; |
1350 |
|
1351 |
# Reset test data |
1352 |
$transfer_2->datesent(undef)->store; |
1353 |
$transfer_2->datearrived(undef)->store; |
1354 |
$transfer_1->datecancelled(undef)->store; |
1355 |
|
1356 |
my $prefetched_item = Koha::Items->search( |
1357 |
{ 'me.itemnumber' => $item->itemnumber }, |
1358 |
{ prefetch => ['current_branchtransfers'] } |
1359 |
)->next; |
1360 |
|
1361 |
$transfers = $prefetched_item->get_transfers(); |
1362 |
is( |
1363 |
$transfers->count, 3, |
1364 |
'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all' |
1365 |
); |
1366 |
$result_1 = $transfers->next; |
1367 |
$result_2 = $transfers->next; |
1368 |
$result_3 = $transfers->next; |
1369 |
is( |
1370 |
$result_1->branchtransfer_id, $transfer_1->branchtransfer_id, |
1371 |
'Koha::Item->get_transfers returns the oldest transfer request first' |
1372 |
); |
1373 |
is( |
1374 |
$result_2->branchtransfer_id, $transfer_2->branchtransfer_id, |
1375 |
'Koha::Item->get_transfers returns the newer transfer request second' |
1376 |
); |
1377 |
is( |
1378 |
$result_3->branchtransfer_id, $transfer_3->branchtransfer_id, |
1379 |
'Koha::Item->get_transfers returns the newest transfer request last' |
1380 |
); |
1381 |
|
1382 |
$transfer_2->datesent( \'NOW()' )->store; |
1383 |
$prefetched_item = Koha::Items->search( |
1384 |
{ 'me.itemnumber' => $item->itemnumber }, |
1385 |
{ prefetch => ['current_branchtransfers'] } |
1386 |
)->next; |
1387 |
$transfers = $prefetched_item->get_transfers(); |
1388 |
is( |
1389 |
$transfers->count, 3, |
1390 |
'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all' |
1391 |
); |
1392 |
$result_1 = $transfers->next; |
1393 |
$result_2 = $transfers->next; |
1394 |
$result_3 = $transfers->next; |
1395 |
is( |
1396 |
$result_1->branchtransfer_id, $transfer_2->branchtransfer_id, |
1397 |
'Koha::Item->get_transfers returns the active transfer request first' |
1398 |
); |
1399 |
is( |
1400 |
$result_2->branchtransfer_id, $transfer_1->branchtransfer_id, |
1401 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1402 |
); |
1403 |
is( |
1404 |
$result_3->branchtransfer_id, $transfer_3->branchtransfer_id, |
1405 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1406 |
); |
1407 |
|
1408 |
$transfer_2->datearrived( \'NOW()' )->store; |
1409 |
$prefetched_item = Koha::Items->search( |
1410 |
{ 'me.itemnumber' => $item->itemnumber }, |
1411 |
{ prefetch => ['current_branchtransfers'] } |
1412 |
)->next; |
1413 |
$transfers = $prefetched_item->get_transfers(); |
1414 |
is( |
1415 |
$transfers->count, 2, |
1416 |
'Once a transfer is received, it no longer appears in the list from ->get_transfers()' |
1417 |
); |
1418 |
$result_1 = $transfers->next; |
1419 |
$result_2 = $transfers->next; |
1420 |
is( |
1421 |
$result_1->branchtransfer_id, $transfer_1->branchtransfer_id, |
1422 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1423 |
); |
1424 |
is( |
1425 |
$result_2->branchtransfer_id, $transfer_3->branchtransfer_id, |
1426 |
'Koha::Item->get_transfers returns the other transfers oldest to newest' |
1427 |
); |
1428 |
|
1429 |
$transfer_1->datecancelled( \'NOW()' )->store; |
1430 |
$prefetched_item = Koha::Items->search( |
1431 |
{ 'me.itemnumber' => $item->itemnumber }, |
1432 |
{ prefetch => ['current_branchtransfers'] } |
1433 |
)->next; |
1434 |
$transfers = $prefetched_item->get_transfers(); |
1435 |
is( |
1436 |
$transfers->count, 1, |
1437 |
'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()' |
1438 |
); |
1439 |
$result_1 = $transfers->next; |
1440 |
is( |
1441 |
$result_1->branchtransfer_id, $transfer_3->branchtransfer_id, |
1442 |
'Koha::Item->get_transfers returns the only transfer that remains' |
1443 |
); |
1444 |
}; |
1445 |
|
1199 |
$schema->storage->txn_rollback; |
1446 |
$schema->storage->txn_rollback; |
1200 |
}; |
1447 |
}; |
1201 |
|
1448 |
|