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
=> 8;
26 use File
::Temp qw
/tempfile/;
33 use_ok
( 'C4::Templates' );
34 can_ok
( 'C4::Templates',
45 my $query = CGI
->new();
46 my $columns = C4
::Templates
::GetColumnDefs
( $query );
48 is
( ref( $columns ) eq 'HASH', 1, 'GetColumnDefs returns a hashref' );
49 # get the tables names, sorted
50 my @keys = sort keys %{$columns};
51 is
( scalar @keys, 6, 'GetColumnDefs correctly returns the 5 tables defined in columns.def' );
52 my @tables = qw( biblio biblioitems borrowers items statistics subscription );
53 cmp_deeply
( \
@keys, \
@tables, 'GetColumnDefs returns the expected tables');
55 subtest
'Testing themelanguage' => sub {
58 my $module_language = Test
::MockModule
->new('C4::Languages');
60 $module_language->mock(
63 return $testing_language;
68 my $htdocs = C4
::Context
->config('intrahtdocs');
69 my $section = 'intranet';
70 t
::lib
::Mocks
::mock_preference
( 'template', 'prog' );
73 $testing_language = 'en';
74 my ($theme, $lang, $availablethemes) = C4
::Templates
::themelanguage
( $htdocs, 'about.tt', $section, $cgi);
75 is
($theme,'prog',"Expected theme: set en - $theme");
76 is
($lang,'en','Expected language: set en');
77 cmp_deeply
( $availablethemes, [ 'prog' ], 'We only expect one available theme for set en' );
79 # trigger second case.
80 $testing_language = q{};
81 ($theme, $lang, $availablethemes) = C4
::Templates
::themelanguage
($htdocs, 'about.tt', $section, $cgi);
82 is
($theme,'prog',"Expected theme: default en - $theme");
83 is
($lang,'en','Expected language: default en');
84 cmp_deeply
( $availablethemes, [ 'prog' ], 'We only expect one available theme for default en' );
87 my $template = $htdocs . '/prog/en/modules/about.tt';
88 ($theme, $lang, $availablethemes) = C4
::Templates
::themelanguage
($htdocs, $template, $section, $cgi);
89 is
($theme,'prog',"Expected defined theme: unset - $theme");
90 is
($lang,q{},'Expected language: unset');
91 cmp_deeply
( $availablethemes, [ 'prog' ], 'We only expect one available theme for unset' );
94 $template = $htdocs . '/prog/en/kaboom/about.tt';
95 ($theme, $lang, $availablethemes) = C4
::Templates
::themelanguage
($htdocs, $template, $section, $cgi);
96 is
($lang,undef,'Expected language: not coded for');
97 is
( $availablethemes, undef, 'We do not expect any available themes -- not coded for' );
98 is
($theme,undef,"Expected no theme: not coded for");
103 subtest
'Testing gettemplate/badtemplatecheck' => sub {
108 warning_like
{ eval { $template = C4
::Templates
::gettemplate
( '/etc/passwd', 'opac', $cgi ) }; warn $@
if $@
; } qr/bad template/, 'Bad template check';
109 is
( $template ?
$template->output: '', '', 'Check output' );
111 # Test a few more bad paths to gettemplate triggering badtemplatecheck
112 warning_like
{ eval { C4
::Templates
::gettemplate
( '../topsecret.tt', 'opac', $cgi ) }; warn $@
if $@
; } qr
/bad template
/, 'No safe chars';
113 warning_like
{ eval { C4
::Templates
::gettemplate
( '/noaccess/topsecret.tt', 'opac', $cgi ) }; warn $@
if $@
; } qr/bad template/, 'Directory not allowed';
114 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';
116 # Allow templates from /tmp
117 t
::lib
::Mocks
::mock_config
( 'pluginsdir', [ '/tmp' ] );
118 warning_like
{ eval { C4
::Templates
::badtemplatecheck
( '/tmp/about.tt' ) }; warn $@
if $@
; } undef, 'No warn on template from plugin dir';
119 # Refuse wrong extension
120 warning_like
{ eval { C4
::Templates
::badtemplatecheck
( '/tmp/about.tmpl' ) }; warn $@
if $@
; } qr/bad template/, 'Warn on bad extension';
123 subtest
"Absolute path change in _get_template_file" => sub {
126 # We create a simple template in /tmp.
127 # We simulate an anonymous OPAC session; the OPACBaseURL template variable
128 # should be filled by get_template_and_user.
129 t
::lib
::Mocks
::mock_config
( 'pluginsdir', [ C4
::Context
::temporary_directory
] );
130 t
::lib
::Mocks
::mock_preference
( 'OPACBaseURL', 'without any doubt' );
131 my ( $fh, $fn ) = tempfile
( SUFFIX
=> '.tt', UNLINK
=> 1, DIR
=> C4
::Context
::temporary_directory
);
132 print $fh q
|I am a
[% quality
%] template
[% OPACBaseURL
%]|;
134 my ( $template, $login, $cookie ) = C4
::Auth
::get_template_and_user
({
135 template_name
=> $fn,
138 authnotrequired
=> 1,
140 $template->param( quality
=> 'good-for-nothing' );
141 like
( $template->output, qr/a good.+template.+doubt/, 'Testing a template with an absolute path' );