Bug 39900: Add tests

prove t/db_dependent/api/v1/additional_contents.t

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
(cherry picked from commit 8e4793a604)
Signed-off-by: Paul Derscheid <paul.derscheid@lmscloud.de>
This commit is contained in:
Pedro Amorim 2025-05-14 12:19:55 +00:00 committed by Paul Derscheid
parent 8bed105c7b
commit 41b3dc9a99
No known key found for this signature in database
GPG Key ID: C25F9FD44CED595B
1 changed files with 97 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses>.
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;
};