3 use Test::More tests => 115;
9 use_ok("MARC::Record");
10 use_ok("C4::MarcModificationTemplates");
12 my $schema = Koha::Database->new->schema;
13 $schema->storage->txn_begin;
14 my $dbh = C4::Context->dbh;
16 $dbh->do(q|DELETE FROM marc_modification_templates|);
19 my $template_id = AddModificationTemplate("template_name");
20 like( $template_id, qr|^\d+$|, "new template returns an id" );
22 is( AddModificationTemplateAction(
23 $template_id, 'move_field', 1,
24 '464', 'u', '', '464', '3',
26 '', '', '', '', '', '',
27 'move first 464$u to 464$3'
28 ), 1, "Add first action");
30 is( AddModificationTemplateAction(
31 $template_id, 'update_field', 0,
32 '099', 't', 'LIV', '', '',
34 'if', '200', 'b', 'equals', 'Text', '',
35 'Update field 099$t with value LIV if 200$b matches "Text"'
36 ), 1, "Add second action");
38 is( AddModificationTemplateAction(
39 $template_id, 'copy_field', 0,
40 '606', 'a', '', '607', 'a',
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");
46 is( AddModificationTemplateAction(
47 $template_id, 'add_field', 0,
48 '650', 'a', 'Additional', '', '',
50 'unless', '650', 'a', 'exists', '', '',
51 'Add field 650$aAdditional unless 650$a exists'
52 ), 1, "Add fourth action");
55 my @actions = GetModificationTemplateActions( $template_id );
56 is( @actions, 4, "4 actions are insered");
58 for my $action ( @actions ) {
59 isnt( GetModificationTemplateAction( $action->{mmta_id} ), undef, "action with id $action->{mmta_id} exists" );
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" );
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" );
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" );
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" );
110 is( ModModificationTemplateAction(
111 $actions[1]->{mmta_id}, 'update_field', 0,
112 '100', 'u', 'LIV', '', '',
114 'if', '200', 'c', 'equals', 'Text', '',
115 'Update field 099$t with value LIV if 200$b matches "Text"'
116 ), 1, "Modify second action");
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" );
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' );
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' );
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)' );
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' );
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" );
153 is( DelModificationTemplate( $template_id ), 1, "The template has been deleted" );
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" );
159 is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" );
161 # ModifyRecordWithTemplate
172 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
173 C4::Context->set_userenv ( @USERENV );
175 $template_id = AddModificationTemplate("new_template_test");
176 like( $template_id, qr|^\d+$|, "new template returns an id" );
178 is( AddModificationTemplateAction(
179 $template_id, 'delete_field', 0,
180 '245', '', '', '', '',
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"');
186 is( AddModificationTemplateAction(
187 $template_id, 'copy_field', 0,
188 '245', 'a', '', '246', 'a',
190 '', '', '', '', '', '',
191 'copy field 245$a to 246$a'
192 ), 1, 'Add second action: copy 245$a to 246$a');
194 is( AddModificationTemplateAction(
195 $template_id, 'delete_field', 0,
196 '650', 'a', '', '', '',
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');
202 is( AddModificationTemplateAction(
203 $template_id, 'update_field', 0,
204 '952', 'p', '3010023917_updated', '', '',
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');
210 is( AddModificationTemplateAction(
211 $template_id, 'move_field', 0,
212 '952', 'd', '', '952', 'e',
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/');
218 is( AddModificationTemplateAction(
219 $template_id, 'update_field', 0,
220 '650', 'a', 'Computer algorithms.', '', '',
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');
226 is( AddModificationTemplateAction(
227 $template_id, 'move_field', 0,
228 '650', '', '', '651', '',
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');
234 is( AddModificationTemplateAction(
235 $template_id, 'update_field', 0,
236 '999', 'a', 'non existent.', '', '',
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."');
242 is( AddModificationTemplateAction(
243 $template_id, 'update_field', 0,
244 '999', 'a', 'existent - updated.', '', '',
246 '', '', '', '', '', '',
247 'Update existent field 999$a with "existent - updated."'
248 ), 1, 'Add ninth action: update field non existent 999$a with "existent - updated."');
250 is( AddModificationTemplateAction(
251 $template_id, 'add_field', 0,
252 '999', 'a', 'additional existent.', '', '',
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."');
258 is( AddModificationTemplateAction(
259 $template_id, 'add_field', 0,
260 '007', '', 'vxcdq', '', '',
262 '', '', '', '', '', '',
263 'Add new existent field 999$a with "additional existent"'
264 ), 1, 'Add eleventh action: add additional field existent 007');
266 my $record = new_record();
267 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
269 my $expected_record = expected_record_1();
270 is_deeply( $record, $expected_record, "Record modification as expected");
272 $template_id = AddModificationTemplate("another_template_test");
274 # Duplicate 245 => 3x245
275 is( AddModificationTemplateAction(
276 $template_id, 'copy_field', 0,
277 '245', '', '', '245', '',
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"');
283 $record = new_record();
284 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
286 my @fields_245a = Koha::SimpleMARC::read_field({
291 is_deeply( \@fields_245a, [
292 'The art of computer programming',
295 ], 'Copy field has copied the "Bad title"' );
297 # Update first "Bad title"
298 is( AddModificationTemplateAction(
299 $template_id, 'update_field', 1,
300 '245', 'a', 'Bad title updated', '', '',
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');
306 $record = new_record();
307 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
309 @fields_245a = Koha::SimpleMARC::read_field({
314 is_deeply( \@fields_245a, [
315 'The art of computer programming',
318 ], 'update_field has update first the "Bad title"' );
320 # Duplicate first 245 => 3x245
321 is( AddModificationTemplateAction(
322 $template_id, 'copy_field', 1,
323 '245', '', '', '245', '',
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"');
329 $record = new_record();
330 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
332 @fields_245a = Koha::SimpleMARC::read_field({
337 is_deeply( \@fields_245a, [
338 'The art of computer programming',
342 ], 'Copy field has copied first "^Bad title"' );
344 # Delete first ^Bad title
345 is( AddModificationTemplateAction(
346 $template_id, 'delete_field', 1,
347 '245', '', '', '', '',
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');
353 $record = new_record();
354 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
355 @fields_245a = Koha::SimpleMARC::read_field({
360 is_deeply( \@fields_245a, [
361 'The art of computer programming',
364 ], 'delete field has been deleted the right field"' );
366 is( AddModificationTemplateAction(
367 $template_id, 'delete_field', 0,
368 '245', '', '', '', '',
370 'if', '245', 'a', 'equals', 'updated$', '1',
371 'Delete first 245$a mathing updated$'
372 ), 1, 'Delete first 245$a mathing updated$');
374 $record = new_record();
375 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
376 @fields_245a = Koha::SimpleMARC::read_field({
381 is_deeply( \@fields_245a, [
382 'The art of computer programming',
384 ], 'delete field has been deleted the right field"' );
386 subtest 'GetModificationTemplates' => sub {
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'] );
396 subtest "not_equals" => sub {
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', '',
404 'if', '650', '9', 'not_equals', '499', '',
405 'Move field 650 to 651 if 650$9 != 499'
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' );
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', '',
418 'if', '650', 'b', 'not_equals', '499', '',
419 'Move field 650 to 651 if 650$b != 499'
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' );
428 my $record = MARC::Record->new;
429 $record->leader('03174nam a2200445 a 4500');
433 a => 'Knuth, Donald Ervin',
438 a => 'The art of computer programming',
439 c => 'Donald E. Knuth.',
444 c => 'Donald E. Knuth.',
448 a => 'Computer programming.',
453 a => 'Computer programming.',
464 $record->append_fields(@fields);
468 sub expected_record_1 {
469 my $record = MARC::Record->new;
470 $record->leader('03174nam a2200445 a 4500');
474 a => 'Knuth, Donald Ervin',
479 a => 'The art of computer programming',
480 c => 'Donald E. Knuth.',
488 p => '3010023917_updated',
495 a => 'The art of computer programming',
499 a => 'Computer algorithms.',
504 a => 'existent - updated.',
508 a => 'additional existent.',
514 $record->append_fields(@fields);
518 sub expected_record_2 {
519 my $record = MARC::Record->new;
520 $record->leader('03174nam a2200445 a 4500');
524 a => 'Knuth, Donald Ervin',
529 a => 'The art of computer programming',
530 c => 'Donald E. Knuth.',
535 c => 'Donald E. Knuth.',
539 a => 'Computer programming.',
551 a => 'Computer programming.',
555 $record->append_fields(@fields);
559 # C4::Context->userenv
561 return { branchcode => 'CPL' };