3 use Test::More tests => 94;
8 use_ok("MARC::Record");
9 use_ok("C4::MarcModificationTemplates");
11 my $dbh = C4::Context->dbh;
12 $dbh->{AutoCommit} = 0;
13 $dbh->{RaiseError} = 1;
15 $dbh->do(q|DELETE FROM marc_modification_templates|);
18 my $template_id = AddModificationTemplate("template_name");
19 like( $template_id, qr|^\d+$|, "new template returns an id" );
21 is( AddModificationTemplateAction(
22 $template_id, 'move_field', 1,
23 '464', 'u', '', '464', '3',
25 '', '', '', '', '', '',
26 'move first 464$u to 464$3'
27 ), 1, "Add first action");
29 is( AddModificationTemplateAction(
30 $template_id, 'update_field', 0,
31 '099', 't', 'LIV', '', '',
33 'if', '200', 'b', 'equals', 'Text', '',
34 'Update field 099$t with value LIV if 200$b matches "Text"'
35 ), 1, "Add second action");
37 is( AddModificationTemplateAction(
38 $template_id, 'copy_field', 0,
39 '606', 'a', '', '607', 'a',
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");
46 my @actions = GetModificationTemplateActions( $template_id );
47 is( @actions, 3, "3 actions are insered");
49 for my $action ( @actions ) {
50 isnt( GetModificationTemplateAction( $action->{mmta_id} ), undef, "action with id $action->{mmta_id} exists" );
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" );
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" );
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" );
89 is( ModModificationTemplateAction(
90 $actions[1]->{mmta_id}, 'update_field', 0,
91 '100', 'u', 'LIV', '', '',
93 'if', '200', 'c', 'equals', 'Text', '',
94 'Update field 099$t with value LIV if 200$b matches "Text"'
95 ), 1, "Modify second action");
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" );
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' );
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' );
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)' );
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' );
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" );
130 is( DelModificationTemplate( $template_id ), 1, "The template has been deleted" );
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" );
135 is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" );
137 # ModifyRecordWithTemplate
148 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
149 C4::Context->set_userenv ( @USERENV );
151 $template_id = AddModificationTemplate("new_template_test");
152 like( $template_id, qr|^\d+$|, "new template returns an id" );
154 is( AddModificationTemplateAction(
155 $template_id, 'delete_field', 0,
156 '245', '', '', '', '',
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"');
162 is( AddModificationTemplateAction(
163 $template_id, 'copy_field', 0,
164 '245', 'a', '', '246', 'a',
166 '', '', '', '', '', '',
167 'copy field 245$a to 246$a'
168 ), 1, 'Add second action: copy 245$a to 246$a');
170 is( AddModificationTemplateAction(
171 $template_id, 'delete_field', 0,
172 '650', 'a', '', '', '',
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');
178 is( AddModificationTemplateAction(
179 $template_id, 'update_field', 0,
180 '952', 'p', '3010023917_updated', '', '',
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');
186 is( AddModificationTemplateAction(
187 $template_id, 'move_field', 0,
188 '952', 'd', '', '952', 'e',
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/');
194 is( AddModificationTemplateAction(
195 $template_id, 'update_field', 0,
196 '650', 'a', 'Computer algorithms.', '', '',
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');
202 is( AddModificationTemplateAction(
203 $template_id, 'move_field', 0,
204 '650', '', '', '651', '',
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');
210 is( AddModificationTemplateAction(
211 $template_id, 'update_field', 0,
212 '999', 'a', 'non existent.', '', '',
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."');
218 my $record = new_record();
220 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
222 my $expected_record = expected_record_1();
223 is_deeply( $record, $expected_record, "Record modification as expected");
225 $template_id = AddModificationTemplate("another_template_test");
227 # Duplicate 245 => 3x245
228 is( AddModificationTemplateAction(
229 $template_id, 'copy_field', 0,
230 '245', '', '', '245', '',
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"');
236 $record = new_record();
237 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
239 my @fields_245a = Koha::SimpleMARC::read_field({
244 is_deeply( \@fields_245a, [
245 'The art of computer programming',
248 ], 'Copy field has copied the "Bad title"' );
250 # Update first "Bad title"
251 is( AddModificationTemplateAction(
252 $template_id, 'update_field', 1,
253 '245', 'a', 'Bad title updated', '', '',
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');
259 $record = new_record();
260 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
262 @fields_245a = Koha::SimpleMARC::read_field({
267 is_deeply( \@fields_245a, [
268 'The art of computer programming',
271 ], 'update_field has update first the "Bad title"' );
273 # Duplicate first 245 => 3x245
274 is( AddModificationTemplateAction(
275 $template_id, 'copy_field', 1,
276 '245', '', '', '245', '',
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"');
282 $record = new_record();
283 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
285 @fields_245a = Koha::SimpleMARC::read_field({
290 is_deeply( \@fields_245a, [
291 'The art of computer programming',
295 ], 'Copy field has copied first "^Bad title"' );
297 # Delete first ^Bad title
298 is( AddModificationTemplateAction(
299 $template_id, 'delete_field', 1,
300 '245', '', '', '', '',
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');
306 $record = new_record();
307 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
308 @fields_245a = Koha::SimpleMARC::read_field({
313 is_deeply( \@fields_245a, [
314 'The art of computer programming',
317 ], 'delete field has been deleted the right field"' );
319 is( AddModificationTemplateAction(
320 $template_id, 'delete_field', 0,
321 '245', '', '', '', '',
323 'if', '245', 'a', 'equals', 'updated$', '1',
324 'Delete first 245$a mathing updated$'
325 ), 1, 'Delete first 245$a mathing updated$');
327 $record = new_record();
328 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
329 @fields_245a = Koha::SimpleMARC::read_field({
334 is_deeply( \@fields_245a, [
335 'The art of computer programming',
337 ], 'delete field has been deleted the right field"' );
340 my $record = MARC::Record->new;
341 $record->leader('03174nam a2200445 a 4500');
345 a => 'Knuth, Donald Ervin',
350 a => 'The art of computer programming',
351 c => 'Donald E. Knuth.',
356 c => 'Donald E. Knuth.',
360 a => 'Computer programming.',
365 a => 'Computer programming.',
376 $record->append_fields(@fields);
380 sub expected_record_1 {
381 my $record = MARC::Record->new;
382 $record->leader('03174nam a2200445 a 4500');
386 a => 'Knuth, Donald Ervin',
391 a => 'The art of computer programming',
392 c => 'Donald E. Knuth.',
400 p => '3010023917_updated',
407 a => 'The art of computer programming',
411 a => 'Computer algorithms.',
416 a => 'non existent.',
419 $record->append_fields(@fields);
423 sub expected_record_2 {
424 my $record = MARC::Record->new;
425 $record->leader('03174nam a2200445 a 4500');
429 a => 'Knuth, Donald Ervin',
434 a => 'The art of computer programming',
435 c => 'Donald E. Knuth.',
443 p => '3010023917_updated',
450 a => 'The art of computer programming',
454 a => 'Computer algorithms.',
459 a => 'non existent.',
462 $record->append_fields(@fields);
466 # C4::Context->userenv
468 return { branchcode => 'CPL' };