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