Bug 17260 - DBRev 16.12.00.011
[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 => 6;
23 use Test::Deep;
24 use Test::MockModule;
25
26 use t::lib::Mocks;
27
28 BEGIN {
29     use_ok( 'C4::Templates' );
30     can_ok( 'C4::Templates',
31          qw/ GetColumnDefs
32              getlanguagecookie
33              setlanguagecookie
34              themelanguage
35              gettemplate
36              _get_template_file
37              param
38              output /);
39 }
40
41 my $query   = CGI->new();
42 my $columns = C4::Templates::GetColumnDefs( $query );
43
44 is( ref( $columns ) eq 'HASH', 1, 'GetColumnDefs returns a hashref' );
45 # get the tables names, sorted
46 my @keys = sort keys %{$columns};
47 is( scalar @keys, 6, 'GetColumnDefs correctly returns the 5 tables defined in columns.def' );
48 my @tables = qw( biblio biblioitems borrowers items statistics subscription );
49 cmp_deeply( \@keys, \@tables, 'GetColumnDefs returns the expected tables');
50
51 subtest 'Testing themelanguage' => sub {
52     plan tests => 12;
53     my $testing_language;
54     my $module_language = Test::MockModule->new('C4::Languages');
55
56     $module_language->mock(
57         'getlanguage',
58         sub {
59             return $testing_language;
60         }
61     );
62
63     my $cgi = CGI->new();
64     my $htdocs = C4::Context->config('intrahtdocs');
65     my $section = 'intranet';
66     t::lib::Mocks::mock_preference( 'template', 'prog' );
67
68     # trigger first case.
69     $testing_language = 'en';
70     my ($theme, $lang, $availablethemes) = C4::Templates::themelanguage( $htdocs, 'about.tt', $section, $cgi);
71     is($theme,'prog',"Expected theme: set en - $theme");
72     is($lang,'en','Expected language: set en');
73     cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for set en' );
74
75     # trigger second case.
76     $testing_language = q{};
77     ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, 'about.tt', $section, $cgi);
78     is($theme,'prog',"Expected theme: default en - $theme");
79     is($lang,'en','Expected language: default en');
80     cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for default en' );
81
82     # trigger third case.
83     my $template = $htdocs . '/prog/en/modules/about.tt';
84     ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, $template, $section, $cgi);
85     is($theme,'prog',"Expected defined theme: unset - $theme");
86     is($lang,q{},'Expected language: unset');
87     cmp_deeply( $availablethemes, [ 'prog' ], 'We only expect one available theme for unset' );
88
89     # trigger bad case.
90     $template = $htdocs . '/prog/en/kaboom/about.tt';
91     ($theme, $lang, $availablethemes) = C4::Templates::themelanguage($htdocs, $template, $section, $cgi);
92     is($lang,undef,'Expected language: not coded for');
93     is( $availablethemes, undef, 'We do not expect any available themes -- not coded for' );
94     is($theme,undef,"Expected no theme: not coded for");
95
96     return;
97 };
98
99 1;