Bug 26734: (QA follow-up) Unit tests for C4::Letters
[koha.git] / t / Output.t
1 #!/usr/bin/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::More tests => 7;
21 use Test::Warn;
22 use CGI qw ( -utf8 );
23
24 use t::lib::Mocks;
25
26 BEGIN {
27     use_ok('C4::Output');
28 }
29
30 my $query = CGI->new();
31 my $cookie;
32 my $output = 'foobarbaz';
33
34 {
35     local *STDOUT;
36     my $stdout;
37     open STDOUT, '>', \$stdout;
38     output_html_with_http_headers $query, $cookie, $output, undef, { force_no_caching => 1 };
39     like($stdout, qr/Cache-control: no-cache, no-store, max-age=0/, 'force_no_caching sets Cache-control as desired');
40     like($stdout, qr/Expires: /, 'force_no_caching sets an Expires header');
41     $stdout = '';
42     close STDOUT;
43     open STDOUT, '>', \$stdout;
44     output_html_with_http_headers $query, $cookie, $output, undef, undef;
45     like($stdout, qr/Cache-control: no-cache[^,]/, 'not using force_no_caching sets Cache-control as desired');
46     unlike($stdout, qr/Expires: /, 'force_no_caching does not set an Expires header');
47 }
48
49 subtest 'parametrized_url' => sub {
50     plan tests => 2;
51
52     my $url = 'https://somesite.com/search?q={TITLE}&author={AUTHOR}{SUFFIX}';
53     my $subs = { TITLE => '_title_', AUTHOR => undef, ISBN => '123456789' };
54     my $res;
55     warning_is { $res = C4::Output::parametrized_url( $url, $subs ) }
56         q{}, 'No warning expected on undefined author';
57     is( $res, 'https://somesite.com/search?q=_title_&author=',
58         'Title replaced, author empty and SUFFIX removed' );
59 };
60
61 subtest 'output_with_http_headers() tests' => sub {
62
63     plan tests => 4;
64
65     local *STDOUT;
66     my $stdout;
67
68     my $query = CGI->new();
69     my $cookie;
70     my $output = 'foobarbaz';
71
72     open STDOUT, '>', \$stdout;
73     t::lib::Mocks::mock_preference('AccessControlAllowOrigin','');
74     output_html_with_http_headers $query, $cookie, $output, undef;
75     unlike($stdout, qr/Access-control-allow-origin/, 'No header set if no value on syspref');
76     close STDOUT;
77
78     open STDOUT, '>', \$stdout;
79     t::lib::Mocks::mock_preference('AccessControlAllowOrigin',undef);
80     output_html_with_http_headers $query, $cookie, $output, undef;
81     unlike($stdout, qr/Access-control-allow-origin/, 'No header set if no value on syspref');
82     close STDOUT;
83
84     open STDOUT, '>', \$stdout;
85     t::lib::Mocks::mock_preference('AccessControlAllowOrigin','*');
86     output_html_with_http_headers $query, $cookie, $output, undef;
87     like($stdout, qr/Access-control-allow-origin: \*/, 'Header set to *');
88     close STDOUT;
89
90     open STDOUT, '>', \$stdout;
91     t::lib::Mocks::mock_preference('AccessControlAllowOrigin','https://koha-community.org');
92     output_html_with_http_headers $query, $cookie, $output, undef;
93     like($stdout, qr/Access-control-allow-origin: https:\/\/koha-community\.org/, 'Header set to https://koha-community.org');
94     close STDOUT;
95 };