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 => 4;
24 use t::lib::TestBuilder;
27 use C4::Biblio qw( AddBiblio );
31 use_ok('Koha::Biblio::Metadatas');
34 my $schema = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
37 subtest 'record() tests' => sub {
41 $schema->storage->txn_begin;
43 my $title = 'Oranges and Peaches';
45 # Create a valid record
46 my $record = MARC::Record->new();
47 my $field = MARC::Field->new( '245', '', '', 'a' => $title );
48 $record->append_fields($field);
49 my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' );
51 my $metadata = Koha::Biblios->find($biblio_id)->metadata;
52 my $record2 = $metadata->record;
54 is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
55 is( $record2->field('245')->subfield("a"),
56 $title, 'Title in 245$a matches title from original record object' );
58 my $bad_data = $builder->build_object(
59 { class => 'Koha::Biblio::Metadatas',
60 value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
65 { throws_ok { $bad_data->record; }
66 'Koha::Exceptions::Metadata::Invalid', 'Exception thrown on bad record'; }
67 qr/parser error : Start tag expected, '<' not found/,
68 'Warning thrown excplicitly';
71 is( $exception->id, $bad_data->id, 'id passed correctly to exception' );
72 is( $exception->format, 'marcxml', 'format passed correctly to exception' );
73 is( $exception->schema, 'MARC21', 'schema passed correctly to exception' );
75 my $bad_format = $builder->build_object(
76 { class => 'Koha::Biblio::Metadatas',
77 value => { format => 'mij', schema => 'MARC21', metadata => 'something' }
81 throws_ok { $bad_format->record; }
82 'Koha::Exceptions::Metadata', 'Exception thrown on unhandled format';
85 'Koha::Biblio::Metadata->record called on unhandled format: mij',
86 'Exception message built correctly'
89 $schema->storage->txn_rollback;
92 subtest 'record_strip_nonxml() tests' => sub {
96 $schema->storage->txn_begin;
98 my $title = 'Oranges and' . chr(31) . ' Peaches';
100 # Create a valid record
101 my $record = MARC::Record->new();
102 my $field = MARC::Field->new( '245', '', '', 'a' => $title );
103 $record->append_fields($field);
104 my ($biblio_id) = C4::Biblio::AddBiblio( $record, '' );
106 my $metadata = Koha::Biblios->find($biblio_id)->metadata;
107 my $record2 = $metadata->record_strip_nonxml;
109 is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
111 $record2->field('245')->subfield("a"),
112 "Oranges and Peaches", 'Title in 245$a matches title with control character removed'
115 my $bad_data = $builder->build_object(
117 class => 'Koha::Biblio::Metadatas',
118 value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
122 warning_like { $record2 = $bad_data->record_strip_nonxml; }
123 qr/parser error : Start tag expected, '<' not found/,
124 'Warning thrown explicitly';
128 "record_strip_nonxml returns undef when the record cannot be parsed after removing nonxml characters"
131 my $builder = t::lib::TestBuilder->new;
132 my $item = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } );
134 # Emptied the OpacHiddenItems pref
135 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
136 my ($itemfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber');
138 $record2 = $metadata->record_strip_nonxml( { embed_items => 1 } );
140 my @items = $record2->field($itemfield);
142 is( scalar @items, 1, "We got back our item" );
144 $schema->storage->txn_rollback;
147 subtest '_embed_items' => sub {
150 $schema->storage->txn_begin();
152 my $builder = t::lib::TestBuilder->new;
153 my $library1 = $builder->build({
156 my $library2 = $builder->build({
159 my $itemtype = $builder->build({
160 source => 'Itemtype',
163 my $biblio = $builder->build_sample_biblio();
165 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
166 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
167 { homebranch => $library1->{branchcode}, holdingbranch => $library1->{branchcode} },
168 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
169 { homebranch => $library2->{branchcode}, holdingbranch => $library2->{branchcode} },
170 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
171 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
172 { homebranch => $library1->{branchcode}, holdingbranch => $library2->{branchcode} },
174 my $number_of_items = scalar @$item_infos;
175 my $number_of_items_with_homebranch_is_CPL =
176 grep { $_->{homebranch} eq $library1->{branchcode} } @$item_infos;
179 for my $item_info (@$item_infos) {
180 my $itemnumber = $builder->build_sample_item(
182 biblionumber => $biblio->biblionumber,
183 homebranch => $item_info->{homebranch},
184 holdingbranch => $item_info->{holdingbranch},
185 itype => $itemtype->{itemtype}
189 push @itemnumbers, $itemnumber;
192 # Emptied the OpacHiddenItems pref
193 t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
195 throws_ok { Koha::Biblio::Metadata->record() }
196 'Koha::Exceptions::Metadata',
197 'Koha::Biblio::Metadata->record must be called on an instantiated object or like a class method with a record passed in parameter';
200 C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
201 my $record = $biblio->metadata->record;
202 Koha::Biblio::Metadata->record(
206 biblionumber => $biblio->biblionumber
209 my @items = $record->field($itemfield);
210 is( scalar @items, $number_of_items, 'Should return all items' );
212 my $marc_with_items = $biblio->metadata->record({ embed_items => 1 });
213 is_deeply( $record, $marc_with_items, 'A direct call to GetMarcBiblio with items matches');
215 $record = $biblio->metadata->record({ embed_items => 1, itemnumbers => [ $itemnumbers[1], $itemnumbers[3] ] });
216 @items = $record->field($itemfield);
217 is( scalar @items, 2, 'Should return all items present in the list' );
219 $record = $biblio->metadata->record({ embed_items => 1, opac => 1 });
220 @items = $record->field($itemfield);
221 is( scalar @items, $number_of_items, 'Should return all items for opac' );
223 my $opachiddenitems = "
224 homebranch: ['$library1->{branchcode}']";
225 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
227 $record = $biblio->metadata->record({ embed_items => 1 });
228 @items = $record->field($itemfield);
231 'Even with OpacHiddenItems set, all items should have been embedded' );
233 $record = $biblio->metadata->record({ embed_items => 1, opac => 1 });
234 @items = $record->field($itemfield);
237 $number_of_items - $number_of_items_with_homebranch_is_CPL,
238 'For OPAC, the pref OpacHiddenItems should have been take into account. Only items with homebranch ne CPL should have been embedded'
242 homebranch: ['$library1->{branchcode}', '$library2->{branchcode}']";
243 t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
244 $record = $biblio->metadata->record({ embed_items => 1, opac => 1 });
245 @items = $record->field($itemfield);
249 'For OPAC, If all items are hidden, no item should have been embedded'
252 # Check position of 952 in response of embed_items marc
253 t::lib::Mocks::mock_preference( 'OpacHiddenItems', q{} );
254 $record = $biblio->metadata->record;
255 $record->insert_fields_ordered(
256 MARC::Field->new( '951', '', '', a => 'before items' ),
257 MARC::Field->new( '953', '', '', a => 'after items' ),
259 C4::Biblio::ModBiblio( $record, $biblio->biblionumber, q{} );
260 my $field_list = join ',', map { $_->tag } $record->fields;
261 ok( $field_list =~ /951,953/, "951 and 953 in $field_list" );
262 $biblio->discard_changes;
263 $record = $biblio->metadata->record({ embed_items => 1 });
264 $field_list = join ',', map { $_->tag } $record->fields;
265 ok( $field_list =~ /951,(952,)+953/, "951-952s-953 in $field_list" );
267 $schema->storage->txn_rollback;