Bug 15839: Koha::Reviews - Remove approvereview & unapprovereview
[koha.git] / t / db_dependent / MarcModificationTemplates.t
1 use Modern::Perl;
2
3 use Test::More tests => 95;
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 sub new_record {
350     my $record = MARC::Record->new;
351     $record->leader('03174nam a2200445 a 4500');
352     my @fields = (
353         MARC::Field->new(
354             100, '1', ' ',
355             a => 'Knuth, Donald Ervin',
356             d => '1938',
357         ),
358         MARC::Field->new(
359             245, '1', '4',
360             a => 'The art of computer programming',
361             c => 'Donald E. Knuth.',
362         ),
363         MARC::Field->new(
364             245, '1', '4',
365             a => 'Bad title',
366             c => 'Donald E. Knuth.',
367         ),
368         MARC::Field->new(
369             650, ' ', '0',
370             a => 'Computer programming.',
371             9 => '462',
372         ),
373         MARC::Field->new(
374             650, ' ', '0',
375             a => 'Computer programming.',
376             9 => '499',
377         ),
378         MARC::Field->new(
379             952, ' ', ' ',
380             p => '3010023917',
381             y => 'BK',
382             c => 'GEN',
383             d => '2001-06-25',
384         ),
385     );
386     $record->append_fields(@fields);
387     return $record;
388 }
389
390 sub expected_record_1 {
391     my $record = MARC::Record->new;
392     $record->leader('03174nam a2200445 a 4500');
393     my @fields = (
394         MARC::Field->new(
395             100, '1', ' ',
396             a => 'Knuth, Donald Ervin',
397             d => '1938',
398         ),
399         MARC::Field->new(
400             245, '1', '4',
401             a => 'The art of computer programming',
402             c => 'Donald E. Knuth.',
403         ),
404         MARC::Field->new(
405             650, ' ', '0',
406             9 => '462',
407         ),
408         MARC::Field->new(
409             952, ' ', ' ',
410             p => '3010023917_updated',
411             y => 'BK',
412             c => 'GEN',
413             e => '2001-06-25',
414         ),
415         MARC::Field->new(
416             246, '', ' ',
417             a => 'The art of computer programming',
418         ),
419         MARC::Field->new(
420             651, ' ', '0',
421             a => 'Computer algorithms.',
422             9 => '499',
423         ),
424         MARC::Field->new(
425             999, ' ', ' ',
426             a => 'non existent.',
427         ),
428     );
429     $record->append_fields(@fields);
430     return $record;
431 }
432
433 sub expected_record_2 {
434     my $record = MARC::Record->new;
435     $record->leader('03174nam a2200445 a 4500');
436     my @fields = (
437         MARC::Field->new(
438             100, '1', ' ',
439             a => 'Knuth, Donald Ervin',
440             d => '1938',
441         ),
442         MARC::Field->new(
443             245, '1', '4',
444             a => 'The art of computer programming',
445             c => 'Donald E. Knuth.',
446         ),
447         MARC::Field->new(
448             650, ' ', '0',
449             9 => '462',
450         ),
451         MARC::Field->new(
452             952, ' ', ' ',
453             p => '3010023917_updated',
454             y => 'BK',
455             c => 'GEN',
456             e => '2001-06-25',
457         ),
458         MARC::Field->new(
459             246, '', ' ',
460             a => 'The art of computer programming',
461         ),
462         MARC::Field->new(
463             651, ' ', '0',
464             a => 'Computer algorithms.',
465             9 => '499',
466         ),
467         MARC::Field->new(
468             999, ' ', ' ',
469             a => 'non existent.',
470         ),
471     );
472     $record->append_fields(@fields);
473     return $record;
474 }
475
476 # C4::Context->userenv
477 sub Mock_userenv {
478     return { branchcode => 'CPL' };
479 }