Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha.git] / t / db_dependent / Koha / UI / Form / Builder / Item.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 use Test::More tests => 9;
20 use Data::Dumper qw( Dumper );
21 use utf8;
22
23 use List::MoreUtils qw( uniq );
24
25 use Koha::Libraries;
26 use Koha::MarcSubfieldStructures;
27 use Koha::UI::Form::Builder::Item;
28 use t::lib::TestBuilder;
29 use t::lib::Mocks;
30
31 my $schema = Koha::Database->new->schema;
32 $schema->storage->txn_begin;
33
34 my $builder = t::lib::TestBuilder->new;
35
36 my $cache = Koha::Caches->get_instance();
37 $cache->clear_from_cache("MarcStructure-0-");
38 $cache->clear_from_cache("MarcStructure-1-");
39 $cache->clear_from_cache("MarcSubfieldStructure-");
40
41 # 952 $x $é are not linked with a kohafield
42 # $952$x $é repeatable
43 # $952$t is not repeatable
44 # 952$z is linked with items.itemnotes and is repeatable
45 # 952$t is linked with items.copynumber and is not repeatable
46 setup_mss();
47
48 # FIXME Later in this script we are comparing itemtypes, ordered by their description.
49 # MySQL and Perl don't sort _ identically.
50 # If you have one itemtype BK and another one B_K, MySQL will sort B_K first when Perl will sort it last
51 my @itemtypes = Koha::ItemTypes->search->as_list;
52 for my $itemtype ( @itemtypes ) {
53     my $d = $itemtype->description;
54     $d =~ s|_||g;
55     $itemtype->description($d)->store;
56 }
57
58 subtest 'authorised values' => sub {
59     #plan tests => 1;
60
61     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
62     my $subfields =
63       Koha::UI::Form::Builder::Item->new(
64         { biblionumber => $biblio->biblionumber } )->edit_form;
65
66     my @display_orders = uniq map { $_->{display_order} } @$subfields;
67     is_deeply( \@display_orders, [sort {$a <=> $b} @display_orders], 'subfields are sorted by display order' );
68
69     subtest 'normal AV' => sub {
70         plan tests => 2;
71         my ($subfield) =
72           grep { $_->{kohafield} eq 'items.notforloan' } @$subfields;
73         my $avs = Koha::AuthorisedValues->search( { category => 'NOT_LOAN' } );
74
75         is_deeply(
76             $subfield->{marc_value}->{values},
77             [
78                 "",
79                 map    { $_->authorised_value }
80                   sort { $a->lib cmp $b->lib }
81                   $avs->as_list
82             ],
83             'AVs are sorted by lib and en empty option is created first'
84         );
85         is_deeply(
86             $subfield->{marc_value}->{labels},
87             {
88                 map    { $_->authorised_value => $_->lib }
89                   sort { $a->lib cmp $b->lib }
90                   $avs->as_list
91             }
92         );
93     };
94
95     subtest 'cn_source' => sub {
96         plan tests => 2;
97         my ( $subfield ) = grep { $_->{kohafield} eq 'items.cn_source' } @$subfields;
98         is_deeply( $subfield->{marc_value}->{values}, [ '', 'ddc', 'lcc' ] );
99         is_deeply(
100             $subfield->{marc_value}->{labels},
101             {
102                 ddc => "Dewey Decimal Classification",
103                 lcc => "Library of Congress Classification",
104             }
105         );
106     };
107     subtest 'branches' => sub {
108         plan tests => 2;
109         my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
110         my $libraries = Koha::Libraries->search({}, { order_by => 'branchname' });
111         is_deeply(
112             $subfield->{marc_value}->{values},
113             [ $libraries->get_column('branchcode') ]
114         );
115         is_deeply(
116             $subfield->{marc_value}->{labels},
117             { map { $_->branchcode => $_->branchname } $libraries->as_list }
118         );
119     };
120
121     subtest 'itemtypes' => sub {
122         plan tests => 2;
123         my ($subfield) = grep { $_->{kohafield} eq 'items.itype' } @$subfields;
124         my @itemtypes = Koha::ItemTypes->search->as_list;
125
126         my $expected = [
127             "",
128             map    { $_->itemtype }
129               # We need to sort using uc or perl won't be case insensitive
130               sort { uc($a->translated_description) cmp uc($b->translated_description) }
131               @itemtypes
132         ];
133         is_deeply(
134             $subfield->{marc_value}->{values},
135             $expected,
136             "Item types should be sorted by description and an empty entry should be shown"
137         );
138
139         is_deeply( $subfield->{marc_value}->{labels},
140             { map { $_->itemtype => $_->description } @itemtypes},
141             'Labels should be correctly displayed'
142         );
143     };
144 };
145
146 subtest 'prefill_with_default_values' => sub {
147     plan tests => 3;
148
149     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
150     my $subfields =
151       Koha::UI::Form::Builder::Item->new(
152         { biblionumber => $biblio->biblionumber } )->edit_form;
153
154
155     my ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
156     is( $subfield->{marc_value}->{value}, '', 'no default value if prefill_with_default_values not passed' );
157
158     $subfields =
159       Koha::UI::Form::Builder::Item->new(
160         { biblionumber => $biblio->biblionumber } )->edit_form({ prefill_with_default_values => 1 });
161
162
163     ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
164     is( $subfield->{marc_value}->{value}, 'ééé', 'default value should be set if prefill_with_default_values passed');
165
166     # Do the same for an existing item; we do not expect the defaultvalue to popup
167     my $item = $builder->build_sample_item;
168     $subfields = Koha::UI::Form::Builder::Item->new({
169         biblionumber => $biblio->biblionumber,
170         item => $item->unblessed,
171     })->edit_form({ prefill_with_default_values => 1 });
172     ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
173     is( $subfield->{marc_value}->{value}, q{}, 'default value not applied to existing item');
174
175 };
176
177 subtest 'subfields_to_prefill' => sub {
178     plan tests => 2;
179
180     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
181
182     my $more_subfields_xml = Koha::Item::Attributes->new({ "é" => "prefill é" })->to_marcxml;
183     my $subfields =
184       Koha::UI::Form::Builder::Item->new(
185         { biblionumber => $biblio->biblionumber, item => {more_subfields_xml => $more_subfields_xml}})->edit_form({subfields_to_prefill => ['é']});
186     my ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
187     is( $subfield->{marc_value}->{value}, 'prefill é', 'Not mapped subfield prefilled if needed' );
188
189     $subfields =
190       Koha::UI::Form::Builder::Item->new(
191         { biblionumber => $biblio->biblionumber, item => {itemnotes => 'prefill z'}})->edit_form({subfields_to_prefill => ['z']});
192     ($subfield) = grep { $_->{subfield} eq 'z' } @$subfields;
193     is( $subfield->{marc_value}->{value}, 'prefill z', 'Mapped subfield prefilled if needed');
194 };
195
196 subtest 'branchcode' => sub {
197     plan tests => 2;
198
199     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
200     my $library = $builder->build_object({ class => 'Koha::Libraries' });
201     my $subfields =
202       Koha::UI::Form::Builder::Item->new(
203         { biblionumber => $biblio->biblionumber } )->edit_form;
204
205     my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
206     is( $subfield->{marc_value}->{default}, '', 'no library preselected if no branchcode passed');
207
208     $subfields =
209       Koha::UI::Form::Builder::Item->new(
210         { biblionumber => $biblio->biblionumber } )->edit_form({ branchcode => $library->branchcode });
211
212     ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
213     is( $subfield->{marc_value}->{default}, $library->branchcode, 'the correct library should be preselected if branchcode is passed');
214 };
215
216 subtest 'default_branches_empty' => sub {
217     plan tests => 2;
218
219     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
220     my $subfields =
221       Koha::UI::Form::Builder::Item->new(
222         { biblionumber => $biblio->biblionumber } )->edit_form;
223
224     my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
225     isnt( $subfield->{marc_value}->{values}->[0], "", 'No empty option for branches' );
226
227     $subfields =
228       Koha::UI::Form::Builder::Item->new(
229         { biblionumber => $biblio->biblionumber } )->edit_form({ default_branches_empty => 1 });
230
231     ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
232     is( $subfield->{marc_value}->{values}->[0], "", 'empty option for branches if default_branches_empty passed' );
233 };
234
235 subtest 'kohafields_to_ignore' => sub {
236     plan tests => 2;
237
238     my $biblio =
239       $builder->build_sample_biblio( { value => { frameworkcode => '' } } );
240     my $subfields =
241       Koha::UI::Form::Builder::Item->new(
242         { biblionumber => $biblio->biblionumber } )->edit_form;
243
244     my ($subfield) = grep { $_->{kohafield} eq 'items.barcode' } @$subfields;
245     isnt( $subfield, undef, 'barcode subfield should be in the subfield list' );
246
247     $subfields =
248       Koha::UI::Form::Builder::Item->new(
249         { biblionumber => $biblio->biblionumber } )
250       ->edit_form( { kohafields_to_ignore => ['items.barcode'] } );
251
252     ($subfield) = grep { $_->{kohafield} eq 'items.barcode' } @$subfields;
253     is( $subfield, undef,
254         'barcode subfield should have not been built if passed to kohafields_to_ignore'
255     );
256 };
257
258 subtest 'subfields_to_allow & ignore_not_allowed_subfields' => sub {
259     plan tests => 6;
260
261     my ( $tag_cn, $subtag_cn ) = C4::Biblio::GetMarcFromKohaField("items.itemcallnumber");
262     my ( $tag_notes, $subtag_notes ) = C4::Biblio::GetMarcFromKohaField("items.itemnotes");
263     my $biblio = $builder->build_sample_biblio( { value => { frameworkcode => '' } } );
264     my $subfields =
265       Koha::UI::Form::Builder::Item->new(
266         { biblionumber => $biblio->biblionumber } )->edit_form(
267             {
268                 subfields_to_allow => [
269                     sprintf( '%s$%s', $tag_cn,    $subtag_cn ),
270                     sprintf( '%s$%s', $tag_notes, $subtag_notes )
271                 ]
272             }
273         );
274
275     isnt( scalar(@$subfields), 2, "There are more than the 2 subfields we allowed" );
276     my ($subfield) = grep { $_->{kohafield} eq 'items.itemcallnumber' } @$subfields;
277     is( $subfield->{marc_value}->{readonly}, undef, "subfields to allowed are not marked as readonly" );
278     ($subfield) = grep { $_->{kohafield} eq 'items.copynumber' } @$subfields;
279     isnt( $subfield->{marc_value}->{readonly}, 1, "subfields that are not in the allow list are marked as readonly" );
280
281     $subfields =
282       Koha::UI::Form::Builder::Item->new(
283         { biblionumber => $biblio->biblionumber } )->edit_form(
284             {
285                 subfields_to_allow => [
286                     sprintf( '%s$%s', $tag_cn,    $subtag_cn ),
287                     sprintf( '%s$%s', $tag_notes, $subtag_notes )
288                 ],
289                 ignore_not_allowed_subfields => 1,
290             }
291         );
292
293     is( scalar(@$subfields), 2, "With ignore_not_allowed_subfields, only the subfields to ignore are returned" );
294     ($subfield) =
295       grep { $_->{kohafield} eq 'items.itemcallnumber' } @$subfields;
296     is( $subfield->{marc_value}->{readonly}, undef, "subfields to allowed are not marked as readonly" );
297     ($subfield) = grep { $_->{kohafield} eq 'items.copynumber' } @$subfields;
298     is( $subfield, undef, "subfield that is not in the allow list is not returned" );
299 };
300
301 subtest 'ignore_invisible_subfields' => sub {
302     plan tests => 2;
303
304     my $biblio =
305       $builder->build_sample_biblio( { value => { frameworkcode => '' } } );
306     my $item = $builder->build_sample_item(
307         {
308             issues => 42,
309         }
310     );
311
312     # items.issues is mapped with 952$l
313     my $subfields = Koha::UI::Form::Builder::Item->new(
314         {
315             biblionumber => $biblio->biblionumber,
316             item         => $item->unblessed,
317         }
318     )->edit_form;
319     ( my $subfield ) = grep { $_->{subfield} eq 'l' } @$subfields;
320     is( $subfield->{marc_value}->{value}, 42, 'items.issues copied' );
321
322     $subfields = Koha::UI::Form::Builder::Item->new(
323         {
324             biblionumber => $biblio->biblionumber,
325             item         => $item->unblessed,
326         }
327     )->edit_form(
328         {
329             ignore_invisible_subfields => 1
330         }
331     );
332     ($subfield) = grep { $_->{subfield} eq 'l' } @$subfields;
333     is( $subfield->{marc_value}->{value},
334         undef, 'items.issues not copied if ignore_invisible_subfields is passed' );
335 };
336
337 subtest 'Fix subfill_with_default_values - no biblionumber passed' => sub {
338     plan tests => 1;
339
340     t::lib::Mocks::mock_preference('itemcallnumber', '082ab,092ab');
341     my $item = $builder->build_sample_item;
342     my $subfields = Koha::UI::Form::Builder::Item->new(
343         {
344             item         => $item->unblessed,
345         }
346     )->edit_form({ prefill_with_default_values => 1 });
347     pass();
348 };
349
350 $cache->clear_from_cache("MarcStructure-0-");
351 $cache->clear_from_cache("MarcStructure-1-");
352 $cache->clear_from_cache("MarcSubfieldStructure-");
353
354 sub setup_mss {
355
356     my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
357
358     Koha::MarcSubfieldStructures->search(
359         {
360             frameworkcode => '',
361             tagfield => $itemtag,
362             tagsubfield => 'é',
363         }
364     )->delete;    # In case it exist already
365
366     Koha::MarcSubfieldStructure->new(
367         {
368             frameworkcode => '',
369             tagfield      => $itemtag,
370             tagsubfield   => 'é',
371             kohafield     => undef,
372             repeatable    => 1,
373             defaultvalue  => 'ééé',
374             tab           => 10,
375         }
376     )->store;
377
378     Koha::MarcSubfieldStructures->search(
379         {
380             frameworkcode => '',
381             tagfield      => $itemtag,
382             tagsubfield   => [ 'x' ]
383         }
384     )->update( { kohafield => undef } );
385
386     Koha::MarcSubfieldStructures->search(
387         {
388             frameworkcode => '',
389             tagfield => $itemtag,
390             tagsubfield => [ 'x', 'é' ],
391         }
392     )->update( { repeatable => 1 } );
393
394     Koha::MarcSubfieldStructures->search(
395         {
396             frameworkcode => '',
397             tagfield => $itemtag,
398             tagsubfield => ['t'],
399         }
400     )->update( { repeatable => 0 } );
401
402     Koha::MarcSubfieldStructures->search(
403         {
404             frameworkcode => '',
405             tagfield => $itemtag,
406             tagsubfield => ['l'],
407         }
408     )->update( { hidden => -4 } );
409
410     Koha::MarcSubfieldStructures->search(
411         {
412             frameworkcode => '',
413             tagfield => $itemtag,
414             tagsubfield => ['z'],
415         }
416     )->update( { kohafield => 'items.itemnotes', repeatable => 1 } );
417
418     Koha::MarcSubfieldStructures->search(
419         {
420             frameworkcode => '',
421             tagfield => $itemtag,
422         }
423     )->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
424 }