Bug 11413: Fix return for ModifyRecordWithTemplate
[koha.git] / t / db_dependent / MarcModificationTemplates.t
1 use Modern::Perl;
2
3 use Test::More tests => 78;
4
5 use_ok("MARC::Field");
6 use_ok("MARC::Record");
7 use_ok("C4::MarcModificationTemplates");
8
9 my $dbh = C4::Context->dbh;
10 $dbh->{AutoCommit} = 0;
11 $dbh->{RaiseError} = 1;
12
13 $dbh->do(q|DELETE FROM marc_modification_templates|);
14
15 # Creation
16 my $template_id = AddModificationTemplate("template_name");
17 like( $template_id, qr|^\d+$|, "new template returns an id" );
18
19 is( AddModificationTemplateAction(
20     $template_id, 'move_field', 1,
21     '464', 'u', '', '464', '3',
22     '', '', '',
23     '', '', '', '', '', '',
24     'move first 464$u to 464$3'
25 ), 1, "Add first action");
26
27 is( AddModificationTemplateAction(
28     $template_id, 'update_field', 0,
29     '099', 't', 'LIV', '', '',
30     '', '', '',
31     'if', '200', 'b', 'equals', 'Text', '',
32     'Update field 099$t with value LIV if 200$b matches "Text"'
33 ), 1, "Add second action");
34
35 is( AddModificationTemplateAction(
36     $template_id, 'copy_field', 0,
37     '606', 'a', '', '607', 'a',
38     '', '', '',
39     'unless', '606', 'a', 'not_equals', '^AJAX', '1',
40     'Copy field 606$a to 607$a unless 606$a matches RegEx m^AJAX'
41 ), 1, "Add third action");
42
43 # Getter
44 my @actions = GetModificationTemplateActions( $template_id );
45 is( @actions, 3, "3 actions are insered");
46
47 for my $action ( @actions ) {
48     isnt( GetModificationTemplateAction( $action->{mmta_id} ), undef, "action with id $action->{mmta_id} exists" );
49 }
50
51 my $first_action = $actions[0];
52 is( $first_action->{ordering}, 1, "test ordering for first action" );
53 is( $first_action->{action}, 'move_field', "test action for first action" );
54 is( $first_action->{from_field}, '464', "test from_field for first action" );
55 is( $first_action->{from_subfield}, 'u', "test from_subfield for first action" );
56 is( $first_action->{to_field}, '464', "test to_field for first action" );
57 is( $first_action->{to_subfield}, '3', "test to_subfield for first action" );
58
59 my $second_action = $actions[1];
60 is( $second_action->{ordering}, 2, "test ordering for second action" );
61 is( $second_action->{action}, 'update_field', "test action for second action" );
62 is( $second_action->{from_field}, '099',"test from_field for second action" );
63 is( $second_action->{from_subfield}, 't', "test from_subfield for second action" );
64 is( $second_action->{field_value}, 'LIV', "test firld_value for second action" );
65 is( $second_action->{to_field}, '', "test to_field for second action" );
66 is( $second_action->{to_subfield}, '', "test to_subfield for second action" );
67 is( $second_action->{conditional}, 'if', "test conditional for second action" );
68 is( $second_action->{conditional_field}, '200', "test conditional_field for second action" );
69 is( $second_action->{conditional_subfield}, 'b', "test conditional_subfield for second action" );
70 is( $second_action->{conditional_comparison}, 'equals', "test conditional_comparison for second action" );
71
72 my $third_action = $actions[2];
73 is( $third_action->{ordering}, 3, "test ordering for third action" );
74 is( $third_action->{action}, 'copy_field', "test  factionor third action" );
75 is( $third_action->{from_field}, '606', "test from_field for third action" );
76 is( $third_action->{from_subfield}, 'a', "test from_subfield for third action" );
77 is( $third_action->{to_field}, '607', "test to_field for third action" );
78 is( $third_action->{to_subfield}, 'a', "test to_subfield for third action" );
79 is( $third_action->{conditional}, 'unless', "test conditional for third action" );
80 is( $third_action->{conditional_field}, '606', "test conditional_field for third action" );
81 is( $third_action->{conditional_subfield}, 'a', "test conditional_subfield for third action" );
82 is( $third_action->{conditional_comparison}, 'not_equals', "test conditional_comparison for third action" );
83 is( $third_action->{conditional_value}, '^AJAX', "test conditional_value for third action" );
84
85
86 # Modifications
87 is( ModModificationTemplateAction(
88     $actions[1]->{mmta_id}, 'update_field', 0,
89     '100', 'u', 'LIV', '', '',
90     '', '', '',
91     'if', '200', 'c', 'equals', 'Text', '',
92     'Update field 099$t with value LIV if 200$b matches "Text"'
93 ), 1, "Modify second action");
94
95 $second_action = GetModificationTemplateAction( $actions[1]->{mmta_id} );
96 is( $second_action->{ordering}, 2, "test ordering for second action modified" );
97 is( $second_action->{action}, 'update_field', "test action for second action modified" );
98 is( $second_action->{from_field}, '100',"test from_field for second action modified" );
99 is( $second_action->{from_subfield}, 'u', "test from_subfield for second action modified" );
100 is( $second_action->{field_value}, 'LIV', "test firld_value for second action modified" );
101 is( $second_action->{to_field}, '', "test to_field for second action modified" );
102 is( $second_action->{to_subfield}, '', "test to_subfield for second action modified" );
103 is( $second_action->{conditional}, 'if', "test conditional for second action modified" );
104 is( $second_action->{conditional_field}, '200', "test conditional_field for second action modified" );
105 is( $second_action->{conditional_subfield}, 'c', "test conditional_subfield for second action modified" );
106 is( $second_action->{conditional_comparison}, 'equals', "test conditional_comparison for second action modified" );
107
108 # Up and down
109 is( MoveModificationTemplateAction( $actions[2]->{mmta_id}, 'top' ), '1', 'Move the third action on top' );
110 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'bottom' ), '1', 'Move the first action on bottom' );
111
112 is( GetModificationTemplateAction( $actions[0]->{mmta_id} )->{ordering}, '3', 'First becomes third' );
113 is( GetModificationTemplateAction( $actions[1]->{mmta_id} )->{ordering}, '2', 'Second stays second' );
114 is( GetModificationTemplateAction( $actions[2]->{mmta_id} )->{ordering}, '1', 'Third becomes first' );
115
116 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'up' ), '1', 'Move up the first action (was third)' );
117 is( MoveModificationTemplateAction( $actions[0]->{mmta_id}, 'up' ), '1', 'Move up the first action (was second)' );
118 is( MoveModificationTemplateAction( $actions[2]->{mmta_id}, 'down' ), '1', 'Move down the third action (was second)' );
119
120 is( GetModificationTemplateAction( $actions[0]->{mmta_id} )->{ordering}, '1', 'First becomes again first' );
121 is( GetModificationTemplateAction( $actions[1]->{mmta_id} )->{ordering}, '2', 'Second stays again second' );
122 is( GetModificationTemplateAction( $actions[2]->{mmta_id} )->{ordering}, '3', 'Third becomes again third' );
123
124 # Cleaning
125 is( DelModificationTemplateAction( $actions[0]->{mmta_id} ), 2, "Delete the first action, 2 others are reordered" );
126 is( GetModificationTemplateAction( $actions[0]->{mmta_id} ), undef, "first action does not exist anymore" );
127
128 is( DelModificationTemplate( $template_id ), 1, "The template has been deleted" );
129
130 is( GetModificationTemplateAction( $actions[1]->{mmta_id} ), undef, "second action does not exist anymore" );
131 is( GetModificationTemplateAction( $actions[2]->{mmta_id} ), undef, "third action does not exist anymore" );
132
133 is( GetModificationTemplateActions( $template_id ), 0, "There is no action for deleted template" );
134
135 # ModifyRecordWithTemplate
136 my @USERENV = (
137     1,
138     'test',
139     'MASTERTEST',
140     'Test',
141     'Test',
142     't',
143     'Test',
144     0,
145 );
146 C4::Context->_new_userenv ('DUMMY_SESSION_ID');
147 C4::Context->set_userenv ( @USERENV );
148
149 $template_id = AddModificationTemplate("new_template_test");
150 like( $template_id, qr|^\d+$|, "new template returns an id" );
151
152 is( AddModificationTemplateAction(
153     $template_id, 'delete_field', 0,
154     '245', '', '', '', '', '',
155     '', '', '',
156     'if', '245', 'a', 'equals', 'Bad title', '',
157     'Delete field 245 if 245$a eq "Bad title"'
158 ), 1, 'Add first action: delete field 245 if 245$a eq "Bad title"');
159
160 is( AddModificationTemplateAction(
161     $template_id, 'copy_field', 0,
162     '245', 'a', '', '246', 'a',
163     '', '', '',
164     '', '', '', '', '', '',
165     'copy field 245$a to 246$a'
166 ), 1, 'Add second action: copy 245$a to 246$a');
167
168 is( AddModificationTemplateAction(
169     $template_id, 'delete_field', 0,
170     '650', 'a', '', '', '',
171     '', '', '',
172     'if', '650', '9', 'equals', '462', '',
173     'Delete field 650$a if 650$9=462'
174 ), 1, 'Add third action: delete field 650$a if 650$9=462');
175
176 is( AddModificationTemplateAction(
177     $template_id, 'update_field', 0,
178     '952', 'p', '3010023917_updated', '', '',
179     '', '', '',
180     'unless', '650', '9', 'equals', '42', '',
181     'Update field 952$p with "3010023917_updated" if 650$9 != 42'
182 ), 1, 'Add fourth action: update field 952$p with "3010023917_updated" if 650$9 != 42');
183
184 is( AddModificationTemplateAction(
185     $template_id, 'move_field', 0,
186     '952', 'd', '', '952', 'e', '',
187     '', '', '',
188     'if', '952', 'c', 'equals', '^GEN', '1',
189     'Move field 952$d to 952$e if 952$c =~ /^GE/'
190 ), 1, 'Add fifth action: move field 952$d to 952$e if 952$c =~ /^GE/');
191
192 is( AddModificationTemplateAction(
193     $template_id, 'update_field', 0,
194     '650', 'a', 'Computer algorithms.', '', '', '',
195     '', '', '',
196     'if', '650', '9', 'equals', '499', '',
197     'Update field 650$a with "Computer algorithms." to 651 if 650$9 == 499'
198 ), 1, 'Add sixth action: update field 650$a with "Computer algorithms." if 650$9 == 499');
199
200 is( AddModificationTemplateAction(
201     $template_id, 'move_field', 0,
202     '650', '', '', '651', '', '',
203     '', '', '',
204     'if', '650', '9', 'equals', '499', '',
205     'Move field 650 to 651 if 650$9 == 499'
206 ), 1, 'Add seventh action: move field 650 to 651 if 650$9 == 499');
207
208
209 my $record = new_record();
210
211 is( ModifyRecordWithTemplate( $template_id, $record ), undef, "The ModifyRecordWithTemplate returns undef" );
212
213 my $expected_record = expected_record();
214 is_deeply( $record, $expected_record, "Record modification as expected");
215
216 sub new_record {
217     my $record = MARC::Record->new;
218     $record->leader('03174nam a2200445 a 4500');
219     my @fields = (
220         MARC::Field->new(
221             100, '1', ' ',
222             a => 'Knuth, Donald Ervin',
223             d => '1938',
224         ),
225         MARC::Field->new(
226             245, '1', '4',
227             a => 'The art of computer programming',
228             c => 'Donald E. Knuth.',
229         ),
230         MARC::Field->new(
231             245, '1', '4',
232             a => 'field to remove',
233             c => 'Donald E. Knuth.',
234         ),
235         MARC::Field->new(
236             650, ' ', '0',
237             a => 'Computer programming.',
238             9 => '462',
239         ),
240         MARC::Field->new(
241             650, ' ', '0',
242             a => 'Computer programming.',
243             9 => '499',
244         ),
245         MARC::Field->new(
246             952, ' ', ' ',
247             p => '3010023917',
248             y => 'BK',
249             c => 'GEN',
250             d => '2001-06-25',
251         ),
252     );
253     $record->append_fields(@fields);
254     return $record;
255 }
256
257 sub expected_record {
258     my $record = MARC::Record->new;
259     $record->leader('03174nam a2200445 a 4500');
260     my @fields = (
261         MARC::Field->new(
262             100, '1', ' ',
263             a => 'Knuth, Donald Ervin',
264             d => '1938',
265         ),
266         MARC::Field->new(
267             245, '1', '4',
268             a => 'The art of computer programming',
269             c => 'Donald E. Knuth.',
270         ),
271         MARC::Field->new(
272             650, ' ', '0',
273             9 => '462',
274         ),
275         MARC::Field->new(
276             952, ' ', ' ',
277             p => '3010023917_updated',
278             y => 'BK',
279             c => 'GEN',
280             e => '2001-06-25',
281         ),
282         MARC::Field->new(
283             246, '', ' ',
284             a => 'The art of computer programming',
285         ),
286         MARC::Field->new(
287             651, ' ', '0',
288             a => 'Computer algorithms.',
289             9 => '499',
290         ),
291     );
292     $record->append_fields(@fields);
293     return $record;
294 }
295
296
297 # C4::Context->userenv
298 sub Mock_userenv {
299     return { branchcode => 'CPL' };
300 }