From 574d308e28319abd82a09c4c0d600414b13bf5b1 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 --- t/db_dependent/Circulation.t | 49 ++++++++++++++++++++++++++++++++++++++++- t/db_dependent/Koha/Checkouts.t | 31 +++++++++++++++++++++++++- t/db_dependent/Koha/Patrons.t | 7 ++++++ 3 files changed, 85 insertions(+), 2 deletions(-) diff --git a/t/db_dependent/Circulation.t b/t/db_dependent/Circulation.t index 4b17bf5..6682984 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -117,6 +117,11 @@ $cache->clear_from_cache('single_holidays'); $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', }); @@ -3368,7 +3373,6 @@ subtest 'Incremented fee tests' => sub { my $library = $builder->build_object( { class => 'Koha::Libraries' } )->store; - my $module = new Test::MockModule('C4::Context'); $module->mock( 'userenv', sub { { branch => $library->id } } ); my $patron = $builder->build_object( @@ -3647,6 +3651,49 @@ subtest "Test Backdating of Returns" => sub { is( $accountline->amount+0, 0, 'Fee amount was reduced to 0' ); }; +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; C4::Context->clear_syspref_cache(); $cache->clear_from_cache('single_holidays'); diff --git a/t/db_dependent/Koha/Checkouts.t b/t/db_dependent/Koha/Checkouts.t index dfb90e4..193f0bf 100644 --- 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 f94bd2a..d7a8b54 100644 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -1404,6 +1404,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.7.4