Bug 27002: Update Koha::Biblio->pickup_locations to return a resultset
[koha.git] / xt / tt_valid.t
blob7285019e0abce60ae522134571ff980ee8ac1ca4
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Copyright (C) 2011 Tamil s.a.r.l.
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.
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.
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>.
20 use Modern::Perl;
21 use Test::More tests => 3;
22 use File::Find;
23 use Cwd;
24 use C4::TTParser;
26 my @themes;
28 # OPAC themes
29 my $opac_dir = 'koha-tmpl/opac-tmpl';
30 opendir ( my $dh, $opac_dir ) or die "can't opendir $opac_dir: $!";
31 for my $theme ( grep { not /^\.|lib|js|xslt/ } readdir($dh) ) {
32 push @themes, "$opac_dir/$theme/en";
34 close $dh;
36 # STAFF themes
37 my $staff_dir = 'koha-tmpl/intranet-tmpl';
38 opendir ( $dh, $staff_dir ) or die "can't opendir $staff_dir: $!";
39 for my $theme ( grep { not /^\.|lib|js/ } readdir($dh) ) {
40 push @themes, "$staff_dir/$theme/en";
42 close $dh;
44 my $checkers = [
46 description => 'TT syntax: not using TT directive within HTML tag',
47 check => sub {
48 my ($self, $name, $token) = @_;
49 my $attr = $token->{_attr};
50 next unless $attr;
51 push @{$self->{errors}->{$name}}, $token->{_lc} if $attr->{'[%'} or $attr->{'[%-'};
53 errors => {},
56 description => '<body> tag with id and class attributes',
57 check => sub {
58 my ($self, $name, $token) = @_;
59 return if $name =~ /bodytag\.inc/;
60 my $tag = $token->{_string};
61 push @{$self->{errors}->{$name}}, $token->{_lc}
62 if $tag =~ /^<body/ &&
63 ($tag !~ /id=".+"/ || $tag !~ /class=".+"/);
65 errors => {},
68 find( sub {
69 my $dir = getcwd();
70 return if $dir =~ /blib/;
71 return unless /\.(tt|inc)$/;
72 ($dir) = $dir =~ /koha-tmpl\/(.*)$/;
73 my $name = $_;
74 my $parser = C4::TTParser->new;
75 $parser->build_tokens( $name );
76 while ( my $token = $parser->next_token ) {
77 my $attr = $token->{_attr};
78 next unless $attr;
79 for my $checker (@$checkers) {
80 $checker->{check}->($checker, "$dir/$name", $token);
83 }, @themes
86 for my $check (@$checkers) {
87 my @files = sort keys %{$check->{errors}};
88 ok( !@files, $check->{description} )
89 or diag(
90 "Files list: \n",
91 join( "\n", map { "$_: " . join(', ', @{$check->{errors}->{$_}})
92 } @files )
96 my $testtoken = 0;
97 my $ttparser = C4::TTParser->new();
98 $ttparser->unshift_token($testtoken);
99 my $testtokenagain = C4::TTParser::next_token();
100 is( $testtoken, $testtokenagain, "Token received same as original put on stack");
103 =head1 NAME
105 tt_valid.t
107 =head1 DESCRIPTION
109 This test validate Template Toolkit (TT) Koha files.
111 For the time being, two validations are done:
113 [1] Test if TT files contain TT directive within HTML tag. For example:
115 <li[% IF
117 This kind of construction MUST be avoided because it breaks Koha translation
118 process.
120 [2] Test tag <body> tags have both attibutes 'id' and 'class'
122 =head1 USAGE
124 From Koha root directory:
126 prove -v xt/tt_valid.t
128 =cut