From dbecb3f9755ae31ea91f8f1330e1df03d6ab9aeb Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Tue, 31 Jul 2018 11:26:29 +0200 Subject: [PATCH] Bug 21284: Unit tests --- t/db_dependent/ILSDI_Services.t | 123 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 122 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/ILSDI_Services.t b/t/db_dependent/ILSDI_Services.t index d918e43..1e8eead 100644 --- a/t/db_dependent/ILSDI_Services.t +++ b/t/db_dependent/ILSDI_Services.t @@ -19,10 +19,12 @@ use Modern::Perl; use CGI qw ( -utf8 ); -use Test::More tests => 4; +use Test::More tests => 5; use Test::MockModule; use t::lib::Mocks; use t::lib::TestBuilder; +use C4::Circulation; +use C4::Reserves; use Koha::AuthUtils; @@ -34,6 +36,18 @@ my $schema = Koha::Database->schema; my $dbh = C4::Context->dbh; my $builder = t::lib::TestBuilder->new; +# Mock userenv +local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ }; +my $userenv; +*C4::Context::userenv = \&Mock_userenv; + +# C4::Context->userenv +sub Mock_userenv { + return $userenv; +} + + + subtest 'AuthenticatePatron test' => sub { plan tests => 14; @@ -92,6 +106,113 @@ subtest 'AuthenticatePatron test' => sub { $schema->storage->txn_rollback; }; +subtest 'GetPatronInfo test for holds' => sub { + plan tests => 8; + + $schema->storage->txn_begin; + $schema->resultset( 'Issue' )->delete_all; + $schema->resultset( 'Reserve' )->delete_all; + $schema->resultset( 'Borrower' )->delete_all; + $schema->resultset( 'Category' )->delete_all; + $schema->resultset( 'Item' )->delete_all; # 'Branch' deps. on this + $schema->resultset( 'Branch' )->delete_all; + + # Configure Koha to enable ILS-DI server + t::lib::Mocks::mock_preference( 'ILS-DI', 1 ); + + # Set up a library/branch for our user to belong to: + my $branchcode = 'T_ILSDI'; + my $lib = $builder->build( { + source => 'Branch', + value => { + branchcode => $branchcode, + } + } ); + + # Create a new category for user to belong to: + my $cat = $builder->build( { + source => 'Category', + value => { + category_type => 'A', + BlockExpiredPatronOpacActions => -1, + } + } ); + + # Create new users: + my $brwr = $builder->build( { + source => 'Borrower', + value => { + categorycode => $cat->{'categorycode'}, + branchcode => $lib->{'branchcode'}, + } + } ); + my $brwr2 = $builder->build( { + source => 'Borrower', + value => { + categorycode => $cat->{'categorycode'}, + branchcode => $lib->{'branchcode'}, + } + } ); + my $brwr3 = $builder->build( { + source => 'Borrower', + value => { + categorycode => $cat->{'categorycode'}, + branchcode => $lib->{'branchcode'}, + } + } ); + + + # Place a loan + my $biblio = $builder->build( { source => 'Biblio', } ); + my $item = $builder->build( { source => 'Item', value => { biblionumber => $biblio->{biblionumber}, barcode => "mybarcode"} } ); + my $biblioitem = $builder->build( { source => 'Biblioitem', value => { biblionumber => $biblio->{biblionumber} } } ); + $userenv = { flags => 1, id => $brwr->{borrowernumber}, branch => 'T_ILSDI' }; + my $issue = AddIssue($brwr, $item->{barcode}); + + # Prepare and send web request for IL-SDI server: + my $query = new CGI; + $query->param( 'service', 'GetPatronInfo' ); + $query->param( 'patron_id', $brwr->{'borrowernumber'} ); + $query->param( 'show_attributes', '0' ); + $query->param( 'show_loans', '1' ); + my $reply = C4::ILSDI::Services::GetPatronInfo( $query ); + + # Check that this loan is not on hold + is ( $reply->{loans}->{loan}[0]->{recordonhold}, "0", "Record is not on hold"); + is ( $reply->{loans}->{loan}[0]->{itemonhold}, "0", "Item is not on hold"); + + # Add a hold on the biblio + my $biblioreserve = AddReserve($branchcode, $brwr2->{'borrowernumber'}, $biblio->{biblionumber}); + + # Check that it is on hold on biblio level + $reply = C4::ILSDI::Services::GetPatronInfo( $query ); + is ( $reply->{loans}->{loan}[0]->{recordonhold}, "1", "Record is on hold"); + is ( $reply->{loans}->{loan}[0]->{itemonhold}, "0", "Item is on hold"); + + # Delete holds + $schema->resultset( 'Reserve' )->delete_all; + + # Add a hold on the item + my $itemreserve = AddReserve($branchcode, $brwr2->{'borrowernumber'}, $biblio->{biblionumber}, + [$biblio->{biblionumber}], undef, undef, undef, undef, undef, $item->{itemnumber}); + + # When a specific item has a reserve, the item is on hold as well as the record + $reply = C4::ILSDI::Services::GetPatronInfo( $query ); + is ( $reply->{loans}->{loan}[0]->{recordonhold}, "1", "Record is on hold"); + is ( $reply->{loans}->{loan}[0]->{itemonhold}, "1", "Item is on hold"); + + # Add another hold on the biblio + $biblioreserve = AddReserve($branchcode, $brwr3->{'borrowernumber'}, $biblio->{biblionumber}); + + # Check that there are 2 holds on the biblio and 1 on this specific item + $reply = C4::ILSDI::Services::GetPatronInfo( $query ); + is ( $reply->{loans}->{loan}[0]->{recordonhold}, "2", "Record is on hold twice"); + is ( $reply->{loans}->{loan}[0]->{itemonhold}, "1", "Item is on hold"); + + # Cleanup + $schema->storage->txn_rollback; + +}; subtest 'GetPatronInfo/GetBorrowerAttributes test for extended patron attributes' => sub { -- 2.7.4