Bug 31889: Add unit test to show problem
[koha-ffzg.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 => 3;
21 use Test::Exception;
22
23 use t::lib::TestBuilder;
24 use t::lib::Mocks;
25
26 use C4::Biblio qw( AddBiblio );
27 use Koha::Database;
28
29 BEGIN {
30     use_ok('Koha::Biblio::Metadatas');
31 }
32
33 my $schema  = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'record() tests' => sub {
37
38     plan tests => 8;
39
40     $schema->storage->txn_begin;
41
42     my $title = 'Oranges and Peaches';
43
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, '' );
49
50     my $metadata = Koha::Biblios->find($biblio_id)->metadata;
51     my $record2  = $metadata->record;
52
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' );
56
57     my $bad_data = $builder->build_object(
58         {   class => 'Koha::Biblio::Metadatas',
59             value => { format => 'marcxml', schema => 'MARC21', metadata => 'this_is_not_marcxml' }
60         }
61     );
62
63     throws_ok { $bad_data->record; }
64     'Koha::Exceptions::Metadata::Invalid', 'Exception thrown on bad record';
65
66     my $exception = $@;
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' );
70
71     my $bad_format = $builder->build_object(
72         {   class => 'Koha::Biblio::Metadatas',
73             value => { format => 'mij', schema => 'MARC21', metadata => 'something' }
74         }
75     );
76
77     throws_ok { $bad_format->record; }
78     'Koha::Exceptions::Metadata', 'Exception thrown on unhandled format';
79
80     is( "$@",
81         'Koha::Biblio::Metadata->record called on unhandled format: mij',
82         'Exception message built correctly'
83     );
84
85     $schema->storage->txn_rollback;
86 };
87
88 subtest '_embed_items' => sub {
89     plan tests => 10;
90
91     $schema->storage->txn_begin();
92
93     my $builder = t::lib::TestBuilder->new;
94     my $library1 = $builder->build({
95         source => 'Branch',
96     });
97     my $library2 = $builder->build({
98         source => 'Branch',
99     });
100     my $itemtype = $builder->build({
101         source => 'Itemtype',
102     });
103
104     my $biblio = $builder->build_sample_biblio();
105     my $item_infos = [
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} },
114     ];
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;
118
119     my @itemnumbers;
120     for my $item_info (@$item_infos) {
121         my $itemnumber = $builder->build_sample_item(
122             {
123                 biblionumber  => $biblio->biblionumber,
124                 homebranch    => $item_info->{homebranch},
125                 holdingbranch => $item_info->{holdingbranch},
126                 itype         => $itemtype->{itemtype}
127             }
128         )->itemnumber;
129
130         push @itemnumbers, $itemnumber;
131     }
132
133     # Emptied the OpacHiddenItems pref
134     t::lib::Mocks::mock_preference( 'OpacHiddenItems', '' );
135
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';
139
140     my ($itemfield) =
141       C4::Biblio::GetMarcFromKohaField( 'items.itemnumber' );
142     my $record = $biblio->metadata->record;
143     Koha::Biblio::Metadata->record(
144         {
145             record       => $record,
146             embed_items  => 1,
147             biblionumber => $biblio->biblionumber
148         }
149     );
150     my @items = $record->field($itemfield);
151     is( scalar @items, $number_of_items, 'Should return all items' );
152
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');
155
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' );
159
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' );
163
164     my $opachiddenitems = "
165         homebranch: ['$library1->{branchcode}']";
166     t::lib::Mocks::mock_preference( 'OpacHiddenItems', $opachiddenitems );
167
168     $record = $biblio->metadata->record({ embed_items => 1 });
169     @items = $record->field($itemfield);
170     is( scalar @items,
171         $number_of_items,
172         'Even with OpacHiddenItems set, all items should have been embedded' );
173
174     $record = $biblio->metadata->record({ embed_items => 1, opac => 1 });
175     @items = $record->field($itemfield);
176     is(
177         scalar @items,
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'
180     );
181
182     $opachiddenitems = "
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);
187     is(
188         scalar @items,
189         0,
190 'For OPAC, If all items are hidden, no item should have been embedded'
191     );
192
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' ),
199     );
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" );
207
208     $schema->storage->txn_rollback;
209 };