From e1bfa7e21d1e5dd136206cb1918aa4005469cd2c Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 2 Jan 2015 15:46:03 +0100 Subject: [PATCH] Bug 12252: Add tests for EmbedItemsInMarcBiblio Signed-off-by: Jonathan Druart --- t/db_dependent/Items.t | 91 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Items.t b/t/db_dependent/Items.t index b41b483..dfd04ec 100755 --- a/t/db_dependent/Items.t +++ b/t/db_dependent/Items.t @@ -23,7 +23,10 @@ use C4::Biblio; use C4::Branch; use Koha::Database; -use Test::More tests => 6; +use t::lib::Mocks; + +use Test::More tests => 7; +use Test::Warn; BEGIN { use_ok('C4::Items'); @@ -386,6 +389,92 @@ subtest 'SearchItems test' => sub { $dbh->rollback; }; +subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub { + plan tests => 7; + + $dbh->{AutoCommit} = 0; + $dbh->{RaiseError} = 1; + + my ( $biblionumber, $biblioitemnumber ) = get_biblio(); + my $item_infos = [ + { homebranch => 'CPL', holdingbranch => 'CPL' }, + { homebranch => 'CPL', holdingbranch => 'CPL' }, + { homebranch => 'CPL', holdingbranch => 'CPL' }, + { homebranch => 'MPL', holdingbranch => 'MPL' }, + { homebranch => 'MPL', holdingbranch => 'MPL' }, + { homebranch => 'CPL', holdingbranch => 'MPL' }, + { homebranch => 'CPL', holdingbranch => 'MPL' }, + { homebranch => 'CPL', holdingbranch => 'MPL' }, + ]; + my $number_of_items = scalar @$item_infos; + my $number_of_items_with_homebranch_is_CPL = + grep { $_->{homebranch} eq 'CPL' } @$item_infos; + + my @itemnumbers; + for my $item_info (@$item_infos) { + my ( undef, undef, $itemnumber ) = AddItem( + { + homebranch => $item_info->{homebranch}, + holdingbranch => $item_info->{holdingbanch} + }, + $biblionumber + ); + push @itemnumbers, $itemnumber; + } + + # Emptied the OpacHiddenItems pref + t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' ); + + my ($itemfield) = + C4::Biblio::GetMarcFromKohaField( 'items.itemnumber', '' ); + my $record = C4::Biblio::GetMarcBiblio($biblionumber); + warning_is { C4::Biblio::EmbedItemsInMarcBiblio() } + { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' }, + 'Should crap is no record passed.'; + + C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber ); + my @items = $record->field($itemfield); + is( scalar @items, $number_of_items, 'Should return all items' ); + + C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, + [ $itemnumbers[1], $itemnumbers[3] ] ); + @items = $record->field($itemfield); + is( scalar @items, 2, 'Should return all items present in the list' ); + + C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 ); + @items = $record->field($itemfield); + is( scalar @items, $number_of_items, 'Should return all items for opac' ); + + my $opachiddenitems = " + homebranch: ['CPL']"; + t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems ); + + C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber ); + @items = $record->field($itemfield); + is( scalar @items, + $number_of_items, + 'Even with OpacHiddenItems set, all items should have been embeded' ); + + C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 ); + @items = $record->field($itemfield); + is( + scalar @items, + $number_of_items - $number_of_items_with_homebranch_is_CPL, +'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embeded' + ); + + $opachiddenitems = " + homebranch: ['CPL', 'MPL']"; + t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems ); + C4::Biblio::EmbedItemsInMarcBiblio( $record, $biblionumber, undef, 1 ); + @items = $record->field($itemfield); + is( + scalar @items, + 0, +'For OPAC, If all items are hidden, no item should have been embeded' + ); +}; + # Helper method to set up a Biblio. sub get_biblio { my $bib = MARC::Record->new(); -- 2.4.6