Bug 34932: Patron.t - Pass borrowernumber of manager to userenv
[koha.git] / t / db_dependent / Koha / Biblio / Metadata.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 4;
21 use Test::Exception;
22 use Test::Warn;
23
24 use t::lib::TestBuilder;
25 use t::lib::Mocks;
26
27 use C4::Biblio qw( AddBiblio );
28 use Koha::Database;
29
30 BEGIN {
31     use_ok('Koha::Biblio::Metadatas');
32 }
33
34 my $schema  = Koha::Database->new->schema;
35 my $builder = t::lib::TestBuilder->new;
36
37 subtest 'record() tests' => sub {
38
39     plan tests => 9;
40
41     $schema->storage->txn_begin;
42
43     my $title = 'Oranges and Peaches';
44
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, '' );
50
51     my $metadata = Koha::Biblios->find($biblio_id)->metadata;
52     my $record2  = $metadata->record;
53
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' );
57
58     my $bad_data = $builder->build_object(
59         {   class => 'Koha::Biblio::Metadatas',
60             value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
61         }
62     );
63
64     warning_like
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';
69
70     my $exception = $@;
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' );
74
75     my $bad_format = $builder->build_object(
76         {   class => 'Koha::Biblio::Metadatas',
77             value => { format => 'mij', schema => 'MARC21', metadata => 'something' }
78         }
79     );
80
81     throws_ok { $bad_format->record; }
82     'Koha::Exceptions::Metadata', 'Exception thrown on unhandled format';
83
84     is( "$@",
85         'Koha::Biblio::Metadata->record called on unhandled format: mij',
86         'Exception message built correctly'
87     );
88
89     $schema->storage->txn_rollback;
90 };
91
92 subtest 'record_strip_nonxml() tests' => sub {
93
94     plan tests => 5;
95
96     $schema->storage->txn_begin;
97
98     my $title = 'Oranges and' . chr(31) . ' Peaches';
99
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, '' );
105
106     my $metadata = Koha::Biblios->find($biblio_id)->metadata;
107     my $record2  = $metadata->record_strip_nonxml;
108
109     is( ref $record2, 'MARC::Record', 'Method record() returned a MARC::Record object' );
110     is(
111         $record2->field('245')->subfield("a"),
112         "Oranges and Peaches", 'Title in 245$a matches title with control character removed'
113     );
114
115     my $bad_data = $builder->build_object(
116         {
117             class => 'Koha::Biblio::Metadatas',
118             value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
119         }
120     );
121
122     warning_like { $record2 = $bad_data->record_strip_nonxml; }
123     qr/parser error : Start tag expected, '<' not found/,
124         'Warning thrown explicitly';
125
126     is(
127         $record2, undef,
128         "record_strip_nonxml returns undef when the record cannot be parsed after removing nonxml characters"
129     );
130
131     my $builder = t::lib::TestBuilder->new;
132     my $item    = $builder->build_sample_item( { biblionumber => $metadata->biblionumber } );
133
134     # Emptied the OpacHiddenItems pref
135     t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
136     my ($itemfield) = C4::Biblio::GetMarcFromKohaField('items.itemnumber');
137
138     $record2 = $metadata->record_strip_nonxml( { embed_items => 1 } );
139
140     my @items = $record2->field($itemfield);
141
142     is( scalar @items, 1, "We got back our item" );
143
144     $schema->storage->txn_rollback;
145 };
146
147 subtest '_embed_items' => sub {
148     plan tests => 10;
149
150     $schema->storage->txn_begin();
151
152     my $builder = t::lib::TestBuilder->new;
153     my $library1 = $builder->build({
154         source => 'Branch',
155     });
156     my $library2 = $builder->build({
157         source => 'Branch',
158     });
159     my $itemtype = $builder->build({
160         source => 'Itemtype',
161     });
162
163     my $biblio = $builder->build_sample_biblio();
164     my $item_infos = [
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} },
173     ];
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;
177
178     my @itemnumbers;
179     for my $item_info (@$item_infos) {
180         my $itemnumber = $builder->build_sample_item(
181             {
182                 biblionumber  => $biblio->biblionumber,
183                 homebranch    => $item_info->{homebranch},
184                 holdingbranch => $item_info->{holdingbranch},
185                 itype         => $itemtype->{itemtype}
186             }
187         )->itemnumber;
188
189         push @itemnumbers, $itemnumber;
190     }
191
192     # Emptied the OpacHiddenItems pref
193     t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
194
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';
198
199     my ($itemfield) =
200       C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
201     my $record = $biblio->metadata->record;
202     Koha::Biblio::Metadata->record(
203         {
204             record       => $record,
205             embed_items  => 1,
206             biblionumber => $biblio->biblionumber
207         }
208     );
209     my @items = $record->field($itemfield);
210     is( scalar @items, $number_of_items, 'Should return all items' );
211
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');
214
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' );
218
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' );
222
223     my $opachiddenitems = "
224         homebranch: ['$library1->{branchcode}']";
225     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
226
227     $record = $biblio->metadata->record({ embed_items => 1 });
228     @items = $record->field($itemfield);
229     is( scalar @items,
230         $number_of_items,
231         'Even with OpacHiddenItems set, all items should have been embedded' );
232
233     $record = $biblio->metadata->record({ embed_items => 1, opac => 1 });
234     @items = $record->field($itemfield);
235     is(
236         scalar @items,
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'
239     );
240
241     $opachiddenitems = "
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);
246     is(
247         scalar @items,
248         0,
249 'For OPAC, If all items are hidden, no item should have been embedded'
250     );
251
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' ),
258     );
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" );
266
267     $schema->storage->txn_rollback;
268 };