From 3ead2ab623d2702c46416a635b9b80d2f09e6109 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 12 Apr 2023 10:45:33 +0200 Subject: [PATCH] Bug 33105: Add tests Signed-off-by: Jonathan Field Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/REST/V1/Acquisitions/Vendor/Issues.pm | 72 ++++++++++++++++ t/db_dependent/Koha/Acquisition/Booksellers.t | 30 ++++++- .../api/v1/acquisitions_vendor_issues.t | 82 +++++++++++++++++++ 3 files changed, 183 insertions(+), 1 deletion(-) create mode 100644 Koha/REST/V1/Acquisitions/Vendor/Issues.pm create mode 100755 t/db_dependent/api/v1/acquisitions_vendor_issues.t diff --git a/Koha/REST/V1/Acquisitions/Vendor/Issues.pm b/Koha/REST/V1/Acquisitions/Vendor/Issues.pm new file mode 100644 index 0000000000..683d52ae79 --- /dev/null +++ b/Koha/REST/V1/Acquisitions/Vendor/Issues.pm @@ -0,0 +1,72 @@ +package Koha::REST::V1::Acquisitions::Vendor::Issues; + +# 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 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, see . + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Try::Tiny qw( catch try ); + +=head1 NAME + +Koha::REST::V1::Acquisitions::Vendor::Issues + +=head1 API + +=head2 Class methods + +=head3 list + +Controller function that handles vendor issues + +=cut + +=head3 list + +Return the list of issues for a given vendor + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + return try { + + my $vendor_id = $c->validation->param('vendor_id'); + my $vendor = Koha::Acquisition::Booksellers->find($vendor_id); + + unless ($vendor) { + return $c->render( + status => 404, + openapi => { error => "Vendor not found." } + ); + } + + my $issues_rs = $vendor->issues; + my $issues = $c->objects->search( $issues_rs ); + + return $c->render( + status => 200, + openapi => $issues, + ); + } + catch { + $c->unhandled_exception($_); + }; +} + +1; diff --git a/t/db_dependent/Koha/Acquisition/Booksellers.t b/t/db_dependent/Koha/Acquisition/Booksellers.t index 32106b86c1..3f9b8a6c87 100755 --- a/t/db_dependent/Koha/Acquisition/Booksellers.t +++ b/t/db_dependent/Koha/Acquisition/Booksellers.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 5; +use Test::More tests => 6; use t::lib::TestBuilder; @@ -225,3 +225,31 @@ subtest 'interfaces' => sub { $schema->storage->txn_rollback(); }; + +subtest 'issues' => sub { + + plan tests => 4; + + $schema->storage->txn_begin(); + + my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } ); + + is( $vendor->issues->count, 0, 'Vendor has no issues' ); + + Koha::Acquisition::Bookseller::Issue->new( + { + vendor_id => $vendor->id, + type => 'MAINTENANCE', + notes => 'a vendor issue' + } + )->store; + + $vendor = $vendor->get_from_storage; + my $issues = $vendor->issues; + is( $issues->count, 1, '1 issue stored' ); + is( ref($issues), 'Koha::Acquisition::Bookseller::Issues', 'Type is correct' ); + + is( $issues->next->strings_map->{type}->{str}, 'Maintenance' ); + + $schema->storage->txn_rollback(); +}; diff --git a/t/db_dependent/api/v1/acquisitions_vendor_issues.t b/t/db_dependent/api/v1/acquisitions_vendor_issues.t new file mode 100755 index 0000000000..7c0d8e973e --- /dev/null +++ b/t/db_dependent/api/v1/acquisitions_vendor_issues.t @@ -0,0 +1,82 @@ +#!/usr/bin/env perl + +# 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 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, see . + +use Modern::Perl; + +use Test::More tests => 1; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::Acquisition::Booksellers; +use Koha::Database; + +my $schema = Koha::Database->new->schema; +my $builder = t::lib::TestBuilder->new; + +my $t = Test::Mojo->new('Koha::REST::V1'); +t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); + +subtest 'list() tests' => sub { + + plan tests => 11; + + $schema->storage->txn_begin; + + my $vendor = $builder->build_object( { class => 'Koha::Acquisition::Booksellers' } ); + my $vendor_id = $vendor->id; + + my $librarian = $builder->build_object( + { + class => 'Koha::Patrons', + value => { flags => 2 ** 11 } + } + ); + my $password = 'thePassword123'; + $librarian->set_password( { password => $password, skip_validation => 1 } ); + my $userid = $librarian->userid; + + # No issues, so empty array should be returned + $t->get_ok("//$userid:$password@/api/v1/acquisitions/vendors/$vendor_id/issues") + ->status_is(200) + ->json_is( [] ); + + my $issue = Koha::Acquisition::Bookseller::Issue->new( + { + vendor_id => $vendor_id, + type => 'MAINTENANCE', + notes => 'a vendor issue' + } + )->store; + # One issue created, should get returned + $t->get_ok("//$userid:$password@/api/v1/acquisitions/vendors/$vendor_id/issues") + ->status_is(200) + ->json_is( [$issue->to_api] ); + # Embed the AV description + $t->get_ok("//$userid:$password@/api/v1/acquisitions/vendors/$vendor_id/issues" + => { 'x-koha-embed' => '+strings' }) + ->status_is(200) + ->json_is( [$issue->to_api({ strings => 1 })] ); + + $vendor->delete; + # No vendor, should get 404 + $t->get_ok("//$userid:$password@/api/v1/acquisitions/vendors/$vendor_id/issues") + ->status_is(404); + + $schema->storage->txn_rollback; +}; -- 2.39.5