@@ -, +, @@ --- t/db_dependent/Items.t | 91 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 90 insertions(+), 1 deletion(-) --- a/t/db_dependent/Items.t +++ a/t/db_dependent/Items.t @@ -23,7 +23,10 @@ use C4::Biblio; use C4::Branch; use Koha::Database; -use Test::More tests => 8; +use t::lib::Mocks; + +use Test::More tests => 9; + use Test::Warn; BEGIN { @@ -431,6 +434,92 @@ subtest 'Koha::Item(s) tests' => sub { is( $holdingbranch->branchcode(), $branch2, "Home branch code matches holdingbranch" ); }; +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(); --