Bug 21777: Add a regression test
[koha.git] / t / db_dependent / MarcModificationTemplates.t
1 use Modern::Perl;
2
3 use Test::More tests => 115;
4
5 use Koha::SimpleMARC;
6
7 use_ok("MARC::Field");
8 use_ok("MARC::Record");
9 use_ok("C4::MarcModificationTemplates");
10
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
14
15 $dbh->do(q|DELETE FROM marc_modification_templates|);
16
17 # Creation
18 my $template_id = AddModificationTemplate("template_name");
19 like( $template_id, qr|^\d+$|, "new template returns an id" );
20
21 is( AddModificationTemplateAction(
22     $template_id, 'move_field', 1,
23     '464', 'u', '', '464', '3',
24     '', '', '',
25     '', '', '', '', '', '',
26     'move first 464$u to 464$3'
27 ), 1, "Add first action");
28
29 is( AddModificationTemplateAction(
30     $template_id, 'update_field', 0,
31     '099', 't', 'LIV', '', '',
32     '', '', '',
33     'if', '200', 'b', 'equals', 'Text', '',
34     'Update field 099$t with value LIV if 200$b matches "Text"'
35 ), 1, "Add second action");
36
37 is( AddModificationTemplateAction(
38     $template_id, 'copy_field', 0,
39     '606', 'a', '', '607', 'a',
40     '', '', '',
41     'unless', '606', 'a', 'not_equals', '^AJAX', '1',
42     'Copy field 606$a to 607$a unless 606$a matches RegEx m^AJAX'
43 ), 1, "Add third action");
44
45 is( AddModificationTemplateAction(
46     $template_id, 'add_field', 0,
47     '650', 'a', 'Additional', '', '',
48     '', '', '',
49     'unless', '650', 'a', 'exists', '', '',
50     'Add field 650$aAdditional unless 650$a exists'
51 ), 1, "Add fourth action");
52 # Getter
53
54 my @actions = GetModificationTemplateActions( $template_id );
55 is( @actions, 4, "4 actions are insered");
56
57 for my $action ( @actions ) {
58     isnt( GetModificationTemplateAction( $action->{mmta_id} ), undef, "action with id $action->{mmta_id} exists" );
59 }
60
61 my $first_action = $actions[0];
62 is( $first_action->{ordering}, 1, "test ordering for first action" );
63 is( $first_action->{action}, 'move_field', "test action for first action" );
64 is( $first_action->{from_field}, '464', "test from_field for first action" );
65 is( $first_action->{from_subfield}, 'u', "test from_subfield for first action" );
66 is( $first_action->{to_field}, '464', "test to_field for first action" );
67 is( $first_action->{to_subfield}, '3', "test to_subfield for first action" );
68
69 my $second_action = $actions[1];
70 is( $second_action->{ordering}, 2, "test ordering for second action" );
71 is( $second_action->{action}, 'update_field', "test action for second action" );
72 is( $second_action->{from_field}, '099',"test from_field for second action" );
73 is( $second_action->{from_subfield}, 't', "test from_subfield for second action" );
74 is( $second_action->{field_value}, 'LIV', "test firld_value for second action" );
75 is( $second_action->{to_field}, '', "test to_field for second action" );
76 is( $second_action->{to_subfield}, '', "test to_subfield for second action" );
77 is( $second_action->{conditional}, 'if', "test conditional for second action" );
78 is( $second_action->{conditional_field}, '200', "test conditional_field for second action" );
79 is( $second_action->{conditional_subfield}, 'b', "test conditional_subfield for second action" );
80 is( $second_action->{conditional_comparison}, 'equals', "test conditional_comparison for second action" );
81
82 my $third_action = $actions[2];
83 is( $third_action->{ordering}, 3, "test ordering for third action" );
84 is( $third_action->{action}, 'copy_field', "test action for third action" );
85 is( $third_action->{from_field}, '606', "test from_field for third action" );
86 is( $third_action->{from_subfield}, 'a', "test from_subfield for third action" );
87 is( $third_action->{to_field}, '607', "test to_field for third action" );
88 is( $third_action->{to_subfield}, 'a', "test to_subfield for third action" );
89 is( $third_action->{conditional}, 'unless', "test conditional for third action" );
90 is( $third_action->{conditional_field}, '606', "test conditional_field for third action" );
91 is( $third_action->{conditional_subfield}, 'a', "test conditional_subfield for third action" );
92 is( $third_action->{conditional_comparison}, 'not_equals', "test conditional_comparison for third action" );
93 is( $third_action->{conditional_value}, '^AJAX', "test conditional_value for third action" );
94
95 my $fourth_action = $actions[3];
96 is( $fourth_action->{ordering}, 4, "test ordering for fourth action" );
97 is( $fourth_action->{action}, 'add_field', "test action for fourth action" );
98 is( $fourth_action->{from_field}, '650', "test from_field for fourth action" );
99 is( $fourth_action->{from_subfield}, 'a', "test from_subfield for fourth action" );
100 is( $fourth_action->{to_field}, '', "test to_field for fourth action" );
101 is( $fourth_action->{to_subfield}, '', "test to_subfield for fourth action" );
102 is( $fourth_action->{conditional}, 'unless', "test conditional for fourth action" );
103 is( $fourth_action->{conditional_field}, '650', "test conditional_field for fourth action" );
104 is( $fourth_action->{conditional_subfield}, 'a', "test conditional_subfield for fourth action" );
105 is( $fourth_action->{conditional_comparison}, 'exists', "test conditional_comparison for fourth action" );
106 is( $fourth_action->{conditional_value}, '', "test conditional_value for fourth action" );
107
108 # Modifications
109 is( ModModificationTemplateAction(
110     $actions[1]->{mmta_id}, 'update_field', 0,
111     '100', 'u', 'LIV', '', '',
112     '', '', '',
113     'if', '200', 'c', 'equals', 'Text', '',
114     'Update field 099$t with value LIV if 200$b matches "Text"'
115 ), 1, "Modify second action");
116
117 $second_action = GetModificationTemplateAction( $actions[1]->{mmta_id} );
118 is( $second_action->{ordering}, 2, "test ordering for second action modified" );
119 is( $second_action->{action}, 'update_field', "test action for second action modified" );
120 is( $second_action->{from_field}, '100',"test from_field for second action modified" );
121 is( $second_action->{from_subfield}, 'u', "test from_subfield for second action modified" );
122 is( $second_action->{field_value}, 'LIV', "test firld_value for second action modified" );
123 is( $second_action->{to_field}, '', "test to_field for second action modified" );
124 is( $second_action->{to_subfield}, '', "test to_subfield for second action modified" );
125 is( $second_action->{conditional}, 'if', "test conditional for second action modified" );
126 is( $second_action->{conditional_field}, '200', "test conditional_field for second action modified" );
127 is( $second_action->{conditional_subfield}, 'c', "test conditional_subfield for second action modified" );
128 is( $second_action->{conditional_comparison}, 'equals', "test conditional_comparison for second action modified" );
129
130 # Up and down
131 is( MoveModificationTemplateAction( $actions[2]->{mmta_id}, 'top' ), '1', 'Move the third action on top' );
132 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'bottom' ), '1', 'Move the first action on bottom' );
133
134 is( GetModificationTemplateAction( $actions[0]->{mmta_id} )->{ordering}, '4', 'First becomes fourth' );
135 is( GetModificationTemplateAction( $actions[1]->{mmta_id} )->{ordering}, '2', 'Second stays second' );
136 is( GetModificationTemplateAction( $actions[2]->{mmta_id} )->{ordering}, '1', 'Third becomes first' );
137 is( GetModificationTemplateAction( $actions[3]->{mmta_id} )->{ordering}, '3', 'Fourth becomes third' );
138
139 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'up' ), '1', 'Move up the first action (was fourth)' );
140 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'up' ), '1', 'Move up the first action (was third)' );
141 is( MoveModificationTemplateAction( $actions[2]->{mmta_id}, 'down' ), '1', 'Move down the third action (was first)' );
142
143 is( GetModificationTemplateAction( $actions[0]->{mmta_id} )->{ordering}, '1', 'First becomes again first' );
144 is( GetModificationTemplateAction( $actions[1]->{mmta_id} )->{ordering}, '3', 'Second becomes third' );
145 is( GetModificationTemplateAction( $actions[2]->{mmta_id} )->{ordering}, '2', 'Third becomes second' );
146 is( GetModificationTemplateAction( $actions[3]->{mmta_id} )->{ordering}, '4', 'Fourth becomes again fourth' );
147
148 # Cleaning
149 is( DelModificationTemplateAction( $actions[0]->{mmta_id} ), 3, "Delete the first action, 2 others are reordered" );
150 is( GetModificationTemplateAction( $actions[0]->{mmta_id} ), undef, "first action does not exist anymore" );
151
152 is( DelModificationTemplate( $template_id ), 1, "The template has been deleted" );
153
154 is( GetModificationTemplateAction( $actions[1]->{mmta_id} ), undef, "second action does not exist anymore" );
155 is( GetModificationTemplateAction( $actions[2]->{mmta_id} ), undef, "third action does not exist anymore" );
156 is( GetModificationTemplateAction( $actions[3]->{mmta_id} ), undef, "fourth action does not exist anymore" );
157
158 is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" );
159
160 # ModifyRecordWithTemplate
161 my @USERENV = (
162     1,
163     'test',
164     'MASTERTEST',
165     'Test',
166     'Test',
167     't',
168     'Test',
169     0,
170 );
171 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
172 C4::Context->set_userenv ( @USERENV );
173
174 $template_id = AddModificationTemplate("new_template_test");
175 like( $template_id, qr|^\d+$|, "new template returns an id" );
176
177 is( AddModificationTemplateAction(
178     $template_id, 'delete_field', 0,
179     '245', '', '', '', '',
180     '', '', '',
181     'if', '245', 'a', 'equals', 'Bad title', '',
182     'Delete field 245 if 245$a eq "Bad title"'
183 ), 1, 'Add first action: delete field 245 if 245$a eq "Bad title"');
184
185 is( AddModificationTemplateAction(
186     $template_id, 'copy_field', 0,
187     '245', 'a', '', '246', 'a',
188     '', '', '',
189     '', '', '', '', '', '',
190     'copy field 245$a to 246$a'
191 ), 1, 'Add second action: copy 245$a to 246$a');
192
193 is( AddModificationTemplateAction(
194     $template_id, 'delete_field', 0,
195     '650', 'a', '', '', '',
196     '', '', '',
197     'if', '650', '9', 'equals', '462', '',
198     'Delete field 650$a if 650$9=462'
199 ), 1, 'Add third action: delete field 650$a if 650$9=462');
200
201 is( AddModificationTemplateAction(
202     $template_id, 'update_field', 0,
203     '952', 'p', '3010023917_updated', '', '',
204     '', '', '',
205     'unless', '650', '9', 'equals', '42', '',
206     'Update field 952$p with "3010023917_updated" if 650$9 != 42'
207 ), 1, 'Add fourth action: update field 952$p with "3010023917_updated" if 650$9 != 42');
208
209 is( AddModificationTemplateAction(
210     $template_id, 'move_field', 0,
211     '952', 'd', '', '952', 'e',
212     '', '', '',
213     'if', '952', 'c', 'equals', '^GEN', '1',
214     'Move field 952$d to 952$e if 952$c =~ /^GEN/'
215 ), 1, 'Add fifth action: move field 952$d to 952$e if 952$c =~ /^GEN/');
216
217 is( AddModificationTemplateAction(
218     $template_id, 'update_field', 0,
219     '650', 'a', 'Computer algorithms.', '', '',
220     '', '', '',
221     'if', '650', '9', 'equals', '499', '',
222     'Update field 650$a with "Computer algorithms." to 651 if 650$9 == 499'
223 ), 1, 'Add sixth action: update field 650$a with "Computer algorithms." if 650$9 == 499');
224
225 is( AddModificationTemplateAction(
226     $template_id, 'move_field', 0,
227     '650', '', '', '651', '',
228     '', '', '',
229     'if', '650', '9', 'equals', '499', '',
230     'Move field 650 to 651 if 650$9 == 499'
231 ), 1, 'Add seventh action: move field 650 to 651 if 650$9 == 499');
232
233 is( AddModificationTemplateAction(
234     $template_id, 'update_field', 0,
235     '999', 'a', 'non existent.', '', '',
236     '', '', '',
237     '', '', '', '', '', '',
238     'Update non existent field 999$a with "non existent"'
239 ), 1, 'Add eighth action: update field non existent 999$a with "non existent."');
240
241 is( AddModificationTemplateAction(
242     $template_id, 'update_field', 0,
243     '999', 'a', 'existent - updated.', '', '',
244     '', '', '',
245     '', '', '', '', '', '',
246     'Update existent field 999$a with "existent - updated."'
247 ), 1, 'Add ninth action: update field non existent 999$a with "existent - updated."');
248
249 is( AddModificationTemplateAction(
250     $template_id, 'add_field', 0,
251     '999', 'a', 'additional existent.', '', '',
252     '', '', '',
253     '', '', '', '', '', '',
254     'Add new existent field 999$a with "additional existent"'
255 ), 1, 'Add tenth action: add additional field existent 999$a with "additional existent."');
256
257 is( AddModificationTemplateAction(
258     $template_id, 'add_field', 0,
259     '007', '', 'vxcdq', '', '',
260     '', '', '',
261     '', '', '', '', '', '',
262     'Add new existent field 999$a with "additional existent"'
263 ), 1, 'Add eleventh action: add additional field existent 007');
264
265 my $record = new_record();
266 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
267
268 my $expected_record = expected_record_1();
269 is_deeply( $record, $expected_record, "Record modification as expected");
270
271 $template_id = AddModificationTemplate("another_template_test");
272
273 # Duplicate 245 => 3x245
274 is( AddModificationTemplateAction(
275     $template_id, 'copy_field', 0,
276     '245', '', '', '245', '',
277     '', '', '',
278     'if', '245', 'a', 'equals', 'Bad title', '',
279     'Copy field 245 if 245$a eq "Bad title"'
280 ), 1, 'Add action: copy field 245 if 245$a eq "Bad title"');
281
282 $record = new_record();
283 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
284
285 my @fields_245a = Koha::SimpleMARC::read_field({
286     record => $record,
287     field => '245',
288     subfield => 'a',
289 });
290 is_deeply( \@fields_245a, [
291         'The art of computer programming',
292         'Bad title',
293         'Bad title',
294     ], 'Copy field has copied the "Bad title"' );
295
296 # Update first "Bad title"
297 is( AddModificationTemplateAction(
298     $template_id, 'update_field', 1,
299     '245', 'a', 'Bad title updated', '', '',
300     '', '', '',
301     'if', '245', 'a', 'equals', 'Bad title', '',
302     'Update first 245$a matching "Bad title" with "Bad title updated"'
303 ), 1, 'Add action: update field 245$a matching "Bad title" with "Bad title updated');
304
305 $record = new_record();
306 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
307
308 @fields_245a = Koha::SimpleMARC::read_field({
309     record => $record,
310     field => '245',
311     subfield => 'a',
312 });
313 is_deeply( \@fields_245a, [
314         'The art of computer programming',
315         'Bad title updated',
316         'Bad title',
317     ], 'update_field has update first the "Bad title"' );
318
319 # Duplicate first 245 => 3x245
320 is( AddModificationTemplateAction(
321     $template_id, 'copy_field', 1,
322     '245', '', '', '245', '',
323     '', '', '',
324     'if', '245', 'a', 'equals', '^Bad title', '1',
325     'Copy field 245 if 245$a =~ "^Bad title"'
326 ), 1, 'Add action: copy field 245 if 245$a =~ "^Bad title"');
327
328 $record = new_record();
329 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
330
331 @fields_245a = Koha::SimpleMARC::read_field({
332     record => $record,
333     field => '245',
334     subfield => 'a',
335 });
336 is_deeply( \@fields_245a, [
337         'The art of computer programming',
338         'Bad title updated',
339         'Bad title',
340         'Bad title updated',
341     ], 'Copy field has copied first "^Bad title"' );
342
343 # Delete first ^Bad title
344 is( AddModificationTemplateAction(
345     $template_id, 'delete_field', 1,
346     '245', '', '', '', '',
347     '', '', '',
348     'if', '245', 'a', 'equals', '^Bad title', '1',
349     'Delete first 245$a mathing ^Bad title'
350 ), 1, 'Delete first 245$a mathing ^Bad title');
351
352 $record = new_record();
353 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
354 @fields_245a = Koha::SimpleMARC::read_field({
355     record => $record,
356     field => '245',
357     subfield => 'a',
358 });
359 is_deeply( \@fields_245a, [
360         'The art of computer programming',
361         'Bad title',
362         'Bad title updated',
363     ], 'delete field has been deleted the right field"' );
364
365 is( AddModificationTemplateAction(
366     $template_id, 'delete_field', 0,
367     '245', '', '', '', '',
368     '', '', '',
369     'if', '245', 'a', 'equals', 'updated$', '1',
370     'Delete first 245$a mathing updated$'
371 ), 1, 'Delete first 245$a mathing updated$');
372
373 $record = new_record();
374 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
375 @fields_245a = Koha::SimpleMARC::read_field({
376     record => $record,
377     field => '245',
378     subfield => 'a',
379 });
380 is_deeply( \@fields_245a, [
381         'The art of computer programming',
382         'Bad title'
383     ], 'delete field has been deleted the right field"' );
384
385 subtest 'GetModificationTemplates' => sub {
386     plan tests => 1;
387     $dbh->do(q|DELETE FROM marc_modification_templates|);
388     AddModificationTemplate("zzz");
389     AddModificationTemplate("aaa");
390     AddModificationTemplate("mmm");
391     my @templates = GetModificationTemplates();
392     is_deeply( [map{$_->{name}} @templates], ['aaa', 'mmm', 'zzz'] );
393 };
394
395 subtest "not_equals" => sub {
396     plan tests => 2;
397     $dbh->do(q|DELETE FROM marc_modification_templates|);
398     my $template_id = AddModificationTemplate("template_name");
399     AddModificationTemplateAction(
400         $template_id, 'move_field', 0,
401         '650', '', '', '651', '',
402         '', '', '',
403         'if', '650', '9', 'not_equals', '499', '',
404         'Move field 650 to 651 if 650$9 != 499'
405     );
406     my $record = new_record();
407     ModifyRecordWithTemplate( $template_id, $record );
408     my $expected_record = expected_record_2();
409     is_deeply( $record, $expected_record, '650 has been moved to 651 when 650$9 != 499' );
410
411     $dbh->do(q|DELETE FROM marc_modification_templates|);
412     $template_id = AddModificationTemplate("template_name");
413     AddModificationTemplateAction(
414         $template_id, 'move_field', 0,
415         '650', '', '', '651', '',
416         '', '', '',
417         'if', '650', 'b', 'not_equals', '499', '',
418         'Move field 650 to 651 if 650$b != 499'
419     );
420     $record = new_record();
421     ModifyRecordWithTemplate( $template_id, $record );
422     $expected_record = new_record();
423     is_deeply( $record, $expected_record, 'None 650 have been moved, no $650$b exists' );
424 };
425
426 sub new_record {
427     my $record = MARC::Record->new;
428     $record->leader('03174nam a2200445 a 4500');
429     my @fields = (
430         MARC::Field->new(
431             100, '1', ' ',
432             a => 'Knuth, Donald Ervin',
433             d => '1938',
434         ),
435         MARC::Field->new(
436             245, '1', '4',
437             a => 'The art of computer programming',
438             c => 'Donald E. Knuth.',
439         ),
440         MARC::Field->new(
441             245, '1', '4',
442             a => 'Bad title',
443             c => 'Donald E. Knuth.',
444         ),
445         MARC::Field->new(
446             650, ' ', '0',
447             a => 'Computer programming.',
448             9 => '462',
449         ),
450         MARC::Field->new(
451             650, ' ', '0',
452             a => 'Computer programming.',
453             9 => '499',
454         ),
455         MARC::Field->new(
456             952, ' ', ' ',
457             p => '3010023917',
458             y => 'BK',
459             c => 'GEN',
460             d => '2001-06-25',
461         ),
462     );
463     $record->append_fields(@fields);
464     return $record;
465 }
466
467 sub expected_record_1 {
468     my $record = MARC::Record->new;
469     $record->leader('03174nam a2200445 a 4500');
470     my @fields = (
471         MARC::Field->new(
472             100, '1', ' ',
473             a => 'Knuth, Donald Ervin',
474             d => '1938',
475         ),
476         MARC::Field->new(
477             245, '1', '4',
478             a => 'The art of computer programming',
479             c => 'Donald E. Knuth.',
480         ),
481         MARC::Field->new(
482             650, ' ', '0',
483             9 => '462',
484         ),
485         MARC::Field->new(
486             952, ' ', ' ',
487             p => '3010023917_updated',
488             y => 'BK',
489             c => 'GEN',
490             e => '2001-06-25',
491         ),
492         MARC::Field->new(
493             246, '', ' ',
494             a => 'The art of computer programming',
495         ),
496         MARC::Field->new(
497             651, ' ', '0',
498             a => 'Computer algorithms.',
499             9 => '499',
500         ),
501         MARC::Field->new(
502             999, ' ', ' ',
503             a => 'existent - updated.',
504         ),
505         MARC::Field->new(
506             999, ' ', ' ',
507             a => 'additional existent.',
508         ),
509         MARC::Field->new(
510            '007', 'vxcdq',
511         ),
512     );
513     $record->append_fields(@fields);
514     return $record;
515 }
516
517 sub expected_record_2 {
518     my $record = MARC::Record->new;
519     $record->leader('03174nam a2200445 a 4500');
520     my @fields = (
521         MARC::Field->new(
522             100, '1', ' ',
523             a => 'Knuth, Donald Ervin',
524             d => '1938',
525         ),
526         MARC::Field->new(
527             245, '1', '4',
528             a => 'The art of computer programming',
529             c => 'Donald E. Knuth.',
530         ),
531         MARC::Field->new(
532             245, '1', '4',
533             a => 'Bad title',
534             c => 'Donald E. Knuth.',
535         ),
536         MARC::Field->new(
537             650, ' ', '0',
538             a => 'Computer programming.',
539             9 => '499',
540         ),
541         MARC::Field->new(
542             952, ' ', ' ',
543             p => '3010023917',
544             y => 'BK',
545             c => 'GEN',
546             d => '2001-06-25',
547         ),
548         MARC::Field->new(
549             651, ' ', '0',
550             a => 'Computer programming.',
551             9 => '462',
552         ),
553     );
554     $record->append_fields(@fields);
555     return $record;
556 }
557
558 # C4::Context->userenv
559 sub Mock_userenv {
560     return { branchcode => 'CPL' };
561 }