From dfae5e00767c991767bd05e5853a808ca7e6a4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joonas=20Kylm=C3=A4l=C3=A4?= Date: Wed, 10 Jun 2020 20:21:46 +0300 Subject: [PATCH] Bug 11175: (follow-up) Add tests Signed-off-by: Nick Clemens Signed-off-by: Pasi Kallinen --- t/db_dependent/Koha/Biblio.t | 119 +++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 55 deletions(-) diff --git a/t/db_dependent/Koha/Biblio.t b/t/db_dependent/Koha/Biblio.t index c396bd8824..55b656eac3 100755 --- a/t/db_dependent/Koha/Biblio.t +++ b/t/db_dependent/Koha/Biblio.t @@ -17,14 +17,18 @@ use Modern::Perl; -use Test::More tests => 14; +use Test::More tests => 15; use C4::Biblio; use Koha::Database; use Koha::Acquisition::Orders; +use MARC::Field; +use MARC::Record; + use t::lib::TestBuilder; use t::lib::Mocks; +use Test::MockModule; BEGIN { use_ok('Koha::Biblio'); @@ -472,6 +476,41 @@ subtest 'suggestions() tests' => sub { $schema->storage->txn_rollback; }; +subtest 'components() tests' => sub { + + plan tests => 3; + + $schema->storage->txn_begin; + + my ($host_bibnum) = C4::Biblio::AddBiblio(host_record(), ''); + my $host_biblio = Koha::Biblios->find($host_bibnum); + t::lib::Mocks::mock_preference( 'SearchEngine', 'Zebra' ); + my $search_mod = Test::MockModule->new( 'Koha::SearchEngine::Zebra::Search' ); + $search_mod->mock( 'simple_search_compat', \&search_component_record2 ); + + my @components = $host_biblio->components; + is( ref(\@components), 'ARRAY', 'Return type is correct' ); + + is_deeply( + [@components], + [()], + '->components returns an empty ARRAY' + ); + + $search_mod->unmock( 'simple_search_compat'); + $search_mod->mock( 'simple_search_compat', \&search_component_record1 ); + my $component_record = component_record1()->as_xml(); + + is_deeply( + $host_biblio->components, + [($component_record)], + '->components returns the related component part record' + ); + $search_mod->unmock( 'simple_search_compat'); + + $schema->storage->txn_rollback; +}; + subtest 'orders() and active_orders() tests' => sub { plan tests => 5; @@ -552,61 +591,31 @@ subtest 'subscriptions() tests' => sub { $schema->storage->txn_rollback; }; -subtest 'get_marc_notes() MARC21 tests' => sub { - plan tests => 11; - - $schema->storage->txn_begin; - - t::lib::Mocks::mock_preference( 'NotesToHide', '520' ); - - my $biblio = $builder->build_sample_biblio; - my $record = $biblio->metadata->record; - $record->append_fields( - MARC::Field->new( '500', '', '', a => 'Note1' ), - MARC::Field->new( '505', '', '', a => 'Note2', u => 'http://someserver.com' ), - MARC::Field->new( '520', '', '', a => 'Note3 skipped' ), - MARC::Field->new( '541', '0', '', a => 'Note4 skipped on opac' ), - MARC::Field->new( '541', '', '', a => 'Note5' ), +sub component_record1 { + my $marc = MARC::Record->new; + $marc->append_fields( + MARC::Field->new( '001', '3456' ), + MARC::Field->new( '245', '', '', a => 'Some title 1' ), + MARC::Field->new( '773', '', '', w => '(FIRST)1234' ), ); - C4::Biblio::ModBiblio( $record, $biblio->biblionumber ); - $biblio = Koha::Biblios->find( $biblio->biblionumber); - my $notes = $biblio->get_marc_notes({ marcflavour => 'MARC21' }); - is( $notes->[0]->{marcnote}, 'Note1', 'First note' ); - is( $notes->[1]->{marcnote}, 'Note2', 'Second note' ); - is( $notes->[2]->{marcnote}, 'http://someserver.com', 'URL separated' ); - is( $notes->[3]->{marcnote}, 'Note4 skipped on opac',"Not shows if not opac" ); - is( $notes->[4]->{marcnote}, 'Note5', 'Fifth note' ); - is( @$notes, 5, 'No more notes' ); - $notes = $biblio->get_marc_notes({ marcflavour => 'MARC21', opac => 1 }); - is( $notes->[0]->{marcnote}, 'Note1', 'First note' ); - is( $notes->[1]->{marcnote}, 'Note2', 'Second note' ); - is( $notes->[2]->{marcnote}, 'http://someserver.com', 'URL separated' ); - is( $notes->[3]->{marcnote}, 'Note5', 'Fifth note shows after fourth skipped' ); - is( @$notes, 4, 'No more notes' ); - - $schema->storage->txn_rollback; -}; - -subtest 'get_marc_notes() UNIMARC tests' => sub { - plan tests => 3; - - $schema->storage->txn_begin; + return $marc; +} +sub search_component_record1 { + my @results = ( component_record1()->as_xml() ); + return ( undef, \@results, 1 ); +} - t::lib::Mocks::mock_preference( 'NotesToHide', '310' ); +sub search_component_record2 { + my @results; + return ( undef, \@results, 0 ); +} - my $biblio = $builder->build_sample_biblio; - my $record = $biblio->metadata->record; - $record->append_fields( - MARC::Field->new( '300', '', '', a => 'Note1' ), - MARC::Field->new( '300', '', '', a => 'Note2' ), - MARC::Field->new( '310', '', '', a => 'Note3 skipped' ), +sub host_record { + my $marc = MARC::Record->new; + $marc->append_fields( + MARC::Field->new( '001', '1234' ), + MARC::Field->new( '003', 'FIRST' ), + MARC::Field->new( '245', '', '', a => 'Some title' ), ); - C4::Biblio::ModBiblio( $record, $biblio->biblionumber ); - $biblio = Koha::Biblios->find( $biblio->biblionumber); - my $notes = $biblio->get_marc_notes({ marcflavour => 'UNIMARC' }); - is( $notes->[0]->{marcnote}, 'Note1', 'First note' ); - is( $notes->[1]->{marcnote}, 'Note2', 'Second note' ); - is( @$notes, 2, 'No more notes' ); - - $schema->storage->txn_rollback; -}; + return $marc; +} -- 2.31.1