Bug 33852: Add one more test
[koha.git] / t / db_dependent / Templates.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 CGI;
21
22 use Test::More tests => 5;
23 use Test::Deep;
24 use Test::MockModule;
25 use Test::Warn;
26 use File::Temp qw/tempfile/;
27
28 use t::lib::Mocks;
29
30 use C4::Auth qw( get_template_and_user );
31
32 BEGIN {
33     use_ok('C4::Templates', qw( getlanguagecookie setlanguagecookie themelanguage gettemplate param output availablethemes badtemplatecheck ));
34     can_ok(
35         'C4::Templates',
36         qw(
37           getlanguagecookie
38           setlanguagecookie
39           themelanguage
40           gettemplate
41           _get_template_file
42           param
43           output
44           )
45     );
46 }
47
48 subtest 'Testing themelanguage' => sub {
49     plan tests => 12;
50     my $testing_language;
51     my $module_language = Test::MockModule->new('C4::Languages');
52
53     $module_language->mock(
54         'getlanguage',
55         sub {
56             return $testing_language;
57         }
58     );
59
60     my $cgi = CGI->new();
61     my $htdocs = C4::Context->config('intrahtdocs');
62     my $section = 'intranet';
63     t::lib::Mocks::mock_preference( 'template', 'prog' );
64
65     # trigger first case.
66     $testing_language = 'en';
67     my ($theme, $lang, $availablethemes) = C4::Templates::themelanguage( $htdocs, 'about.tt', $section, $cgi);
68     is($theme,'prog',"Expected theme: set en - $theme");
69     is($lang,'en','Expected language: set en');
70     cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for set en' );
71
72     # trigger second case.
73     $testing_language = q{};
74     ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, 'about.tt', $section, $cgi);
75     is($theme,'prog',"Expected theme: default en - $theme");
76     is($lang,'en','Expected language: default en');
77     cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for default en' );
78
79     # trigger third case.
80     my $template = $htdocs . '/prog/en/modules/about.tt';
81     ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, $template, $section, $cgi);
82     is($theme,'prog',"Expected defined theme: unset - $theme");
83     is($lang,q{},'Expected language: unset');
84     cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for unset' );
85
86     # trigger bad case.
87     $template = $htdocs . '/prog/en/kaboom/about.tt';
88     ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, $template, $section, $cgi);
89     is($lang,undef,'Expected language: not coded for');
90     is( $availablethemes, undef, 'We do not expect any available themes -- not coded for' );
91     is($theme,undef,"Expected no theme: not coded for");
92
93     return;
94 };
95
96 subtest 'Testing gettemplate/badtemplatecheck' => sub {
97     plan tests => 8;
98
99     my $cgi = CGI->new;
100     my $template;
101     warning_like { eval { $template = C4::Templates::gettemplate( '/etc/passwd', 'opac', $cgi ) }; warn $@ if $@; } qr/bad template/, 'Bad template check';
102     is( $template ? $template->output: '', '', 'Check output' );
103
104     # Test a few more bad paths to gettemplate triggering badtemplatecheck
105     warning_like { eval { C4::Templates::gettemplate( '../topsecret.tt', 'opac', $cgi ) }; warn $@ if $@; } qr/bad template/, 'No safe chars';
106     warning_like { eval { C4::Templates::gettemplate( '/noaccess/topsecret.tt', 'opac', $cgi ) }; warn $@ if $@; } qr/bad template/, 'Directory not allowed';
107     warning_like { eval { C4::Templates::gettemplate( C4::Context->config('intrahtdocs') . '2/prog/en/modules/about.tt', 'intranet', $cgi ) }; warn $@ if $@; } qr/bad template/, 'Directory not allowed too';
108
109     # Allow templates from /tmp
110     t::lib::Mocks::mock_config( 'pluginsdir', [ '/tmp' ] );
111     warning_like { eval { C4::Templates::badtemplatecheck( '/tmp/about.tt' ) }; warn $@ if $@; } undef, 'No warn on template from plugin dir';
112     # Refuse wrong extension
113     warning_like { eval { C4::Templates::badtemplatecheck( '/tmp/about.tmpl' ) }; warn $@ if $@; } qr/bad template/, 'Warn on bad extension';
114
115     # Make sure badtemplatecheck works on a copy of pluginsdir and doesn't modify its values
116     t::lib::Mocks::mock_config( 'pluginsdir', [ '/tmp', '/tmp2' ] );
117     C4::Templates::badtemplatecheck('/tmp/test.tt');
118     cmp_deeply( C4::Context->config('pluginsdir'), ['/tmp', '/tmp2'], "Doesn't modify configuration values" );
119
120 };
121
122 subtest "Absolute path change in _get_template_file" => sub {
123     plan tests => 1;
124
125     # We create a simple template in /tmp.
126     # We simulate an anonymous OPAC session; the OPACBaseURL template variable
127     # should be filled by get_template_and_user.
128     t::lib::Mocks::mock_config( 'pluginsdir', [ C4::Context::temporary_directory ] );
129     t::lib::Mocks::mock_preference( 'OPACBaseURL', 'without any doubt' );
130     my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1, DIR => C4::Context::temporary_directory );
131     print $fh q|I am a [% quality %] template [% OPACBaseURL %]|;
132     close $fh;
133     my ( $template, $login, $cookie ) = C4::Auth::get_template_and_user({
134         template_name => $fn,
135         query => CGI::new,
136         type => "opac",
137         authnotrequired => 1,
138     });
139     $template->param( quality => 'good-for-nothing' );
140     like( $template->output, qr/a good.+template.+doubt/, 'Testing a template with an absolute path' );
141 };