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