View | Details | Raw Unified | Return to bug 39900
Collapse All | Expand All

(-)a/t/db_dependent/api/v1/additional_contents.t (-1 / +97 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::NoWarnings;
21
use Test::More tests => 2;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Koha::AdditionalContents;
28
use Koha::Database;
29
use Koha::DateUtils qw( dt_from_string );
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
my $t = Test::Mojo->new('Koha::REST::V1');
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
subtest 'anonymous access' => sub {
38
    plan tests => 9;
39
40
    $schema->storage->txn_begin;
41
42
    Koha::AdditionalContents->search->delete;
43
44
    my $today     = dt_from_string;
45
    my $yesterday = dt_from_string->add( days => -1 );
46
    my $tomorrow  = dt_from_string->add( days => 1 );
47
    my $res;
48
49
    $builder->build_object(
50
        {
51
            class => 'Koha::AdditionalContents',
52
            value => {
53
                expirationdate => $tomorrow,
54
                published_on   => $tomorrow,
55
                category       => 'news',
56
                location       => 'staff_and_opac',
57
                branchcode     => undef,
58
                number         => 3,
59
            }
60
        }
61
    );
62
63
    t::lib::Mocks::mock_preference( 'RESTPublicAnonymousRequests', 1 );
64
65
    $res = $t->get_ok("/api/v1/public/additional_contents")->status_is(200)->tx->res->json;
66
67
    is( scalar @{$res}, 0, 'The only additional content is not active and not public' );
68
69
    my $public_additional_contents = $builder->build_object(
70
        {
71
            class => 'Koha::AdditionalContents',
72
            value => {
73
                expirationdate => $tomorrow,
74
                published_on   => $yesterday,
75
                category       => 'news',
76
                location       => 'staff_and_opac',
77
                branchcode     => undef,
78
                number         => 3,
79
            }
80
        }
81
    );
82
83
    $res = $t->get_ok("/api/v1/public/additional_contents")->status_is(200)->tx->res->json;
84
85
    is( scalar @{$res}, 1, 'There is now one active and public additional content' );
86
87
    $t->get_ok( "/api/v1/public/additional_contents" => { 'x-koha-embed' => 'translated_contents' } )->status_is(200)
88
        ->json_is(
89
        '/0' => {
90
            %{ $public_additional_contents->to_api( { public => 1 } ) },
91
            translated_contents => $public_additional_contents->translated_contents->to_api( { public => 1 } )
92
        }
93
        );
94
95
    $schema->storage->txn_rollback;
96
97
};

Return to bug 39900