Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
use Try::Tiny; |
20 |
|
21 |
use MARC::Record; |
22 |
|
23 |
use C4::Context; |
24 |
use C4::Biblio; |
25 |
use Koha::Database; #?? |
26 |
|
27 |
use Test::More tests => 23; |
28 |
use Test::MockModule; |
29 |
|
30 |
use Koha::MarcMergeRules; |
31 |
use Koha::MarcMergeRulesModules; |
32 |
|
33 |
use t::lib::Mocks; |
34 |
|
35 |
my $schema = Koha::Database->schema; |
36 |
$schema->storage->txn_begin; |
37 |
|
38 |
t::lib::Mocks::mock_preference('MARCMergeRules', '1'); |
39 |
|
40 |
# Create a record |
41 |
my $orig_record = MARC::Record->new(); |
42 |
$orig_record->append_fields ( |
43 |
MARC::Field->new('250', '','', 'a' => '250 bottles of beer on the wall'), |
44 |
MARC::Field->new('250', '','', 'a' => '256 bottles of beer on the wall'), |
45 |
MARC::Field->new('500', '','', 'a' => 'One bottle of beer in the fridge'), |
46 |
); |
47 |
|
48 |
my $modules_rs = Koha::MarcMergeRulesModules->search(undef, { order_by => { -desc => 'specificity' } }); |
49 |
my @modules; |
50 |
push @modules, $modules_rs->next; |
51 |
|
52 |
my $incoming_record = MARC::Record->new(); |
53 |
$incoming_record->append_fields( |
54 |
MARC::Field->new('250', '', '', 'a' => '256 bottles of beer on the wall'), # Unchanged |
55 |
MARC::Field->new('250', '', '', 'a' => '251 bottles of beer on the wall'), # Appended |
56 |
# MARC::Field->new('250', '', '', 'a' => '250 bottles of beer on the wall'), # Removed |
57 |
# MARC::Field->new('500', '', '', 'a' => 'One bottle of beer in the fridge'), # Deleted |
58 |
MARC::Field->new('501', '', '', 'a' => 'One cold bottle of beer in the fridge'), # Added |
59 |
MARC::Field->new('501', '', '', 'a' => 'Two cold bottles of beer in the fridge'), # Added |
60 |
); |
61 |
|
62 |
# Test default behavior when MARCMergeRules is enabled, but no rules defined (overwrite) |
63 |
subtest 'Record fields has been overwritten when no merge rules are defined' => sub { |
64 |
plan tests => 4; |
65 |
|
66 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
67 |
|
68 |
my @all_fields = $merged_record->fields(); |
69 |
|
70 |
cmp_ok(scalar @all_fields, '==', 4, "Record has the expected number of fields"); |
71 |
is_deeply( |
72 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
73 |
['256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
74 |
'"250" fields has been appended and removed' |
75 |
); |
76 |
|
77 |
my @fields = $merged_record->field('500'); |
78 |
cmp_ok(scalar @fields, '==', 0, '"500" field has been deleted'); |
79 |
|
80 |
is_deeply( |
81 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
82 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
83 |
'"501" fields has been added' |
84 |
); |
85 |
}; |
86 |
|
87 |
my $rule = Koha::MarcMergeRules->find_or_create({ |
88 |
tag => '*', |
89 |
module => $modules[0]->name, |
90 |
filter => '*', |
91 |
add => 0, |
92 |
append => 0, |
93 |
remove => 0, |
94 |
delete => 0 |
95 |
}); |
96 |
|
97 |
subtest 'Record fields has been protected when matched merge all rule operations are set to "0"' => sub { |
98 |
plan tests => 3; |
99 |
|
100 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
101 |
|
102 |
my @all_fields = $merged_record->fields(); |
103 |
cmp_ok(scalar @all_fields, '==', 3, "Record has the expected number of fields"); |
104 |
|
105 |
is_deeply( |
106 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
107 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall'], |
108 |
'"250" fields has retained their original value' |
109 |
); |
110 |
is_deeply( |
111 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
112 |
['One bottle of beer in the fridge'], |
113 |
'"500" field has retained it\'s original value' |
114 |
); |
115 |
}; |
116 |
|
117 |
subtest 'Only new fields has been added when add = 1, append = 0, remove = 0, delete = 0' => sub { |
118 |
plan tests => 4; |
119 |
|
120 |
$rule->set( |
121 |
{ |
122 |
'add' => 1, |
123 |
'append' => 0, |
124 |
'remove' => 0, |
125 |
'delete' => 0, |
126 |
} |
127 |
); |
128 |
$rule->store(); |
129 |
|
130 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
131 |
|
132 |
my @all_fields = $merged_record->fields(); |
133 |
cmp_ok(scalar @all_fields, '==', 5, "Record has the expected number of fields"); |
134 |
|
135 |
is_deeply( |
136 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
137 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall'], |
138 |
'"250" fields retain their original value' |
139 |
); |
140 |
|
141 |
is_deeply( |
142 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
143 |
['One bottle of beer in the fridge'], |
144 |
'"500" field retain it\'s original value' |
145 |
); |
146 |
|
147 |
is_deeply( |
148 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
149 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
150 |
'"501" fields has been added' |
151 |
); |
152 |
}; |
153 |
|
154 |
subtest 'Only appended fields has been added when add = 0, append = 1, remove = 0, delete = 0' => sub { |
155 |
plan tests => 3; |
156 |
|
157 |
$rule->set( |
158 |
{ |
159 |
'add' => 0, |
160 |
'append' => 1, |
161 |
'remove' => 0, |
162 |
'delete' => 0, |
163 |
} |
164 |
); |
165 |
$rule->store(); |
166 |
|
167 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
168 |
|
169 |
my @all_fields = $merged_record->fields(); |
170 |
cmp_ok(scalar @all_fields, '==', 4, "Record has the expected number of fields"); |
171 |
|
172 |
is_deeply( |
173 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
174 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
175 |
'"251" field has been appended' |
176 |
); |
177 |
|
178 |
is_deeply( |
179 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
180 |
['One bottle of beer in the fridge'], |
181 |
'"500" field has retained it\'s original value' |
182 |
); |
183 |
|
184 |
}; |
185 |
|
186 |
subtest 'Appended and added fields has been added when add = 1, append = 1, remove = 0, delete = 0' => sub { |
187 |
plan tests => 4; |
188 |
|
189 |
$rule->set( |
190 |
{ |
191 |
'add' => 1, |
192 |
'append' => 1, |
193 |
'remove' => 0, |
194 |
'delete' => 0, |
195 |
} |
196 |
); |
197 |
$rule->store(); |
198 |
|
199 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
200 |
|
201 |
my @all_fields = $merged_record->fields(); |
202 |
cmp_ok(scalar @all_fields, '==', 6, "Record has the expected number of fields"); |
203 |
|
204 |
is_deeply( |
205 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
206 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
207 |
'"251" field has been appended' |
208 |
); |
209 |
|
210 |
is_deeply( |
211 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
212 |
['One bottle of beer in the fridge'], |
213 |
'"500" field has retained it\'s original value' |
214 |
); |
215 |
|
216 |
is_deeply( |
217 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
218 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
219 |
'"501" fields has been added' |
220 |
); |
221 |
}; |
222 |
|
223 |
subtest 'Record fields has been only removed when add = 0, append = 0, remove = 1, delete = 0' => sub { |
224 |
plan tests => 3; |
225 |
|
226 |
$rule->set( |
227 |
{ |
228 |
'add' => 0, |
229 |
'append' => 0, |
230 |
'remove' => 1, |
231 |
'delete' => 0, |
232 |
} |
233 |
); |
234 |
$rule->store(); |
235 |
|
236 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
237 |
|
238 |
my @all_fields = $merged_record->fields(); |
239 |
cmp_ok(scalar @all_fields, '==', 2, "Record has the expected number of fields"); |
240 |
|
241 |
is_deeply( |
242 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
243 |
['256 bottles of beer on the wall'], |
244 |
'"250" field has been removed' |
245 |
); |
246 |
is_deeply( |
247 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
248 |
['One bottle of beer in the fridge'], |
249 |
'"500" field has retained it\'s original value' |
250 |
); |
251 |
}; |
252 |
|
253 |
subtest 'Record fields has been added and removed when add = 1, append = 0, remove = 1, delete = 0' => sub { |
254 |
plan tests => 4; |
255 |
|
256 |
$rule->set( |
257 |
{ |
258 |
'add' => 1, |
259 |
'append' => 0, |
260 |
'remove' => 1, |
261 |
'delete' => 0, |
262 |
} |
263 |
); |
264 |
$rule->store(); |
265 |
|
266 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
267 |
|
268 |
my @all_fields = $merged_record->fields(); |
269 |
cmp_ok(scalar @all_fields, '==', 4, "Record has the expected number of fields"); |
270 |
|
271 |
is_deeply( |
272 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
273 |
['256 bottles of beer on the wall'], |
274 |
'"250" field has been removed' |
275 |
); |
276 |
is_deeply( |
277 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
278 |
['One bottle of beer in the fridge'], |
279 |
'"500" field has retained it\'s original value' |
280 |
); |
281 |
|
282 |
is_deeply( |
283 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
284 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
285 |
'"501" fields has been added' |
286 |
); |
287 |
}; |
288 |
|
289 |
subtest 'Record fields has been appended and removed when add = 0, append = 1, remove = 1, delete = 0' => sub { |
290 |
plan tests => 3; |
291 |
|
292 |
$rule->set( |
293 |
{ |
294 |
'add' => 0, |
295 |
'append' => 1, |
296 |
'remove' => 1, |
297 |
'delete' => 0, |
298 |
} |
299 |
); |
300 |
$rule->store(); |
301 |
|
302 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
303 |
|
304 |
my @all_fields = $merged_record->fields(); |
305 |
cmp_ok(scalar @all_fields, '==', 3, "Record has the expected number of fields"); |
306 |
|
307 |
is_deeply( |
308 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
309 |
['256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
310 |
'"250" fields has been appended and removed' |
311 |
); |
312 |
is_deeply( |
313 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
314 |
['One bottle of beer in the fridge'], |
315 |
'"500" field has retained it\'s original value' |
316 |
); |
317 |
}; |
318 |
|
319 |
subtest 'Record fields has been added, appended and removed when add = 0, append = 1, remove = 1, delete = 0' => sub { |
320 |
plan tests => 4; |
321 |
|
322 |
$rule->set( |
323 |
{ |
324 |
'add' => 1, |
325 |
'append' => 1, |
326 |
'remove' => 1, |
327 |
'delete' => 0, |
328 |
} |
329 |
); |
330 |
$rule->store(); |
331 |
|
332 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
333 |
|
334 |
my @all_fields = $merged_record->fields(); |
335 |
cmp_ok(scalar @all_fields, '==', 5, "Record has the expected number of fields"); |
336 |
|
337 |
is_deeply( |
338 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
339 |
['256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
340 |
'"250" fields has been appended and removed' |
341 |
); |
342 |
|
343 |
is_deeply( |
344 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
345 |
['One bottle of beer in the fridge'], |
346 |
'"500" field has retained it\'s original value' |
347 |
); |
348 |
|
349 |
is_deeply( |
350 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
351 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
352 |
'"501" fields has been added' |
353 |
); |
354 |
}; |
355 |
|
356 |
subtest 'Record fields has been deleted when add = 0, append = 0, remove = 0, delete = 1' => sub { |
357 |
plan tests => 2; |
358 |
|
359 |
$rule->set( |
360 |
{ |
361 |
'add' => 0, |
362 |
'append' => 0, |
363 |
'remove' => 0, |
364 |
'delete' => 1, |
365 |
} |
366 |
); |
367 |
$rule->store(); |
368 |
|
369 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
370 |
|
371 |
my @all_fields = $merged_record->fields(); |
372 |
cmp_ok(scalar @all_fields, '==', 2, "Record has the expected number of fields"); |
373 |
|
374 |
is_deeply( |
375 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
376 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall'], |
377 |
'"250" fields has retained their original value' |
378 |
); |
379 |
}; |
380 |
|
381 |
subtest 'Record fields has been added and deleted when add = 1, append = 0, remove = 0, delete = 1' => sub { |
382 |
plan tests => 3; |
383 |
|
384 |
$rule->set( |
385 |
{ |
386 |
'add' => 1, |
387 |
'append' => 0, |
388 |
'remove' => 0, |
389 |
'delete' => 1, |
390 |
} |
391 |
); |
392 |
$rule->store(); |
393 |
|
394 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
395 |
|
396 |
my @all_fields = $merged_record->fields(); |
397 |
cmp_ok(scalar @all_fields, '==', 4, "Record has the expected number of fields"); |
398 |
|
399 |
is_deeply( |
400 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
401 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall'], |
402 |
'"250" fields has retained their original value' |
403 |
); |
404 |
|
405 |
is_deeply( |
406 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
407 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
408 |
'"501" fields has been added' |
409 |
); |
410 |
}; |
411 |
|
412 |
subtest 'Record fields has been appended and deleted when add = 0, append = 1, remove = 0, delete = 1' => sub { |
413 |
plan tests => 2; |
414 |
|
415 |
$rule->set( |
416 |
{ |
417 |
'add' => 0, |
418 |
'append' => 1, |
419 |
'remove' => 0, |
420 |
'delete' => 1, |
421 |
} |
422 |
); |
423 |
$rule->store(); |
424 |
|
425 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
426 |
|
427 |
my @all_fields = $merged_record->fields(); |
428 |
cmp_ok(scalar @all_fields, '==', 3, "Record has the expected number of fields"); |
429 |
|
430 |
is_deeply( |
431 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
432 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
433 |
'"250" field has been appended' |
434 |
); |
435 |
}; |
436 |
|
437 |
subtest 'Record fields has been added, appended and deleted when add = 1, append = 1, remove = 0, delete = 1' => sub { |
438 |
plan tests => 3; |
439 |
|
440 |
$rule->set( |
441 |
{ |
442 |
'add' => 1, |
443 |
'append' => 1, |
444 |
'remove' => 0, |
445 |
'delete' => 1, |
446 |
} |
447 |
); |
448 |
$rule->store(); |
449 |
|
450 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
451 |
|
452 |
my @all_fields = $merged_record->fields(); |
453 |
cmp_ok(scalar @all_fields, '==', 5, "Record has the expected number of fields"); |
454 |
|
455 |
is_deeply( |
456 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
457 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
458 |
'"250" field has been appended' |
459 |
); |
460 |
|
461 |
is_deeply( |
462 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
463 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
464 |
'"501" fields has been added' |
465 |
); |
466 |
}; |
467 |
|
468 |
subtest 'Record fields has been removed and deleted when add = 0, append = 0, remove = 1, delete = 1' => sub { |
469 |
plan tests => 2; |
470 |
|
471 |
$rule->set( |
472 |
{ |
473 |
'add' => 0, |
474 |
'append' => 0, |
475 |
'remove' => 1, |
476 |
'delete' => 1, |
477 |
} |
478 |
); |
479 |
$rule->store(); |
480 |
|
481 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
482 |
|
483 |
my @all_fields = $merged_record->fields(); |
484 |
cmp_ok(scalar @all_fields, '==', 1, "Record has the expected number of fields"); |
485 |
|
486 |
is_deeply( |
487 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
488 |
['256 bottles of beer on the wall'], |
489 |
'"250" field has been removed' |
490 |
); |
491 |
}; |
492 |
|
493 |
subtest 'Record fields has been added, removed and deleted when add = 1, append = 0, remove = 1, delete = 1' => sub { |
494 |
plan tests => 3; |
495 |
|
496 |
$rule->set( |
497 |
{ |
498 |
'add' => 1, |
499 |
'append' => 0, |
500 |
'remove' => 1, |
501 |
'delete' => 1, |
502 |
} |
503 |
); |
504 |
$rule->store(); |
505 |
|
506 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
507 |
|
508 |
my @all_fields = $merged_record->fields(); |
509 |
cmp_ok(scalar @all_fields, '==', 3, "Record has the expected number of fields"); |
510 |
|
511 |
is_deeply( |
512 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
513 |
['256 bottles of beer on the wall'], |
514 |
'"250" field has been removed' |
515 |
); |
516 |
|
517 |
is_deeply( |
518 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
519 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
520 |
'"501" fields has been added' |
521 |
); |
522 |
}; |
523 |
|
524 |
subtest 'Record fields has been appended, removed and deleted when add = 0, append = 1, remove = 1, delete = 1' => sub { |
525 |
plan tests => 2; |
526 |
|
527 |
$rule->set( |
528 |
{ |
529 |
'add' => 0, |
530 |
'append' => 1, |
531 |
'remove' => 1, |
532 |
'delete' => 1, |
533 |
} |
534 |
); |
535 |
$rule->store(); |
536 |
|
537 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
538 |
|
539 |
my @all_fields = $merged_record->fields(); |
540 |
cmp_ok(scalar @all_fields, '==', 2, "Record has the expected number of fields"); |
541 |
|
542 |
is_deeply( |
543 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
544 |
['256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
545 |
'"250" fields has been appended and removed' |
546 |
); |
547 |
}; |
548 |
|
549 |
subtest 'Record fields has been overwritten when add = 1, append = 1, remove = 1, delete = 1' => sub { |
550 |
plan tests => 4; |
551 |
|
552 |
$rule->set( |
553 |
{ |
554 |
'add' => 1, |
555 |
'append' => 1, |
556 |
'remove' => 1, |
557 |
'delete' => 1, |
558 |
} |
559 |
); |
560 |
$rule->store(); |
561 |
|
562 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
563 |
|
564 |
my @all_fields = $merged_record->fields(); |
565 |
|
566 |
cmp_ok(scalar @all_fields, '==', 4, "Record has the expected number of fields"); |
567 |
is_deeply( |
568 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
569 |
['256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
570 |
'"250" fields has been appended and removed' |
571 |
); |
572 |
|
573 |
my @fields = $merged_record->field('500'); |
574 |
cmp_ok(scalar @fields, '==', 0, '"500" field has been deleted'); |
575 |
|
576 |
is_deeply( |
577 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
578 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
579 |
'"501" fields has been added' |
580 |
); |
581 |
}; |
582 |
|
583 |
# Test rule tag specificity |
584 |
|
585 |
# Protect field 500 with more specific tag value |
586 |
my $skip_all_rule = Koha::MarcMergeRules->find_or_create({ |
587 |
tag => '500', |
588 |
module => $modules[0]->name, |
589 |
filter => '*', |
590 |
add => 0, |
591 |
append => 0, |
592 |
remove => 0, |
593 |
delete => 0 |
594 |
}); |
595 |
|
596 |
subtest '"500" field has been protected when rule matching on tag "500" is add = 0, append = 0, remove = 0, delete = 0' => sub { |
597 |
plan tests => 4; |
598 |
|
599 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
600 |
|
601 |
my @all_fields = $merged_record->fields(); |
602 |
|
603 |
cmp_ok(scalar @all_fields, '==', 5, "Record has the expected number of fields"); |
604 |
is_deeply( |
605 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
606 |
['256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
607 |
'"250" fields has been appended and removed' |
608 |
); |
609 |
|
610 |
is_deeply( |
611 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
612 |
['One bottle of beer in the fridge'], |
613 |
'"500" field has retained it\'s original value' |
614 |
); |
615 |
|
616 |
is_deeply( |
617 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
618 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
619 |
'"501" fields has been added' |
620 |
); |
621 |
}; |
622 |
|
623 |
# Test regexp matching |
624 |
subtest '"5XX" fields has been protected when rule matching on regexp "5\d{2}" is add = 0, append = 0, remove = 0, delete = 0' => sub { |
625 |
plan tests => 3; |
626 |
|
627 |
$skip_all_rule->set( |
628 |
{ |
629 |
'tag' => '5\d{2}', |
630 |
} |
631 |
); |
632 |
$skip_all_rule->store(); |
633 |
|
634 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
635 |
|
636 |
my @all_fields = $merged_record->fields(); |
637 |
|
638 |
cmp_ok(scalar @all_fields, '==', 3, "Record has the expected number of fields"); |
639 |
is_deeply( |
640 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
641 |
['256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
642 |
'"250" fields has been appended and removed' |
643 |
); |
644 |
|
645 |
is_deeply( |
646 |
[map { $_->subfield('a') } $merged_record->field('500') ], |
647 |
['One bottle of beer in the fridge'], |
648 |
'"500" field has retained it\'s original value' |
649 |
); |
650 |
}; |
651 |
|
652 |
# Test module specificity, the 0 all rule should no longer be included in set of applied rules |
653 |
subtest 'Record fields has been overwritten when non wild card rule with filter match is add = 1, append = 1, remove = 1, delete = 1' => sub { |
654 |
plan tests => 4; |
655 |
|
656 |
$rule->set( |
657 |
{ |
658 |
'filter' => 'test', |
659 |
} |
660 |
); |
661 |
$rule->store(); |
662 |
|
663 |
my $merged_record = Koha::MarcMergeRules->merge_records($orig_record, $incoming_record, { $modules[0]->name => 'test' }); |
664 |
|
665 |
my @all_fields = $merged_record->fields(); |
666 |
|
667 |
cmp_ok(scalar @all_fields, '==', 4, "Record has the expected number of fields"); |
668 |
is_deeply( |
669 |
[map { $_->subfield('a') } $merged_record->field('250') ], |
670 |
['256 bottles of beer on the wall', '251 bottles of beer on the wall'], |
671 |
'"250" fields has been appended and removed' |
672 |
); |
673 |
|
674 |
my @fields = $merged_record->field('500'); |
675 |
cmp_ok(scalar @fields, '==', 0, '"500" field has been deleted'); |
676 |
|
677 |
is_deeply( |
678 |
[map { $_->subfield('a') } $merged_record->field('501') ], |
679 |
['One cold bottle of beer in the fridge', 'Two cold bottles of beer in the fridge'], |
680 |
'"501" fields has been added' |
681 |
); |
682 |
}; |
683 |
|
684 |
subtest 'An exception is thrown when append = 1, remove = 0 is set for control field rule' => sub { |
685 |
plan tests => 2; |
686 |
my $exception = try { |
687 |
Koha::MarcMergeRules->validate({ |
688 |
'tag' => '008', |
689 |
'append' => 1, |
690 |
'remove' => 0, |
691 |
}); |
692 |
} |
693 |
catch { |
694 |
return $_; |
695 |
}; |
696 |
ok(defined $exception, "Exception was caught"); |
697 |
ok($exception->isa('Koha::Exceptions::MarcMergeRule::InvalidControlFieldActions'), "Exception is of correct class"); |
698 |
}; |
699 |
|
700 |
subtest 'An exception is thrown when rule tag is set to invalid regexp' => sub { |
701 |
plan tests => 2; |
702 |
|
703 |
my $exception = try { |
704 |
Koha::MarcMergeRules->validate({ |
705 |
'tag' => '**' |
706 |
}); |
707 |
} |
708 |
catch { |
709 |
return $_; |
710 |
}; |
711 |
ok(defined $exception, "Exception was caught"); |
712 |
ok($exception->isa('Koha::Exceptions::MarcMergeRule::InvalidTagRegExp'), "Exception is of correct class"); |
713 |
}; |
714 |
|
715 |
$skip_all_rule->delete(); |
716 |
|
717 |
subtest 'context option in ModBiblio is handled correctly' => sub { |
718 |
plan tests => 6; |
719 |
$rule->set( |
720 |
{ |
721 |
tag => '250', |
722 |
module => $modules[0]->name, |
723 |
filter => '*', |
724 |
'add' => 0, |
725 |
'append' => 0, |
726 |
'remove' => 0, |
727 |
'delete' => 0, |
728 |
} |
729 |
); |
730 |
$rule->store(); |
731 |
my ($biblionumber) = AddBiblio($orig_record, ''); |
732 |
|
733 |
# Since marc merc rules are not run on save, only update |
734 |
# saved record should be identical to orig_record |
735 |
my $saved_record = GetMarcBiblio({ biblionumber => $biblionumber }); |
736 |
|
737 |
my @all_fields = $saved_record->fields(); |
738 |
# Koha also adds 999c field, therefore 4 not 3 |
739 |
cmp_ok(scalar @all_fields, '==', 4, 'Saved record has the expected number of fields'); |
740 |
is_deeply( |
741 |
[map { $_->subfield('a') } $saved_record->field('250') ], |
742 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall'], |
743 |
'All "250" fields of saved record are identical to original record passed to AddBiblio' |
744 |
); |
745 |
|
746 |
is_deeply( |
747 |
[map { $_->subfield('a') } $saved_record->field('500') ], |
748 |
['One bottle of beer in the fridge'], |
749 |
'All "500" fields of saved record are identical to original record passed to AddBiblio' |
750 |
); |
751 |
|
752 |
$saved_record->append_fields( |
753 |
MARC::Field->new('250', '', '', 'a' => '251 bottles of beer on the wall'), # Appended |
754 |
MARC::Field->new('500', '', '', 'a' => 'One cold bottle of beer in the fridge'), # Appended |
755 |
); |
756 |
|
757 |
ModBiblio($saved_record, $biblionumber, '', { context => { $modules[0]->name => 'test' } }); |
758 |
|
759 |
my $updated_record = GetMarcBiblio({ biblionumber => $biblionumber }); |
760 |
|
761 |
@all_fields = $updated_record->fields(); |
762 |
cmp_ok(scalar @all_fields, '==', 5, 'Updated record has the expected number of fields'); |
763 |
is_deeply( |
764 |
[map { $_->subfield('a') } $updated_record->field('250') ], |
765 |
['250 bottles of beer on the wall', '256 bottles of beer on the wall'], |
766 |
'"250" fields have retained their original values' |
767 |
); |
768 |
|
769 |
is_deeply( |
770 |
[map { $_->subfield('a') } $updated_record->field('500') ], |
771 |
['One bottle of beer in the fridge', 'One cold bottle of beer in the fridge'], |
772 |
'"500" field has been appended' |
773 |
); |
774 |
|
775 |
# To trigger removal from search index etc |
776 |
DelBiblio($biblionumber); |
777 |
}; |
778 |
|
779 |
# Explicityly delete rule to trigger clearing of cache |
780 |
$rule->delete(); |
781 |
|
782 |
$schema->storage->txn_rollback; |
783 |
|
784 |
1; |