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>.
22 use Test::More tests => 5;
26 use File::Temp qw/tempfile/;
30 use C4::Auth qw( get_template_and_user );
33 use_ok('C4::Templates', qw( getlanguagecookie setlanguagecookie themelanguage gettemplate param output availablethemes badtemplatecheck ));
48 subtest 'Testing themelanguage' => sub {
51 my $module_language = Test::MockModule->new('C4::Languages');
53 $module_language->mock(
56 return $testing_language;
61 my $htdocs = C4::Context->config('intrahtdocs');
62 my $section = 'intranet';
63 t::lib::Mocks::mock_preference( 'template', 'prog' );
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' );
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' );
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' );
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");
96 subtest 'Testing gettemplate/badtemplatecheck' => sub {
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' );
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';
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';
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" );
122 subtest "Absolute path change in _get_template_file" => sub {
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 %]|;
133 my ( $template, $login, $cookie ) = C4::Auth::get_template_and_user({
134 template_name => $fn,
137 authnotrequired => 1,
139 $template->param( quality => 'good-for-nothing' );
140 like( $template->output, qr/a good.+template.+doubt/, 'Testing a template with an absolute path' );