Bug 19057: Move C4::Reserve::GetReserve to Koha::Holds
[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 => 7;
23 use Test::Deep;
24 use Test::MockModule;
25 use Test::Warn;
26
27 use t::lib::Mocks;
28
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 /);
40 }
41
42 my $query   = CGI->new();
43 my $columns = C4::Templates::GetColumnDefs( $query );
44
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');
51
52 subtest 'Testing themelanguage' => sub {
53     plan tests => 12;
54     my $testing_language;
55     my $module_language = Test::MockModule->new('C4::Languages');
56
57     $module_language->mock(
58         'getlanguage',
59         sub {
60             return $testing_language;
61         }
62     );
63
64     my $cgi = CGI->new();
65     my $htdocs = C4::Context->config('intrahtdocs');
66     my $section = 'intranet';
67     t::lib::Mocks::mock_preference( 'template', 'prog' );
68
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' );
75
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' );
82
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' );
89
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");
96
97     return;
98 };
99
100 subtest 'Testing gettemplate' => sub {
101     plan tests => 2;
102
103     my $template;
104     warning_like { eval { $template = C4::Templates::gettemplate( '/etc/passwd', 'opac', CGI->new, 1 ) }; warn $@ if $@; } qr/bad template/, 'Bad template check';
105     is( $template ? $template->output: '', '', 'Check output' );
106 };
107