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