Bug 13618: Remove html filter for LAST
[koha_ffzg] / t / lib / QA / TemplateFilters.pm
1 package t::lib::QA::TemplateFilters;
2
3 use Modern::Perl;
4
5 our @tt_directives = (
6     qr{^\s*INCLUDE},
7     qr{^\s*USE},
8     qr{^\s*IF},
9     qr{^\s*UNLESS},
10     qr{^\s*ELSE},
11     qr{^\s*ELSIF},
12     qr{^\s*END},
13     qr{^\s*SET},
14     qr{^\s*FOR},
15     qr{^\s*FOREACH},
16     qr{^\s*MACRO},
17     qr{^\s*SWITCH},
18     qr{^\s*CASE},
19     qr{^\s*PROCESS},
20     qr{^\s*DEFAULT},
21     qr{^\s*TRY},
22     qr{^\s*CATCH},
23     qr{^\s*BLOCK},
24     qr{^\s*FILTER},
25     qr{^\s*STOP},
26     qr{^\s*NEXT},
27     qr{^\s*LAST},
28 );
29
30 sub missing_filters {
31     my ($content) = @_;
32     my ( $use_raw, $has_use_raw );
33     my @errors;
34     my $line_number;
35     for my $line ( split "\n", $content ) {
36         $line_number++;
37         if ( $line =~ m{\[%[^%]+%\]} ) {
38
39             # handle exceptions first
40             $use_raw = 1
41               if $line =~ m{|\s*\$raw};    # Is the file use the raw filter?
42
43             # Do we have Asset without the raw filter?
44             if ( $line =~ m{^\s*\[% Asset} ) {
45                 push @errors, { error => 'asset_must_be_raw', line => $line, line_number => $line_number }
46                   and next
47                   unless $line =~ m{\|\s*\$raw};
48             }
49
50             $has_use_raw++
51               if $line =~ m{\[% USE raw %\]};    # Does [% Use raw %] exist?
52
53             # Loop on TT blocks
54             while (
55                 $line =~ m{
56                     \[%
57                     (?<pre_chomp>(\s|\-|~)*)
58                     (?<tt_block>[^%\-~]+)
59                     (?<post_chomp>(\s|\-|~)*)
60                     %\]}gmxs
61               )
62             {
63                 my $tt_block = $+{tt_block};
64
65                 # It's a TT directive, no filters needed
66                 next if grep { $tt_block =~ $_ } @tt_directives;
67
68                 next
69                   if   $tt_block =~ m{\s?\|\s?\$KohaDates\s?$}
70                     or $tt_block =~ m{\s?\|\s?\$Price\s?$}
71                   ;    # We could escape it but should be safe
72                 next if $tt_block =~ m{^\#};    # Is a comment, skip it
73
74                 push @errors, { error => 'missing_filter', line => $line, line_number => $line_number }
75                   if $tt_block !~ m{\|\s?\$raw}   # already escaped correctly with raw
76                   && $tt_block !~ m{=}            # assignment, maybe we should require to use SET (?)
77                   && $tt_block !~ m{\|\s?ur(l|i)} # already has url or uri filter
78                   && $tt_block !~ m{\|\s?html}    # already has html filter
79                   && $tt_block !~ m{^(?<before>\S+)\s+UNLESS\s+(?<after>\S+)} # Specific for [% foo UNLESS bar %]
80                 ;
81             }
82         }
83     }
84
85     return @errors;
86 }
87
88 1;
89
90 =head1 NAME
91
92 t::lib::QA::TemplateFilters - Module used by tests and QA script to catch missing filters in template files
93
94 =head1 SYNOPSIS
95
96     my $content = read_file($filename);
97     my @e = t::lib::QA::TemplateFilters::missing_filters($content);
98
99 =head1 DESCRIPTION
100
101 The goal of this module is to make the subroutine reusable from the QA scripts
102 and to not duplicate the code.
103
104 =head1 METHODS
105
106 =head2 missing_filters
107
108     Take a template content file in parameter and return an array of errors.
109     An error is a hashref with 2 keys, error and line.
110     * error can be:
111     asset_must_be_raw - When Asset is called without using raw
112     missing_filter    - When a TT variable is displayed without filter
113
114     * line is the line where the error has been found.
115
116 =head1 AUTHORS
117
118 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
119
120 =head1 COPYRIGHT
121
122 Copyright 2017 - Koha Development Team
123
124 =head1 LICENSE
125
126 This file is part of Koha.
127
128 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
129 the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
130
131 Koha is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
132
133 You should have received a copy of the GNU General Public License along with Koha; if not, see <http://www.gnu.org/licenses>.
134
135 =cut
136
137 1;