|
Line 0
Link Here
|
| 0 |
- |
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 |
use Test::NoWarnings; |
| 20 |
use Test::More; |
| 21 |
use C4::TTParser; |
| 22 |
use Array::Utils qw( array_minus ); |
| 23 |
|
| 24 |
use Koha::Devel::Files; |
| 25 |
my $dev_files = Koha::Devel::Files->new( { context => 'tidy' } ); |
| 26 |
my @tt_files = $dev_files->ls_tt_files; |
| 27 |
my @exclusions = qw( |
| 28 |
t/db_dependent/misc/translator/tt/en/sample-not-working.tt |
| 29 |
t/db_dependent/misc/translator/tt/en/sample-not-working-2.tt |
| 30 |
t/db_dependent/misc/translator/tt/en/sample.tt |
| 31 |
); |
| 32 |
@tt_files = array_minus @tt_files, @exclusions; |
| 33 |
|
| 34 |
plan tests => scalar(@tt_files) + 1; |
| 35 |
|
| 36 |
my @translatable_attributes = qw(alt content title value label placeholder aria-label); |
| 37 |
|
| 38 |
my $checkers = [ |
| 39 |
{ |
| 40 |
description => 'TT syntax: incorrectly uses of TT tags in HTML tags', |
| 41 |
check => sub { |
| 42 |
my ( $self, $name, $token ) = @_; |
| 43 |
my $attr = $token->{_attr}; |
| 44 |
next unless $attr; |
| 45 |
my @errors; |
| 46 |
if ( $attr->{'[%'} or $attr->{'[%-'} ) { |
| 47 |
for my $attribute (@translatable_attributes) { |
| 48 |
|
| 49 |
# The following tests come from tmpl_process3.pl sub text_replace_tag |
| 50 |
next if $attribute eq 'label' && $token->{_string} !~ /^<optgroup/; |
| 51 |
next if $attribute eq 'content' && $token->{_string} !~ /^meta/; |
| 52 |
next |
| 53 |
if $attribute eq 'value' |
| 54 |
&& ( $token->{_string} !~ /^input/ ); # || additional check on checkbox|hidden|radio needed? |
| 55 |
if ( exists $attr->{$attribute} ) { |
| 56 |
if ( $attr->{$attribute}->[2] =~ m{$attribute=[^[]} ) { |
| 57 |
push @errors, $token->{_lc}; |
| 58 |
} |
| 59 |
} elsif ( exists $attr->{"%]$attribute"} ) { |
| 60 |
push @errors, $token->{_lc}; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
return @errors; |
| 65 |
}, |
| 66 |
}, |
| 67 |
{ |
| 68 |
description => '<body> tag with id and class attributes', |
| 69 |
check => sub { |
| 70 |
my ( $self, $name, $token ) = @_; |
| 71 |
return if $name =~ /bodytag\.inc/; |
| 72 |
my $tag = $token->{_string}; |
| 73 |
return ( $tag =~ /^<body/ && ( $tag !~ /id=".+"/ || $tag !~ /class=".+"/ ) ) |
| 74 |
? ( $token->{_lc} ) |
| 75 |
: (); |
| 76 |
}, |
| 77 |
}, |
| 78 |
]; |
| 79 |
|
| 80 |
for my $filepath (@tt_files) { |
| 81 |
my $parser = C4::TTParser->new; |
| 82 |
$parser->build_tokens($filepath); |
| 83 |
my @errors; |
| 84 |
while ( my $token = $parser->next_token ) { |
| 85 |
my $attr = $token->{_attr}; |
| 86 |
next unless $attr; |
| 87 |
|
| 88 |
for my $checker (@$checkers) { |
| 89 |
my @e = $checker->{check}->( $checker, $filepath, $token ); |
| 90 |
push @errors, @e if @e; |
| 91 |
} |
| 92 |
} |
| 93 |
is( scalar(@errors), 0 ) or diag( "$filepath: " . join( ', ', @errors ) ); |
| 94 |
} |
| 95 |
|
| 96 |
=head1 NAME |
| 97 |
|
| 98 |
xt/author/tt_valid.t |
| 99 |
|
| 100 |
=head1 DESCRIPTION |
| 101 |
|
| 102 |
This test validates .tt files. |
| 103 |
|
| 104 |
For the time being, two validations are done: |
| 105 |
|
| 106 |
[1] Test if TT files contain TT directive within HTML tag. For example: |
| 107 |
|
| 108 |
<li[% IF |
| 109 |
|
| 110 |
This kind of construction MUST be avoided because it breaks Koha translation |
| 111 |
process. |
| 112 |
|
| 113 |
[2] Test tag <body> tags have both attibutes 'id' and 'class' |
| 114 |
|
| 115 |
=cut |