Bug 23710: (follow-up) Add tests for new features in Koha::REST::V!::Holds::add and...
authorAgustin Moyano <agustinmoyano@theke.io>
Thu, 3 Oct 2019 17:14:47 +0000 (14:14 -0300)
committerMartin Renvoize <martin.renvoize@ptfs-europe.com>
Mon, 7 Oct 2019 12:00:17 +0000 (13:00 +0100)
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Koha/REST/V1/Holds.pm
t/db_dependent/api/v1/holds.t

index 04d1014..00faf2f 100644 (file)
@@ -84,7 +84,14 @@ sub add {
         my $item_type         = $body->{item_type};
         my $expiration_date   = $body->{expiration_date};
         my $notes             = $body->{notes};
-        my $hold_date         = C4::Context->preference( 'AllowHoldDateInFuture' )?$body->{hold_date}:undef;
+        my $hold_date         = $body->{hold_date};
+
+        if(!C4::Context->preference( 'AllowHoldDateInFuture' ) && $hold_date) {
+            return $c->render(
+                status  => 400,
+                openapi => { error => "Hold date in future not allowed" }
+            );
+        }
 
         if ( $item_id and $biblio_id ) {
 
index 0997579..a1234da 100644 (file)
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 6;
+use Test::More tests => 8;
 use Test::Mojo;
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -34,6 +34,7 @@ use Koha::DateUtils;
 use Koha::Biblios;
 use Koha::Biblioitems;
 use Koha::Items;
+use Koha::CirculationRules;
 
 my $schema  = Koha::Database->new->schema;
 my $builder = t::lib::TestBuilder->new();
@@ -267,6 +268,67 @@ subtest 'Reserves with itemtype' => sub {
       ->json_is('/0/item_type', $itemtype);
 };
 
+
+subtest 'test AllowHoldDateInFuture' => sub {
+
+    plan tests => 6;
+
+    $dbh->do('DELETE FROM reserves');
+
+    my $future_hold_date = DateTime->now->add(days => 10)->truncate( to => 'day' );
+
+    my $post_data = {
+        patron_id => int($patron_1->borrowernumber),
+        biblio_id => int($biblio_1->biblionumber),
+        item_id => int($item_1->itemnumber),
+        pickup_library_id => $branchcode,
+        expiration_date => output_pref({ dt => $expiration_date, dateformat => 'rfc3339', dateonly => 1 }),
+        hold_date => output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }),
+        priority => 2,
+    };
+
+    t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 0 );
+
+    $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
+      ->status_is(400)
+      ->json_has('/error');
+
+    t::lib::Mocks::mock_preference( 'AllowHoldDateInFuture', 1 );
+
+    $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
+      ->status_is(201)
+      ->json_is('/hold_date', output_pref({ dt => $future_hold_date, dateformat => 'rfc3339', dateonly => 1 }));
+};
+
+subtest 'test AllowHoldPolicyOverride' => sub {
+
+    plan tests => 5;
+
+    $dbh->do('DELETE FROM reserves');
+
+    Koha::CirculationRules->set_rules(
+        {
+            categorycode => undef,
+            itemtype     => undef,
+            branchcode   => undef,
+            rules        => {
+                holdallowed              => 1
+            }
+        }
+    );
+
+    t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 0 );
+
+    $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
+      ->status_is(403)
+      ->json_has('/error');
+
+    t::lib::Mocks::mock_preference( 'AllowHoldPolicyOverride', 1 );
+
+    $t->post_ok( "//$userid_3:$password@/api/v1/holds" => json => $post_data )
+      ->status_is(201);
+};
+
 $schema->storage->txn_rollback;
 
 subtest 'suspend and resume tests' => sub {
@@ -442,4 +504,4 @@ subtest 'PUT /holds/{hold_id}/priority tests' => sub {
     is( $hold_3->discard_changes->priority, 3, 'Priority adjusted correctly' );
 
     $schema->storage->txn_rollback;
-};
+};
\ No newline at end of file