|
Lines 17-23
Link Here
|
| 17 |
|
17 |
|
| 18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
| 19 |
|
19 |
|
| 20 |
use Test::More tests => 4; |
20 |
use Test::More tests => 5; |
| 21 |
use Test::Mojo; |
21 |
use Test::Mojo; |
| 22 |
|
22 |
|
| 23 |
use t::lib::TestBuilder; |
23 |
use t::lib::TestBuilder; |
|
Lines 394-396
subtest 'delete() tests' => sub {
Link Here
|
| 394 |
|
394 |
|
| 395 |
$schema->storage->txn_rollback; |
395 |
$schema->storage->txn_rollback; |
| 396 |
}; |
396 |
}; |
| 397 |
- |
397 |
|
|
|
398 |
subtest 'update() tests' => sub { |
| 399 |
|
| 400 |
plan tests => 12; |
| 401 |
|
| 402 |
$schema->storage->txn_begin; |
| 403 |
|
| 404 |
my $patron = $builder->build_object( |
| 405 |
{ |
| 406 |
class => 'Koha::Patrons', |
| 407 |
value => { flags => 2**4 } # 'borrowers' flag == 4 |
| 408 |
} |
| 409 |
); |
| 410 |
my $password = 'thePassword123'; |
| 411 |
$patron->set_password( { password => $password, skip_validation => 1 } ); |
| 412 |
my $userid = $patron->userid; |
| 413 |
|
| 414 |
my $repeatable_attr_type = $builder->build_object( |
| 415 |
{ |
| 416 |
class => 'Koha::Patron::Attribute::Types', |
| 417 |
value => { |
| 418 |
mandatory => 0, |
| 419 |
repeatable => 1, |
| 420 |
unique_id => 0, |
| 421 |
category_code => undef |
| 422 |
} |
| 423 |
} |
| 424 |
); |
| 425 |
my $unique_attr_type = $builder->build_object( |
| 426 |
{ |
| 427 |
class => 'Koha::Patron::Attribute::Types', |
| 428 |
value => { |
| 429 |
mandatory => 0, |
| 430 |
repeatable => 0, |
| 431 |
unique_id => 1, |
| 432 |
category_code => undef |
| 433 |
} |
| 434 |
} |
| 435 |
); |
| 436 |
|
| 437 |
# Add a unique attribute to our patron |
| 438 |
my $unique_attribute = $patron->add_extended_attribute( |
| 439 |
{ |
| 440 |
code => $unique_attr_type->code, |
| 441 |
attribute => 'WOW' |
| 442 |
} |
| 443 |
); |
| 444 |
|
| 445 |
# Let's have an attribute ID we are sure doesn't exist on the DB |
| 446 |
my $non_existent_attribute = $patron->add_extended_attribute( |
| 447 |
{ |
| 448 |
code => $repeatable_attr_type->code, |
| 449 |
attribute => 'BOO' |
| 450 |
} |
| 451 |
); |
| 452 |
my $non_existent_attribute_id = $non_existent_attribute->id; |
| 453 |
$non_existent_attribute->delete; |
| 454 |
|
| 455 |
my $non_existent_patron = |
| 456 |
$builder->build_object( { class => 'Koha::Patrons' } ); |
| 457 |
my $non_existent_patron_id = $non_existent_patron->id; |
| 458 |
|
| 459 |
# get rid of the patron |
| 460 |
$non_existent_patron->delete; |
| 461 |
|
| 462 |
$t->patch_ok( "//$userid:$password@/api/v1/patrons/" |
| 463 |
. $non_existent_patron_id |
| 464 |
. '/extended_attributes/' |
| 465 |
. 123 => json => { value => 'something' } )->status_is(404) |
| 466 |
->json_is( '/error' => 'Patron not found' ); |
| 467 |
|
| 468 |
$t->patch_ok( "//$userid:$password@/api/v1/patrons/" |
| 469 |
. $patron->id |
| 470 |
. '/extended_attributes/' |
| 471 |
. $non_existent_attribute_id => json => { value => 'something' } ) |
| 472 |
->status_is(404)->json_is( '/error' => 'Attribute not found' ); |
| 473 |
|
| 474 |
my $response = |
| 475 |
$t->patch_ok( "//$userid:$password@/api/v1/patrons/" |
| 476 |
. $patron->id |
| 477 |
. '/extended_attributes/' |
| 478 |
. $unique_attribute->id => json => { value => 'HEY' } ) |
| 479 |
->status_is(200)->tx->res->json; |
| 480 |
|
| 481 |
is_deeply( |
| 482 |
Koha::Patron::Attributes->find( $response->{extended_attribute_id} ) |
| 483 |
->to_api, |
| 484 |
$response, |
| 485 |
"The returned object is on the DB" |
| 486 |
); |
| 487 |
|
| 488 |
my $unique_value = 'HEHE'; |
| 489 |
|
| 490 |
# Add a patron with the unique attribute to test changing to it |
| 491 |
$builder->build_object( { class => 'Koha::Patrons' } ) |
| 492 |
->add_extended_attribute( |
| 493 |
{ |
| 494 |
code => $unique_attr_type->code, |
| 495 |
attribute => $unique_value |
| 496 |
} |
| 497 |
); |
| 498 |
|
| 499 |
$t->patch_ok( "//$userid:$password@/api/v1/patrons/" |
| 500 |
. $patron->id |
| 501 |
. '/extended_attributes/' |
| 502 |
. $unique_attribute->id => json => { value => $unique_value } ) |
| 503 |
->status_is(409) |
| 504 |
->json_is( '/error' => |
| 505 |
"Your action breaks a unique constraint on the attribute. type=" |
| 506 |
. $unique_attr_type->code |
| 507 |
. " value=$unique_value" ); |
| 508 |
|
| 509 |
$schema->storage->txn_rollback; |
| 510 |
}; |