View | Details | Raw Unified | Return to bug 27138
Collapse All | Expand All

(-)a/C4/XSLT.pm (-1 / +5 lines)
Lines 326-331 Is only used in this module currently. Link Here
326
sub buildKohaItemsNamespace {
326
sub buildKohaItemsNamespace {
327
    my ($biblionumber, $hidden_items, $items_rs) = @_;
327
    my ($biblionumber, $hidden_items, $items_rs) = @_;
328
328
329
    my @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $biblionumber );
329
    $hidden_items ||= [];
330
    $hidden_items ||= [];
330
331
331
    my $query = {};
332
    my $query = {};
Lines 333-339 sub buildKohaItemsNamespace { Link Here
333
      if $hidden_items;
334
      if $hidden_items;
334
335
335
    unless ( $items_rs && ref($items_rs) eq 'Koha::Items' ) {
336
    unless ( $items_rs && ref($items_rs) eq 'Koha::Items' ) {
336
        $query->{'me.biblionumber'} = $biblionumber;
337
        $query->{ '-or' } = [
338
            'me.biblionumber' => $biblionumber,
339
            'me.itemnumber' => { -in => \@host_itemnumbers }
340
        ];
337
        $items_rs = Koha::Items->new;
341
        $items_rs = Koha::Items->new;
338
    }
342
    }
339
343
(-)a/Koha/Biblio/Metadata.pm (-3 / +8 lines)
Lines 21-27 use MARC::File::XML; Link Here
21
use Scalar::Util qw( blessed );
21
use Scalar::Util qw( blessed );
22
22
23
use C4::Biblio qw( GetMarcFromKohaField );
23
use C4::Biblio qw( GetMarcFromKohaField );
24
use C4::Items qw( GetMarcItem );
24
use C4::Items qw( GetMarcItem get_hostitemnumbers_of );
25
use Koha::Database;
25
use Koha::Database;
26
use Koha::Exceptions::Metadata;
26
use Koha::Exceptions::Metadata;
27
27
Lines 157-165 sub _embed_items { Link Here
157
            $record->delete_field($field);
157
            $record->delete_field($field);
158
        }
158
        }
159
159
160
        my $biblio = Koha::Biblios->find($biblionumber);
160
        push @$itemnumbers, C4::Items::get_hostitemnumbers_of( $biblionumber ) if $params->{embed_host_items};
161
161
162
        my $items = $biblio->items;
162
        my $items = Koha::Items->search([
163
           'me.biblionumber' => $biblionumber,
164
           'me.itemnumber' => {
165
              -in => $itemnumbers
166
              }
167
        ]);
163
        if ( @$itemnumbers ) {
168
        if ( @$itemnumbers ) {
164
            $items = $items->search({ itemnumber => { -in => $itemnumbers } });
169
            $items = $items->search({ itemnumber => { -in => $itemnumbers } });
165
        }
170
        }
(-)a/Koha/SearchEngine/Elasticsearch/Indexer.pm (-1 / +2 lines)
Lines 341-353 sub _get_record { Link Here
341
341
342
    if ( $self->index eq $Koha::SearchEngine::BIBLIOS_INDEX ) {
342
    if ( $self->index eq $Koha::SearchEngine::BIBLIOS_INDEX ) {
343
        my $biblio = Koha::Biblios->find($record_id);
343
        my $biblio = Koha::Biblios->find($record_id);
344
        $record = $biblio->metadata->record( { embed_items => 1 } )
344
        $record = $biblio->metadata->record( { embed_items => 1, embed_host_items => 1 } )
345
          if $biblio;
345
          if $biblio;
346
    } else {
346
    } else {
347
        $record = C4::AuthoritiesMarc::GetAuthority($record_id);
347
        $record = C4::AuthoritiesMarc::GetAuthority($record_id);
348
    }
348
    }
349
349
350
    return $record;
350
    return $record;
351
351
}
352
}
352
353
353
=head2 delete_index($biblionums)
354
=head2 delete_index($biblionums)
(-)a/misc/migration_tools/rebuild_zebra.pl (-1 / +13 lines)
Lines 511-517 sub export_marc_records_from_sth { Link Here
511
            if ($record_type eq 'biblio'){
511
            if ($record_type eq 'biblio'){
512
                my $biblio = Koha::Biblios->find($record_number);
512
                my $biblio = Koha::Biblios->find($record_number);
513
                next unless $biblio;
513
                next unless $biblio;
514
                my $items = $biblio->items;
514
                my @host_itemnumbers = C4::Items::get_hostitemnumbers_of( $record_number );
515
                my $items = Koha::Items->search({
516
                    -or => [
517
                        biblionumber => $record_number,
518
                        itemnumber => {
519
                            -in => \@host_itemnumbers
520
                        }
521
                    ]
522
                });
515
                if ($items->count){
523
                if ($items->count){
516
                    my $record = MARC::Record->new;
524
                    my $record = MARC::Record->new;
517
                    $record->encoding('UTF-8');
525
                    $record->encoding('UTF-8');
Lines 681-690 sub get_raw_marc_record { Link Here
681
689
682
    my $marc;
690
    my $marc;
683
    if ($record_type eq 'biblio') {
691
    if ($record_type eq 'biblio') {
692
<<<<<<< HEAD
684
        eval {
693
        eval {
685
            my $biblio = Koha::Biblios->find($record_number);
694
            my $biblio = Koha::Biblios->find($record_number);
686
            $marc = $biblio->metadata->record({ embed_items => 1 });
695
            $marc = $biblio->metadata->record({ embed_items => 1 });
687
        };
696
        };
697
=======
698
        eval { $marc = C4::Biblio::GetMarcBiblio({ biblionumber => $record_number, embed_items => 1, embed_host_items => 1 }); };
699
>>>>>>> Bug 27138: Index host items in child records when sending to search engine
688
        if ($@ || !$marc) {
700
        if ($@ || !$marc) {
689
            # here we do warn since catching an exception
701
            # here we do warn since catching an exception
690
            # means that the bib was found but failed
702
            # means that the bib was found but failed
(-)a/t/db_dependent/Items.t (-1 / +155 lines)
Lines 665-670 subtest 'Koha::Item(s) tests' => sub { Link Here
665
    $schema->storage->txn_rollback;
665
    $schema->storage->txn_rollback;
666
};
666
};
667
667
668
subtest 'C4::Biblio::EmbedItemsInMarcBiblio' => sub {
669
    plan tests => 11;
670
671
    $schema->storage->txn_begin();
672
673
    my $builder = t::lib::TestBuilder->new;
674
    my $library1 = $builder->build({
675
        source => 'Branch',
676
    });
677
    my $library2 = $builder->build({
678
        source => 'Branch',
679
    });
680
    my $itemtype = $builder->build({
681
        source => 'Itemtype',
682
    });
683
684
    my $biblio = $builder->build_sample_biblio();
685
    my $item_infos = [
686
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
687
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
688
        { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
689
        { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
690
        { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
691
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
692
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
693
        { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
694
    ];
695
    my $number_of_items = scalar @$item_infos;
696
    my $number_of_items_with_homebranch_is_CPL =
697
      grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
698
699
    my @itemnumbers;
700
    for my $item_info (@$item_infos) {
701
        my $itemnumber = $builder->build_sample_item(
702
            {
703
                biblionumber  => $biblio->biblionumber,
704
                homebranch    => $item_info->{homebranch},
705
                holdingbranch => $item_info->{holdingbranch},
706
                itype         => $itemtype->{itemtype}
707
            }
708
        )->itemnumber;
709
710
        push @itemnumbers, $itemnumber;
711
    }
712
713
    my $host_item = $builder->build_sample_item({ homebranch => $library2->{branchcode} });
714
    my $child_field = PrepHostMarcField( $host_item->biblionumber, $host_item->itemnumber, 'MARC21');
715
    my $child_record = $biblio->metadata->record;
716
    $child_record->append_fields($child_field);
717
    ModBiblio( $child_record, $biblio->biblionumber, '' );
718
719
    # Emptied the OpacHiddenItems pref
720
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
721
722
    my ($itemfield) =
723
      C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
724
    my $record = C4::Biblio::GetMarcBiblio({ biblionumber => $biblio->biblionumber });
725
    warning_is { C4::Biblio::EmbedItemsInMarcBiblio() }
726
    { carped => 'EmbedItemsInMarcBiblio: No MARC record passed' },
727
      'Should carp is no record passed.';
728
729
    C4::Biblio::EmbedItemsInMarcBiblio({
730
        marc_record  => $record,
731
        biblionumber => $biblio->biblionumber });
732
    my @items = $record->field($itemfield);
733
    is( scalar @items, $number_of_items, 'Should return all items' );
734
735
    my $marc_with_items = C4::Biblio::GetMarcBiblio({
736
        biblionumber => $biblio->biblionumber,
737
        embed_items  => 1 });
738
    is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
739
740
    C4::Biblio::EmbedItemsInMarcBiblio({
741
        marc_record  => $record,
742
        biblionumber => $biblio->biblionumber,
743
        embed_host_items => 1
744
    });
745
    @items = $record->field($itemfield);
746
    is( scalar @items, $number_of_items + 1, 'Should return all items plus host items' );
747
748
    $marc_with_items = C4::Biblio::GetMarcBiblio({
749
        biblionumber => $biblio->biblionumber,
750
        embed_items  => 1,
751
        embed_host_items => 1
752
    });
753
    is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
754
755
    C4::Biblio::EmbedItemsInMarcBiblio({
756
        marc_record  => $record,
757
        biblionumber => $biblio->biblionumber,
758
        item_numbers => [ $itemnumbers[1], $itemnumbers[3] ] });
759
    @items = $record->field($itemfield);
760
    is( scalar @items, 2, 'Should return all items present in the list' );
761
762
    C4::Biblio::EmbedItemsInMarcBiblio({
763
        marc_record  => $record,
764
        biblionumber => $biblio->biblionumber,
765
        opac         => 1 });
766
    @items = $record->field($itemfield);
767
    is( scalar @items, $number_of_items, 'Should return all items for opac' );
768
769
    my $opachiddenitems = "
770
        homebranch: ['$library1->{branchcode}']";
771
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
772
773
    C4::Biblio::EmbedItemsInMarcBiblio({
774
        marc_record  => $record,
775
        biblionumber => $biblio->biblionumber });
776
    @items = $record->field($itemfield);
777
    is( scalar @items,
778
        $number_of_items,
779
        'Even with OpacHiddenItems set, all items should have been embedded' );
780
781
    C4::Biblio::EmbedItemsInMarcBiblio({
782
        marc_record  => $record,
783
        biblionumber => $biblio->biblionumber,
784
        opac         => 1 });
785
    @items = $record->field($itemfield);
786
    is(
787
        scalar @items,
788
        $number_of_items - $number_of_items_with_homebranch_is_CPL,
789
'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
790
    );
791
792
    C4::Biblio::EmbedItemsInMarcBiblio({
793
        marc_record  => $record,
794
        biblionumber => $biblio->biblionumber,
795
        opac         => 1,
796
        embed_host_items => 1
797
    });
798
    @items = $record->field($itemfield);
799
    is(
800
        scalar @items,
801
        $number_of_items - $number_of_items_with_homebranch_is_CPL + 1,
802
'For OPAC, the pref OpacHiddenItems should have been take into account and host items embedded. Only items with homebranch ne CPL should have been embedded'
803
    );
804
805
806
    $opachiddenitems = "
807
        homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
808
    t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
809
    C4::Biblio::EmbedItemsInMarcBiblio({
810
        marc_record  => $record,
811
        biblionumber => $biblio->biblionumber,
812
        opac         => 1 });
813
    @items = $record->field($itemfield);
814
    is(
815
        scalar @items,
816
        0,
817
'For OPAC, If all items are hidden, no item should have been embedded'
818
    );
819
820
    $schema->storage->txn_rollback;
821
};
822
668
subtest 'get_hostitemnumbers_of' => sub {
823
subtest 'get_hostitemnumbers_of' => sub {
669
    plan tests => 3;
824
    plan tests => 3;
670
825
671
- 

Return to bug 27138