|
Lines 19-25
Link Here
|
| 19 |
|
19 |
|
| 20 |
use Modern::Perl; |
20 |
use Modern::Perl; |
| 21 |
|
21 |
|
| 22 |
use Test::More tests => 17; |
22 |
use Test::More tests => 18; |
| 23 |
use Test::Exception; |
23 |
use Test::Exception; |
| 24 |
use Test::Warn; |
24 |
use Test::Warn; |
| 25 |
|
25 |
|
|
Lines 341-426
subtest 'Return same values as DBIx::Class' => sub {
Link Here
|
| 341 |
$schema->storage->txn_begin; |
341 |
$schema->storage->txn_begin; |
| 342 |
|
342 |
|
| 343 |
subtest 'Simple Koha::Objects - Koha::Cities' => sub { |
343 |
subtest 'Simple Koha::Objects - Koha::Cities' => sub { |
| 344 |
plan tests => 3; |
344 |
plan tests => 2; |
| 345 |
|
345 |
|
| 346 |
my ( $r_us, $e_us, $r_them, $e_them ); |
346 |
subtest 'Koha::Object->delete' => sub { |
| 347 |
{ |
347 |
|
| 348 |
my $c = Koha::City->new({ city_name => 'city4test' }); |
348 |
plan tests => 4; |
| 349 |
try {$r_us = $c->delete;} catch { $e_us = ref($_) }; |
349 |
|
| 350 |
|
350 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 351 |
|
351 |
|
| 352 |
$c = $schema->resultset('City')->new({ city_name => 'city4test' }); |
352 |
# CASE 1 - Delete an existing object |
| 353 |
try {$r_them = $c->delete;} catch { $e_them = ref($_) }; |
353 |
my $c = Koha::City->new( { city_name => 'city4test' } )->store; |
| 354 |
|
354 |
try { $r_us = $c->delete; } catch { $e_us = $_ }; |
| 355 |
is( $r_us, $r_them ); |
355 |
$c = $schema->resultset('City')->new( { city_name => 'city4test_2' } )->update_or_insert; |
| 356 |
is( $e_us, $e_them ); # FIXME This is need adjustment, we want to throw a Koha::Exception |
356 |
try { $r_them = $c->delete; } catch { $e_them = $_ }; |
| 357 |
} |
357 |
ok( ref($r_us) && ref($r_them), |
| 358 |
|
358 |
'Successful delete should return the object ' ); |
| 359 |
{ |
359 |
ok( !defined $e_us && !defined $e_them, |
| 360 |
|
360 |
'Successful delete should not raise an exception' ); |
| 361 |
my $city = $builder->build_object({ class => 'Koha::Cities' }); |
361 |
|
| 362 |
try{ |
362 |
# CASE 2 - Delete an object that is not in storage |
| 363 |
$schema->storage->txn_do(sub{ |
363 |
try { $r_us = $r_us->delete; } catch { $e_us = $_ }; |
| 364 |
my $c = Koha::Cities->find($city->cityid); |
364 |
try { $r_them = $r_them->delete; } catch { $e_them = $_ }; |
| 365 |
$r_us = $c->delete; |
365 |
ok( |
| 366 |
$c->update({force_fail=>'foo'}); |
366 |
defined $e_us && defined $e_them, |
| 367 |
});}; |
367 |
'Delete an object that is not in storage should raise an exception' |
| 368 |
try{ |
368 |
); |
| 369 |
$schema->storage->txn_do(sub{ |
369 |
is( ref($e_us), 'DBIx::Class::Exception' ) |
| 370 |
my $c = $schema->resultset('City')->find($city->cityid); |
370 |
; # FIXME This needs adjustement, we want to throw a Koha::Exception |
| 371 |
$r_them = $c->delete; |
371 |
|
| 372 |
$c->update({force_fail=>'foo'}); |
372 |
}; |
| 373 |
});}; |
373 |
|
| 374 |
is( $r_us, $r_them ); |
374 |
subtest 'Koha::Objects->delete' => sub { |
| 375 |
} |
375 |
|
| 376 |
}; |
376 |
plan tests => 4; |
| 377 |
|
377 |
|
| 378 |
subtest 'Overwritten Koha::Objects->delete - Koha::Patrons' => sub { |
378 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 379 |
plan tests => 4; |
379 |
|
| 380 |
|
380 |
# CASE 1 - Delete existing objects |
| 381 |
my ( $r_us, $e_us, $r_them, $e_them ); |
381 |
my $city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 382 |
|
382 |
my $city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 383 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
383 |
my $city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 384 |
my $patron_data = $patron->unblessed; |
384 |
my $cities = Koha::Cities->search( |
| 385 |
$patron->delete; |
385 |
{ |
| 386 |
|
386 |
cityid => { |
| 387 |
{ |
387 |
-in => [ |
| 388 |
my $p = Koha::Patron->new( $patron_data ); |
388 |
$city_1->cityid, |
| 389 |
try {$r_us = $p->delete;} catch { $e_us = ref($_) }; |
389 |
$city_2->cityid, |
|
|
390 |
$city_3->cityid, |
| 391 |
] |
| 392 |
} |
| 393 |
} |
| 394 |
); |
| 395 |
|
| 396 |
try { $r_us = $cities->delete; } catch { $e_us = $_ }; |
| 397 |
|
| 398 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 399 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 400 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 401 |
$cities = $schema->resultset('City')->search( |
| 402 |
{ |
| 403 |
cityid => { |
| 404 |
-in => [ |
| 405 |
$city_1->cityid, |
| 406 |
$city_2->cityid, |
| 407 |
$city_3->cityid, |
| 408 |
] |
| 409 |
} |
| 410 |
} |
| 411 |
); |
| 412 |
|
| 413 |
try { $r_them = $cities->delete; } catch { $e_them = $_ }; |
| 414 |
|
| 415 |
ok( $r_us == 3 && $r_them == 3 ); |
| 416 |
ok (!defined($e_us) && !defined($e_them)); |
| 417 |
|
| 418 |
# CASE 2 - One of the object is not in storage |
| 419 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 420 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 421 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 422 |
$cities = Koha::Cities->search( |
| 423 |
{ |
| 424 |
cityid => { |
| 425 |
-in => [ |
| 426 |
$city_1->cityid, |
| 427 |
$city_2->cityid, |
| 428 |
$city_3->cityid, |
| 429 |
] |
| 430 |
} |
| 431 |
} |
| 432 |
); |
| 433 |
|
| 434 |
$city_2->delete; # We delete one of the object |
| 435 |
try { $r_us = $cities->delete; } catch { $e_us = $_ }; |
| 436 |
|
| 437 |
$city_1 = $builder->build_object({ class => 'Koha::Cities' }); |
| 438 |
$city_2 = $builder->build_object({ class => 'Koha::Cities' }); |
| 439 |
$city_3 = $builder->build_object({ class => 'Koha::Cities' }); |
| 440 |
$cities = $schema->resultset('City')->search( |
| 441 |
{ |
| 442 |
cityid => { |
| 443 |
-in => [ |
| 444 |
$city_1->cityid, |
| 445 |
$city_2->cityid, |
| 446 |
$city_3->cityid, |
| 447 |
] |
| 448 |
} |
| 449 |
} |
| 450 |
); |
| 390 |
|
451 |
|
|
|
452 |
$city_2->delete; # We delete one of the object |
| 453 |
try { $r_them = $cities->delete; } catch { $e_them = $_ }; |
| 391 |
|
454 |
|
| 392 |
$p = $schema->resultset('Borrower')->new( $patron_data ); |
455 |
ok( $r_us == 2 && $r_them == 2 ); |
| 393 |
try {$r_them = $p->delete;} catch { $e_them = ref($_) }; |
456 |
ok (!defined($e_us) && !defined($e_them)); |
|
|
457 |
}; |
| 458 |
}; |
| 394 |
|
459 |
|
| 395 |
is( $r_us, $r_them ); |
460 |
subtest 'Overwritten Koha::Objects->delete - Koha::Patrons' => sub { |
| 396 |
is( $e_us, $e_them ); # FIXME This is need adjustment, we want to throw a Koha::Exception |
|
|
| 397 |
} |
| 398 |
|
461 |
|
| 399 |
{ |
462 |
plan tests => 2; |
| 400 |
try { |
463 |
|
| 401 |
$schema->storage->txn_do( |
464 |
subtest 'Koha::Object->delete' => sub { |
| 402 |
sub { |
465 |
|
| 403 |
my $p = Koha::Patrons->find( $patron->borrowernumber ); |
466 |
plan tests => 6; |
| 404 |
$r_us = $p->delete; |
467 |
|
| 405 |
$p->update( { force_fail => 'foo' } ); |
468 |
my ( $r_us, $e_us, $r_them, $e_them ); |
|
|
469 |
|
| 470 |
# CASE 1 - Delete an existing patron |
| 471 |
my $patron = $builder->build_object({ class => 'Koha::Patrons' }); |
| 472 |
my $patron_data = $patron->unblessed; |
| 473 |
$patron->delete; |
| 474 |
|
| 475 |
$patron = Koha::Patron->new( $patron_data )->store; |
| 476 |
try {$r_us = $patron->delete;} catch { $e_us = $_ }; |
| 477 |
$patron = $schema->resultset('Borrower')->new( $patron_data )->update_or_insert; |
| 478 |
try {$r_them = $patron->delete;} catch { $e_them = $_ }; |
| 479 |
ok( ref($r_us) && ref($r_them), |
| 480 |
'Successful delete should return the patron object' ); |
| 481 |
ok( !defined $e_us && !defined $e_them, |
| 482 |
'Successful delete should not raise an exception' ); |
| 483 |
|
| 484 |
# CASE 2 - Delete a patron that is not in storage |
| 485 |
try { $r_us = $r_us->delete; } catch { $e_us = $_ }; |
| 486 |
try { $r_them = $r_them->delete; } catch { $e_them = $_ }; |
| 487 |
ok( |
| 488 |
defined $e_us && defined $e_them, |
| 489 |
'Delete a patron that is not in storage should raise an exception' |
| 490 |
); |
| 491 |
is( ref($e_us), 'DBIx::Class::Exception' ) |
| 492 |
; # FIXME This needs adjustement, we want to throw a Koha::Exception |
| 493 |
|
| 494 |
# CASE 3 - Delete a patron that cannot be deleted (as a checkout) |
| 495 |
$patron = Koha::Patron->new($patron_data)->store; |
| 496 |
$builder->build_object( |
| 497 |
{ |
| 498 |
class => 'Koha::Checkouts', |
| 499 |
value => { borrowernumber => $patron->borrowernumber } |
| 500 |
} |
| 501 |
); |
| 502 |
try { $r_us = $r_us->delete; } catch { $e_us = $_ }; |
| 503 |
$patron = $schema->resultset('Borrower')->find( $patron->borrowernumber ); |
| 504 |
try { $r_them = $r_them->delete; } catch { $e_them = $_ }; |
| 505 |
ok( |
| 506 |
defined $e_us && defined $e_them, |
| 507 |
'Delete a patron that cannot be deleted should raise an exception' |
| 508 |
); |
| 509 |
is( ref($e_us), 'DBIx::Class::Exception' ) |
| 510 |
; # FIXME This needs adjustement, we want to throw a Koha::Exception |
| 511 |
}; |
| 512 |
|
| 513 |
subtest 'Koha::Objects->delete' => sub { |
| 514 |
|
| 515 |
plan tests => 9; |
| 516 |
|
| 517 |
my ( $r_us, $e_us, $r_them, $e_them ); |
| 518 |
|
| 519 |
# CASE 1 - Delete existing objects |
| 520 |
my $patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 521 |
my $patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 522 |
my $patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 523 |
my $patrons = Koha::Patrons->search( |
| 524 |
{ |
| 525 |
borrowernumber => { |
| 526 |
-in => [ |
| 527 |
$patron_1->borrowernumber, |
| 528 |
$patron_2->borrowernumber, |
| 529 |
$patron_3->borrowernumber |
| 530 |
] |
| 406 |
} |
531 |
} |
| 407 |
); |
532 |
} |
| 408 |
}; |
533 |
); |
| 409 |
try { |
534 |
|
| 410 |
$schema->storage->txn_do( |
535 |
try { $r_us = $patrons->delete; } catch { $e_us = $_ }; |
| 411 |
sub { |
536 |
|
| 412 |
my $p = $schema->resultset('City')->find( $patron->borrowernumber ); |
537 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 413 |
$r_them = $p->delete; |
538 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 414 |
$p->update( { force_fail => 'foo' } ); |
539 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
|
|
540 |
$patrons = $schema->resultset('Borrower')->search( |
| 541 |
{ |
| 542 |
borrowernumber => { |
| 543 |
-in => [ |
| 544 |
$patron_1->borrowernumber, |
| 545 |
$patron_2->borrowernumber, |
| 546 |
$patron_3->borrowernumber |
| 547 |
] |
| 415 |
} |
548 |
} |
| 416 |
); |
549 |
} |
| 417 |
}; |
550 |
); |
| 418 |
is( $r_us, $r_them ); |
551 |
|
| 419 |
} |
552 |
try { $r_them = $patrons->delete; } catch { $e_them = $_ }; |
| 420 |
|
553 |
|
| 421 |
# TODO Test value of Koha::Object->delete when the row cannot be deleted |
554 |
ok( $r_us == 3 && $r_them == 3, '->delete should return the number of deleted patrons' ); |
| 422 |
|
555 |
ok (!defined($e_us) && !defined($e_them), '->delete should not raise exception if everything went well'); |
| 423 |
# TODO Add tests for Koha::Objects->delete |
556 |
|
|
|
557 |
# CASE 2 - One of the patrons is not in storage |
| 558 |
undef $_ for $r_us, $e_us, $r_them, $e_them; |
| 559 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 560 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 561 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 562 |
$patrons = Koha::Patrons->search( |
| 563 |
{ |
| 564 |
borrowernumber => { |
| 565 |
-in => [ |
| 566 |
$patron_1->borrowernumber, |
| 567 |
$patron_2->borrowernumber, |
| 568 |
$patron_3->borrowernumber |
| 569 |
] |
| 570 |
} |
| 571 |
} |
| 572 |
); |
| 573 |
|
| 574 |
$patron_2->delete; # We delete one of the patron |
| 575 |
try { $r_us = $patrons->delete; } catch { $e_us = $_ }; |
| 576 |
|
| 577 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 578 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 579 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 580 |
$patrons = $schema->resultset('Borrower')->search( |
| 581 |
{ |
| 582 |
borrowernumber => { |
| 583 |
-in => [ |
| 584 |
$patron_1->borrowernumber, |
| 585 |
$patron_2->borrowernumber, |
| 586 |
$patron_3->borrowernumber |
| 587 |
] |
| 588 |
} |
| 589 |
} |
| 590 |
); |
| 591 |
|
| 592 |
$patron_2->delete; # We delete one of the patron |
| 593 |
try { $r_them = $patrons->delete; } catch { $e_them = $_ }; |
| 594 |
|
| 595 |
ok( $r_us == 2 && $r_them == 2, 'Delete patrons with one that was not in storage should delete the patrons' ); |
| 596 |
ok (!defined($e_us) && !defined($e_them), 'no exception should be raised if at least one patron was not in storage'); |
| 597 |
|
| 598 |
# CASE 3 - Delete a set of patrons with one that that cannot be deleted (as a checkout) |
| 599 |
undef $_ for $r_us, $e_us, $r_them, $e_them; |
| 600 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 601 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 602 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 603 |
$patrons = Koha::Patrons->search( |
| 604 |
{ |
| 605 |
borrowernumber => { |
| 606 |
-in => [ |
| 607 |
$patron_1->borrowernumber, |
| 608 |
$patron_2->borrowernumber, |
| 609 |
$patron_3->borrowernumber |
| 610 |
] |
| 611 |
} |
| 612 |
} |
| 613 |
); |
| 614 |
|
| 615 |
# Adding a checkout to patron_2 |
| 616 |
$builder->build_object( |
| 617 |
{ |
| 618 |
class => 'Koha::Checkouts', |
| 619 |
value => { borrowernumber => $patron_2->borrowernumber } |
| 620 |
} |
| 621 |
); |
| 622 |
|
| 623 |
warning_like { |
| 624 |
try { $r_us = $patrons->delete; } catch { $e_us = $_ }; |
| 625 |
} |
| 626 |
qr{DBD::mysql::st execute failed: Cannot delete or update a parent row: a foreign key constraint fails}, |
| 627 |
"Foreign key constraint DBI error should be logged"; |
| 628 |
my $not_deleted_us = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage; |
| 629 |
|
| 630 |
$patron_1 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 631 |
$patron_2 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 632 |
$patron_3 = $builder->build_object({ class => 'Koha::Patrons' }); |
| 633 |
$patrons = $schema->resultset('Borrower')->search( |
| 634 |
{ |
| 635 |
borrowernumber => { |
| 636 |
-in => [ |
| 637 |
$patron_1->borrowernumber, |
| 638 |
$patron_2->borrowernumber, |
| 639 |
$patron_3->borrowernumber |
| 640 |
] |
| 641 |
} |
| 642 |
} |
| 643 |
); |
| 644 |
|
| 645 |
# Adding a checkout to patron_2 |
| 646 |
$builder->build_object( |
| 647 |
{ |
| 648 |
class => 'Koha::Checkouts', |
| 649 |
value => { borrowernumber => $patron_2->borrowernumber } |
| 650 |
} |
| 651 |
); |
| 652 |
|
| 653 |
warning_like { |
| 654 |
try { $r_them = $patrons->delete; } catch { $e_them = $_ }; |
| 655 |
} |
| 656 |
qr{DBD::mysql::st execute failed: Cannot delete or update a parent row: a foreign key constraint fails}, |
| 657 |
"Foreign key constraint DBI error should be logged"; |
| 658 |
|
| 659 |
my $not_deleted_them = $patron_1->in_storage + $patron_2->in_storage + $patron_3->in_storage; |
| 660 |
ok( |
| 661 |
defined $e_us && defined $e_them, |
| 662 |
'Delete patrons with one that cannot be deleted should raise an exception' |
| 663 |
); |
| 664 |
is( ref($e_us), 'DBIx::Class::Exception' ) |
| 665 |
; # FIXME This needs adjustement, we want to throw a Koha::Exception |
| 666 |
|
| 667 |
ok($not_deleted_us == 3 && $not_deleted_them == 3, 'If one patron cannot be deleted, none should have been deleted'); |
| 668 |
}; |
| 424 |
}; |
669 |
}; |
| 425 |
|
670 |
|
| 426 |
$schema->storage->txn_rollback; |
671 |
$schema->storage->txn_rollback; |
| 427 |
- |
|
|