|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 2; |
22 |
use Test::More tests => 3; |
| 23 |
|
23 |
|
| 24 |
use t::lib::TestBuilder; |
24 |
use t::lib::TestBuilder; |
| 25 |
use Test::Exception; |
25 |
use Test::Exception; |
|
Lines 292-294
subtest 'type() tests' => sub {
Link Here
|
| 292 |
|
292 |
|
| 293 |
$schema->storage->txn_rollback; |
293 |
$schema->storage->txn_rollback; |
| 294 |
}; |
294 |
}; |
| 295 |
- |
295 |
|
|
|
296 |
subtest 'merge_and_replace_with' => sub { |
| 297 |
plan tests => 2; |
| 298 |
|
| 299 |
my $schema = Koha::Database->new->schema; |
| 300 |
$schema->storage->txn_begin; |
| 301 |
|
| 302 |
my $patron = $builder->build_object({class=> 'Koha::Patrons'}); |
| 303 |
|
| 304 |
my $unique_attribute_type = $builder->build_object( |
| 305 |
{ |
| 306 |
class => 'Koha::Patron::Attribute::Types', |
| 307 |
value => { unique_id=> 1, repeatable => 0 } |
| 308 |
} |
| 309 |
); |
| 310 |
my $repeatable_attribute_type = $builder->build_object( |
| 311 |
{ |
| 312 |
class => 'Koha::Patron::Attribute::Types', |
| 313 |
value => { unique_id => 0, repeatable => 1 } |
| 314 |
} |
| 315 |
); |
| 316 |
my $normal_attribute_type = $builder->build_object( |
| 317 |
{ |
| 318 |
class => 'Koha::Patron::Attribute::Types', |
| 319 |
value => { unique_id => 0, repeatable => 0 } |
| 320 |
} |
| 321 |
); |
| 322 |
my $non_existent_attribute_type = $builder->build_object( |
| 323 |
{ |
| 324 |
class => 'Koha::Patron::Attribute::Types', |
| 325 |
} |
| 326 |
); |
| 327 |
my $non_existent_attribute_type_code = $non_existent_attribute_type->code; |
| 328 |
$non_existent_attribute_type->delete; |
| 329 |
|
| 330 |
my $attributes = [ |
| 331 |
{ |
| 332 |
attribute => 'my unique attribute 1', |
| 333 |
code => $unique_attribute_type->code(), |
| 334 |
}, |
| 335 |
{ |
| 336 |
attribute => 'my repeatable attribute 1', |
| 337 |
code => $repeatable_attribute_type->code(), |
| 338 |
}, |
| 339 |
{ |
| 340 |
attribute => 'my normal attribute 1', |
| 341 |
code => $normal_attribute_type->code(), |
| 342 |
} |
| 343 |
]; |
| 344 |
$patron->extended_attributes($attributes); |
| 345 |
|
| 346 |
my $new_attributes = [ |
| 347 |
{ |
| 348 |
attribute => 'my repeatable attribute 2', |
| 349 |
code => $repeatable_attribute_type->code(), |
| 350 |
}, |
| 351 |
{ |
| 352 |
attribute => 'my repeatable attribute 3', |
| 353 |
code => $repeatable_attribute_type->code(), |
| 354 |
}, |
| 355 |
{ |
| 356 |
attribute => 'my normal attribute 2', |
| 357 |
code => $normal_attribute_type->code(), |
| 358 |
}, |
| 359 |
{ |
| 360 |
attribute => 'my unique attribute 2', |
| 361 |
code => $unique_attribute_type->code(), |
| 362 |
} |
| 363 |
]; |
| 364 |
|
| 365 |
my $new_extended_attributes = $patron->extended_attributes->merge_and_replace_with($new_attributes); |
| 366 |
|
| 367 |
my $expected = [ |
| 368 |
{ |
| 369 |
attribute => 'my normal attribute 2', # Attribute 1 has been replaced by attribute 2 |
| 370 |
code => $normal_attribute_type->code(), |
| 371 |
}, |
| 372 |
{ |
| 373 |
attribute => 'my unique attribute 2', # Attribute 1 has been replaced by attribute 2 |
| 374 |
code => $unique_attribute_type->code(), |
| 375 |
}, |
| 376 |
{ |
| 377 |
attribute => 'my repeatable attribute 1', |
| 378 |
code => $repeatable_attribute_type->code(), |
| 379 |
}, |
| 380 |
{ |
| 381 |
attribute => 'my repeatable attribute 2', |
| 382 |
code => $repeatable_attribute_type->code(), |
| 383 |
}, |
| 384 |
{ |
| 385 |
attribute => 'my repeatable attribute 3', |
| 386 |
code => $repeatable_attribute_type->code(), |
| 387 |
}, |
| 388 |
]; |
| 389 |
$expected = [ sort { $a->{code} cmp $b->{code} || $a->{attribute} cmp $b->{attribute} } @$expected ]; |
| 390 |
is_deeply($new_extended_attributes, $expected); |
| 391 |
|
| 392 |
|
| 393 |
throws_ok |
| 394 |
{ |
| 395 |
$patron->extended_attributes->merge_and_replace_with( |
| 396 |
[ |
| 397 |
{ code => $non_existent_attribute_type_code, attribute => 'foobar' }, |
| 398 |
] |
| 399 |
); |
| 400 |
} |
| 401 |
'Koha::Exceptions::Patron::Attribute::InvalidType', |
| 402 |
'Exception thrown on invalid attribute type'; |
| 403 |
|
| 404 |
$schema->storage->txn_rollback; |
| 405 |
|
| 406 |
}; |