Bug 16174: (QA followup) Fix remaining tests
[koha.git] / xt / author / valid-templates.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2011 Catalyst IT
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22
23 =head1 NAME
24
25 valid-templates.t
26
27 =head1 DESCRIPTION
28
29 This test checks all staff and OPAC templates and includes for syntax errors 
30
31 =cut
32
33
34 use File::Find;
35 use File::Spec;
36 use Template;
37 use Test::More;
38
39 my @themes;
40
41 # OPAC themes
42 my $opac_dir  = 'koha-tmpl/opac-tmpl';
43 opendir ( my $dh, $opac_dir ) or die "can't opendir $opac_dir: $!";
44 for my $theme ( grep { not /^\.|lib|js|xslt/ } readdir($dh) ) {
45     push @themes, {
46         type     => "opac",
47         theme    => $theme,
48         modules  => "$opac_dir/$theme/en/modules",
49         includes => "$opac_dir/$theme/en/includes",
50     }
51 }
52 close $dh;
53
54 # STAFF themes
55 my $staff_dir = 'koha-tmpl/intranet-tmpl';
56 opendir ( $dh, $staff_dir ) or die "can't opendir $staff_dir: $!";
57 for my $theme ( grep { not /^\.|lib|js/ } readdir($dh) ) {
58     push @themes, {
59         type     => "staff",
60         theme    => $theme,
61         modules  => "$staff_dir/$theme/en/modules",
62         includes => "$staff_dir/$theme/en/includes",
63     }
64 }
65 close $dh;
66
67 # Tests
68 foreach my $theme ( @themes ) {
69     print "Testing $theme->{'type'} $theme->{'theme'} templates\n";
70     if ( $theme->{'theme'} eq 'bootstrap' ) {
71         run_template_test(
72             $theme->{'modules'},
73             $theme->{'includes'},
74             # templates to exclude from testing because
75             # they cannot stand alone
76             'doc-head-close.inc',
77             'opac-bottom.inc',
78         );
79     }
80     else {
81         run_template_test(
82             $theme->{'modules'},
83             $theme->{'includes'},
84         );
85     }
86 }
87
88 done_testing();
89
90 sub run_template_test {
91     my $template_path = shift;
92     my $include_path  = shift;
93     my @exclusions = @_;
94     my $template_dir  = File::Spec->rel2abs($template_path);
95     my $include_dir   = File::Spec->rel2abs($include_path);
96     my $template_test = create_template_test($include_dir, @exclusions);
97     find( { wanted => $template_test, no_chdir => 1 },
98         $template_dir, $include_dir );
99 }
100
101 sub create_template_test {
102     my $includes = shift;
103     my @exclusions = @_;
104     return sub {
105         my $tt = Template->new(
106             {
107                 ABSOLUTE     => 1,
108                 INCLUDE_PATH => $includes,
109                 PLUGIN_BASE  => 'Koha::Template::Plugin',
110             }
111         );
112         foreach my $exclusion (@exclusions) {
113             if ($_ =~ /${exclusion}$/) {
114                 diag("excluding template $_ because it cannot stand on its own");
115                 return;
116             }
117         }
118         my $vars;
119         my $output;
120         if ( ! -d $_ ) {    # skip dirs
121             if ( !ok( $tt->process( $_, $vars, \$output ), $_ ) ) {
122                 diag( $tt->error );
123             }
124         }
125     }
126 }
127
128 =head1 AUTHOR
129
130 Koha Development Team <http://koha-community.org>
131
132 Chris Cormack <chrisc@catalyst.net.nz>
133
134 =cut