Bug 30447: Unit tests
[koha-ffzg.git] / t / db_dependent / Holds.t
index 283ee0a..41e652c 100755 (executable)
@@ -7,7 +7,7 @@ use t::lib::TestBuilder;
 
 use C4::Context;
 
-use Test::More tests => 73;
+use Test::More tests => 75;
 use Test::Exception;
 
 use MARC::Record;
@@ -22,13 +22,15 @@ use Koha::Biblios;
 use Koha::CirculationRules;
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
-use Koha::Holds;
+use Koha::Holds qw( search );
 use Koha::Checkout;
 use Koha::Item::Transfer::Limits;
 use Koha::Items;
 use Koha::Libraries;
 use Koha::Library::Groups;
 use Koha::Patrons;
+use Koha::Hold qw( get_items_that_can_fill );
+use Koha::Item::Transfers;
 
 BEGIN {
     use FindBin;
@@ -1549,7 +1551,34 @@ subtest 'non priority holds' => sub {
     is( $err, 'on_reserve', 'Item is on hold' );
 
     $schema->storage->txn_rollback;
+};
+
+subtest 'CanItemBeReserved / recall' => sub {
+    plan tests => 1;
 
+    $schema->storage->txn_begin;
+
+    my $itemtype1 = $builder->build_object( { class => 'Koha::ItemTypes' } );
+    my $library1  = $builder->build_object( { class => 'Koha::Libraries', value => {pickup_location => 1} } );
+    my $patron1   = $builder->build_object( { class => 'Koha::Patrons', value => {branchcode => $library1->branchcode} } );
+    my $biblio1 = $builder->build_sample_biblio({ itemtype => $itemtype1->itemtype });
+    my $item1   = $builder->build_sample_item(
+        {
+            biblionumber => $biblio1->biblionumber,
+            library      => $library1->branchcode
+        }
+    );
+    Koha::Recall->new({
+        patron_id => $patron1->borrowernumber,
+        biblio_id => $biblio1->biblionumber,
+        pickup_library_id => $library1->branchcode,
+        item_id => $item1->itemnumber,
+        created_date => '2020-05-04 10:10:10',
+        item_level => 1,
+    })->store;
+    is( CanItemBeReserved( $patron1, $item1, $library1->branchcode )->{status}, 'recall', "Can't reserve an item that they have already recalled" );
+
+    $schema->storage->txn_rollback;
 };
 
 subtest 'CanItemBeReserved rule precedence tests' => sub {
@@ -1675,3 +1704,93 @@ subtest 'ModReserve can only update expirationdate for found holds' => sub {
     $schema->storage->txn_rollback;
 
 };
+
+subtest 'Koha::Holds->get_items_that_can_fill returns items with datecancelled or (inclusive) datearrived' => sub {
+    plan tests => 8;
+    # biblio item with date arrived and date cancelled
+    my $biblio1 = $builder->build_sample_biblio();
+    my $item1 = $builder->build_sample_item({ biblionumber => $biblio1->biblionumber });
+
+    my $transfer1 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
+        datecancelled => '2022-06-12',
+        itemnumber => $item1->itemnumber
+    }});
+
+    my $hold1 = $builder->build_object({ class => 'Koha::Holds', value => {
+        biblionumber => $biblio1->biblionumber,
+        itemnumber => undef,
+        itemtype => undef,
+        found => undef
+    }});
+
+    # biblio item with date arrived and NO date cancelled
+    my $biblio2 = $builder->build_sample_biblio();
+    my $item2 = $builder->build_sample_item({ biblionumber => $biblio2->biblionumber });
+
+    my $transfer2 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
+        datecancelled => undef,
+        itemnumber => $item2->itemnumber
+    }});
+
+    my $hold2 = $builder->build_object({ class => 'Koha::Holds', value => {
+        biblionumber => $biblio2->biblionumber,
+        itemnumber => undef,
+        itemtype => undef,
+        found => undef
+    }});
+
+    # biblio item with NO date arrived and date cancelled
+    my $biblio3 = $builder->build_sample_biblio();
+    my $item3 = $builder->build_sample_item({ biblionumber => $biblio3->biblionumber });
+
+    my $transfer3 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
+        datecancelled => '2022-06-12',
+        itemnumber => $item3->itemnumber,
+        datearrived => undef
+    }});
+
+    my $hold3 = $builder->build_object({ class => 'Koha::Holds', value => {
+        biblionumber => $biblio3->biblionumber,
+        itemnumber => undef,
+        itemtype => undef,
+        found => undef
+    }});
+
+
+    # biblio item with NO date arrived and NO date cancelled
+    my $biblio4 = $builder->build_sample_biblio();
+    my $item4 = $builder->build_sample_item({ biblionumber => $biblio4->biblionumber });
+
+    my $transfer4 = $builder->build_object({ class => "Koha::Item::Transfers", value => {
+        datecancelled => undef,
+        itemnumber => $item4->itemnumber,
+        datearrived => undef
+    }});
+
+    my $hold4 = $builder->build_object({ class => 'Koha::Holds', value => {
+        biblionumber => $biblio4->biblionumber,
+        itemnumber => undef,
+        itemtype => undef,
+        found => undef
+    }});
+
+    # create the holds which get_items_that_can_fill will be ran on
+    my $holds1 = Koha::Holds->search({reserve_id => $hold1->id});
+    my $holds2 = Koha::Holds->search({reserve_id => $hold2->id});
+    my $holds3 = Koha::Holds->search({reserve_id => $hold3->id});
+    my $holds4 = Koha::Holds->search({reserve_id => $hold4->id});
+
+    my $items_that_can_fill1 = $holds1->get_items_that_can_fill;
+    my $items_that_can_fill2 = $holds2->get_items_that_can_fill;
+    my $items_that_can_fill3 = $holds3->get_items_that_can_fill;
+    my $items_that_can_fill4 = $holds4->get_items_that_can_fill;
+
+    is($items_that_can_fill1->next->id, $item1->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and datecancelled");
+    is($items_that_can_fill1->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
+    is($items_that_can_fill2->next->id, $item2->id, "Koha::Holds->get_items_that_can_fill returns item with defined datearrived and undefined datecancelled");
+    is($items_that_can_fill2->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
+    is($items_that_can_fill3->next->id, $item3->id, "Koha::Holds->get_items_that_can_fill returns item with undefined datearrived and defined datecancelled");
+    is($items_that_can_fill3->count, 1, "Koha::Holds->get_items_that_can_fill returns 1 item with correct parameters");
+    is($items_that_can_fill4->next, undef, "Koha::Holds->get_items_that_can_fill doesn't return item with undefined datearrived and undefined datecancelled");
+    is($items_that_can_fill4->count, 0, "Koha::Holds->get_items_that_can_fill returns 0 item");
+}