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=> 8;
27 use Koha::Item::Attributes;
28 use Koha::MarcSubfieldStructures;
30 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
38 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
40 # 952 $x $é $y are not linked with a kohafield
41 # $952$x $é repeatable
42 # $952$t is not repeatable
43 # 952$z is linked with items.itemnotes and is repeatable
44 # 952$t is linked with items.copynumber and is not repeatable
47 my $biblio = $builder->build_sample_biblio({ frameworkcode => '' });
48 my $item = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
50 my $items = Koha::Items->search({itemnumber => $item->itemnumber});
52 subtest 'MARC subfield linked with kohafield' => sub {
55 $items->batch_update({
56 new_values => {itemnotes => 'new note'}
60 $item = $item->get_from_storage;
61 is( $item->itemnotes, 'new note' );
62 is( $item->as_marc_field->subfield('t'), undef );
64 is( $items->batch_update({
65 new_values => {itemnotes=> 'another note'}
66 })->count, 1, 'Can be chained');
69 $items->batch_update({new_values => {itemnotes=> undef }})->reset;
70 $item = $item->get_from_storage;
71 is( $item->itemnotes, undef, "blank" );
72 is( $item->as_marc_field->subfield('t'), undef, '' );
74 $items->batch_update({new_values => {itemnotes=> 'new note', copynumber => 'new copynumber'}})->reset;
75 $item = $item->get_from_storage;
76 is( $item->itemnotes, 'new note', "multi" );
77 is( $item->as_marc_field->subfield('z'), 'new note', '' );
78 is( $item->copynumber, 'new copynumber', "multi" );
79 is( $item->as_marc_field->subfield('t'), 'new copynumber', '' );
82 subtest 'More marc subfields (no linked)' => sub {
85 $items->batch_update({new_values => {x => 'new xxx' }})->reset;
86 is( $item->get_from_storage->as_marc_field->subfield('x'), 'new xxx' );
89 subtest 'repeatable' => sub {
92 subtest 'linked' => sub {
95 $items->batch_update({new_values => {itemnotes => 'new zzz 1|new zzz 2' }})->reset;
96 is( $item->get_from_storage->itemnotes, 'new zzz 1|new zzz 2');
97 is_deeply( [$item->get_from_storage->as_marc_field->subfield('z')], ['new zzz 1', 'new zzz 2'], 'z is repeatable' );
99 $items->batch_update({new_values => {copynumber => 'new ttt 1|new ttt 2' }})->reset;
100 is( $item->get_from_storage->copynumber, 'new ttt 1|new ttt 2');
101 is_deeply( [$item->get_from_storage->as_marc_field->subfield('t')], ['new ttt 1|new ttt 2'], 't is not repeatable' );
104 subtest 'not linked' => sub {
107 $items->batch_update({new_values => {x => 'new xxx 1|new xxx 2' }})->reset;
108 is_deeply( [$item->get_from_storage->as_marc_field->subfield('x')], ['new xxx 1', 'new xxx 2'], 'i is repeatable' );
110 $items->batch_update({new_values => {y => 'new yyy 1|new yyy 2' }})->reset;
111 is_deeply( [$item->get_from_storage->as_marc_field->subfield('y')], ['new yyy 1|new yyy 2'], 'y is not repeatable' );
115 subtest 'blank' => sub {
118 $items->batch_update(
121 itemnotes => 'new notes 1|new notes 2',
122 copynumber => 'new cn 1|new cn 2',
123 x => 'new xxx 1|new xxx 2',
124 y => 'new yyy 1|new yyy 2',
130 $items->batch_update(
140 $item = $item->get_from_storage;
141 is( $item->itemnotes, undef );
142 is( $item->copynumber, undef );
143 is( $item->as_marc_field->subfield('x'), undef );
144 is_deeply( [ $item->as_marc_field->subfield('y') ],
145 ['new yyy 1|new yyy 2'] );
147 $items->batch_update(
155 is( $item->get_from_storage->more_subfields_xml, undef );
159 subtest 'regex' => sub {
162 $items->batch_update(
165 itemnotes => 'new notes 1|new notes 2',
166 copynumber => 'new cn 1|new cn 2',
167 x => 'new xxx 1|new xxx 2',
168 y => 'new yyy 1|new yyy 2',
176 replace => 'awesome',
179 $items->batch_update(
182 { itemnotes => $re, copynumber => $re, x => $re, y => $re }
185 $item = $item->get_from_storage;
186 is( $item->itemnotes, 'awesome notes 1|new notes 2' );
188 [ $item->as_marc_field->subfield('z') ],
189 [ 'awesome notes 1', 'new notes 2' ],
193 is( $item->copynumber, 'awesome cn 1|new cn 2' );
194 is_deeply( [ $item->as_marc_field->subfield('t') ],
195 ['awesome cn 1|new cn 2'], 't is not repeatable' );
198 [ $item->as_marc_field->subfield('x') ],
199 [ 'awesome xxx 1', 'new xxx 2' ],
204 [ $item->as_marc_field->subfield('y') ],
205 ['awesome yyy 1|new yyy 2'],
206 'y is not repeatable'
210 search => '(awesome)',
214 $items->batch_update(
217 { itemnotes => $re, copynumber => $re, x => $re, y => $re }
220 $item = $item->get_from_storage;
221 is( $item->itemnotes, 'awesomeness notes 1|new notes 2' );
223 [ $item->as_marc_field->subfield('z') ],
224 [ 'awesomeness notes 1', 'new notes 2' ],
228 is( $item->copynumber, 'awesomeness cn 1|new cn 2' );
229 is_deeply( [ $item->as_marc_field->subfield('t') ],
230 ['awesomeness cn 1|new cn 2'], 't is not repeatable' );
233 [ $item->as_marc_field->subfield('x') ],
234 [ 'awesomeness xxx 1', 'new xxx 2' ],
239 [ $item->as_marc_field->subfield('y') ],
240 ['awesomeness yyy 1|new yyy 2'],
241 'y is not repeatable'
245 subtest 'encoding' => sub {
248 $items->batch_update({
249 new_values => { 'é' => 'new note é'}
253 $item = $item->get_from_storage;
254 is( $item->as_marc_field->subfield('é'), 'new note é', );
257 subtest 'mark_items_returned' => sub {
260 my $circ = Test::MockModule->new( 'C4::Circulation' );
261 $circ->mock( 'MarkIssueReturned', sub {
262 warn "MarkIssueReturned";
265 my $issue = $builder->build_object({class => 'Koha::Checkouts'});
266 my $items = Koha::Items->search({ itemnumber => $issue->itemnumber });
269 { $items->batch_update({new_values => {},mark_items_returned => 1}) }
270 qq{MarkIssueReturned},
271 "MarkIssueReturned called for item";
276 { $items->batch_update({new_values => {},mark_items_returned => 0}) }
278 "MarkIssueReturned not called for item";
282 subtest 'report' => sub {
286 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
288 $builder->build_sample_item( { biblionumber => $biblio->biblionumber } );
290 my $items = Koha::Items->search(
291 { itemnumber => [ $item_1->itemnumber, $item_2->itemnumber ] } );
293 my ($report) = $items->batch_update(
295 new_values => { itemnotes => 'new note' }
302 modified_itemnumbers =>
303 [ $item_1->itemnumber, $item_2->itemnumber ],
308 ($report) = $items->batch_update(
310 new_values => { itemnotes => 'new note', copynumber => 'new cn' }
318 modified_itemnumbers =>
319 [ $item_1->itemnumber, $item_2->itemnumber ],
324 $item_1->get_from_storage->update( { itemnotes => 'not new note' } );
325 ($report) = $items->batch_update(
327 new_values => { itemnotes => 'new note', copynumber => 'new cn' }
335 modified_itemnumbers => [ $item_1->itemnumber ],
340 ($report) = $items->batch_update(
342 new_values => { x => 'new xxx', y => 'new yyy' }
350 modified_itemnumbers =>
351 [ $item_1->itemnumber, $item_2->itemnumber ],
358 replace => 'awesome',
362 $item_2->get_from_storage->update( { itemnotes => 'awesome note' } );
363 ($report) = $items->batch_update(
366 { itemnotes => $re, copynumber => $re, x => $re, y => $re }
374 modified_itemnumbers =>
375 [ $item_1->itemnumber, $item_2->itemnumber ],
382 Koha::Caches->get_instance->clear_from_cache( "MarcStructure-1-" );
386 my ( $itemtag, $itemsubfield ) = C4::Biblio::GetMarcFromKohaField( "items.itemnumber" );
388 Koha::MarcSubfieldStructures->search(
391 tagfield => $itemtag,
394 )->delete; # In case it exist already
396 Koha::MarcSubfieldStructure->new(
399 tagfield => $itemtag,
407 Koha::MarcSubfieldStructures->search(
410 tagfield => $itemtag,
411 tagsubfield => [ 'x', 'y' ]
413 )->update( { kohafield => undef } );
415 Koha::MarcSubfieldStructures->search(
418 tagfield => $itemtag,
419 tagsubfield => [ 'x', 'é' ],
421 )->update( { repeatable => 1 } );
423 Koha::MarcSubfieldStructures->search(
426 tagfield => $itemtag,
427 tagsubfield => ['t'],
429 )->update( { repeatable => 0 } );
431 Koha::MarcSubfieldStructures->search(
434 tagfield => $itemtag,
435 tagsubfield => ['z'],
437 )->update( { kohafield => 'items.itemnotes', repeatable => 1 } );
439 Koha::MarcSubfieldStructures->search(
442 tagfield => $itemtag,
444 )->update( { display_order => \['FLOOR( 1 + RAND( ) * 10 )'] } );