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>.
19 use Test::More tests => 7;
20 use Data::Dumper qw( Dumper );
23 use List::MoreUtils qw( uniq );
26 use Koha::MarcSubfieldStructures;
27 use Koha::UI::Form::Builder::Item;
28 use t::lib::TestBuilder;
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $builder = t::lib::TestBuilder->new;
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-");
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
48 subtest 'authorised values' => sub {
51 my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
53 Koha::UI::Form::Builder::Item->new(
54 { biblionumber => $biblio->biblionumber } )->edit_form;
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' );
59 subtest 'normal AV' => sub {
62 grep { $_->{kohafield} eq 'items.notforloan' } @$subfields;
63 my $avs = Koha::AuthorisedValues->search( { category => 'NOT_LOAN' } );
66 $subfield->{marc_value}->{values},
69 map { $_->authorised_value }
70 sort { $a->lib cmp $b->lib }
73 'AVs are sorted by lib and en empty option is created first'
76 $subfield->{marc_value}->{labels},
78 map { $_->authorised_value => $_->lib }
79 sort { $a->lib cmp $b->lib }
85 subtest 'cn_source' => sub {
87 my ( $subfield ) = grep { $_->{kohafield} eq 'items.cn_source' } @$subfields;
88 is_deeply( $subfield->{marc_value}->{values}, [ '', 'ddc', 'lcc' ] );
90 $subfield->{marc_value}->{labels},
92 ddc => "Dewey Decimal Classification",
93 lcc => "Library of Congress Classification",
97 subtest 'branches' => sub {
99 my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
100 my $libraries = Koha::Libraries->search({}, { order_by => 'branchname' });
102 $subfield->{marc_value}->{values},
103 [ $libraries->get_column('branchcode') ]
106 $subfield->{marc_value}->{labels},
107 { map { $_->branchcode => $_->branchname } $libraries->as_list }
111 subtest 'itemtypes' => sub {
113 my ($subfield) = grep { $_->{kohafield} eq 'items.itype' } @$subfields;
114 my $itemtypes = Koha::ItemTypes->search;
117 $subfield->{marc_value}->{values},
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) }
125 "Item types should be sorted by description and an empty entries should be shown"
127 or diag("Itemtypes details: ".Dumper($subfield->{marc_value}->{values}, [map { $_->itemtype } $itemtypes->as_list]));
129 is_deeply( $subfield->{marc_value}->{labels},
130 { map { $_->itemtype => $_->description } $itemtypes->as_list },
131 'Labels should be correctly displayed'
136 subtest 'prefill_with_default_values' => sub {
139 my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
141 Koha::UI::Form::Builder::Item->new(
142 { biblionumber => $biblio->biblionumber } )->edit_form;
145 my ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
146 is( $subfield->{marc_value}->{value}, '', 'no default value if prefill_with_default_values not passed' );
149 Koha::UI::Form::Builder::Item->new(
150 { biblionumber => $biblio->biblionumber } )->edit_form({ prefill_with_default_values => 1 });
153 ($subfield) = grep { $_->{subfield} eq 'é' } @$subfields;
154 is( $subfield->{marc_value}->{value}, 'ééé', 'default value should be set if prefill_with_default_values passed');
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');
167 subtest 'subfields_to_prefill' => sub {
170 my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
172 my $more_subfields_xml = Koha::Item::Attributes->new({ "é" => "prefill é" })->to_marcxml;
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' );
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');
186 subtest 'branchcode' => sub {
189 my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
190 my $library = $builder->build_object({ class => 'Koha::Libraries' });
192 Koha::UI::Form::Builder::Item->new(
193 { biblionumber => $biblio->biblionumber } )->edit_form;
195 my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
196 is( $subfield->{marc_value}->{default}, '', 'no library preselected if no branchcode passed');
199 Koha::UI::Form::Builder::Item->new(
200 { biblionumber => $biblio->biblionumber } )->edit_form({ branchcode => $library->branchcode });
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');
206 subtest 'default_branches_empty' => sub {
209 my $biblio = $builder->build_sample_biblio({ value => {frameworkcode => ''}});
211 Koha::UI::Form::Builder::Item->new(
212 { biblionumber => $biblio->biblionumber } )->edit_form;
214 my ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
215 isnt( $subfield->{marc_value}->{values}->[0], "", 'No empty option for branches' );
218 Koha::UI::Form::Builder::Item->new(
219 { biblionumber => $biblio->biblionumber } )->edit_form({ default_branches_empty => 1 });
221 ( $subfield ) = grep { $_->{kohafield} eq 'items.homebranch' } @$subfields;
222 is( $subfield->{marc_value}->{values}->[0], "", 'empty option for branches if default_branches_empty passed' );
225 subtest 'kohafields_to_ignore' => sub {
229 $builder->build_sample_biblio( { value => { frameworkcode => '' } } );
231 Koha::UI::Form::Builder::Item->new(
232 { biblionumber => $biblio->biblionumber } )->edit_form;
234 my ($subfield) = grep { $_->{kohafield} eq 'items.barcode' } @$subfields;
235 isnt( $subfield, undef, 'barcode subfield should be in the subfield list' );
238 Koha::UI::Form::Builder::Item->new(
239 { biblionumber => $biblio->biblionumber } )
240 ->edit_form( { kohafields_to_ignore => ['items.barcode'] } );
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'
248 subtest 'subfields_to_allow & ignore_not_allowed_subfields' => sub {
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 => '' } } );
255 Koha::UI::Form::Builder::Item->new(
256 { biblionumber => $biblio->biblionumber } )->edit_form(
258 subfields_to_allow => [
259 sprintf( '%s$%s', $tag_cn, $subtag_cn ),
260 sprintf( '%s$%s', $tag_notes, $subtag_notes )
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" );
272 Koha::UI::Form::Builder::Item->new(
273 { biblionumber => $biblio->biblionumber } )->edit_form(
275 subfields_to_allow => [
276 sprintf( '%s$%s', $tag_cn, $subtag_cn ),
277 sprintf( '%s$%s', $tag_notes, $subtag_notes )
279 ignore_not_allowed_subfields => 1,
283 is( scalar(@$subfields), 2, "With ignore_not_allowed_subfields, only the subfields to ignore are returned" );
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" );
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-");
299 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
301 Koha::MarcSubfieldStructures->search(
304 tagfield => $itemtag,
307 )->delete; # In case it exist already
309 Koha::MarcSubfieldStructure->new(
312 tagfield => $itemtag,
316 defaultvalue => 'ééé',
321 Koha::MarcSubfieldStructures->search(
324 tagfield => $itemtag,
325 tagsubfield => [ 'x' ]
327 )->update( { kohafield => undef } );
329 Koha::MarcSubfieldStructures->search(
332 tagfield => $itemtag,
333 tagsubfield => [ 'x', 'é' ],
335 )->update( { repeatable => 1 } );
337 Koha::MarcSubfieldStructures->search(
340 tagfield => $itemtag,
341 tagsubfield => ['t'],
343 )->update( { repeatable => 0 } );
345 Koha::MarcSubfieldStructures->search(
348 tagfield => $itemtag,
349 tagsubfield => ['z'],
351 )->update( { kohafield => 'items.itemnotes', repeatable => 1 } );
353 Koha::MarcSubfieldStructures->search(
356 tagfield => $itemtag,
358 )->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );