From 174b895d76ad415e57ba2b051f90809a23e6817b Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 23 Sep 2020 11:35:53 -0300 Subject: [PATCH] Bug 26515: Unit tests Signed-off-by: Tomas Cohen Arazi --- t/db_dependent/Koha/Acquisition/Order.t | 111 +++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/Acquisition/Order.t b/t/db_dependent/Koha/Acquisition/Order.t index 2fa7310ae60..f1881f46cd2 100755 --- a/t/db_dependent/Koha/Acquisition/Order.t +++ b/t/db_dependent/Koha/Acquisition/Order.t @@ -19,13 +19,18 @@ use Modern::Perl; -use Test::More tests => 11; +use Test::More tests => 12; +use Test::Exception; use t::lib::TestBuilder; use t::lib::Mocks; +use C4::Circulation; + +use Koha::Biblios; use Koha::Database; use Koha::DateUtils qw(dt_from_string); +use Koha::Items; my $schema = Koha::Database->schema; my $builder = t::lib::TestBuilder->new; @@ -582,5 +587,109 @@ subtest 'filter_by_current & filter_by_cancelled' => sub { is( $orders->filter_by_cancelled->count, 1); + $schema->storage->txn_rollback; +}; + +subtest 'cancel() tests' => sub { + + plan tests => 15; + + $schema->storage->txn_begin; + + # Scenario: + # * order with one item attached + # * the item is on loan + # => order is not cancelled + # => item in order is not removed + # => exception is thrown + + my $item = $builder->build_sample_item; + my $biblio_id = $item->biblio->id; + my $order = $builder->build_object( + { + class => 'Koha::Acquisition::Orders', + value => { + orderstatus => 'new', + biblionumber => $item->biblio->id, + datecancellationprinted => undef, + cancellationreason => undef, + } + } + ); + $order->add_item( $item->id ); + + my $patron = $builder->build_object({ class => 'Koha::Patrons' }); + t::lib::Mocks::mock_userenv( + { patron => $patron, branchcode => $patron->branchcode } ); + + # Add a checkout so cancelling fails because od 'book_on_loan' + C4::Circulation::AddIssue( $patron->unblessed, $item->barcode ); + + throws_ok + { $order->cancel({ reason => 'Some reason' }); } + 'Koha::Exceptions::Object::CannotBeDeleted', + 'An exception is thrown'; + + like( + "$@", + qr/Cannot delete Koha::Item=HASH\(.*\) object \(id=\d*\). Reason: book_on_loan/, + 'The exception is correct, item on loan' + ); + + # refresh the order object + $order->discard_changes; + + isnt( $order->orderstatus, 'cancelled', 'Order is not marked as cancelled' ); + is( $order->datecancellationprinted, undef, 'datecancellationprinted is undef' ); + is( $order->cancellationreason, undef, 'cancellationreason is undef' ); + is( ref(Koha::Items->find($item->id)), 'Koha::Item', 'The item is present' ); + + # Scenario: + # * order with one item attached + # * the item is no longer on loan + # => order is cancelled + # => item in order is removed + + C4::Circulation::AddReturn( $item->barcode ); + + $order->cancel({ reason => 'Some reason' }) + ->discard_changes; + + is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' ); + isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' ); + is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' ); + is( Koha::Items->find($item->id), undef, 'The item is no longer present' ); + + # Scenario: + # * order with one item attached + # => order is cancelled + # => item in order is removed + # => the extra item remains untouched + + my $item_1 = $builder->build_sample_item; + $biblio_id = $item_1->biblio->id; + my $item_2 = $builder->build_sample_item({ biblionumber => $biblio_id }); + $order = $builder->build_object( + { + class => 'Koha::Acquisition::Orders', + value => { + orderstatus => 'new', + biblionumber => $biblio_id, + datecancellationprinted => undef, + cancellationreason => undef, + } + } + ); + $order->add_item( $item_1->id ); + + $order->cancel({ reason => 'Some reason' }) + ->discard_changes; + + is( $order->orderstatus, 'cancelled', 'Order is marked as cancelled' ); + isnt( $order->datecancellationprinted, undef, 'datecancellationprinted is set' ); + is( $order->cancellationreason, 'Some reason', 'cancellationreason is undef' ); + is( Koha::Items->find($item_1->id), undef, 'The item is no longer present' ); + is( ref(Koha::Items->find($item_2->id)), 'Koha::Item', 'The item is still present' ); + $schema->storage->txn_rollback; }; -- 2.28.0