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