From ba7e43c3269c7a1384f16474439c6e5660566507 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 14 May 2025 12:19:55 +0000 Subject: [PATCH] Bug 39900: Add tests prove t/db_dependent/api/v1/additional_contents.t Signed-off-by: David Nind --- t/db_dependent/api/v1/additional_contents.t | 97 +++++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100755 t/db_dependent/api/v1/additional_contents.t diff --git a/t/db_dependent/api/v1/additional_contents.t b/t/db_dependent/api/v1/additional_contents.t new file mode 100755 index 0000000000..3d1b4ef369 --- /dev/null +++ b/t/db_dependent/api/v1/additional_contents.t @@ -0,0 +1,97 @@ +#!/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::NoWarnings; +use Test::More tests => 2; +use Test::Mojo; + +use t::lib::TestBuilder; +use t::lib::Mocks; + +use Koha::AdditionalContents; +use Koha::Database; +use Koha::DateUtils qw( dt_from_string ); + +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 'anonymous access' => sub { + plan tests => 9; + + $schema->storage->txn_begin; + + Koha::AdditionalContents->search->delete; + + my $today = dt_from_string; + my $yesterday = dt_from_string->add( days => -1 ); + my $tomorrow = dt_from_string->add( days => 1 ); + my $res; + + $builder->build_object( + { + class => 'Koha::AdditionalContents', + value => { + expirationdate => $tomorrow, + published_on => $tomorrow, + category => 'news', + location => 'staff_and_opac', + branchcode => undef, + number => 3, + } + } + ); + + t::lib::Mocks::mock_preference( 'RESTPublicAnonymousRequests', 1 ); + + $res = $t->get_ok("/api/v1/public/additional_contents")->status_is(200)->tx->res->json; + + is( scalar @{$res}, 0, 'The only additional content is not active and not public' ); + + my $public_additional_contents = $builder->build_object( + { + class => 'Koha::AdditionalContents', + value => { + expirationdate => $tomorrow, + published_on => $yesterday, + category => 'news', + location => 'staff_and_opac', + branchcode => undef, + number => 3, + } + } + ); + + $res = $t->get_ok("/api/v1/public/additional_contents")->status_is(200)->tx->res->json; + + is( scalar @{$res}, 1, 'There is now one active and public additional content' ); + + $t->get_ok( "/api/v1/public/additional_contents" => { 'x-koha-embed' => 'translated_contents' } )->status_is(200) + ->json_is( + '/0' => { + %{ $public_additional_contents->to_api( { public => 1 } ) }, + translated_contents => $public_additional_contents->translated_contents->to_api( { public => 1 } ) + } + ); + + $schema->storage->txn_rollback; + +}; -- 2.39.5