Bug 12252: Add tests for EmbedItemsInMarcBiblio
authorJonathan Druart <jonathan.druart@biblibre.com>
Fri, 2 Jan 2015 14:46:03 +0000 (15:46 +0100)
committerTomas Cohen Arazi <tomascohen@unc.edu.ar>
Thu, 17 Sep 2015 14:02:17 +0000 (11:02 -0300)
Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Signed-off-by: Signed-off-by: Gaetan Boisson <gaetan.boisson@biblibre.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Signed-off-by: Tomas Cohen Arazi <tomascohen@unc.edu.ar>
t/db_dependent/Items.t

index d18471d..f96e4d3 100755 (executable)
@@ -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 {
@@ -441,6 +444,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();