3 use Test::More tests => 96;
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");
47 my @actions = GetModificationTemplateActions( $template_id );
48 is( @actions, 3, "3 actions are insered");
50 for my $action ( @actions ) {
51 isnt( GetModificationTemplateAction( $action->{mmta_id} ), undef, "action with id $action->{mmta_id} exists" );
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" );
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" );
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" );
90 is( ModModificationTemplateAction(
91 $actions[1]->{mmta_id}, 'update_field', 0,
92 '100', 'u', 'LIV', '', '',
94 'if', '200', 'c', 'equals', 'Text', '',
95 'Update field 099$t with value LIV if 200$b matches "Text"'
96 ), 1, "Modify second action");
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" );
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' );
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' );
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)' );
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' );
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" );
131 is( DelModificationTemplate( $template_id ), 1, "The template has been deleted" );
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" );
136 is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" );
138 # ModifyRecordWithTemplate
149 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
150 C4::Context->set_userenv ( @USERENV );
152 $template_id = AddModificationTemplate("new_template_test");
153 like( $template_id, qr|^\d+$|, "new template returns an id" );
155 is( AddModificationTemplateAction(
156 $template_id, 'delete_field', 0,
157 '245', '', '', '', '',
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"');
163 is( AddModificationTemplateAction(
164 $template_id, 'copy_field', 0,
165 '245', 'a', '', '246', 'a',
167 '', '', '', '', '', '',
168 'copy field 245$a to 246$a'
169 ), 1, 'Add second action: copy 245$a to 246$a');
171 is( AddModificationTemplateAction(
172 $template_id, 'delete_field', 0,
173 '650', 'a', '', '', '',
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');
179 is( AddModificationTemplateAction(
180 $template_id, 'update_field', 0,
181 '952', 'p', '3010023917_updated', '', '',
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');
187 is( AddModificationTemplateAction(
188 $template_id, 'move_field', 0,
189 '952', 'd', '', '952', 'e',
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/');
195 is( AddModificationTemplateAction(
196 $template_id, 'update_field', 0,
197 '650', 'a', 'Computer algorithms.', '', '',
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');
203 is( AddModificationTemplateAction(
204 $template_id, 'move_field', 0,
205 '650', '', '', '651', '',
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');
211 is( AddModificationTemplateAction(
212 $template_id, 'update_field', 0,
213 '999', 'a', 'non existent.', '', '',
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."');
219 my $record = new_record();
221 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
223 my $expected_record = expected_record_1();
224 is_deeply( $record, $expected_record, "Record modification as expected");
226 $template_id = AddModificationTemplate("another_template_test");
228 # Duplicate 245 => 3x245
229 is( AddModificationTemplateAction(
230 $template_id, 'copy_field', 0,
231 '245', '', '', '245', '',
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"');
237 $record = new_record();
238 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
240 my @fields_245a = Koha::SimpleMARC::read_field({
245 is_deeply( \@fields_245a, [
246 'The art of computer programming',
249 ], 'Copy field has copied the "Bad title"' );
251 # Update first "Bad title"
252 is( AddModificationTemplateAction(
253 $template_id, 'update_field', 1,
254 '245', 'a', 'Bad title updated', '', '',
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');
260 $record = new_record();
261 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
263 @fields_245a = Koha::SimpleMARC::read_field({
268 is_deeply( \@fields_245a, [
269 'The art of computer programming',
272 ], 'update_field has update first the "Bad title"' );
274 # Duplicate first 245 => 3x245
275 is( AddModificationTemplateAction(
276 $template_id, 'copy_field', 1,
277 '245', '', '', '245', '',
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"');
283 $record = new_record();
284 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
286 @fields_245a = Koha::SimpleMARC::read_field({
291 is_deeply( \@fields_245a, [
292 'The art of computer programming',
296 ], 'Copy field has copied first "^Bad title"' );
298 # Delete first ^Bad title
299 is( AddModificationTemplateAction(
300 $template_id, 'delete_field', 1,
301 '245', '', '', '', '',
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');
307 $record = new_record();
308 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 ], 'delete field has been deleted the right field"' );
320 is( AddModificationTemplateAction(
321 $template_id, 'delete_field', 0,
322 '245', '', '', '', '',
324 'if', '245', 'a', 'equals', 'updated$', '1',
325 'Delete first 245$a mathing updated$'
326 ), 1, 'Delete first 245$a mathing updated$');
328 $record = new_record();
329 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
330 @fields_245a = Koha::SimpleMARC::read_field({
335 is_deeply( \@fields_245a, [
336 'The art of computer programming',
338 ], 'delete field has been deleted the right field"' );
340 subtest 'GetModificationTemplates' => sub {
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'] );
350 subtest "not_equals" => sub {
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', '',
358 'if', '650', '9', 'not_equals', '499', '',
359 'Move field 650 to 651 if 650$9 != 499'
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' );
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', '',
372 'if', '650', 'b', 'not_equals', '499', '',
373 'Move field 650 to 651 if 650$b != 499'
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' );
382 my $record = MARC::Record->new;
383 $record->leader('03174nam a2200445 a 4500');
387 a => 'Knuth, Donald Ervin',
392 a => 'The art of computer programming',
393 c => 'Donald E. Knuth.',
398 c => 'Donald E. Knuth.',
402 a => 'Computer programming.',
407 a => 'Computer programming.',
418 $record->append_fields(@fields);
422 sub expected_record_1 {
423 my $record = MARC::Record->new;
424 $record->leader('03174nam a2200445 a 4500');
428 a => 'Knuth, Donald Ervin',
433 a => 'The art of computer programming',
434 c => 'Donald E. Knuth.',
442 p => '3010023917_updated',
449 a => 'The art of computer programming',
453 a => 'Computer algorithms.',
458 a => 'non existent.',
461 $record->append_fields(@fields);
465 sub expected_record_2 {
466 my $record = MARC::Record->new;
467 $record->leader('03174nam a2200445 a 4500');
471 a => 'Knuth, Donald Ervin',
476 a => 'The art of computer programming',
477 c => 'Donald E. Knuth.',
482 c => 'Donald E. Knuth.',
486 a => 'Computer programming.',
498 a => 'Computer programming.',
502 $record->append_fields(@fields);
506 # C4::Context->userenv
508 return { branchcode => 'CPL' };