Bug 20912: (QA follow-up) Fix some test failures
[koha.git] / t / db_dependent / Circulation / issue.t
index e2cbf9d..c748eaa 100644 (file)
 
 use Modern::Perl;
 
-use Test::More tests => 32;
+use Test::More tests => 33;
 use DateTime::Duration;
 
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
-use C4::Biblio;
 use C4::Circulation;
-use C4::Context;
 use C4::Items;
-use C4::Members;
+use C4::Biblio;
+use C4::Context;
 use C4::Reserves;
+use Koha::Checkouts;
 use Koha::Database;
 use Koha::DateUtils;
 use Koha::Holds;
+use Koha::Items;
 use Koha::Library;
 use Koha::Patrons;
 
@@ -84,6 +85,22 @@ my $categorycode = $builder->build({
         value => { enrolmentfee => undef }
     })->{categorycode};
 
+# A default issuingrule should always be present
+my $issuingrule = $builder->build(
+    {
+        source => 'Issuingrule',
+        value  => {
+            itemtype      => '*',
+            categorycode  => '*',
+            branchcode    => '*',
+            lengthunit    => 'days',
+            issuelength   => 0,
+            renewalperiod => 0,
+            renewalsallowed => 0
+        }
+    }
+);
+
 # Add Dates
 my $dt_today = dt_from_string;
 my $today    = output_pref(
@@ -142,37 +159,24 @@ my @sampleitem2 = C4::Items::AddItem(
 my $item_id2 = $sampleitem2[2];
 
 #Add borrower
-my $borrower_id1 = C4::Members::AddMember(
+my $borrower_id1 = Koha::Patron->new({
     firstname    => 'firstname1',
     surname      => 'surname1 ',
     categorycode => $categorycode,
     branchcode   => $branchcode_1
-);
-my $borrower_1 = Koha::Patrons->find( $borrower_id1 )->unblessed;
-my $borrower_id2 = C4::Members::AddMember(
+})->store->borrowernumber;
+my $patron_1 = Koha::Patrons->find( $borrower_id1 );
+my $borrower_1 = $patron_1->unblessed;
+my $borrower_id2 = Koha::Patron->new({
     firstname    => 'firstname2',
     surname      => 'surname2 ',
     categorycode => $categorycode,
     branchcode   => $branchcode_2,
-);
-my $borrower_2 = Koha::Patrons->find( $borrower_id2 )->unblessed;
-
-my @USERENV = (
-    $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_1,
-    $branchcode_1, 'email@example.org'
-);
-
-my @USERENV_DIFFERENT_LIBRARY = (
-    $borrower_id1, 'test', 'MASTERTEST', 'firstname', $branchcode_3,
-    $branchcode_3, 'email@example.org'
-);
+})->store->borrowernumber;
+my $patron_2 = Koha::Patrons->find( $borrower_id2 );
+my $borrower_2 = $patron_2->unblessed;
 
-
-C4::Context->_new_userenv('DUMMY_SESSION_ID');
-C4::Context->set_userenv(@USERENV);
-
-my $userenv = C4::Context->userenv
-  or BAIL_OUT("No userenv");
+t::lib::Mocks::mock_userenv({ patron => $patron_1 });
 
 #Begin Tests
 
@@ -183,19 +187,18 @@ $sth->execute;
 my $countissue = $sth -> fetchrow_array;
 is ($countissue ,0, "there is no issue");
 my $issue1 = C4::Circulation::AddIssue( $borrower_1, $barcode_1, $daysago10,0, $today, '' );
-is( ref $issue1, 'Koha::Schema::Result::Issue',
-       'AddIssue returns a Koha::Schema::Result::Issue object' );
+is( ref $issue1, 'Koha::Checkout',
+       'AddIssue returns a Koha::Checkout object' );
 my $datedue1 = dt_from_string( $issue1->date_due() );
 like(
     $datedue1,
     qr/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/,
-    "Koha::Schema::Result::Issue->date_due() returns a date"
+    "Koha::Checkout->date_due() returns a date"
 );
-my $issue_id1 = $dbh->last_insert_id( undef, undef, 'issues', undef );
+my $issue_id1 = $issue1->issue_id;
 
 my $issue2 = C4::Circulation::AddIssue( $borrower_1, 'nonexistent_barcode' );
 is( $issue2, undef, "AddIssue returns undef if no datedue is specified" );
-my $issue_id2 = $dbh->last_insert_id( undef, undef, 'issues', undef );
 
 $sth->execute;
 $countissue = $sth -> fetchrow_array;
@@ -205,15 +208,16 @@ is ($countissue,1,"1 issues have been added");
 $query = " SELECT count(*) FROM accountlines";
 $sth = $dbh->prepare($query);
 $sth->execute;
-my $countaccount = $sth -> fetchrow_array;
+my $countaccount = $sth->fetchrow_array;
 is ($countaccount,0,"0 accountline exists");
-my $offset = C4::Circulation::AddIssuingCharge( $item_id1, $borrower_id1, $issue_id1, 10 );
-is( ref( $offset ), 'Koha::Account::Offset', "An issuing charge has been added" );
-my $charge = Koha::Account::Lines->find( $offset->debit_id );
+my $checkout = Koha::Checkouts->find( $issue_id1 );
+my $charge = C4::Circulation::AddIssuingCharge( $checkout, 10 );
+is( ref( $charge ), 'Koha::Account::Line', "An issuing charge has been added" );
 is( $charge->issue_id, $issue_id1, 'Issue id is set correctly for issuing charge' );
-my $account_id = $dbh->last_insert_id( undef, undef, 'accountlines', undef );
+my $offset = Koha::Account::Offsets->find( { debit_id => $charge->id } );
+is( $offset->credit_id, undef, 'Offset was created');
 $sth->execute;
-$countaccount = $sth -> fetchrow_array;
+$countaccount = $sth->fetchrow_array;
 is ($countaccount,1,"1 accountline has been added");
 
 # Test AddRenewal
@@ -222,9 +226,13 @@ my $se = Test::MockModule->new( 'C4::Context' );
 $se->mock( 'interface', sub {return 'intranet'});
 
 # Let's renew this one at a different library for statistical purposes to test Bug 17781
-C4::Context->set_userenv(@USERENV_DIFFERENT_LIBRARY);
+# Mocking userenv with a different branchcode
+t::lib::Mocks::mock_userenv({ patron => $patron_2, branchcode => $branchcode_3 });
+
 my $datedue3 = AddRenewal( $borrower_id1, $item_id1, $branchcode_1, $datedue1, $daysago10 );
-C4::Context->set_userenv(@USERENV);
+
+# Restoring the userenv with the original branchcode
+t::lib::Mocks::mock_userenv({ patron => $patron_1});
 
 like(
     $datedue3,
@@ -358,17 +366,17 @@ my $itemnumber;
 
 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
 AddReturn( 'barcode_3', $branchcode_1 );
-my $item = GetItem( $itemnumber );
-ok( $item->{notforloan} eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
+my $item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
 
 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
 AddReturn( 'barcode_3', $branchcode_1 );
-$item = GetItem( $itemnumber );
-ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
 
 AddReturn( 'barcode_3', $branchcode_1 );
-$item = GetItem( $itemnumber );
-ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
 
 # Bug 14640 - Cancel the hold on checking out if asked
 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,