9d195373e096ef11c771094128e6e91255eb6b9a
[koha.git] / 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 Modern::Perl;
21
22 use threads;    # used for parallel
23 use Parallel::ForkManager;
24 use Sys::CPU;
25
26 =head1 NAME
27
28 valid-templates.t
29
30 =head1 DESCRIPTION
31
32 This test checks all staff and OPAC templates and includes for syntax errors 
33
34 =cut
35
36
37 use File::Find;
38 use File::Spec;
39 use Template;
40 use Test::More;
41
42 my @themes;
43
44 # OPAC themes
45 my $opac_dir  = 'koha-tmpl/opac-tmpl';
46 opendir ( my $dh, $opac_dir ) or die "can't opendir $opac_dir: $!";
47 for my $theme ( grep { not /^\.|lib|js|xslt/ } readdir($dh) ) {
48     push @themes, {
49         type     => "opac",
50         theme    => $theme,
51         modules  => "$opac_dir/$theme/en/modules",
52         includes => "$opac_dir/$theme/en/includes",
53     }
54 }
55 close $dh;
56
57 # STAFF themes
58 my $staff_dir = 'koha-tmpl/intranet-tmpl';
59 opendir ( $dh, $staff_dir ) or die "can't opendir $staff_dir: $!";
60 for my $theme ( grep { not /^\.|lib|js/ } readdir($dh) ) {
61     push @themes, {
62         type     => "staff",
63         theme    => $theme,
64         modules  => "$staff_dir/$theme/en/modules",
65         includes => "$staff_dir/$theme/en/includes",
66     }
67 }
68 close $dh;
69
70 my $ncpu;
71 if ( $ENV{KOHA_PROVE_CPUS} ) {
72     $ncpu = $ENV{KOHA_PROVE_CPUS} ; # set number of cpus to use
73 } else {
74     $ncpu = Sys::CPU::cpu_count();
75 }
76
77 my $pm   = new Parallel::ForkManager($ncpu);
78
79 # Tests
80 foreach my $theme ( @themes ) {
81     $pm->start and next;    # do the fork
82     print "Testing $theme->{'type'} $theme->{'theme'} templates\n";
83     if ( $theme->{'theme'} eq 'bootstrap' ) {
84         run_template_test(
85             $theme->{'modules'},
86             $theme->{'includes'},
87             # templates to exclude from testing because
88             # they cannot stand alone
89             'doc-head-close.inc',
90             'opac-bottom.inc',
91         );
92     }
93     else {
94         run_template_test(
95             $theme->{'modules'},
96             $theme->{'includes'},
97         );
98     }
99     $pm->finish;
100 }
101
102 $pm->wait_all_children;
103
104 done_testing();
105
106 sub run_template_test {
107     my $template_path = shift;
108     my $include_path  = shift;
109     my @exclusions = @_;
110     my $template_dir  = File::Spec->rel2abs($template_path);
111     my $include_dir   = File::Spec->rel2abs($include_path);
112     my $template_test = create_template_test($include_dir, @exclusions);
113     find( { wanted => $template_test, no_chdir => 1 },
114         $template_dir, $include_dir );
115 }
116
117 sub create_template_test {
118     my $includes = shift;
119     my @exclusions = @_;
120
121     my $interface = $includes =~ s|^.*/([^/]*-tmpl).*$|$1|r;
122     my $theme = ($interface =~ /opac/) ? 'bootstrap' : 'prog';
123
124     return sub {
125         my $tt = Template->new(
126             {
127                 ABSOLUTE     => 1,
128                 INCLUDE_PATH => $includes,
129                 PLUGIN_BASE  => 'Koha::Template::Plugin',
130             }
131         );
132         foreach my $exclusion (@exclusions) {
133             if ($_ =~ /${exclusion}$/) {
134                 diag("excluding template $_ because it cannot stand on its own");
135                 return;
136             }
137         }
138         my $vars = { interface => $interface, theme => $theme };
139         my $output;
140         if ( ! -d $_ ) {    # skip dirs
141             if ( !ok( $tt->process( $_, $vars, \$output ), $_ ) ) {
142                 diag( $tt->error );
143             }
144         }
145     }
146 }
147
148 =head1 AUTHOR
149
150 Koha Development Team <http://koha-community.org>
151
152 Chris Cormack <chrisc@catalyst.net.nz>
153
154 =cut