From bbc61d01017c0738d4367581980b85289f9c0f29 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 27 Oct 2021 09:06:44 -0300 Subject: [PATCH] Bug 29290: Unit tests This patch adds unit tests for the new route. All behaviors are tested: To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/api/v1/biblios.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize --- t/db_dependent/api/v1/biblios.t | 67 ++++++++++++++++++++++++++++++++- 1 file changed, 66 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/api/v1/biblios.t b/t/db_dependent/api/v1/biblios.t index 25665ed5cf..821c806a4d 100755 --- a/t/db_dependent/api/v1/biblios.t +++ b/t/db_dependent/api/v1/biblios.t @@ -20,7 +20,7 @@ use Modern::Perl; use utf8; use Encode; -use Test::More tests => 5; +use Test::More tests => 6; use Test::MockModule; use Test::Mojo; use Test::Warn; @@ -29,8 +29,12 @@ use t::lib::Mocks; use t::lib::TestBuilder; use C4::Auth; +use C4::Circulation qw( AddIssue AddReturn ); + use Koha::Biblios; use Koha::Database; +use Koha::Checkouts; +use Koha::Old::Checkouts; my $schema = Koha::Database->new->schema; my $builder = t::lib::TestBuilder->new; @@ -494,3 +498,64 @@ subtest 'pickup_locations() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'get_checkouts() tests' => sub { + + plan tests => 14; + + $schema->storage->txn_begin; + + my $patron = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 0 } + } + ); + my $password = 'thePassword123'; + $patron->set_password( { password => $password, skip_validation => 1 } ); + $patron->discard_changes; + my $userid = $patron->userid; + + my $biblio = $builder->build_sample_biblio(); + $t->get_ok("//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/checkouts") + ->status_is(403); + + $patron->flags(1)->store; # circulate permissions + + $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/checkouts") + ->status_is(200) + ->json_is( '' => [], 'No checkouts on the biblio' ); + + my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber }); + + AddIssue( $patron->unblessed, $item_1->barcode ); + AddIssue( $patron->unblessed, $item_2->barcode ); + + my $ret = $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/checkouts") + ->status_is(200) + ->tx->res->json; + + my $checkout_1 = Koha::Checkouts->find({ itemnumber => $item_1->id }); + my $checkout_2 = Koha::Checkouts->find({ itemnumber => $item_2->id }); + + is_deeply( $ret, [ $checkout_1->to_api, $checkout_2->to_api ] ); + + AddReturn( $item_1->barcode ); + + $ret = $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/checkouts") + ->status_is(200) + ->tx->res->json; + + is_deeply( $ret, [ $checkout_2->to_api ] ); + + $ret = $t->get_ok( "//$userid:$password@/api/v1/biblios/" . $biblio->biblionumber . "/checkouts?checked_in=1") + ->status_is(200) + ->tx->res->json; + + my $old_checkout_1 = Koha::Old::Checkouts->find( $checkout_1->id ); + + is_deeply( $ret, [ $old_checkout_1->to_api ] ); + + $schema->storage->txn_rollback; +}; -- 2.20.1