Bug 7317: Handle backend absense more gracefuly
[koha.git] / t / db_dependent / DecreaseLoanHighHolds.t
index 899de0d..49d3cd8 100755 (executable)
 
 use Modern::Perl;
 
-use DateTime;
-
 use C4::Circulation;
 use Koha::Database;
-use Koha::Borrower;
+use Koha::Patron;
 use Koha::Biblio;
 use Koha::Item;
 use Koha::Holds;
 use Koha::Hold;
+use t::lib::TestBuilder;
+use t::lib::Mocks;
 
-use Test::More tests => 12;
+use Test::More tests => 17;
 
 my $dbh    = C4::Context->dbh;
 my $schema = Koha::Database->new()->schema();
+my $builder = t::lib::TestBuilder->new;
 
 # Start transaction
 $dbh->{RaiseError} = 1;
@@ -41,15 +42,20 @@ $dbh->do('DELETE FROM issuingrules');
 $dbh->do('DELETE FROM borrowers');
 $dbh->do('DELETE FROM items');
 
+my $library  = $builder->build( { source => 'Branch' } );
+my $category = $builder->build( { source => 'Category' } );
+my $itemtype = $builder->build( { source => 'Itemtype' } )->{itemtype};
+
 # Set userenv
 C4::Context->_new_userenv('xxx');
-C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', 'MPL', 'Midway Public Library', '', '', '' );
-is( C4::Context->userenv->{branch}, 'MPL', 'userenv set' );
+C4::Context->set_userenv( 0, 0, 0, 'firstname', 'surname', $library->{branchcode}, 'Midway Public Library', '', '', '' );
+is( C4::Context->userenv->{branch}, $library->{branchcode}, 'userenv set' );
 
+my $patron_category = $builder->build({ source => 'Category', value => { categorycode => 'NOT_X', category_type => 'P', enrolmentfee => 0 } });
 my @patrons;
 for my $i ( 1 .. 20 ) {
-    my $patron = Koha::Borrower->new(
-        { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => 'S', branchcode => 'MPL' } )
+    my $patron = Koha::Patron->new(
+        { cardnumber => $i, firstname => 'Kyle', surname => 'Hall', categorycode => $category->{categorycode}, branchcode => $library->{branchcode}, categorycode => $patron_category->{categorycode}, } )
       ->store();
     push( @patrons, $patron );
 }
@@ -60,44 +66,73 @@ my $biblioitem =
 
 my @items;
 for my $i ( 1 .. 10 ) {
-    my $item = Koha::Item->new( { biblionumber => $biblio->id(), biblioitemnumber => $biblioitem->id(), } )->store();
+    my $item = Koha::Item->new(
+        {
+            biblionumber     => $biblio->id(),
+            biblioitemnumber => $biblioitem->id(),
+            barcode          => $i,
+            itype            => $itemtype
+        }
+    )->store();
     push( @items, $item );
 }
 
-for my $i ( 0 .. 4 ) {
+for my $i ( 0 .. 5 ) {
     my $patron = $patrons[$i];
     my $hold   = Koha::Hold->new(
         {
             borrowernumber => $patron->id,
             biblionumber   => $biblio->id,
-            branchcode     => 'MPL',
+            branchcode     => $library->{branchcode},
         }
     )->store();
 }
 
-$schema->resultset('Issuingrule')
-  ->new( { branchcode => '*', categorycode => '*', itemtype => '*', issuelength => '14', lengthunit => 'days', reservesallowed => '99' } )
-  ->insert();
+$builder->build(
+    {
+        source => 'Issuingrule',
+        value => {
+            branchcode => '*',
+            categorycode => '*',
+            itemtype => '*',
+            issuelength => '14',
+            lengthunit => 'days',
+            reservesallowed => '99',
+        }
+    }
+);
 
 my $item   = pop(@items);
 my $patron = pop(@patrons);
 
-C4::Context->set_preference( 'decreaseLoanHighHolds',               1 );
-C4::Context->set_preference( 'decreaseLoanHighHoldsDuration',       1 );
-C4::Context->set_preference( 'decreaseLoanHighHoldsValue',          1 );
-C4::Context->set_preference( 'decreaseLoanHighHoldsControl',        'static' );
-C4::Context->set_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
+my $orig_due = C4::Circulation::CalcDateDue(
+    DateTime->now(time_zone => C4::Context->tz()),
+    $item->effective_itemtype,
+    $patron->branchcode,
+    $patron->unblessed
+);
 
-my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => 'MPL', holdingbranch => 'MPL' };
-my $patron_hr = { borrower => $patron->id, branchcode => 'MPL' };
+t::lib::Mocks::mock_preference( 'decreaseLoanHighHolds',               1 );
+t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsDuration',       1 );
+t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue',          1 );
+t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl',        'static' );
+t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsIgnoreStatuses', 'damaged,itemlost,notforloan,withdrawn' );
+
+my $item_hr = { itemnumber => $item->id, biblionumber => $biblio->id, homebranch => $library->{branchcode}, holdingbranch => $library->{branchcode}, barcode => $item->barcode };
+my $patron_hr = { borrowernumber => $patron->id, branchcode => $library->{branchcode} };
 
 my $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
