+ $schema->storage->txn_rollback;
+};
+
+subtest 'SearchItems test' => sub {
+ plan tests => 14;
+
+ $schema->storage->txn_begin;
+ my $dbh = C4::Context->dbh;
+ my $builder = t::lib::TestBuilder->new;
+
+ my $library1 = $builder->build({
+ source => 'Branch',
+ });
+ my $library2 = $builder->build({
+ source => 'Branch',
+ });
+
+ t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
+ my $cpl_items_before = SearchItemsByField( 'homebranch', $library1->{branchcode});
+
+ my ($biblionumber) = get_biblio();
+
+ my (undef, $initial_items_count) = SearchItems(undef, {rows => 1});
+
+ # Add two items
+ my (undef, undef, $item1_itemnumber) = AddItem({
+ homebranch => $library1->{branchcode},
+ holdingbranch => $library1->{branchcode},
+ }, $biblionumber);
+ my (undef, undef, $item2_itemnumber) = AddItem({
+ homebranch => $library2->{branchcode},
+ holdingbranch => $library2->{branchcode},
+ }, $biblionumber);
+
+ my ($items, $total_results);
+
+ ($items, $total_results) = SearchItems();
+ is($total_results, $initial_items_count + 2, "Created 2 new items");
+ is(scalar @$items, $total_results, "SearchItems() returns all items");
+
+ ($items, $total_results) = SearchItems(undef, {rows => 1});
+ is($total_results, $initial_items_count + 2);
+ is(scalar @$items, 1, "SearchItems(undef, {rows => 1}) returns only 1 item");
+
+ # Search all items where homebranch = 'CPL'
+ my $filter = {
+ field => 'homebranch',
+ query => $library1->{branchcode},
+ operator => '=',
+ };
+ ($items, $total_results) = SearchItems($filter);
+ ok($total_results > 0, "There is at least one CPL item");
+ my $all_items_are_CPL = 1;
+ foreach my $item (@$items) {
+ if ($item->{homebranch} ne $library1->{branchcode}) {
+ $all_items_are_CPL = 0;
+ last;
+ }
+ }
+ ok($all_items_are_CPL, "All items returned by SearchItems are from CPL");
+
+ # Search all items where homebranch != 'CPL'
+ $filter = {
+ field => 'homebranch',
+ query => $library1->{branchcode},
+ operator => '!=',
+ };
+ ($items, $total_results) = SearchItems($filter);
+ ok($total_results > 0, "There is at least one non-CPL item");
+ my $all_items_are_not_CPL = 1;
+ foreach my $item (@$items) {
+ if ($item->{homebranch} eq $library1->{branchcode}) {
+ $all_items_are_not_CPL = 0;
+ last;
+ }
+ }
+ ok($all_items_are_not_CPL, "All items returned by SearchItems are not from CPL");
+
+ # Search all items where biblio title (245$a) is like 'Silence in the %'
+ $filter = {
+ field => 'marc:245$a',
+ query => 'Silence in the %',
+ operator => 'like',
+ };
+ ($items, $total_results) = SearchItems($filter);
+ ok($total_results >= 2, "There is at least 2 items with a biblio title like 'Silence in the %'");
+
+ # Search all items where biblio title is 'Silence in the library'
+ # and homebranch is 'CPL'
+ $filter = {
+ conjunction => 'AND',
+ filters => [
+ {
+ field => 'marc:245$a',
+ query => 'Silence in the %',
+ operator => 'like',
+ },
+ {
+ field => 'homebranch',
+ query => $library1->{branchcode},
+ operator => '=',
+ },
+ ],
+ };
+ ($items, $total_results) = SearchItems($filter);
+ my $found = 0;
+ foreach my $item (@$items) {
+ if ($item->{itemnumber} == $item1_itemnumber) {
+ $found = 1;
+ last;
+ }
+ }
+ ok($found, "item1 found");
+
+ my ($itemfield) = GetMarcFromKohaField('items.itemnumber', '');
+
+ # Create item subfield 'z' without link
+ $dbh->do('DELETE FROM marc_subfield_structure WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
+ $dbh->do('INSERT INTO marc_subfield_structure (tagfield, tagsubfield, frameworkcode) VALUES (?, "z", "")', undef, $itemfield);
+
+ # Clear cache
+ $C4::Context::context->{marcfromkohafield} = undef;
+ $C4::Biblio::inverted_field_map = undef;
+
+ my $item3_record = new MARC::Record;
+ $item3_record->append_fields(
+ new MARC::Field($itemfield, '', '', 'z' => 'foobar')
+ );
+ my (undef, undef, $item3_itemnumber) = AddItemFromMarc($item3_record,
+ $biblionumber);
+
+ # Search item where item subfield z is "foobar"
+ $filter = {
+ field => 'marc:' . $itemfield . '$z',
+ query => 'foobar',
+ operator => 'like',
+ };
+ ($items, $total_results) = SearchItems($filter);
+ ok(scalar @$items == 1, 'found 1 item with $z = "foobar"');
+
+ # Link $z to items.itemnotes (and make sure there is no other subfields
+ # linked to it)
+ $dbh->do('DELETE FROM marc_subfield_structure WHERE kohafield="items.itemnotes" AND frameworkcode=""', undef, $itemfield);
+ $dbh->do('UPDATE marc_subfield_structure SET kohafield="items.itemnotes" WHERE tagfield=? AND tagsubfield="z" AND frameworkcode=""', undef, $itemfield);
+
+ # Clear cache
+ $C4::Context::context->{marcfromkohafield} = undef;
+ $C4::Biblio::inverted_field_map = undef;
+
+ ModItemFromMarc($item3_record, $biblionumber, $item3_itemnumber);
+
+ # Make sure the link is used
+ my $item3 = GetItem($item3_itemnumber);
+ ok($item3->{itemnotes} eq 'foobar', 'itemnotes eq "foobar"');
+
+ # Do the same search again.
+ # This time it will search in items.itemnotes
+ ($items, $total_results) = SearchItems($filter);
+ ok(scalar @$items == 1, 'found 1 item with itemnotes = "foobar"');
+
+ my $cpl_items_after = SearchItemsByField( 'homebranch', $library1->{branchcode});
+ is( ( scalar( @$cpl_items_after ) - scalar ( @$cpl_items_before ) ), 1, 'SearchItemsByField should return something' );
+
+ $schema->storage->txn_rollback;
+};
+
+subtest 'Koha::Item(s) tests' => sub {
+
+ plan tests => 5;
+
+ $schema->storage->txn_begin();
+
+ my $builder = t::lib::TestBuilder->new;
+ my $library1 = $builder->build({
+ source => 'Branch',
+ });
+ my $library2 = $builder->build({
+ source => 'Branch',
+ });
+
+ # Create a biblio and item for testing
+ t::lib::Mocks::mock_preference('marcflavour', 'MARC21');
+ my ($bibnum, $bibitemnum) = get_biblio();
+ my ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} } , $bibnum);
+
+ # Get item.
+ my $item = Koha::Items->find( $itemnumber );
+ is( ref($item), 'Koha::Item', "Got Koha::Item" );
+
+ my $homebranch = $item->home_branch();
+ is( ref($homebranch), 'Koha::Library', "Got Koha::Library from home_branch method" );
+ is( $homebranch->branchcode(), $library1->{branchcode}, "Home branch code matches homebranch" );
+
+ my $holdingbranch = $item->holding_branch();
+ is( ref($holdingbranch), 'Koha::Library', "Got Koha::Library from holding_branch method" );
+ is( $holdingbranch->branchcode(), $library2->{branchcode}, "Home branch code matches holdingbranch" );
+
+ $schema->storage->txn_rollback;
+};
+
+subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
+ plan tests => 7;
+
+ $schema->storage->txn_begin();
+
+ my $builder = t::lib::TestBuilder->new;
+ my $library1 = $builder->build({
+ source => 'Branch',
+ });
+ my $library2 = $builder->build({
+ source => 'Branch',
+ });
+
+ my ( $biblionumber, $biblioitemnumber ) = get_biblio();
+ my $item_infos = [
+ { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
+ { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
+ { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
+ { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
+ { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
+ { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
+ { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
+ { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
+ ];
+ my $number_of_items = scalar @$item_infos;
+ my $number_of_items_with_homebranch_is_CPL =
+ grep { $_->{homebranch} eq $library1->{branchcode} } @$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: ['$library1->{branchcode}']";
+ 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 embedded' );
+
+ 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 embedded'
+ );
+
+ $opachiddenitems = "
+ homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
+ 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 embedded'
+ );