Bug 29146: Add test for editing existing item
[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 => 7;
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
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $builder = t::lib::TestBuilder->new;
34
35 my $cache = Koha::Caches->get_instance();
36 $cache->clear_from_cache("MarcStructure-0-");
37 $cache->clear_from_cache("MarcStructure-1-");
38 $cache->clear_from_cache("default_value_for_mod_marc-");
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 subtest 'authorised values' => sub {
49     #plan tests => 1;
50
51     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
52     my $subfields =
53       Koha::UI::Form::Builder::Item->new(
54         { biblionumber => $biblio->biblionumber } )->edit_form;
55
56     my @display_orders = uniq map { $_->{display_order} } @$subfields;
57     is_deeply( \@display_orders, [sort {$a <=> $b} @display_orders], 'subfields are sorted by display order' );
58
59     subtest 'normal AV' => sub {
60         plan tests => 2;
61         my ($subfield) =
62           grep { $_->{kohafield} eq 'items.notforloan' } @$subfields;
63         my $avs = Koha::AuthorisedValues->search( { category => 'NOT_LOAN' } );
64
65         is_deeply(
66             $subfield->{marc_value}->{values},
67             [
68                 "",
69                 map    { $_->authorised_value }
70                   sort { $a->lib cmp $b->lib }
71                   $avs->as_list
72             ],
73             'AVs are sorted by lib and en empty option is created first'
74         );
75         is_deeply(
76             $subfield->{marc_value}->{labels},
77             {
78                 map    { $_->authorised_value => $_->lib }
79                   sort { $a->lib cmp $b->lib }
80                   $avs->as_list
81             }
82         );
83     };
84
85     subtest 'cn_source' => sub {
86         plan tests => 2;
87         my ( $subfield ) = grep { $_->{kohafield} eq 'items.cn_source' } @$subfields;
88         is_deeply( $subfield->{marc_value}->{values}, [ '', 'ddc', 'lcc' ] );
89         is_deeply(
90             $subfield->{marc_value}->{labels},
91             {
92                 ddc => "Dewey Decimal Classification",
93                 lcc => "Library of Congress Classification",
94             }
95         );
96     };
97     subtest 'branches' => sub {
98         plan tests => 2;
99         my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
100         my $libraries = Koha::Libraries->search({}, { order_by => 'branchname' });
101         is_deeply(
102             $subfield->{marc_value}->{values},
103             [ $libraries->get_column('branchcode') ]
104         );
105         is_deeply(
106             $subfield->{marc_value}->{labels},
107             { map { $_->branchcode => $_->branchname } $libraries->as_list }
108         );
109     };
110
111     subtest 'itemtypes' => sub {
112         plan tests => 2;
113         my ($subfield) = grep { $_->{kohafield} eq 'items.itype' } @$subfields;
114         my $itemtypes = Koha::ItemTypes->search;
115
116         is_deeply(
117             $subfield->{marc_value}->{values},
118             [
119                 "",
120                 map    { $_->itemtype }
121                   # We need to sort using uc or perl won't be case insensitive
122                   sort { uc($a->translated_description) cmp uc($b->translated_description) }
123                   $itemtypes->as_list
124             ],
125             "Item types should be sorted by description and an empty entries should be shown"
126         )
127         or diag("Itemtypes details: ".Dumper($subfield->{marc_value}->{values}, [map { $_->itemtype } $itemtypes->as_list]));
128
129         is_deeply( $subfield->{marc_value}->{labels},
130             { map { $_->itemtype => $_->description } $itemtypes->as_list },
131             'Labels should be correctly displayed'
132         );
133     };
134 };
135
136 subtest 'prefill_with_default_values' => sub {
137     plan tests => 3;
138
139     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
140     my $subfields =
141       Koha::UI::Form::Builder::Item->new(
142         { biblionumber => $biblio->biblionumber } )->edit_form;
143
144
145     my ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
146     is( $subfield->{marc_value}->{value}, '', 'no default value if prefill_with_default_values not passed' );
147
148     $subfields =
149       Koha::UI::Form::Builder::Item->new(
150         { biblionumber => $biblio->biblionumber } )->edit_form({ prefill_with_default_values => 1 });
151
152
153     ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
154     is( $subfield->{marc_value}->{value}, 'ééé', 'default value should be set if prefill_with_default_values passed');
155
156     # Do the same for an existing item; we do not expect the defaultvalue to popup
157     my $item = $builder->build_sample_item;
158     $subfields = Koha::UI::Form::Builder::Item->new({
159         biblionumber => $biblio->biblionumber,
160         item => $item->unblessed,
161     })->edit_form({ prefill_with_default_values => 1 });
162     ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
163     is( $subfield->{marc_value}->{value}, q{}, 'default value not applied to existing item');
164
165 };
166
167 subtest 'subfields_to_prefill' => sub {
168     plan tests => 2;
169
170     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
171
172     my $more_subfields_xml = Koha::Item::Attributes->new({ "é" => "prefill é" })->to_marcxml;
173     my $subfields =
174       Koha::UI::Form::Builder::Item->new(
175         { biblionumber => $biblio->biblionumber, item => {more_subfields_xml => $more_subfields_xml}})->edit_form({subfields_to_prefill => ['é']});
176     my ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
177     is( $subfield->{marc_value}->{value}, 'prefill é', 'Not mapped subfield prefilled if needed' );
178
179     $subfields =
180       Koha::UI::Form::Builder::Item->new(
181         { biblionumber => $biblio->biblionumber, item => {itemnotes => 'prefill z'}})->edit_form({subfields_to_prefill => ['z']});
182     ($subfield) = grep { $_->{subfield} eq 'z' } @$subfields;
183     is( $subfield->{marc_value}->{value}, 'prefill z', 'Mapped subfield prefilled if needed');
184 };
185
186 subtest 'branchcode' => sub {
187     plan tests => 2;
188
189     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
190     my $library = $builder->build_object({ class => 'Koha::Libraries' });
191     my $subfields =
192       Koha::UI::Form::Builder::Item->new(
193         { biblionumber => $biblio->biblionumber } )->edit_form;
194
195     my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
196     is( $subfield->{marc_value}->{default}, '', 'no library preselected if no branchcode passed');
197
198     $subfields =
199       Koha::UI::Form::Builder::Item->new(
200         { biblionumber => $biblio->biblionumber } )->edit_form({ branchcode => $library->branchcode });
201
202     ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
203     is( $subfield->{marc_value}->{default}, $library->branchcode, 'the correct library should be preselected if branchcode is passed');
204 };
205
206 subtest 'default_branches_empty' => sub {
207     plan tests => 2;
208
209     my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
210     my $subfields =
211       Koha::UI::Form::Builder::Item->new(
212         { biblionumber => $biblio->biblionumber } )->edit_form;
213
214     my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
215     isnt( $subfield->{marc_value}->{values}->[0], "", 'No empty option for branches' );
216
217     $subfields =
218       Koha::UI::Form::Builder::Item->new(
219         { biblionumber => $biblio->biblionumber } )->edit_form({ default_branches_empty => 1 });
220
221     ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
222     is( $subfield->{marc_value}->{values}->[0], "", 'empty option for branches if default_branches_empty passed' );
223 };
224
225 subtest 'kohafields_to_ignore' => sub {
226     plan tests => 2;
227
228     my $biblio =
229       $builder->build_sample_biblio( { value => { frameworkcode => '' } } );
230     my $subfields =
231       Koha::UI::Form::Builder::Item->new(
232         { biblionumber => $biblio->biblionumber } )->edit_form;
233
234     my ($subfield) = grep { $_->{kohafield} eq 'items.barcode' } @$subfields;
235     isnt( $subfield, undef, 'barcode subfield should be in the subfield list' );
236
237     $subfields =
238       Koha::UI::Form::Builder::Item->new(
239         { biblionumber => $biblio->biblionumber } )
240       ->edit_form( { kohafields_to_ignore => ['items.barcode'] } );
241
242     ($subfield) = grep { $_->{kohafield} eq 'items.barcode' } @$subfields;
243     is( $subfield, undef,
244         'barcode subfield should have not been built if passed to kohafields_to_ignore'
245     );
246 };
247
248 subtest 'subfields_to_allow & ignore_not_allowed_subfields' => sub {
249     plan tests => 6;
250
251     my ( $tag_cn, $subtag_cn ) = C4::Biblio::GetMarcFromKohaField("items.itemcallnumber");
252     my ( $tag_notes, $subtag_notes ) = C4::Biblio::GetMarcFromKohaField("items.itemnotes");
253     my $biblio = $builder->build_sample_biblio( { value => { frameworkcode => '' } } );
254     my $subfields =
255       Koha::UI::Form::Builder::Item->new(
256         { biblionumber => $biblio->biblionumber } )->edit_form(
257             {
258                 subfields_to_allow => [
259                     sprintf( '%s$%s', $tag_cn,    $subtag_cn ),
260                     sprintf( '%s$%s', $tag_notes, $subtag_notes )
261                 ]
262             }
263         );
264
265     isnt( scalar(@$subfields), 2, "There are more than the 2 subfields we allowed" );
266     my ($subfield) = grep { $_->{kohafield} eq 'items.itemcallnumber' } @$subfields;
267     is( $subfield->{marc_value}->{readonly}, undef, "subfields to allowed are not marked as readonly" );
268     ($subfield) = grep { $_->{kohafield} eq 'items.copynumber' } @$subfields;
269     isnt( $subfield->{marc_value}->{readonly}, 1, "subfields that are not in the allow list are marked as readonly" );
270
271     $subfields =
272       Koha::UI::Form::Builder::Item->new(
273         { biblionumber => $biblio->biblionumber } )->edit_form(
274             {
275                 subfields_to_allow => [
276                     sprintf( '%s$%s', $tag_cn,    $subtag_cn ),
277                     sprintf( '%s$%s', $tag_notes, $subtag_notes )
278                 ],
279                 ignore_not_allowed_subfields => 1,
280             }
281         );
282
283     is( scalar(@$subfields), 2, "With ignore_not_allowed_subfields, only the subfields to ignore are returned" );
284     ($subfield) =
285       grep { $_->{kohafield} eq 'items.itemcallnumber' } @$subfields;
286     is( $subfield->{marc_value}->{readonly}, undef, "subfields to allowed are not marked as readonly" );
287     ($subfield) = grep { $_->{kohafield} eq 'items.copynumber' } @$subfields;
288     is( $subfield, undef, "subfield that is not in the allow list is not returned" );
289 };
290
291
292 $cache->clear_from_cache("MarcStructure-0-");
293 $cache->clear_from_cache("MarcStructure-1-");
294 $cache->clear_from_cache("default_value_for_mod_marc-");
295 $cache->clear_from_cache("MarcSubfieldStructure-");
296
297 sub setup_mss {
298
299     my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
300
301     Koha::MarcSubfieldStructures->search(
302         {
303             frameworkcode => '',
304             tagfield => $itemtag,
305             tagsubfield => 'é',
306         }
307     )->delete;    # In case it exist already
308
309     Koha::MarcSubfieldStructure->new(
310         {
311             frameworkcode => '',
312             tagfield      => $itemtag,
313             tagsubfield   => 'é',
314             kohafield     => undef,
315             repeatable    => 1,
316             defaultvalue  => 'ééé',
317             tab           => 10,
318         }
319     )->store;
320
321     Koha::MarcSubfieldStructures->search(
322         {
323             frameworkcode => '',
324             tagfield      => $itemtag,
325             tagsubfield   => [ 'x' ]
326         }
327     )->update( { kohafield => undef } );
328
329     Koha::MarcSubfieldStructures->search(
330         {
331             frameworkcode => '',
332             tagfield => $itemtag,
333             tagsubfield => [ 'x', 'é' ],
334         }
335     )->update( { repeatable => 1 } );
336
337     Koha::MarcSubfieldStructures->search(
338         {
339             frameworkcode => '',
340             tagfield => $itemtag,
341             tagsubfield => ['t'],
342         }
343     )->update( { repeatable => 0 } );
344
345     Koha::MarcSubfieldStructures->search(
346         {
347             frameworkcode => '',
348             tagfield => $itemtag,
349             tagsubfield => ['z'],
350         }
351     )->update( { kohafield => 'items.itemnotes', repeatable => 1 } );
352
353     Koha::MarcSubfieldStructures->search(
354         {
355             frameworkcode => '',
356             tagfield => $itemtag,
357         }
358     )->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );
359 }