From e99f712dc916aa03d89c57f9b6d7cbdc121751ec Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Thu, 31 Oct 2019 14:26:18 +0000 Subject: [PATCH] Bug 23916: Add unit tests This patch adds unit tests for the new functions Signed-off-by: Ben Veasey Signed-off-by: Bouzid Fergani Signed-off-by: Nick Clemens --- t/db_dependent/Circulation.t | 51 +++++++++++++++++++++++++++++++-- t/db_dependent/Koha/Checkouts.t | 31 +++++++++++++++++++- t/db_dependent/Koha/Patrons.t | 7 +++++ 3 files changed, 86 insertions(+), 3 deletions(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 5113217e6e..c1d4ba1457 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -18,7 +18,7 @@ use Modern::Perl; use utf8; -use Test::More tests => 50; +use Test::More tests => 51; use Test::Exception; use Test::MockModule; use Test::Deep qw( cmp_deeply ); @@ -122,6 +122,11 @@ for my $branch ( $branches->next ) { $dbh->do('DELETE FROM issues'); $dbh->do('DELETE FROM borrowers'); +# Disable recording of issuer until we're ready for it +t::lib::Mocks::mock_preference('RecordIssuer', 0); + +my $module = new Test::MockModule('C4::Context'); + my $library = $builder->build({ source => 'Branch', }); @@ -3797,7 +3802,6 @@ subtest 'Incremented fee tests' => sub { my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store; - my $module = Test::MockModule->new('C4::Context'); $module->mock( 'userenv', sub { { branch => $library->id } } ); my $patron = $builder->build_object( @@ -4599,6 +4603,49 @@ subtest 'Checkout should correctly terminate a transfer' => sub { $hold = $hold->get_from_storage; is( $hold->found, undef, 'Hold is waiting' ); is( $hold->priority, 1, ); +} + +subtest 'AddIssue records issuer if appropriate' => sub { + plan tests => 2; + + $module->mock( 'userenv', sub { { branch => $library->{id} } } ); + + my $library = + $builder->build_object( { class => 'Koha::Libraries' } )->store; + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { categorycode => $patron_category->{categorycode} } + } + )->store; + my $issuer = $builder->build_object( + { + class => 'Koha::Patrons', + value => { categorycode => $patron_category->{categorycode} } + } + )->store; + my $item = $builder->build_sample_item( + { + library => $library->{branchcode} + } + ); + + $module->mock( 'userenv', sub { { branch => $library->id, number => $issuer->{borrowernumber} } } ); + + my $dt_from = dt_from_string(); + my $dt_to = dt_from_string()->add( days => 7 ); + + my $issue = + AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from ); + + is( $issue->issuer, undef, "Issuer not recorded when RecordIssuer turned off" ); + + t::lib::Mocks::mock_preference('RecordIssuer', 1); + + my $issue2 = + AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from ); + + is( $issue->issuer, $issuer->{borrowernumber}, "Issuer recorded when RecordIssuer turned on" ); }; $schema->storage->txn_rollback; diff --git a/t/db_dependent/Koha/Checkouts.t b/t/db_dependent/Koha/Checkouts.t index d037970a40..0ee6a4d8b7 100755 --- a/t/db_dependent/Koha/Checkouts.t +++ b/t/db_dependent/Koha/Checkouts.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 7; +use Test::More tests => 8; use C4::Circulation; use Koha::Checkouts; @@ -122,6 +122,35 @@ subtest 'patron' => sub { ); }; +subtest 'issuer' => sub { + plan tests => 3; + my $patron = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}}); + my $issuer = $builder->build_object({class=>'Koha::Patrons', value => {branchcode => $library->{branchcode}}}); + + my $item = $builder->build_object( { class=> 'Koha::Items' } ); + my $checkout = Koha::Checkout->new({ + borrowernumber => $patron->borrowernumber, + issuer => $issuer->borrowernumber, + itemnumber => $item->itemnumber, + branchcode => $library->{branchcode}, + })->store; + + my $i = $checkout->issued_by; + is( ref($i), 'Koha::Patron', + 'Koha::Checkout->issued_by should return a Koha::Patron' ); + is( $i->borrowernumber, $issuer->borrowernumber, + 'Koha::Checkout->issued_by should return the correct patron' ); + + my $issue_id = $checkout->issue_id; + C4::Circulation::MarkIssueReturned( $i->borrowernumber, $checkout->itemnumber ); + $i->delete; + my $old_issue = Koha::Old::Checkouts->find($issue_id); + is( $old_issue->issuer, undef, + 'Koha::Checkout->issuer should return undef if the patron record has been deleted' + ); + +}; + $retrieved_checkout_1->delete; is( Koha::Checkouts->search->count, $nb_of_checkouts + 1, 'Delete should have deleted the checkout' ); diff --git a/t/db_dependent/Koha/Patrons.t b/t/db_dependent/Koha/Patrons.t index c2824082e7..21bfb8ccd7 100755 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -1430,6 +1430,13 @@ subtest 'get_overdues' => sub { value => { branchcode => $library->{branchcode} } } ); + my $issuer = $builder->build( + { + source => 'Borrower', + value => { branchcode => $library->{branchcode} } + } + ); + t::lib::Mocks::mock_userenv({ number => $issuer->{borrowernumber} }); t::lib::Mocks::mock_preference({ branchcode => $library->{branchcode} }); -- 2.20.1