Bug 9016: (follow-up) fix unit tests
[koha.git] / t / db_dependent / MarcModificationTemplates.t
1 use Modern::Perl;
2
3 use Test::More tests => 74;
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("template_name");
150 like( $template_id, qr|^\d+$|, "new template returns an id" );
151
152 is( AddModificationTemplateAction(
153     $template_id, 'copy_field', 0,
154     '245', 'a', '', '246', 'a',
155     '', '', '',
156     '', '', '', '', '', '',
157     'copy field 245$a to 246$a'
158 ), 1, 'Add first action: copy 245$a to 246$a');
159
160 is( AddModificationTemplateAction(
161     $template_id, 'delete_field', 0,
162     '650', 'a', '', '', '',
163     '', '', '',
164     'if', '650', '9', 'equals', '462', '',
165     'Delete field 650$a if 650$9=462'
166 ), 1, 'Add second action: delete field 650$a if 650$9=462');
167
168 is( AddModificationTemplateAction(
169     $template_id, 'update_field', 0,
170     '952', 'p', '3010023917_updated', '', '',
171     '', '', '',
172     'unless', '650', '9', 'equals', '42', '',
173     'Update field 952$p with "3010023917_updated" if 650$9 != 42'
174 ), 1, 'Add third action: update field 952$p with "3010023917_updated" if 650$9 != 42');
175
176 is( AddModificationTemplateAction(
177     $template_id, 'move_field', 0,
178     '952', 'd', '', '952', 'e', '',
179     '', '', '',
180     'if', '952', 'c', 'equals', '^GEN', '1',
181     'Move field 952$d to 952$e if 952$c =~ /^GE/'
182 ), 1, 'Add fourth action: move field 952$d to 952$e if 952$c =~ /^GE/');
183
184 my $record = new_record();
185
186 ModifyRecordWithTemplate( $template_id, $record );
187
188 my $expected_record = expected_record();
189 is_deeply( $record, $expected_record );
190
191 done_testing;
192
193 sub new_record {
194     my $record = MARC::Record->new;
195     $record->leader('03174nam a2200445 a 4500');
196     my @fields = (
197         MARC::Field->new(
198             100, '1', ' ',
199             a => 'Knuth, Donald Ervin',
200             d => '1938',
201         ),
202         MARC::Field->new(
203             245, '1', '4',
204             a => 'The art of computer programming',
205             c => 'Donald E. Knuth.',
206         ),
207         MARC::Field->new(
208             650, ' ', '0',
209             a => 'Computer programming.',
210             9 => '462',
211         ),
212         MARC::Field->new(
213             952, ' ', ' ',
214             p => '3010023917',
215             y => 'BK',
216             c => 'GEN',
217             d => '2001-06-25',
218         ),
219     );
220     $record->append_fields(@fields);
221     return $record;
222 }
223
224 sub expected_record {
225     my $record = MARC::Record->new;
226     $record->leader('03174nam a2200445 a 4500');
227     my @fields = (
228         MARC::Field->new(
229             100, '1', ' ',
230             a => 'Knuth, Donald Ervin',
231             d => '1938',
232         ),
233         MARC::Field->new(
234             245, '1', '4',
235             a => 'The art of computer programming',
236             c => 'Donald E. Knuth.',
237         ),
238         MARC::Field->new(
239             650, ' ', '0',
240             9 => '462',
241         ),
242         MARC::Field->new(
243             952, ' ', ' ',
244             p => '3010023917_updated',
245             y => 'BK',
246             c => 'GEN',
247             e => '2001-06-25',
248         ),
249         MARC::Field->new(
250             246, '', ' ',
251             a => 'The art of computer programming',
252         ),
253     );
254     $record->append_fields(@fields);
255     return $record;
256 }
257
258
259 # C4::Context->userenv
260 sub Mock_userenv {
261     return { branchcode => 'CPL' };
262 }