From 8ed1d8caf5021ee4d850a96f31aa3026af7255bf 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 d078ef1550..86f4a9ee5f 100755 --- a/t/db_dependent/Circulation.t +++ b/t/db_dependent/Circulation.t @@ -121,6 +121,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', }); @@ -3313,7 +3318,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( @@ -4060,6 +4064,49 @@ subtest 'transferbook tests' => sub { }; +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(); $branches = Koha::Libraries->search(); 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 0f00008588..e104974ccf 100755 --- a/t/db_dependent/Koha/Patrons.t +++ b/t/db_dependent/Koha/Patrons.t @@ -1421,6 +1421,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