Bug 15486: Unit tests
authorTomas Cohen Arazi <tomascohen@theke.io>
Wed, 19 Jul 2017 19:30:24 +0000 (16:30 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Wed, 24 Oct 2018 13:24:04 +0000 (13:24 +0000)
This patch introduces unit tests for the new circulation rules option
that allows setting a max holds per day limit.

To test:
- Apply the patch
- Run:
  $ sudo koha-shell kohadev
 k$ cd kohaclone
 k$ prove t/db_dependent/Holds.t
=> FAIL: CanItemBeReserved doesn't check the amount of holds per day
   and the introduced error code is not returned. OK is returned
   instead.

Signed-off-by: Séverine QUEUNE <severine.queune@bulac.fr>

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

t/db_dependent/Holds.t

index 25e12e0..863ae27 100755 (executable)
@@ -7,9 +7,8 @@ use t::lib::TestBuilder;
 
 use C4::Context;
 
-use Test::More tests => 56;
+use Test::More tests => 57;
 use MARC::Record;
-use Koha::Patrons;
 use C4::Items;
 use C4::Biblio;
 use C4::Reserves;
@@ -18,11 +17,12 @@ use C4::Calendar;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Biblios;
+use Koha::CirculationRules;
 use Koha::Holds;
+use Koha::IssuingRules;
 use Koha::Items;
 use Koha::Libraries;
 use Koha::Patrons;
-use Koha::CirculationRules;
 
 BEGIN {
     use FindBin;
@@ -519,6 +519,135 @@ subtest 'Pickup location availability tests' => sub {
        'libraryNotFound', 'Cannot set unknown library as pickup location');
 };
 
+$schema->storage->txn_rollback;
+
+subtest 'CanItemBeReserved / holds_per_day tests' => sub {
+
+    plan tests => 9;
+
+    $schema->storage->txn_begin;
+
+    Koha::Holds->search->delete;
+    $dbh->do('DELETE FROM issues');
+    Koha::Items->search->delete;
+    Koha::Biblios->search->delete;
+
+    my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } );
+    my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $patron   = $builder->build_object( { class => 'Koha::Patrons' } );
+
+    # Create 3 biblios with items
+    my ($bibnum_1) = create_helper_biblio( $itemtype->itemtype );
+    my ( undef, undef, $itemnumber_1 ) = AddItem(
+        {   homebranch    => $library->branchcode,
+            holdingbranch => $library->branchcode
+        },
+        $bibnum
+    );
+    my ($bibnum_2) = create_helper_biblio( $itemtype->itemtype );
+    my ( undef, undef, $itemnumber_2 ) = AddItem(
+        {   homebranch    => $library->branchcode,
+            holdingbranch => $library->branchcode
+        },
+        $bibnum_2
+    );
+    my ($bibnum_3) = create_helper_biblio( $itemtype->itemtype );
+    my ( undef, undef, $itemnumber_3 ) = AddItem(
+        {   homebranch    => $library->branchcode,
+            holdingbranch => $library->branchcode
+        },
+        $bibnum_3
+    );
+
+    Koha::IssuingRules->search->delete;
+    my $issuingrule = Koha::IssuingRule->new(
+        {   categorycode     => '*',
+            branchcode       => '*',
+            itemtype         => $itemtype->itemtype,
+            reservesallowed  => 1,
+            holds_per_record => 99,
+            holds_per_day    => 2
+        }
+    )->store;
+
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
+        { status => 'OK' },
+        'Patron can reserve item with hold limit of 1, no holds placed'
+    );
+
+    AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, );
+
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_1 ),
+        { status => 'tooManyReserves', limit => 1 },
+        'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed'
+    );
+
+    # Raise reservesallowed to avoid tooManyReserves from it
+    $issuingrule->set( { reservesallowed => 3 } )->store;
+
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
+        { status => 'OK' },
+        'Patron can reserve item with 2 reserves daily cap'
+    );
+
+    # Add a second reserve
+    my $res_id = AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, );
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
+        { status => 'tooManyReservesToday', limit => 2 },
+        'Patron cannot a third item with 2 reserves daily cap'
+    );
+
+    # Update last hold so reservedate is in the past, so 2 holds, but different day
+    $hold = Koha::Holds->find($res_id);
+    my $yesterday = dt_from_string() - DateTime::Duration->new( days => 1 );
+    $hold->reservedate($yesterday)->store;
+
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
+        { status => 'OK' },
+        'Patron can reserve item with 2 bib level hold placed on different days, 2 reserves daily cap'
+    );
+
+    # Set holds_per_day to 0
+    $issuingrule->set( { holds_per_day => 0 } )->store;
+
+    # Delete existing holds
+    Koha::Holds->search->delete;
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
+        { status => 'tooManyReservesToday', limit => 0 },
+        'Patron cannot reserve if holds_per_day is 0 (i.e. 0 is 0)'
+    );
+
+    $issuingrule->set( { holds_per_day => undef } )->store;
+    Koha::Holds->search->delete;
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_2 ),
+        { status => 'OK' },
+        'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
+    );
+    AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_1, '', 1, );
+    AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_2, '', 1, );
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
+        { status => 'OK' },
+        'Patron can reserve if holds_per_day is undef (i.e. undef is unlimited daily cap)'
+    );
+    AddReserve( $library->branchcode, $patron->borrowernumber, $bibnum_3, '', 1, );
+    is_deeply(
+        CanItemBeReserved( $patron->borrowernumber, $itemnumber_3 ),
+        { status => 'tooManyReserves', limit => 3 },
+        'Unlimited daily holds, but reached reservesallowed'
+    );
+
+    $schema->storage->txn_rollback;
+};
+
+
 # Helper method to set up a Biblio.
 sub create_helper_biblio {
     my $itemtype = shift;