From 2c9270a8b2dd532dc34f2ade66cd0963b2dc5fcd Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Mon, 20 Dec 2021 14:17:31 -0300 Subject: [PATCH] Bug 29741: Unit tests Signed-off-by: Tomas Cohen Arazi Signed-off-by: David Nind --- t/db_dependent/Koha/Patron.t | 51 +++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Patron.t b/t/db_dependent/Koha/Patron.t index 2929349661..f0724766fc 100755 --- a/t/db_dependent/Koha/Patron.t +++ b/t/db_dependent/Koha/Patron.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 10; +use Test::More tests => 11; use Test::Exception; use Test::Warn; @@ -846,3 +846,52 @@ subtest 'article_requests() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'safe_to_delete() tests' => sub { + + plan tests => 5; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + + ## Make it the anonymous + t::lib::Mocks::mock_preference( 'AnonymousPatron', $patron->id ); + + is( $patron->safe_to_delete, 'is_anonymous_patron', 'Cannot delete, it is the anonymous patron' ); + # cleanup + t::lib::Mocks::mock_preference( 'AnonymousPatron', 0 ); + + ## Make it have a checkout + my $checkout = $builder->build_object( + { + class => 'Koha::Checkouts', + value => { borrowernumber => $patron->id } + } + ); + + is( $patron->safe_to_delete, 'has_checkouts', 'Cannot delete, has checkouts' ); + # cleanup + $checkout->delete; + + ## Make it have a guarantee + t::lib::Mocks::mock_preference( 'borrowerRelationship', 'parent' ); + $builder->build_object({ class => 'Koha::Patrons' }) + ->add_guarantor({ guarantor_id => $patron->id, relationship => 'parent' }); + + is( $patron->safe_to_delete, 'has_guarantees', 'Cannot delete, has guarantees' ); + # cleanup + $patron->guarantee_relationships->delete; + + ## Make it have debt + my $debit = $patron->account->add_debit({ amount => 10, interface => 'intranet', type => 'MANUAL' }); + + is( $patron->safe_to_delete, 'has_debt', 'Cannot delete, has debt' ); + # cleanup + $patron->account->pay({ amount => 10, debits => [ $debit ] }); + + ## Happy case :-D + is( $patron->safe_to_delete, 'ok', 'Can delete, all conditions met' ); + + $schema->storage->txn_rollback; +}; -- 2.20.1