Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / api / v1 / checkouts.t
old mode 100644 (file)
new mode 100755 (executable)
index c4da746..abd32a4
@@ -2,22 +2,22 @@
 
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
-use Test::More tests => 73;
+use Test::More tests => 93;
 use Test::MockModule;
 use Test::Mojo;
 use t::lib::Mocks;
@@ -26,7 +26,7 @@ use t::lib::TestBuilder;
 use DateTime;
 
 use C4::Context;
-use C4::Circulation;
+use C4::Circulation qw( AddIssue AddReturn );
 
 use Koha::Database;
 use Koha::DateUtils;
@@ -59,7 +59,7 @@ my $unauth_userid = $patron->userid;
 my $patron_id = $patron->borrowernumber;
 
 my $branchcode = $builder->build({ source => 'Branch' })->{ branchcode };
-my $module = new Test::MockModule('C4::Context');
+my $module = Test::MockModule->new('C4::Context');
 $module->mock('userenv', sub { { branch => $branchcode } });
 
 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
@@ -71,9 +71,23 @@ $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$notexisting_patron
   ->status_is(200)
   ->json_is([]);
 
+Koha::CirculationRules->set_rules(
+    {
+        categorycode => undef,
+        itemtype     => undef,
+        branchcode   => undef,
+        rules        => {
+            renewalperiod => 7,
+            renewalsallowed => 1,
+            issuelength => 5,
+        }
+    }
+);
+
 my $item1 = $builder->build_sample_item;
 my $item2 = $builder->build_sample_item;
 my $item3 = $builder->build_sample_item;
+my $item4 = $builder->build_sample_item;
 
 my $date_due = DateTime->now->add(weeks => 2);
 my $issue1 = C4::Circulation::AddIssue($patron->unblessed, $item1->barcode, $date_due);
@@ -82,6 +96,8 @@ my $issue2 = C4::Circulation::AddIssue($patron->unblessed, $item2->barcode, $dat
 my $date_due2 = Koha::DateUtils::dt_from_string( $issue2->date_due );
 my $issue3 = C4::Circulation::AddIssue($librarian->unblessed, $item3->barcode, $date_due);
 my $date_due3 = Koha::DateUtils::dt_from_string( $issue3->date_due );
+my $issue4 = C4::Circulation::AddIssue($patron->unblessed, $item4->barcode);
+C4::Circulation::AddReturn($item4->barcode, $branchcode);
 
 $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
   ->status_is(200)
@@ -93,6 +109,23 @@ $t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id" )
   ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
   ->json_hasnt('/2');
 
+# Test checked_in parameter, zero means, the response is same as without it
+$t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&checked_in=0" )
+  ->status_is(200)
+  ->json_is('/0/patron_id' => $patron_id)
+  ->json_is('/0/item_id' => $item1->itemnumber)
+  ->json_is('/0/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due1 }) )
+  ->json_is('/1/patron_id' => $patron_id)
+  ->json_is('/1/item_id' => $item2->itemnumber)
+  ->json_is('/1/due_date' => output_pref({ dateformat => "rfc3339", dt => $date_due2 }) )
+  ->json_hasnt('/2');
+
+# Test checked_in parameter, one measn, the checked in checkout is in the response too
+$t->get_ok( "//$userid:$password@/api/v1/checkouts?patron_id=$patron_id&checked_in=1" )
+  ->status_is(200)
+  ->json_is('/0/patron_id' => $patron_id)
+  ->json_is('/0/item_id' => $item4->itemnumber)
+  ->json_hasnt('/1');
 
 $t->get_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->issue_id )
   ->status_is(403)
@@ -147,16 +180,9 @@ $t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id)
   ->status_is(200)
   ->json_is('/due_date' => output_pref( { dateformat => "rfc3339", dt => $date_due2 }) );
 
-
-$dbh->do('DELETE FROM issuingrules');
-$dbh->do(q{
-    INSERT INTO issuingrules (categorycode, branchcode, itemtype, renewalperiod, renewalsallowed)
-    VALUES (?, ?, ?, ?, ?)
-}, {}, '*', '*', '*', 7, 1);
-
-my $expected_datedue = DateTime->now
+my $expected_datedue = $date_due
     ->set_time_zone('local')
-    ->add(days => 14)
+    ->add(days => 7)
     ->set(hour => 23, minute => 59, second => 0);
 $t->post_ok ( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/renewal" )
   ->status_is(201)
@@ -169,6 +195,16 @@ $t->post_ok( "//$unauth_userid:$unauth_password@/api/v1/checkouts/" . $issue3->i
               required_permissions => { circulate => "circulate_remaining_permissions" }
             });
 
+$t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
+  ->status_is(200)
+  ->json_is({
+        allows_renewal   => Mojo::JSON->true,
+        max_renewals     => 1,
+        unseen_renewals  => 0,
+        current_renewals => 0,
+        error            => undef
+    });
+
 $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/renewal" )
   ->status_is(201)
   ->json_is('/due_date' => output_pref({ dateformat => "rfc3339", dt => $expected_datedue}) )
@@ -179,3 +215,12 @@ $t->post_ok( "//$userid:$password@/api/v1/checkouts/" . $issue1->issue_id . "/re
   ->status_is(403)
   ->json_is({ error => 'Renewal not authorized (too_many)' });
 
+$t->get_ok( "//$userid:$password@/api/v1/checkouts/" . $issue2->issue_id . "/allows_renewal")
+  ->status_is(200)
+  ->json_is({
+        allows_renewal   => Mojo::JSON->false,
+        max_renewals     => 1,
+        unseen_renewals  => 0,
+        current_renewals => 1,
+        error            => 'too_many'
+    });