Bug 31196: Remove 'default_value_for_mod_marc-' clear_from_cache calls
[koha.git] / t / db_dependent / MarcModificationTemplates.t
1 #!/usr/bin/perl
2
3 use Modern::Perl;
4
5 use Test::More tests => 129;
6
7 use Koha::Database;
8 use Koha::SimpleMARC;
9
10 use t::lib::Mocks;
11
12 use_ok("MARC::Field");
13 use_ok("MARC::Record");
14 use_ok('C4::MarcModificationTemplates', qw( AddModificationTemplate AddModificationTemplateAction GetModificationTemplateAction GetModificationTemplateActions ModModificationTemplateAction MoveModificationTemplateAction DelModificationTemplate DelModificationTemplateAction ModifyRecordWithTemplate GetModificationTemplates ));
15
16 my $schema = Koha::Database->new->schema;
17 $schema->storage->txn_begin;
18 my $dbh = C4::Context->dbh;
19
20 $dbh->do(q|DELETE FROM marc_modification_templates|);
21
22 # Creation
23 my $template_id = AddModificationTemplate("template_name");
24 like( $template_id, qr|^\d+$|, "new template returns an id" );
25
26 is( AddModificationTemplateAction(
27     $template_id, 'move_field', 1,
28     '464', 'u', '', '464', '3',
29     '', '', '',
30     '', '', '', '', '', '',
31     'move first 464$u to 464$3'
32 ), 1, "Add first action");
33
34 is( AddModificationTemplateAction(
35     $template_id, 'update_field', 0,
36     '099', 't', 'LIV', '', '',
37     '', '', '',
38     'if', '200', 'b', 'equals', 'Text', '',
39     'Update field 099$t with value LIV if 200$b matches "Text"'
40 ), 1, "Add second action");
41
42 is( AddModificationTemplateAction(
43     $template_id, 'copy_field', 0,
44     '606', 'a', '', '607', 'a',
45     '', '', '',
46     'unless', '606', 'a', 'not_equals', '^AJAX', '1',
47     'Copy field 606$a to 607$a unless 606$a matches RegEx m^AJAX'
48 ), 1, "Add third action");
49
50 is( AddModificationTemplateAction(
51     $template_id, 'add_field', 0,
52     '650', 'a', 'Additional', '', '',
53     '', '', '',
54     'unless', '650', 'a', 'exists', '', '',
55     'Add field 650$aAdditional unless 650$a exists'
56 ), 1, "Add fourth action");
57 # Getter
58
59 my @actions = GetModificationTemplateActions( $template_id );
60 is( @actions, 4, "4 actions are inserted");
61
62 for my $action ( @actions ) {
63     isnt( GetModificationTemplateAction( $action->{mmta_id} ), undef, "action with id $action->{mmta_id} exists" );
64 }
65
66 my $first_action = $actions[0];
67 is( $first_action->{ordering}, 1, "test ordering for first action" );
68 is( $first_action->{action}, 'move_field', "test action for first action" );
69 is( $first_action->{from_field}, '464', "test from_field for first action" );
70 is( $first_action->{from_subfield}, 'u', "test from_subfield for first action" );
71 is( $first_action->{to_field}, '464', "test to_field for first action" );
72 is( $first_action->{to_subfield}, '3', "test to_subfield for first action" );
73
74 my $second_action = $actions[1];
75 is( $second_action->{ordering}, 2, "test ordering for second action" );
76 is( $second_action->{action}, 'update_field', "test action for second action" );
77 is( $second_action->{from_field}, '099',"test from_field for second action" );
78 is( $second_action->{from_subfield}, 't', "test from_subfield for second action" );
79 is( $second_action->{field_value}, 'LIV', "test firld_value for second action" );
80 is( $second_action->{to_field}, '', "test to_field for second action" );
81 is( $second_action->{to_subfield}, '', "test to_subfield for second action" );
82 is( $second_action->{conditional}, 'if', "test conditional for second action" );
83 is( $second_action->{conditional_field}, '200', "test conditional_field for second action" );
84 is( $second_action->{conditional_subfield}, 'b', "test conditional_subfield for second action" );
85 is( $second_action->{conditional_comparison}, 'equals', "test conditional_comparison for second action" );
86
87 my $third_action = $actions[2];
88 is( $third_action->{ordering}, 3, "test ordering for third action" );
89 is( $third_action->{action}, 'copy_field', "test action for third action" );
90 is( $third_action->{from_field}, '606', "test from_field for third action" );
91 is( $third_action->{from_subfield}, 'a', "test from_subfield for third action" );
92 is( $third_action->{to_field}, '607', "test to_field for third action" );
93 is( $third_action->{to_subfield}, 'a', "test to_subfield for third action" );
94 is( $third_action->{conditional}, 'unless', "test conditional for third action" );
95 is( $third_action->{conditional_field}, '606', "test conditional_field for third action" );
96 is( $third_action->{conditional_subfield}, 'a', "test conditional_subfield for third action" );
97 is( $third_action->{conditional_comparison}, 'not_equals', "test conditional_comparison for third action" );
98 is( $third_action->{conditional_value}, '^AJAX', "test conditional_value for third action" );
99
100 my $fourth_action = $actions[3];
101 is( $fourth_action->{ordering}, 4, "test ordering for fourth action" );
102 is( $fourth_action->{action}, 'add_field', "test action for fourth action" );
103 is( $fourth_action->{from_field}, '650', "test from_field for fourth action" );
104 is( $fourth_action->{from_subfield}, 'a', "test from_subfield for fourth action" );
105 is( $fourth_action->{to_field}, '', "test to_field for fourth action" );
106 is( $fourth_action->{to_subfield}, '', "test to_subfield for fourth action" );
107 is( $fourth_action->{conditional}, 'unless', "test conditional for fourth action" );
108 is( $fourth_action->{conditional_field}, '650', "test conditional_field for fourth action" );
109 is( $fourth_action->{conditional_subfield}, 'a', "test conditional_subfield for fourth action" );
110 is( $fourth_action->{conditional_comparison}, 'exists', "test conditional_comparison for fourth action" );
111 is( $fourth_action->{conditional_value}, '', "test conditional_value for fourth action" );
112
113 # Modifications
114 is( ModModificationTemplateAction(
115     $actions[1]->{mmta_id}, 'update_field', 0,
116     '100', 'u', 'LIV', '', '',
117     '', '', '',
118     'if', '200', 'c', 'equals', 'Text', '',
119     'Update field 099$t with value LIV if 200$b matches "Text"'
120 ), 1, "Modify second action");
121
122 $second_action = GetModificationTemplateAction( $actions[1]->{mmta_id} );
123 is( $second_action->{ordering}, 2, "test ordering for second action modified" );
124 is( $second_action->{action}, 'update_field', "test action for second action modified" );
125 is( $second_action->{from_field}, '100',"test from_field for second action modified" );
126 is( $second_action->{from_subfield}, 'u', "test from_subfield for second action modified" );
127 is( $second_action->{field_value}, 'LIV', "test firld_value for second action modified" );
128 is( $second_action->{to_field}, '', "test to_field for second action modified" );
129 is( $second_action->{to_subfield}, '', "test to_subfield for second action modified" );
130 is( $second_action->{conditional}, 'if', "test conditional for second action modified" );
131 is( $second_action->{conditional_field}, '200', "test conditional_field for second action modified" );
132 is( $second_action->{conditional_subfield}, 'c', "test conditional_subfield for second action modified" );
133 is( $second_action->{conditional_comparison}, 'equals', "test conditional_comparison for second action modified" );
134
135 # Up and down
136 is( MoveModificationTemplateAction( $actions[2]->{mmta_id}, 'top' ), '1', 'Move the third action on top' );
137 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'bottom' ), '1', 'Move the first action on bottom' );
138
139 is( GetModificationTemplateAction( $actions[0]->{mmta_id} )->{ordering}, '4', 'First becomes fourth' );
140 is( GetModificationTemplateAction( $actions[1]->{mmta_id} )->{ordering}, '2', 'Second stays second' );
141 is( GetModificationTemplateAction( $actions[2]->{mmta_id} )->{ordering}, '1', 'Third becomes first' );
142 is( GetModificationTemplateAction( $actions[3]->{mmta_id} )->{ordering}, '3', 'Fourth becomes third' );
143
144 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'up' ), '1', 'Move up the first action (was fourth)' );
145 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'up' ), '1', 'Move up the first action (was third)' );
146 is( MoveModificationTemplateAction( $actions[2]->{mmta_id}, 'down' ), '1', 'Move down the third action (was first)' );
147
148 is( GetModificationTemplateAction( $actions[0]->{mmta_id} )->{ordering}, '1', 'First becomes again first' );
149 is( GetModificationTemplateAction( $actions[1]->{mmta_id} )->{ordering}, '3', 'Second becomes third' );
150 is( GetModificationTemplateAction( $actions[2]->{mmta_id} )->{ordering}, '2', 'Third becomes second' );
151 is( GetModificationTemplateAction( $actions[3]->{mmta_id} )->{ordering}, '4', 'Fourth becomes again fourth' );
152
153 # Cleaning
154 is( DelModificationTemplateAction( $actions[0]->{mmta_id} ), 3, "Delete the first action, 2 others are reordered" );
155 is( GetModificationTemplateAction( $actions[0]->{mmta_id} ), undef, "first action does not exist anymore" );
156
157 is( DelModificationTemplate( $template_id ), 1, "The template has been deleted" );
158
159 is( GetModificationTemplateAction( $actions[1]->{mmta_id} ), undef, "second action does not exist anymore" );
160 is( GetModificationTemplateAction( $actions[2]->{mmta_id} ), undef, "third action does not exist anymore" );
161 is( GetModificationTemplateAction( $actions[3]->{mmta_id} ), undef, "fourth action does not exist anymore" );
162
163 is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" );
164
165 # ModifyRecordWithTemplate
166 t::lib::Mocks::mock_userenv();
167
168 $template_id = AddModificationTemplate("new_template_test");
169 like( $template_id, qr|^\d+$|, "new template returns an id" );
170
171 is( AddModificationTemplateAction(
172     $template_id, 'delete_field', 0,
173     '245', '', '', '', '',
174     '', '', '',
175     'if', '245', 'a', 'equals', 'Bad title', '',
176     'Delete field 245 if 245$a eq "Bad title"'
177 ), 1, 'Add first action: delete field 245 if 245$a eq "Bad title"');
178
179 is( AddModificationTemplateAction(
180     $template_id, 'copy_field', 0,
181     '245', 'a', '', '246', 'a',
182     '', '', '',
183     '', '', '', '', '', '',
184     'copy field 245$a to 246$a'
185 ), 1, 'Add second action: copy 245$a to 246$a');
186
187 is( AddModificationTemplateAction(
188     $template_id, 'delete_field', 0,
189     '650', 'a', '', '', '',
190     '', '', '',
191     'if', '650', '9', 'equals', '462', '',
192     'Delete field 650$a if 650$9=462'
193 ), 1, 'Add third action: delete field 650$a if 650$9=462');
194
195 is( AddModificationTemplateAction(
196     $template_id, 'update_field', 0,
197     '952', 'p', '3010023917_updated', '', '',
198     '', '', '',
199     'unless', '650', '9', 'equals', '42', '',
200     'Update field 952$p with "3010023917_updated" if 650$9 != 42'
201 ), 1, 'Add fourth action: update field 952$p with "3010023917_updated" if 650$9 != 42');
202
203 is( AddModificationTemplateAction(
204     $template_id, 'move_field', 0,
205     '952', 'd', '', '952', 'e',
206     '', '', '',
207     'if', '952', 'c', 'equals', '^GEN', '1',
208     'Move field 952$d to 952$e if 952$c =~ /^GEN/'
209 ), 1, 'Add fifth action: move field 952$d to 952$e if 952$c =~ /^GEN/');
210
211 is( AddModificationTemplateAction(
212     $template_id, 'update_field', 0,
213     '650', 'a', 'Computer algorithms.', '', '',
214     '', '', '',
215     'if', '650', '9', 'equals', '499', '',
216     'Update field 650$a with "Computer algorithms." to 651 if 650$9 == 499'
217 ), 1, 'Add sixth action: update field 650$a with "Computer algorithms." if 650$9 == 499');
218
219 is( AddModificationTemplateAction(
220     $template_id, 'move_field', 0,
221     '650', '', '', '651', '',
222     '', '', '',
223     'if', '650', '9', 'equals', '499', '',
224     'Move field 650 to 651 if 650$9 == 499'
225 ), 1, 'Add seventh action: move field 650 to 651 if 650$9 == 499');
226
227 is( AddModificationTemplateAction(
228     $template_id, 'update_field', 0,
229     '999', 'a', 'non existent.', '', '',
230     '', '', '',
231     '', '', '', '', '', '',
232     'Update non existent field 999$a with "non existent"'
233 ), 1, 'Add eighth action: update field non existent 999$a with "non existent."');
234
235 is( AddModificationTemplateAction(
236     $template_id, 'update_field', 0,
237     '999', 'a', 'existent - updated.', '', '',
238     '', '', '',
239     '', '', '', '', '', '',
240     'Update existent field 999$a with "existent - updated."'
241 ), 1, 'Add ninth action: update field non existent 999$a with "existent - updated."');
242
243 is( AddModificationTemplateAction(
244     $template_id, 'add_field', 0,
245     '999', 'a', 'additional existent.', '', '',
246     '', '', '',
247     '', '', '', '', '', '',
248     'Add new existent field 999$a with "additional existent"'
249 ), 1, 'Add tenth action: add additional field existent 999$a with "additional existent."');
250
251 is( AddModificationTemplateAction(
252     $template_id, 'add_field', 0,
253     '007', '', 'vxcdq', '', '',
254     '', '', '',
255     '', '', '', '', '', '',
256     'Add new existent field 999$a with "additional existent"'
257 ), 1, 'Add eleventh action: add additional field existent 007');
258
259 my $record = new_record();
260 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
261
262 my $expected_record = expected_record_1();
263 is_deeply( $record, $expected_record, "Record modification as expected");
264
265 $template_id = AddModificationTemplate("another_template_test");
266
267 # Duplicate 245 => 3x245
268 is( AddModificationTemplateAction(
269     $template_id, 'copy_field', 0,
270     '245', '', '', '245', '',
271     '', '', '',
272     'if', '245', 'a', 'equals', 'Bad title', '',
273     'Copy field 245 if 245$a eq "Bad title"'
274 ), 1, 'Add action: copy field 245 if 245$a eq "Bad title"');
275
276 $record = new_record();
277 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
278
279 my @fields_245a = Koha::SimpleMARC::read_field({
280     record => $record,
281     field => '245',
282     subfield => 'a',
283 });
284 is_deeply( \@fields_245a, [
285         'The art of computer programming',
286         'Bad title',
287         'Bad title',
288     ], 'Copy field has copied the "Bad title"' );
289
290 # Update first "Bad title"
291 is( AddModificationTemplateAction(
292     $template_id, 'update_field', 1,
293     '245', 'a', 'Bad title updated', '', '',
294     '', '', '',
295     'if', '245', 'a', 'equals', 'Bad title', '',
296     'Update first 245$a matching "Bad title" with "Bad title updated"'
297 ), 1, 'Add action: update field 245$a matching "Bad title" with "Bad title updated');
298
299 $record = new_record();
300 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
301
302 @fields_245a = Koha::SimpleMARC::read_field({
303     record => $record,
304     field => '245',
305     subfield => 'a',
306 });
307 is_deeply( \@fields_245a, [
308         'The art of computer programming',
309         'Bad title updated',
310         'Bad title',
311     ], 'update_field has update first the "Bad title"' );
312
313 # Duplicate first 245 => 3x245
314 is( AddModificationTemplateAction(
315     $template_id, 'copy_field', 1,
316     '245', '', '', '245', '',
317     '', '', '',
318     'if', '245', 'a', 'equals', '^Bad title', '1',
319     'Copy field 245 if 245$a =~ "^Bad title"'
320 ), 1, 'Add action: copy field 245 if 245$a =~ "^Bad title"');
321
322 $record = new_record();
323 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
324
325 @fields_245a = Koha::SimpleMARC::read_field({
326     record => $record,
327     field => '245',
328     subfield => 'a',
329 });
330 is_deeply( \@fields_245a, [
331         'The art of computer programming',
332         'Bad title updated',
333         'Bad title',
334         'Bad title updated',
335     ], 'Copy field has copied first "^Bad title"' );
336
337 # Delete first ^Bad title
338 is( AddModificationTemplateAction(
339     $template_id, 'delete_field', 1,
340     '245', '', '', '', '',
341     '', '', '',
342     'if', '245', 'a', 'equals', '^Bad title', '1',
343     'Delete first 245$a mathing ^Bad title'
344 ), 1, 'Delete first 245$a mathing ^Bad title');
345
346 $record = new_record();
347 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
348 @fields_245a = Koha::SimpleMARC::read_field({
349     record => $record,
350     field => '245',
351     subfield => 'a',
352 });
353 is_deeply( \@fields_245a, [
354         'The art of computer programming',
355         'Bad title',
356         'Bad title updated',
357     ], 'delete field has been deleted the right field"' );
358
359 is( AddModificationTemplateAction(
360     $template_id, 'delete_field', 0,
361     '245', '', '', '', '',
362     '', '', '',
363     'if', '245', 'a', 'equals', 'updated$', '1',
364     'Delete first 245$a mathing updated$'
365 ), 1, 'Delete first 245$a mathing updated$');
366
367 $record = new_record();
368 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
369 @fields_245a = Koha::SimpleMARC::read_field({
370     record => $record,
371     field => '245',
372     subfield => 'a',
373 });
374 is_deeply( \@fields_245a, [
375         'The art of computer programming',
376         'Bad title'
377     ], 'delete field has been deleted the right field"' );
378
379 subtest 'GetModificationTemplates' => sub {
380     plan tests => 1;
381     $dbh->do(q|DELETE FROM marc_modification_templates|);
382     AddModificationTemplate("zzz");
383     AddModificationTemplate("aaa");
384     AddModificationTemplate("mmm");
385     my @templates = GetModificationTemplates();
386     is_deeply( [map{$_->{name}} @templates], ['aaa', 'mmm', 'zzz'] );
387 };
388
389 subtest "not_equals" => sub {
390     plan tests => 2;
391     $dbh->do(q|DELETE FROM marc_modification_templates|);
392     my $template_id = AddModificationTemplate("template_name");
393     AddModificationTemplateAction(
394         $template_id, 'move_field', 0,
395         '650', '', '', '651', '',
396         '', '', '',
397         'if', '650', '9', 'not_equals', '499', '',
398         'Move field 650 to 651 if 650$9 != 499'
399     );
400     my $record = new_record();
401     ModifyRecordWithTemplate( $template_id, $record );
402     my $expected_record = expected_record_2();
403     is_deeply( $record, $expected_record, '650 has been moved to 651 when 650$9 != 499' );
404
405     $dbh->do(q|DELETE FROM marc_modification_templates|);
406     $template_id = AddModificationTemplate("template_name");
407     AddModificationTemplateAction(
408         $template_id, 'move_field', 0,
409         '650', '', '', '651', '',
410         '', '', '',
411         'if', '650', 'b', 'not_equals', '499', '',
412         'Move field 650 to 651 if 650$b != 499'
413     );
414     $record = new_record();
415     ModifyRecordWithTemplate( $template_id, $record );
416     $expected_record = new_record();
417     is_deeply( $record, $expected_record, 'None 650 have been moved, no $650$b exists' );
418 };
419
420 subtest "when conditional field doesn't match the from field" => sub {
421     plan tests => 3;
422     $dbh->do(q|DELETE FROM marc_modification_templates|);
423     my $template_id = AddModificationTemplate("template_name");
424     AddModificationTemplateAction(
425         $template_id, 'delete_field', 0,
426         '650', '9', '', '', '',
427         '', '', '',
428         'if', '245', 'a', 'equals', 'Bad title', '',
429         'Delete fields 650$9 if 245$a == "Bad title"'
430     );
431     my $record = new_record();
432     ModifyRecordWithTemplate( $template_id, $record );
433     my $expected_record = expected_record_3();
434     is_deeply( $record, $expected_record, '650$9 fields have been deleted when 245$a == "Bad title"' );
435
436     $dbh->do(q|DELETE FROM marc_modification_templates|);
437     $template_id = AddModificationTemplate("template_name");
438     AddModificationTemplateAction(
439         $template_id, 'delete_field', 0,
440         '650', '9', '', '', '',
441         '', '', '',
442         'if', '245', 'a', 'exists', '', '',
443         'Delete fields 650$9 if 245$a exists'
444     );
445     $record = new_record();
446     ModifyRecordWithTemplate( $template_id, $record );
447     $expected_record = expected_record_3();
448     is_deeply( $record, $expected_record, '650$9 fields have been deleted because 245$a exists' );
449
450     $dbh->do(q|DELETE FROM marc_modification_templates|);
451     $template_id = AddModificationTemplate("template_name");
452     AddModificationTemplateAction(
453         $template_id, 'delete_field', 1,
454         '650', '', '', '', '',
455         '', '', '',
456         'if', '245', 'a', 'exists', '', '',
457         'Delete 1st field 650 if 245$a exists'
458     );
459     $record = new_record();
460     ModifyRecordWithTemplate( $template_id, $record );
461     $expected_record = expected_record_4();
462     is_deeply( $record, $expected_record, '1st 650 field has been deleted because 245$a exists' );
463 };
464
465 sub new_record {
466     my $record = MARC::Record->new;
467     $record->leader('03174nam a2200445 a 4500');
468     my @fields = (
469         MARC::Field->new(
470             100, '1', ' ',
471             a => 'Knuth, Donald Ervin',
472             d => '1938',
473         ),
474         MARC::Field->new(
475             245, '1', '4',
476             a => 'The art of computer programming',
477             c => 'Donald E. Knuth.',
478         ),
479         MARC::Field->new(
480             245, '1', '4',
481             a => 'Bad title',
482             c => 'Donald E. Knuth.',
483         ),
484         MARC::Field->new(
485             650, ' ', '0',
486             a => 'Computer programming.',
487             9 => '462',
488         ),
489         MARC::Field->new(
490             650, ' ', '0',
491             a => 'Computer programming.',
492             9 => '499',
493         ),
494         MARC::Field->new(
495             952, ' ', ' ',
496             p => '3010023917',
497             y => 'BK',
498             c => 'GEN',
499             d => '2001-06-25',
500         ),
501     );
502     $record->append_fields(@fields);
503     return $record;
504 }
505
506 sub expected_record_1 {
507     my $record = MARC::Record->new;
508     $record->leader('03174nam a2200445 a 4500');
509     my @fields = (
510         MARC::Field->new(
511             100, '1', ' ',
512             a => 'Knuth, Donald Ervin',
513             d => '1938',
514         ),
515         MARC::Field->new(
516             245, '1', '4',
517             a => 'The art of computer programming',
518             c => 'Donald E. Knuth.',
519         ),
520         MARC::Field->new(
521             650, ' ', '0',
522             9 => '462',
523         ),
524         MARC::Field->new(
525             952, ' ', ' ',
526             p => '3010023917_updated',
527             y => 'BK',
528             c => 'GEN',
529             e => '2001-06-25',
530         ),
531         MARC::Field->new(
532             246, '', ' ',
533             a => 'The art of computer programming',
534         ),
535         MARC::Field->new(
536             651, ' ', '0',
537             a => 'Computer algorithms.',
538             9 => '499',
539         ),
540         MARC::Field->new(
541             999, ' ', ' ',
542             a => 'existent - updated.',
543         ),
544         MARC::Field->new(
545             999, ' ', ' ',
546             a => 'additional existent.',
547         ),
548         MARC::Field->new(
549            '007', 'vxcdq',
550         ),
551     );
552     $record->append_fields(@fields);
553     return $record;
554 }
555
556 sub expected_record_2 {
557     my $record = MARC::Record->new;
558     $record->leader('03174nam a2200445 a 4500');
559     my @fields = (
560         MARC::Field->new(
561             100, '1', ' ',
562             a => 'Knuth, Donald Ervin',
563             d => '1938',
564         ),
565         MARC::Field->new(
566             245, '1', '4',
567             a => 'The art of computer programming',
568             c => 'Donald E. Knuth.',
569         ),
570         MARC::Field->new(
571             245, '1', '4',
572             a => 'Bad title',
573             c => 'Donald E. Knuth.',
574         ),
575         MARC::Field->new(
576             650, ' ', '0',
577             a => 'Computer programming.',
578             9 => '499',
579         ),
580         MARC::Field->new(
581             952, ' ', ' ',
582             p => '3010023917',
583             y => 'BK',
584             c => 'GEN',
585             d => '2001-06-25',
586         ),
587         MARC::Field->new(
588             651, ' ', '0',
589             a => 'Computer programming.',
590             9 => '462',
591         ),
592     );
593     $record->append_fields(@fields);
594     return $record;
595 }
596
597 sub expected_record_3 {
598     my $record = MARC::Record->new;
599     $record->leader('03174nam a2200445 a 4500');
600     my @fields = (
601         MARC::Field->new(
602             100, '1', ' ',
603             a => 'Knuth, Donald Ervin',
604             d => '1938',
605         ),
606         MARC::Field->new(
607             245, '1', '4',
608             a => 'The art of computer programming',
609             c => 'Donald E. Knuth.',
610         ),
611         MARC::Field->new(
612             245, '1', '4',
613             a => 'Bad title',
614             c => 'Donald E. Knuth.',
615         ),
616         MARC::Field->new(
617             650, ' ', '0',
618             a => 'Computer programming.',
619         ),
620         MARC::Field->new(
621             650, ' ', '0',
622             a => 'Computer programming.',
623         ),
624         MARC::Field->new(
625             952, ' ', ' ',
626             p => '3010023917',
627             y => 'BK',
628             c => 'GEN',
629             d => '2001-06-25',
630         ),
631     );
632     $record->append_fields(@fields);
633     return $record;
634 }
635
636 sub expected_record_4 {
637     my $record = MARC::Record->new;
638     $record->leader('03174nam a2200445 a 4500');
639     my @fields = (
640         MARC::Field->new(
641             100, '1', ' ',
642             a => 'Knuth, Donald Ervin',
643             d => '1938',
644         ),
645         MARC::Field->new(
646             245, '1', '4',
647             a => 'The art of computer programming',
648             c => 'Donald E. Knuth.',
649         ),
650         MARC::Field->new(
651             245, '1', '4',
652             a => 'Bad title',
653             c => 'Donald E. Knuth.',
654         ),
655         MARC::Field->new(
656             650, ' ', '0',
657             a => 'Computer programming.',
658             9 => '499',
659         ),
660         MARC::Field->new(
661             952, ' ', ' ',
662             p => '3010023917',
663             y => 'BK',
664             c => 'GEN',
665             d => '2001-06-25',
666         ),
667     );
668     $record->append_fields(@fields);
669     return $record;
670 }
671
672 # Tests related to use of subfield 0 ($0)
673
674 sub new_record_0 {
675     my $record = MARC::Record->new;
676     $record->leader('03174nam a2200445 a 4500');
677     my @fields = (
678         MARC::Field->new(
679             100, '1', ' ',
680             0 => '12345',
681             a => 'Knuth, Donald Ervin',
682             d => '1938',
683         ),
684         MARC::Field->new(
685             245, '1', '4',
686             0 => '12345',
687             a => 'The art of computer programming',
688             c => 'Donald E. Knuth.',
689         ),
690         MARC::Field->new(
691             650, ' ', '0',
692             0 => '42',
693             a => 'Computer programming.',
694             9 => '462',
695         ),
696         MARC::Field->new(
697             590, ' ', '0',
698             0 => 'Zeroth',
699             a => 'Appolo',
700         ),
701     );
702     $record->append_fields(@fields);
703     return $record;
704 }
705
706 sub expected_record_0 {
707     my $record = MARC::Record->new;
708     $record->leader('03174nam a2200445 a 4500');
709     my @fields = (
710         MARC::Field->new(
711             100, '1', ' ',
712             a => 'Knuth, Donald Ervin',
713             d => '1938',
714             0 => 'TestUpdated',
715             0 => 'TestUpdated',
716         ),
717         MARC::Field->new(
718             245, '1', '4',
719             0 => '12345',
720             a => 'The art of computer programming',
721             c => 'Donald E. Knuth.',
722         ),
723         MARC::Field->new(
724             650, ' ', '0',
725             0 => '42',
726             a => 'Computer programming.',
727             9 => '462',
728         ),
729         MARC::Field->new(
730             590, ' ', '0',
731             0 => 'Zeroth',
732             a => 'Appolo',
733         ),
734         MARC::Field->new(
735             600, ' ', ' ',
736             0 => 'TestUpdated',
737         ),
738         MARC::Field->new(
739             600, ' ', ' ',
740             0 => 'TestUpdated',
741         ),
742         MARC::Field->new(
743             700, ' ', ' ',
744             0 => '12345',
745         ),
746         MARC::Field->new(
747             690, ' ', ' ',
748             0 => 'Zeroth',
749             a => 'Appolo',
750         ),
751         MARC::Field->new(
752             690, ' ', '0',
753             0 => 'Zeroth',
754             a => 'Appolo',
755         ),
756     );
757     $record->append_fields(@fields);
758     return $record;
759 }
760
761 $record = new_record_0();
762 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
763
764 $template_id = AddModificationTemplate("template_test_subfield_0");
765 like( $template_id, qr|^\d+$|, "new template returns an id" );
766
767 # Delete subfield 100$0
768 is( AddModificationTemplateAction(
769     $template_id, 'delete_field', 0,
770     '100', '0', '', '', '',
771     '', '', '',
772     '', '', '', '', '', '',
773     'Action 1: Delete subfield 100$0'
774 ), 1, 'Action 1: Delete subfield 100$0');
775
776 # Add new subfield 100$0 with value "Test"
777 # This adds a new 100 field to the record
778 is( AddModificationTemplateAction(
779     $template_id, 'add_field', 0,
780     '100', '0', 'Test', '', '',
781     '', '', '',
782     '', '', '', '', '', '',
783     'Action 2: Add new subfield 100$0 with value "Test"'
784 ), 1, 'Action 2: Add new subfield 100$0');
785
786 # Update existing or add new subfield 100$0 with value "TestUpdated"
787 # This updates the new 100 create above, and adds a new 100$0 to the original 100 field
788 is( AddModificationTemplateAction(
789     $template_id, 'update_field', 0,
790     '100', '0', 'TestUpdated', '', '',
791     '', '', '',
792     '', '', '', '', '', '',
793     'Action 3: Update existing or add new subfield 100$0 with value "TestUpdated"'
794 ), 1, 'Action 3: Update existing or add new subfield 100$0 with value "TestUpdated"');
795
796 # Move subfield 100$0 to 600$0
797 # This removes the newly created 100, and removes the 100$0 from the original 100 field
798 # Two 600 fields with a single 0 subfield are created
799 is( AddModificationTemplateAction(
800     $template_id, 'move_field', 0,
801     '100', '0', '', '600', '0',
802     '', '', '',
803     '', '', '', '', '', '',
804     'Action 4: Move subfield 100$0 to 600$0'
805 ), 1, 'Action 4: Move subfield 100$0 to 600$0');
806
807 # Copy subfield 600$0 to 100$0
808 # Copy subfield adds to existing fields if found, so we get two 100$0 on the original field
809 is( AddModificationTemplateAction(
810     $template_id, 'copy_field', 0,
811     '600', '0', '', '100', '0',
812     '', '', '',
813     '', '', '', '', '', '',
814     'Action 5: Copy subfield 600$0 to 100$0'
815 ), 1, 'Action 5: Copy subfield 600$0 to 100$0');
816
817 # Copy and replace subfield 245$0 to 700$0
818 # Copy and replace in this case makes a new 700$0 as it wasn't there
819 is( AddModificationTemplateAction(
820     $template_id, 'copy_and_replace_field', 0,
821     '245', '0', '', '700', '0',
822     '', '', '',
823     '', '', '', '', '', '',
824     'Action 6: Copy and replace subfield 245$0 to 700$0'
825 ), 1, 'Action 6: Copy and replace subfield 245$0 to 700$0');
826
827 # Copy subfield 590$0 to 690$0
828 # Copies the single subfield from 590 to a new 690
829 is( AddModificationTemplateAction(
830     $template_id, 'copy_field', 0,
831     '590', '0', '', '690', '0',
832     '', '', '',
833     '', '', '', '', '', '',
834     'Action 7: Copy subfield 590$0 to 690$0'
835 ), 1, 'Action 7: Copy subfield 590$0 to 690$0');
836
837 # Copy subfield 590$a to 690$a
838 # Copy subfield adds to existing 690 a new subfield a
839 is( AddModificationTemplateAction(
840     $template_id, 'copy_field', 0,
841     '590', 'a', '', '690', 'a',
842     '', '', '',
843     '', '', '', '', '', '',
844     'Action 8: Copy subfield 690$a to 690$a'
845 ), 1, 'Action 8: Copy subfield 690$a to 690$a');
846
847
848 # Copy field 590 to 690
849 # Copy field copies existing to a new 690, does not add to existing
850 is( AddModificationTemplateAction(
851     $template_id, 'copy_field', 0,
852     '590', '', '', '690', '',
853     '', '', '',
854     '', '', '', '', '', '',
855     'Action 9: Copy subfield 590 to 690'
856 ), 1, 'Action 9: Copy subfield 590 to 690');
857
858 my @actions_0 = GetModificationTemplateActions( $template_id );
859 is( @actions_0, 9, "9 actions are inserted");
860
861 ModifyRecordWithTemplate( $template_id, $record );
862 my $expected_record_0 = expected_record_0();
863 is_deeply( $record, $expected_record_0, '100$0 has been deleted, added back, updated, moved to 600$0, and copied back to 100$0; finally, 245$0 has been copied and replaced to 700$0' );