From 17c2f4049b4712ab9f79fe6d43bb037522c71f1d Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 8 Mar 2021 14:14:20 +0000 Subject: [PATCH] Bug 27281: Add unit test for Koha::Item->get_transfers In the previous patch we introduced the new get_transfers method to the Koha::Item object. This patch adds proper unit tests for that addition. Test plan 1/ Apply patches and confirm t/db_dependent/Koha/Item.t passes. 2/ Signoff --- t/db_dependent/Koha/Item.t | 101 ++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Item.t b/t/db_dependent/Koha/Item.t index 06a9a6ef52..fcb1891405 100755 --- a/t/db_dependent/Koha/Item.t +++ b/t/db_dependent/Koha/Item.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 8; +use Test::More tests => 9; use Test::Exception; use C4::Biblio; @@ -691,3 +691,102 @@ subtest 'Tests for itemtype' => sub { $schema->storage->txn_rollback; }; + +subtest 'get_transfers' => sub { + plan tests => 16; + $schema->storage->txn_begin; + + my $item = $builder->build_sample_item(); + + my $transfers = $item->get_transfers(); + is(ref($transfers), 'Koha::Item::Transfers', 'Koha::Item->get_transfer should return a Koha::Item::Transfers object' ); + is($transfers->count, 0, 'When no transfers exist, the Koha::Item:Transfers object should be empty'); + + my $library_to = $builder->build_object( { class => 'Koha::Libraries' } ); + + my $transfer_1 = $builder->build_object( + { + class => 'Koha::Item::Transfers', + value => { + itemnumber => $item->itemnumber, + frombranch => $item->holdingbranch, + tobranch => $library_to->branchcode, + reason => 'Manual', + datesent => undef, + datearrived => undef, + datecancelled => undef, + daterequested => \'NOW()' + } + } + ); + + $transfers = $item->get_transfers(); + is($transfers->count, 1, 'When one transfer has been reqeusted, the Koha::Item:Transfers object should contain one result'); + + my $transfer_2 = $builder->build_object( + { + class => 'Koha::Item::Transfers', + value => { + itemnumber => $item->itemnumber, + frombranch => $item->holdingbranch, + tobranch => $library_to->branchcode, + reason => 'Manual', + datesent => undef, + datearrived => undef, + datecancelled => undef, + daterequested => \'NOW()' + } + } + ); + + my $transfer_3 = $builder->build_object( + { + class => 'Koha::Item::Transfers', + value => { + itemnumber => $item->itemnumber, + frombranch => $item->holdingbranch, + tobranch => $library_to->branchcode, + reason => 'Manual', + datesent => undef, + datearrived => undef, + datecancelled => undef, + daterequested => \'NOW()' + } + } + ); + + $transfers = $item->get_transfers(); + is($transfers->count, 3, 'When there are multiple open transfer requests, the Koha::Item::Transfers object contains them all'); + my $result_1 = $transfers->next; + my $result_2 = $transfers->next; + my $result_3 = $transfers->next; + is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the oldest transfer request first'); + is( $result_2->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the newer transfer request second'); + is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the newest transfer request last'); + + $transfer_2->datesent(\'NOW()')->store; + $transfers = $item->get_transfers(); + is($transfers->count, 3, 'When one transfer is set to in_transit, the Koha::Item::Transfers object still contains them all'); + $result_1 = $transfers->next; + $result_2 = $transfers->next; + $result_3 = $transfers->next; + is( $result_1->branchtransfer_id, $transfer_2->branchtransfer_id, 'Koha::Item->get_transfers returns the active transfer request first'); + is( $result_2->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest'); + is( $result_3->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest'); + + $transfer_2->datearrived(\'NOW()')->store; + $transfers = $item->get_transfers(); + is($transfers->count, 2, 'Once a transfer is recieved, it no longer appears in the list from ->get_transfers()'); + $result_1 = $transfers->next; + $result_2 = $transfers->next; + is( $result_1->branchtransfer_id, $transfer_1->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest'); + is( $result_2->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the other transfers oldest to newest'); + + $transfer_1->datecancelled(\'NOW()')->store; + $transfers = $item->get_transfers(); + is($transfers->count, 1, 'Once a transfer is cancelled, it no longer appears in the list from ->get_transfers()'); + $result_1 = $transfers->next; + is( $result_1->branchtransfer_id, $transfer_3->branchtransfer_id, 'Koha::Item->get_transfers returns the only transfer that remains'); + + $schema->storage->txn_rollback; +}; -- 2.20.1