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