1 package t::lib::QA::TemplateFilters;
31 my ( $use_raw, $has_use_raw );
34 for my $line ( split "\n", $content ) {
36 if ( $line =~ m{\[%[^%]+%\]} ) {
38 # handle exceptions first
40 if $line =~ m{|\s*\$raw}; # Is the file use the raw filter?
42 # Do we have Asset without the raw filter?
43 if ( $line =~ m{^\s*\[% Asset} ) {
44 push @errors, { error => 'asset_must_be_raw', line => $line, line_number => $line_number }
46 unless $line =~ m{\|\s*\$raw};
50 if $line =~ m{\[% USE raw %\]}; # Does [% Use raw %] exist?
56 (?<pre_chomp>(\s|\-|~)*)
58 (?<post_chomp>(\s|\-|~)*)
62 my $tt_block = $+{tt_block};
64 # It's a TT directive, no filters needed
65 next if grep { $tt_block =~ $_ } @tt_directives;
68 if $tt_block =~ m{\s?\|\s?\$KohaDates\s?$}
69 or $tt_block =~ m{\s?\|\s?\$Price\s?$}
70 ; # We could escape it but should be safe
71 next if $tt_block =~ m{^\#}; # Is a comment, skip it
73 push @errors, { error => 'missing_filter', line => $line, line_number => $line_number }
74 if $tt_block !~ m{\|\s?\$raw} # already escaped correctly with raw
75 && $tt_block !~ m{=} # assignment, maybe we should require to use SET (?)
76 && $tt_block !~ m{\|\s?ur(l|i)} # already has url or uri filter
77 && $tt_block !~ m{\|\s?html} # already has html filter
78 && $tt_block !~ m{^(?<before>\S+)\s+UNLESS\s+(?<after>\S+)} # Specific for [% foo UNLESS bar %]
91 t::lib::QA::TemplateFilters - Module used by tests and QA script to catch missing filters in template files
95 my $content = read_file($filename);
96 my @e = t::lib::QA::TemplateFilters::missing_filters($content);
100 The goal of this module is to make the subroutine reusable from the QA scripts
101 and to not duplicate the code.
105 =head2 missing_filters
107 Take a template content file in parameter and return an array of errors.
108 An error is a hashref with 2 keys, error and line.
110 asset_must_be_raw - When Asset is called without using raw
111 missing_filter - When a TT variable is displayed without filter
113 * line is the line where the error has been found.
117 Jonathan Druart <jonathan.druart@bugs.koha-community.org>
121 Copyright 2017 - Koha Development Team
125 This file is part of Koha.
127 Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
128 the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
130 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 You should have received a copy of the GNU General Public License along with Koha; if not, see <http://www.gnu.org/licenses>.