Bug 19513: More changes to MarkIssueReturned.t (after bug 19487)
authorMarcel de Rooy <m.de.rooy@rijksmuseum.nl>
Fri, 20 Oct 2017 07:03:35 +0000 (09:03 +0200)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 25 Oct 2017 15:14:40 +0000 (12:14 -0300)
This patch was initially created as QA follow-up on 19487.

$dbh is not used
$categorycode is no longer used
%item_branch_infos not used
$borrowernumber is obsoleted by using $patron
No real need for $anonymous_borrowernumber (Since MarkIssueReturned returns
undef rightaway for an invalid itemnumber, the privacy stuff is irrelevant.)
No AddMember calls, removing unneeded modules

Adding a test that calls MarkIssueReturned on an item already returned.

Test plan:
[1] Run t/db_dependent/Circulation/MarkIssueReturned.t

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
t/db_dependent/Circulation/MarkIssueReturned.t

index 69b1b72..d2cf238 100644 (file)
 
 use Modern::Perl;
 
-use Test::More tests => 2;
+use Test::More tests => 6;
 use Test::Warn;
 
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
 use C4::Circulation;
-use C4::Members;
-use Koha::Library;
+use C4::Context;
+use Koha::Database;
 
 my $schema = Koha::Database->schema;
-my $dbh = C4::Context->dbh;
-
 $schema->storage->txn_begin;
 
 my $builder = t::lib::TestBuilder->new;
@@ -37,19 +35,12 @@ my $builder = t::lib::TestBuilder->new;
 t::lib::Mocks::mock_preference('AnonymousPatron', '');
 
 my $library = $builder->build({ source => 'Branch' });
-my $categorycode = $builder->build({ source => 'Category' })->{ categorycode };
 
 C4::Context->_new_userenv('xxx');
 C4::Context->set_userenv(0,0,0,'firstname','surname', $library->{branchcode}, $library->{branchname}, '', '', '');
 
-my %item_branch_infos = (
-    homebranch => $library->{branchcode},
-    holdingbranch => $library->{branchcode},
-);
-
-my $borrowernumber = AddMember( categorycode => $categorycode, branchcode => $library->{branchcode} );
 my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
-    my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
+my $patron = $builder->build({ source => 'Borrower', value => { branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode} } } );
 
 my $biblioitem = $builder->build( { source => 'Biblioitem' } );
 my $item = $builder->build(
@@ -65,17 +56,22 @@ my $item = $builder->build(
         }
     }
 );
-C4::Circulation::AddIssue( $patron, $item->{barcode} );
+my $issue = C4::Circulation::AddIssue( $patron, $item->{barcode} );
 
-eval { C4::Circulation::MarkIssueReturned( $borrowernumber, $item->{itemnumber}, 'dropbox_branch', 'returndate', 2 ) };
-like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, );
+eval { C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, 'dropbox_branch', 'returndate', 2 ) };
+like ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, 'Fatal error on anonymization' );
 
-my $anonymous_borrowernumber = AddMember( categorycode => $categorycode, branchcode => $library->{branchcode} );
-t::lib::Mocks::mock_preference('AnonymousPatron', $anonymous_borrowernumber);
-# The next call will raise an error, because data are not correctly set
-$dbh->{PrintError} = 0;
-eval { C4::Circulation::MarkIssueReturned( $borrowernumber, 'itemnumber', 'dropbox_branch', 'returndate', 2 ) };
-unlike ( $@, qr<Fatal error: the patron \(\d+\) .* AnonymousPatron>, );
+# The next call will return undef for invalid item number
+my $issue_id;
+eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, 'invalid_itemnumber', 'dropbox_branch', 'returndate', 0 ) };
+is( $@, '', 'No die triggered by invalid itemnumber' );
+is( $issue_id, undef, 'No issue_id returned' );
 
-$schema->storage->txn_rollback;
+# In the next call we return the item and try it another time
+eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, undef, 0 ) };
+is( $issue_id, $issue->issue_id, "Item has been returned (issue $issue_id)" );
+eval { $issue_id = C4::Circulation::MarkIssueReturned( $patron->{borrowernumber}, $item->{itemnumber}, undef, undef, 0 ) };
+is( $@, '', 'No crash on returning item twice' );
+is( $issue_id, undef, 'Cannot return an item twice' );
 
+$schema->storage->txn_rollback;