From 749fe0285402ae2b2d94a2a8ac05367fec2b5d9e Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Fri, 11 Dec 2020 16:51:34 -0300 Subject: [PATCH] Bug 27209: Unit tests Signed-off-by: Tomas Cohen Arazi Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Josef Moravec --- t/db_dependent/Koha/Hold.t | 94 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Hold.t b/t/db_dependent/Koha/Hold.t index bdf4f499d2..dfe0c379c5 100755 --- a/t/db_dependent/Koha/Hold.t +++ b/t/db_dependent/Koha/Hold.t @@ -19,10 +19,15 @@ use Modern::Perl; -use Test::More tests => 1; +use Test::More tests => 2; + +use Test::Exception; +use Test::MockModule; use t::lib::TestBuilder; +use Koha::Libraries; + my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; @@ -48,3 +53,90 @@ subtest 'patron() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'set_pickup_location() tests' => sub { + + plan tests => 10; + + $schema->storage->txn_begin; + + my $mock_biblio = Test::MockModule->new('Koha::Biblio'); + my $mock_item = Test::MockModule->new('Koha::Item'); + + my $library_1 = $builder->build_object({ class => 'Koha::Libraries' }); + my $library_2 = $builder->build_object({ class => 'Koha::Libraries' }); + my $library_3 = $builder->build_object({ class => 'Koha::Libraries' }); + + # let's control what Koha::Biblio->pickup_locations returns, for testing + $mock_biblio->mock( 'pickup_locations', sub { + return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } ); + }); + # let's mock what Koha::Item->pickup_locations returns, for testing + $mock_item->mock( 'pickup_locations', sub { + return Koha::Libraries->search( { branchcode => [ $library_2->branchcode, $library_3->branchcode ] } ); + }); + + my $biblio = $builder->build_sample_biblio; + my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + + # Test biblio-level holds + my $biblio_hold = $builder->build_object( + { + class => "Koha::Holds", + value => { + biblionumber => $biblio->biblionumber, + branchcode => $library_3->branchcode, + itemnumber => undef, + } + } + ); + + throws_ok + { $biblio_hold->set_pickup_location({ library_id => $library_1->branchcode }); } + 'Koha::Exceptions::Hold::InvalidPickupLocation', + 'Exception thrown on invalid pickup location'; + + $biblio_hold->discard_changes; + is( $biblio_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' ); + + my $ret = $biblio_hold->set_pickup_location({ library_id => $library_2->id }); + is( ref($ret), 'Koha::Hold', 'self is returned' ); + + $biblio_hold->discard_changes; + is( $biblio_hold->branchcode, $library_2->id, 'Pickup location changed correctly' ); + + # Test item-level holds + my $item_hold = $builder->build_object( + { + class => "Koha::Holds", + value => { + biblionumber => $biblio->biblionumber, + branchcode => $library_3->branchcode, + itemnumber => $item->itemnumber, + } + } + ); + + throws_ok + { $item_hold->set_pickup_location({ library_id => $library_1->branchcode }); } + 'Koha::Exceptions::Hold::InvalidPickupLocation', + 'Exception thrown on invalid pickup location'; + + $item_hold->discard_changes; + is( $item_hold->branchcode, $library_3->branchcode, 'branchcode remains untouched' ); + + $ret = $item_hold->set_pickup_location({ library_id => $library_2->id }); + is( ref($ret), 'Koha::Hold', 'self is returned' ); + + $item_hold->discard_changes; + is( $item_hold->branchcode, $library_2->id, 'Pickup location changed correctly' ); + + throws_ok + { $item_hold->set_pickup_location({ library_id => undef }); } + 'Koha::Exceptions::MissingParameter', + 'Exception thrown if missing parameter'; + + is( "$@", 'The library_id parameter is mandatory', 'Exception message is clear' ); + + $schema->storage->txn_rollback; +}; -- 2.11.0