From be8e5a683d9dcb92a25935f7472cb2724ebe1eec Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 22 Oct 2024 08:48:08 -0300 Subject: [PATCH] Bug 38224: Unit tests --- t/db_dependent/Koha/Biblio.t | 100 ++++++++++++++++++++++++++++++++++- 1 file changed, 99 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index f768f8a6f71..7f24449c52b 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 39; +use Test::More tests => 41; use Test::Exception; use Test::Warn; @@ -1954,3 +1954,101 @@ subtest 'check_booking tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'get_first_callnumber() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $record = $biblio->record; + + my $cn_1 = q{PA522}; + my $in_1 = q{.M38 1993}; + + my $cn_2 = q{PA523}; + my $in_2 = q{.M38 2024}; + + $record->insert_fields_ordered( MARC::Field->new( '050', ' ', ' ', a => $cn_1, b => $in_1 ) ); + $record->insert_fields_ordered( MARC::Field->new( '051', ' ', ' ', a => $cn_2, b => $in_2 ) ); + + ModBiblio( $record, $biblio->id ); + $biblio = $biblio->get_from_storage; # refresh object to avoid internal cache + + my $expected_callnumber_1 = $cn_1 . $in_1; + my $expected_callnumber_2 = $cn_2 . $in_2; + + t::lib::Mocks::mock_preference( 'itemcallnumber', '' ); + + throws_ok { $biblio->get_first_callnumber(); } + 'Koha::Exceptions::SysPref::NotSet', + 'Exception thrown if method called but missing configuration'; + + t::lib::Mocks::mock_preference( 'itemcallnumber', '050ab,051ab' ); + + is( $biblio->get_first_callnumber(), $expected_callnumber_1 ); + + $record->delete_fields( $record->field('050') ); + ModBiblio( $record, $biblio->id ); + $biblio = $biblio->get_from_storage; # refresh object to avoid internal cache + + is( $biblio->get_first_callnumber(), $expected_callnumber_2 ); + + t::lib::Mocks::mock_preference( 'itemcallnumber', '050ab' ); + throws_ok { $biblio->get_first_callnumber(); } + 'Koha::Exceptions::Biblio::MissingField', + 'Exception thrown if requested field is not present'; + + $schema->storage->txn_rollback; +}; + +subtest 'populate_item_callnumbers() tests' => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + my $biblio = $builder->build_sample_biblio(); + my $record = $biblio->record; + + my $cn = q{PA522}; + my $in = q{.M38 1993}; + + $record->insert_fields_ordered( MARC::Field->new( '050', ' ', ' ', a => $cn, b => $in ) ); + + my $expected_callnumber = $cn . $in; + + t::lib::Mocks::mock_preference( 'itemcallnumber', '' ); + + throws_ok { $biblio->populate_item_callnumbers() } + q{Koha::Exceptions::SysPref::NotSet}; + + t::lib::Mocks::mock_preference( 'itemcallnumber', '050ab' ); + + throws_ok { $biblio->populate_item_callnumbers() } + q{Koha::Exceptions::Biblio::MissingField}; + + ModBiblio( $record, $biblio->id ); + $biblio = $biblio->get_from_storage(); + + $biblio->populate_item_callnumbers(); + + my $message = $biblio->object_messages()->[0]; + is( $message->{payload}->{callnumber}, $expected_callnumber ); + is( $message->{payload}->{updated_items_count}, 0 ); + + my $item_1 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{yes} } ); + my $item_2 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{boo} } ); + my $item_3 = $builder->build_sample_item( { biblionumber => $biblio->id, itemcallnumber => q{yes} } ); + + $biblio = $biblio->get_from_storage(); + $biblio->populate_item_callnumbers( { itemcallnumber => { '!=' => q{boo} } } ); + + $message = $biblio->object_messages()->[0]; + is( $message->{payload}->{callnumber}, $expected_callnumber ); + is( $message->{payload}->{updated_items_count}, 2 ); + is_deeply( $message->{payload}->{modified_item_ids}, [ $item_1->id, $item_3->id ] ); + + $schema->storage->txn_rollback; +}; -- 2.47.0