View | Details | Raw Unified | Return to bug 38720
Collapse All | Expand All

(-)a/xt/tt_valid.t (-130 lines)
Lines 1-129 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Copyright (C) 2011 Tamil s.a.r.l.
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
use Test::More tests => 3;
22
use File::Find;
23
use Cwd;
24
use C4::TTParser;
25
26
my @themes;
27
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";
33
}
34
close $dh;
35
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";
41
}
42
close $dh;
43
44
my $checkers = [
45
    {
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->{'[%-'};
52
        },
53
        errors => {},
54
    },
55
    {
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=".+"/ );
64
        },
65
        errors => {},
66
    },
67
];
68
find(
69
    sub {
70
        my $dir = getcwd();
71
        return if $dir =~ /blib/;
72
        return unless /\.(tt|inc)$/;
73
        ($dir) = $dir =~ /koha-tmpl\/(.*)$/;
74
        my $name   = $_;
75
        my $parser = C4::TTParser->new;
76
        $parser->build_tokens($name);
77
        while ( my $token = $parser->next_token ) {
78
            my $attr = $token->{_attr};
79
            next unless $attr;
80
            for my $checker (@$checkers) {
81
                $checker->{check}->( $checker, "$dir/$name", $token );
82
            }
83
        }
84
    },
85
    @themes
86
);
87
88
for my $check (@$checkers) {
89
    my @files = sort keys %{ $check->{errors} };
90
    ok( !@files, $check->{description} )
91
        or diag(
92
        "Files list: \n",
93
        join( "\n", map { "$_: " . join( ', ', @{ $check->{errors}->{$_} } ) } @files )
94
        );
95
}
96
97
my $testtoken = 0;
98
my $ttparser  = C4::TTParser->new();
99
$ttparser->unshift_token($testtoken);
100
my $testtokenagain = C4::TTParser::next_token();
101
is( $testtoken, $testtokenagain, "Token received same as original put on stack" );
102
103
=head1 NAME
104
105
tt_valid.t
106
107
=head1 DESCRIPTION
108
109
This test validate Template Toolkit (TT) Koha files.
110
111
For the time being, two validations are done:
112
113
[1] Test if TT files contain TT directive within HTML tag. For example:
114
115
  <li[% IF
116
117
This kind of construction MUST be avoided because it breaks Koha translation
118
process.
119
120
[2] Test tag <body> tags have both attibutes 'id' and 'class'
121
122
=head1 USAGE
123
124
From Koha root directory:
125
126
prove -v xt/tt_valid.t
127
128
=cut
129
130
- 

Return to bug 38720