-is( $data->{exceeded},        1,          "Should exceed threshold" );
-is( $data->{outstanding},     5,          "Should have 5 outstanding holds" );
+is( $data->{exceeded},        1,          "Static mode should exceed threshold" );
+is( $data->{outstanding},     6,          "Should have 5 outstanding holds" );
 is( $data->{duration},        1,          "Should have duration of 1" );
 is( ref( $data->{due_date} ), 'DateTime', "due_date should be a DateTime object" );
 
-C4::Context->set_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
+my $duedate = $data->{due_date};
+is($duedate->hour, $orig_due->hour, 'New due hour is equal to original due hour.');
+is($duedate->min, $orig_due->min, 'New due minute is equal to original due minute.');
+is($duedate->sec, 0, 'New due date second is zero.');
+
+t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsControl', 'dynamic' );
 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
 is( $data->{exceeded}, 0, "Should not exceed threshold" );
 
@@ -107,45 +142,52 @@ for my $i ( 5 .. 10 ) {
         {
             borrowernumber => $patron->id,
             biblionumber   => $biblio->id,
-            branchcode     => 'MPL',
+            branchcode     => $library->{branchcode},
         }
     )->store();
 }
 
 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
-is( $data->{exceeded}, 1, "Should exceed threshold" );
+is( $data->{exceeded}, 1, "Should exceed threshold of 1" );
 
-C4::Context->set_preference( 'decreaseLoanHighHoldsValue', 2 );
+t::lib::Mocks::mock_preference( 'decreaseLoanHighHoldsValue', 2 );
 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
-is( $data->{exceeded}, 0, "Should not exceed threshold" );
+is( $data->{exceeded}, 0, "Should not exceed threshold of 2" );
 
 my $unholdable = pop(@items);
 $unholdable->damaged(-1);
 $unholdable->store();
 
 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
-is( $data->{exceeded}, 1, "Should exceed threshold" );
+is( $data->{exceeded}, 1, "Should exceed threshold with one damaged item" );
 
 $unholdable->damaged(0);
 $unholdable->itemlost(-1);
 $unholdable->store();
 
 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
-is( $data->{exceeded}, 1, "Should exceed threshold" );
+is( $data->{exceeded}, 1, "Should exceed threshold with one lost item" );
 
 $unholdable->itemlost(0);
 $unholdable->notforloan(-1);
 $unholdable->store();
 
 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
-is( $data->{exceeded}, 1, "Should exceed threshold" );
+is( $data->{exceeded}, 1, "Should exceed threshold with one notforloan item" );
 
 $unholdable->notforloan(0);
 $unholdable->withdrawn(-1);
 $unholdable->store();
 
 $data = C4::Circulation::checkHighHolds( $item_hr, $patron_hr );
-is( $data->{exceeded}, 1, "Should exceed threshold" );
+is( $data->{exceeded}, 1, "Should exceed threshold with one withdrawn item" );
+
+t::lib::Mocks::mock_preference('CircControl', 'PatronLibrary');
+
+my ( $un, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode );
+ok( $needsconfirmation->{HIGHHOLDS}, "High holds checkout needs confirmation" );
+
+( undef, $needsconfirmation ) = CanBookBeIssued( $patron_hr, $item->barcode, undef, undef, undef, { override_high_holds => 1 } );
+ok( !$needsconfirmation->{HIGHHOLDS}, "High holds checkout does not need confirmation" );
 
 $schema->storage->txn_rollback();
-1;