3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 3;
23 use t::lib::TestBuilder;
26 use C4::Biblio qw( AddBiblio );
30 use_ok('Koha::Biblio::Metadatas');
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'record() tests' => sub {
40 $schema->storage->txn_begin;
42 my $title = 'Oranges and Peaches';
44 # Create a valid record
45 my $record = MARC::Record->new();
46 my $field = MARC::Field->new( '245', '', '', 'a' => $title );
47 $record->append_fields($field);
48 my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' );
50 my $metadata = Koha::Biblios->find($biblio_id)->metadata;
51 my $record2 = $metadata->record;
53 is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
54 is( $record2->field('245')->subfield("a"),
55 $title, 'Title in 245$a matches title from original record object' );
57 my $bad_data = $builder->build_object(
58 { class => 'Koha::Biblio::Metadatas',
59 value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
63 throws_ok { $bad_data->record; }
64 'Koha::Exceptions::Metadata::Invalid', 'Exception thrown on bad record';
67 is( $exception->id, $bad_data->id, 'id passed correctly to exception' );
68 is( $exception->format, 'marcxml', 'format passed correctly to exception' );
69 is( $exception->schema, 'MARC21', 'schema passed correctly to exception' );
71 my $bad_format = $builder->build_object(
72 { class => 'Koha::Biblio::Metadatas',
73 value => { format => 'mij', schema => 'MARC21', metadata => 'something' }
77 throws_ok { $bad_format->record; }
78 'Koha::Exceptions::Metadata', 'Exception thrown on unhandled format';
81 'Koha::Biblio::Metadata->record called on unhandled format: mij',
82 'Exception message built correctly'
85 $schema->storage->txn_rollback;
88 subtest '_embed_items' => sub {
91 $schema->storage->txn_begin();
93 my $builder = t::lib::TestBuilder->new;
94 my $library1 = $builder->build({
97 my $library2 = $builder->build({
100 my $itemtype = $builder->build({
101 source => 'Itemtype',
104 my $biblio = $builder->build_sample_biblio();
106 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
107 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
108 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
109 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
110 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
111 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
112 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
113 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
115 my $number_of_items = scalar @$item_infos;
116 my $number_of_items_with_homebranch_is_CPL =
117 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
120 for my $item_info (@$item_infos) {
121 my $itemnumber = $builder->build_sample_item(
123 biblionumber => $biblio->biblionumber,
124 homebranch => $item_info->{homebranch},
125 holdingbranch => $item_info->{holdingbranch},
126 itype => $itemtype->{itemtype}
130 push @itemnumbers, $itemnumber;
133 # Emptied the OpacHiddenItems pref
134 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
136 throws_ok { Koha::Biblio::Metadata->record() }
137 'Koha::Exceptions::Metadata',
138 'Koha::Biblio::Metadata->record must be called on an instantiated object or like a class method with a record passed in parameter';
141 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
142 my $record = $biblio->metadata->record;
143 Koha::Biblio::Metadata->record(
147 biblionumber => $biblio->biblionumber
150 my @items = $record->field($itemfield);
151 is( scalar @items, $number_of_items, 'Should return all items' );
153 my $marc_with_items = $biblio->metadata->record({ embed_items => 1 });
154 is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
156 $record = $biblio->metadata->record({ embed_items => 1, itemnumbers => [ $itemnumbers[1], $itemnumbers[3] ] });
157 @items = $record->field($itemfield);
158 is( scalar @items, 2, 'Should return all items present in the list' );
160 $record = $biblio->metadata->record({ embed_items => 1, opac => 1 });
161 @items = $record->field($itemfield);
162 is( scalar @items, $number_of_items, 'Should return all items for opac' );
164 my $opachiddenitems = "
165 homebranch: ['$library1->{branchcode}']";
166 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
168 $record = $biblio->metadata->record({ embed_items => 1 });
169 @items = $record->field($itemfield);
172 'Even with OpacHiddenItems set, all items should have been embedded' );
174 $record = $biblio->metadata->record({ embed_items => 1, opac => 1 });
175 @items = $record->field($itemfield);
178 $number_of_items - $number_of_items_with_homebranch_is_CPL,
179 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
183 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
184 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
185 $record = $biblio->metadata->record({ embed_items => 1, opac => 1 });
186 @items = $record->field($itemfield);
190 'For OPAC, If all items are hidden, no item should have been embedded'
193 # Check position of 952 in response of embed_items marc
194 t::lib::Mocks::mock_preference( 'OpacHiddenItems', q{} );
195 $record = $biblio->metadata->record;
196 $record->insert_fields_ordered(
197 MARC::Field->new( '951', '', '', a => 'before items' ),
198 MARC::Field->new( '953', '', '', a => 'after items' ),
200 C4::Biblio::ModBiblio( $record, $biblio->biblionumber, q{} );
201 my $field_list = join ',', map { $_->tag } $record->fields;
202 ok( $field_list =~ /951,953/, "951 and 953 in $field_list" );
203 $biblio->discard_changes;
204 $record = $biblio->metadata->record({ embed_items => 1 });
205 $field_list = join ',', map { $_->tag } $record->fields;
206 ok( $field_list =~ /951,(952,)+953/, "951-952s-953 in $field_list" );
208 $schema->storage->txn_rollback;