Bug 17989: Extend bad template check
[koha.git] / t / db_dependent / Templates.t
blob1057ae5b78049fc79f28f2cad97c24447d6b74be
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use CGI;
22 use Test::More tests => 7;
23 use Test::Deep;
24 use Test::MockModule;
25 use Test::Warn;
27 use t::lib::Mocks;
29 BEGIN {
30 use_ok( 'C4::Templates' );
31 can_ok( 'C4::Templates',
32 qw/ GetColumnDefs
33 getlanguagecookie
34 setlanguagecookie
35 themelanguage
36 gettemplate
37 _get_template_file
38 param
39 output /);
42 my $query = CGI->new();
43 my $columns = C4::Templates::GetColumnDefs( $query );
45 is( ref( $columns ) eq 'HASH', 1, 'GetColumnDefs returns a hashref' );
46 # get the tables names, sorted
47 my @keys = sort keys %{$columns};
48 is( scalar @keys, 6, 'GetColumnDefs correctly returns the 5 tables defined in columns.def' );
49 my @tables = qw( biblio biblioitems borrowers items statistics subscription );
50 cmp_deeply( \@keys, \@tables, 'GetColumnDefs returns the expected tables');
52 subtest 'Testing themelanguage' => sub {
53 plan tests => 12;
54 my $testing_language;
55 my $module_language = Test::MockModule->new('C4::Languages');
57 $module_language->mock(
58 'getlanguage',
59 sub {
60 return $testing_language;
64 my $cgi = CGI->new();
65 my $htdocs = C4::Context->config('intrahtdocs');
66 my $section = 'intranet';
67 t::lib::Mocks::mock_preference( 'template', 'prog' );
69 # trigger first case.
70 $testing_language = 'en';
71 my ($theme, $lang, $availablethemes) = C4::Templates::themelanguage( $htdocs, 'about.tt', $section, $cgi);
72 is($theme,'prog',"Expected theme: set en - $theme");
73 is($lang,'en','Expected language: set en');
74 cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for set en' );
76 # trigger second case.
77 $testing_language = q{};
78 ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, 'about.tt', $section, $cgi);
79 is($theme,'prog',"Expected theme: default en - $theme");
80 is($lang,'en','Expected language: default en');
81 cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for default en' );
83 # trigger third case.
84 my $template = $htdocs . '/prog/en/modules/about.tt';
85 ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, $template, $section, $cgi);
86 is($theme,'prog',"Expected defined theme: unset - $theme");
87 is($lang,q{},'Expected language: unset');
88 cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for unset' );
90 # trigger bad case.
91 $template = $htdocs . '/prog/en/kaboom/about.tt';
92 ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, $template, $section, $cgi);
93 is($lang,undef,'Expected language: not coded for');
94 is( $availablethemes, undef, 'We do not expect any available themes -- not coded for' );
95 is($theme,undef,"Expected no theme: not coded for");
97 return;
100 subtest 'Testing gettemplate/badtemplatecheck' => sub {
101 plan tests => 7;
103 my $cgi = CGI->new;
104 my $template;
105 warning_like { eval { $template = C4::Templates::gettemplate( '/etc/passwd', 'opac', $cgi, 1 ) }; warn $@ if $@; } qr/bad template/, 'Bad template check';
106 is( $template ? $template->output: '', '', 'Check output' );
108 # Test a few more bad paths to gettemplate triggering badtemplatecheck
109 warning_like { eval { C4::Templates::gettemplate( '../topsecret.tt', 'opac', $cgi, 1 ) }; warn $@ if $@; } qr/bad template/, 'No safe chars';
110 warning_like { eval { C4::Templates::gettemplate( '/noaccess/topsecret.tt', 'opac', $cgi, 1 ) }; warn $@ if $@; } qr/bad template/, 'Directory not allowed';
111 warning_like { eval { C4::Templates::gettemplate( C4::Context->config('intrahtdocs') . '2/prog/en/modules/about.tt', 'intranet', $cgi, 1 ) }; warn $@ if $@; } qr/bad template/, 'Directory not allowed too';
113 # Allow templates from /tmp
114 t::lib::Mocks::mock_config( 'pluginsdir', [ '/tmp' ] );
115 warning_like { eval { C4::Templates::badtemplatecheck( '/tmp/about.tt' ) }; warn $@ if $@; } undef, 'No warn on template from plugin dir';
116 # Refuse wrong extension
117 warning_like { eval { C4::Templates::badtemplatecheck( '/tmp/about.tmpl' ) }; warn $@ if $@; } qr/bad template/, 'Warn on bad extension';