Bug 21393: Move missing filters code to a module
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Fri, 21 Sep 2018 13:26:03 +0000 (10:26 -0300)
committerNick Clemens <nick@bywatersolutions.com>
Mon, 1 Oct 2018 13:56:32 +0000 (13:56 +0000)
To make it reusable easily from QA test tools
https://gitlab.com/koha-community/qa-test-tools/issues/3

Signed-off-by: Josef Moravec <josef.moravec@gmail.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>

t/lib/QA/TemplateFilters.pm [new file with mode: 0644]
xt/find-missing-filters.t

diff --git a/t/lib/QA/TemplateFilters.pm b/t/lib/QA/TemplateFilters.pm
new file mode 100644 (file)
index 0000000..0185565
--- /dev/null
@@ -0,0 +1,133 @@
+package t::lib::QA::TemplateFilters;
+
+use Modern::Perl;
+
+our @tt_directives = (
+    qr{^\s*INCLUDE},
+    qr{^\s*USE},
+    qr{^\s*IF},
+    qr{^\s*UNLESS},
+    qr{^\s*ELSE},
+    qr{^\s*ELSIF},
+    qr{^\s*END},
+    qr{^\s*SET},
+    qr{^\s*FOR},
+    qr{^\s*FOREACH},
+    qr{^\s*MACRO},
+    qr{^\s*SWITCH},
+    qr{^\s*CASE},
+    qr{^\s*PROCESS},
+    qr{^\s*DEFAULT},
+    qr{^\s*TRY},
+    qr{^\s*CATCH},
+    qr{^\s*BLOCK},
+    qr{^\s*FILTER},
+    qr{^\s*STOP},
+    qr{^\s*NEXT},
+);
+
+sub missing_filters {
+    my ($content) = @_;
+    my ( $use_raw, $has_use_raw );
+    my @errors;
+    for my $line ( split "\n", $content ) {
+        if ( $line =~ m{\[%[^%]+%\]} ) {
+
+            # handle exceptions first
+            $use_raw = 1
+              if $line =~ m{|\s*\$raw};    # Is the file use the raw filter?
+
+            # Do we have Asset without the raw filter?
+            if ( $line =~ m{^\s*\[% Asset} ) {
+                push @errors, { error => 'asset_must_be_raw', line => $line }
+                  and next
+                  unless $line =~ m{\|\s*\$raw};
+            }
+
+            $has_use_raw++
+              if $line =~ m{\[% USE raw %\]};    # Does [% Use raw %] exist?
+
+            # Loop on TT blocks
+            while (
+                $line =~ m{
+                    \[%
+                    (?<pre_chomp>(\s|\-|~)*)
+                    (?<tt_block>[^%\-~]+)
+                    (?<post_chomp>(\s|\-|~)*)
+                    %\]}gmxs
+              )
+            {
+                my $tt_block = $+{tt_block};
+
+                # It's a TT directive, no filters needed
+                next if grep { $tt_block =~ $_ } @tt_directives;
+
+                next
+                  if $tt_block =~ m{\s?\|\s?\$KohaDates\s?$}
+                  ;    # We could escape it but should be safe
+                next if $tt_block =~ m{^\#};    # Is a comment, skip it
+
+                push @errors, { error => 'missing_filter', line => $line }
+                  if $tt_block !~ m{\|\s?\$raw}   # already escaped correctly with raw
+                  && $tt_block !~ m{=}            # assignment, maybe we should require to use SET (?)
+                  && $tt_block !~ m{\|\s?ur(l|i)} # already has url or uri filter
+                  && $tt_block !~ m{\|\s?html}    # already has html filter
+                  && $tt_block !~ m{^(?<before>\S+)\s+UNLESS\s+(?<after>\S+)} # Specific for [% foo UNLESS bar %]
+                ;
+            }
+        }
+    }
+
+    return @errors;
+}
+
+1;
+
+=head1 NAME
+
+t::lib::QA::TemplateFilters - Module used by tests and QA script to catch missing filters in template files
+
+=head1 SYNOPSIS
+
+    my $content = read_file($filename);
+    my @e = t::lib::QA::TemplateFilters::missing_filters($content);
+
+=head1 DESCRIPTION
+
+The goal of this module is to make the subroutine reusable from the QA scripts
+and to not duplicate the code.
+
+=head1 METHODS
+
+=head2 missing_filters
+
+    Take a template content file in parameter and return an array of errors.
+    An error is a hashref with 2 keys, error and line.
+    * error can be:
+    asset_must_be_raw - When Asset is called without using raw
+    missing_filter    - When a TT variable is displayed without filter
+
+    * line is the line where the error has been found.
+
+=head1 AUTHORS
+
+Jonathan Druart <jonathan.druart@bugs.koha-community.org>
+
+=head1 COPYRIGHT
+
+Copyright 2017 - Koha Development Team
+
+=head1 LICENSE
+
+This file is part of Koha.
+
+Koha is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
+
+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.
+
+You should have received a copy of the GNU General Public License along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+=cut
+
+1;
index b7891e2..6b7e80f 100755 (executable)
@@ -20,6 +20,7 @@ use Test::More tests => 1;
 use File::Find;
 use File::Slurp;
 use Data::Dumper;
+use t::lib::QA::TemplateFilters;
 
 my @themes;
 
@@ -46,92 +47,12 @@ sub wanted {
         if $name =~ m[\.(tt|inc)$] and -f $name;
 }
 
-my @tt_directives = (
-    qr{^\s*INCLUDE},
-    qr{^\s*USE},
-    qr{^\s*IF},
-    qr{^\s*UNLESS},
-    qr{^\s*ELSE},
-    qr{^\s*ELSIF},
-    qr{^\s*END},
-    qr{^\s*SET},
-    qr{^\s*FOR},
-    qr{^\s*FOREACH},
-    qr{^\s*MACRO},
-    qr{^\s*SWITCH},
-    qr{^\s*CASE},
-    qr{^\s*PROCESS},
-    qr{^\s*DEFAULT},
-    qr{^\s*TRY},
-    qr{^\s*CATCH},
-    qr{^\s*BLOCK},
-    qr{^\s*FILTER},
-    qr{^\s*STOP},
-    qr{^\s*NEXT},
-);
-
-sub process_tt_content {
-    my ($content) = @_;
-    my ( $use_raw, $has_use_raw );
-    my @errors;
-    for my $line ( split "\n", $content ) {
-        if ( $line =~ m{\[%[^%]+%\]} ) {
-
-            # handle exceptions first
-            $use_raw = 1
-              if $line =~ m{|\s*\$raw};    # Is the file use the raw filter?
-
-            # Do we have Asset without the raw filter?
-            if ( $line =~ m{^\s*\[% Asset} ) {
-                push @errors, { error => 'asset_must_be_raw', line => $line }
-                  and next
-                  unless $line =~ m{\|\s*\$raw};
-            }
-
-            $has_use_raw++
-              if $line =~ m{\[% USE raw %\]};    # Does [% Use raw %] exist?
-
-            # Loop on TT blocks
-            while (
-                $line =~ m{
-                    \[%
-                    (?<pre_chomp>(\s|\-|~)*)
-                    (?<tt_block>[^%\-~]+)
-                    (?<post_chomp>(\s|\-|~)*)
-                    %\]}gmxs
-              )
-            {
-                my $tt_block = $+{tt_block};
-
-                # It's a TT directive, no filters needed
-                next if grep { $tt_block =~ $_ } @tt_directives;
-
-                next
-                  if $tt_block =~ m{\s?\|\s?\$KohaDates\s?$}
-                  ;    # We could escape it but should be safe
-                next if $tt_block =~ m{^\#};    # Is a comment, skip it
-
-                push @errors, { error => 'missing_filter', line => $line }
-                  if $tt_block !~ m{\|\s?\$raw}   # already escaped correctly with raw
-                  && $tt_block !~ m{=}            # assignment, maybe we should require to use SET (?)
-                  && $tt_block !~ m{\|\s?ur(l|i)} # already has url or uri filter
-                  && $tt_block !~ m{\|\s?html}    # already has html filter
-                  && $tt_block !~ m{^(?<before>\S+)\s+UNLESS\s+(?<after>\S+)} # Specific for [% foo UNLESS bar %]
-                ;
-            }
-        }
-    }
-
-    return @errors;
-}
-
 find({ wanted => \&wanted, no_chdir => 1 }, @themes );
 
 my @errors;
 for my $file ( @files ) {
-    say $file;
     my $content = read_file($file);
-    my @e = process_tt_content($content);
+    my @e = t::lib::QA::TemplateFilters::missing_filters($content);
     push @errors, { file => $file, errors => \@e } if @e;
 }