Bug 9302: Use patron-title.inc
[koha.git] / t / db_dependent / Templates.t
blob73a42a6494143db1845ebb9a9b924757d254f11f
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 => 8;
23 use Test::Deep;
24 use Test::MockModule;
25 use Test::Warn;
26 use File::Spec;
27 use File::Temp qw/tempfile/;
29 use t::lib::Mocks;
31 use C4::Auth qw//;
33 BEGIN {
34 use_ok( 'C4::Templates' );
35 can_ok( 'C4::Templates',
36 qw/ GetColumnDefs
37 getlanguagecookie
38 setlanguagecookie
39 themelanguage
40 gettemplate
41 _get_template_file
42 param
43 output /);
46 my $query = CGI->new();
47 my $columns = C4::Templates::GetColumnDefs( $query );
49 is( ref( $columns ) eq 'HASH', 1, 'GetColumnDefs returns a hashref' );
50 # get the tables names, sorted
51 my @keys = sort keys %{$columns};
52 is( scalar @keys, 6, 'GetColumnDefs correctly returns the 5 tables defined in columns.def' );
53 my @tables = qw( biblio biblioitems borrowers items statistics subscription );
54 cmp_deeply( \@keys, \@tables, 'GetColumnDefs returns the expected tables');
56 subtest 'Testing themelanguage' => sub {
57 plan tests => 12;
58 my $testing_language;
59 my $module_language = Test::MockModule->new('C4::Languages');
61 $module_language->mock(
62 'getlanguage',
63 sub {
64 return $testing_language;
68 my $cgi = CGI->new();
69 my $htdocs = C4::Context->config('intrahtdocs');
70 my $section = 'intranet';
71 t::lib::Mocks::mock_preference( 'template', 'prog' );
73 # trigger first case.
74 $testing_language = 'en';
75 my ($theme, $lang, $availablethemes) = C4::Templates::themelanguage( $htdocs, 'about.tt', $section, $cgi);
76 is($theme,'prog',"Expected theme: set en - $theme");
77 is($lang,'en','Expected language: set en');
78 cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for set en' );
80 # trigger second case.
81 $testing_language = q{};
82 ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, 'about.tt', $section, $cgi);
83 is($theme,'prog',"Expected theme: default en - $theme");
84 is($lang,'en','Expected language: default en');
85 cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for default en' );
87 # trigger third case.
88 my $template = $htdocs . '/prog/en/modules/about.tt';
89 ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, $template, $section, $cgi);
90 is($theme,'prog',"Expected defined theme: unset - $theme");
91 is($lang,q{},'Expected language: unset');
92 cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for unset' );
94 # trigger bad case.
95 $template = $htdocs . '/prog/en/kaboom/about.tt';
96 ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, $template, $section, $cgi);
97 is($lang,undef,'Expected language: not coded for');
98 is( $availablethemes, undef, 'We do not expect any available themes -- not coded for' );
99 is($theme,undef,"Expected no theme: not coded for");
101 return;
104 subtest 'Testing gettemplate/badtemplatecheck' => sub {
105 plan tests => 7;
107 my $cgi = CGI->new;
108 my $template;
109 warning_like { eval { $template = C4::Templates::gettemplate( '/etc/passwd', 'opac', $cgi ) }; warn $@ if $@; } qr/bad template/, 'Bad template check';
110 is( $template ? $template->output: '', '', 'Check output' );
112 # Test a few more bad paths to gettemplate triggering badtemplatecheck
113 warning_like { eval { C4::Templates::gettemplate( '../topsecret.tt', 'opac', $cgi ) }; warn $@ if $@; } qr/bad template/, 'No safe chars';
114 warning_like { eval { C4::Templates::gettemplate( '/noaccess/topsecret.tt', 'opac', $cgi ) }; warn $@ if $@; } qr/bad template/, 'Directory not allowed';
115 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';
117 # Allow templates from /tmp
118 t::lib::Mocks::mock_config( 'pluginsdir', [ '/tmp' ] );
119 warning_like { eval { C4::Templates::badtemplatecheck( '/tmp/about.tt' ) }; warn $@ if $@; } undef, 'No warn on template from plugin dir';
120 # Refuse wrong extension
121 warning_like { eval { C4::Templates::badtemplatecheck( '/tmp/about.tmpl' ) }; warn $@ if $@; } qr/bad template/, 'Warn on bad extension';
124 subtest "Absolute path change in _get_template_file" => sub {
125 plan tests => 1;
127 # We create a simple template in /tmp.
128 # We simulate an anonymous OPAC session; the OPACBaseURL template variable
129 # should be filled by get_template_and_user.
130 t::lib::Mocks::mock_config( 'pluginsdir', [ File::Spec->tmpdir ] );
131 t::lib::Mocks::mock_preference( 'OPACBaseURL', 'without any doubt' );
132 my ( $fh, $fn ) = tempfile( SUFFIX => '.tt', UNLINK => 1 );
133 print $fh q|I am a [% quality %] template [% OPACBaseURL %]|;
134 close $fh;
135 my ( $template, $login, $cookie ) = C4::Auth::get_template_and_user({
136 template_name => $fn,
137 query => CGI::new,
138 type => "opac",
139 authnotrequired => 1,
141 $template->param( quality => 'good-for-nothing' );
142 like( $template->output, qr/a good.+template.+doubt/, 'Testing a template with an absolute path' );