Bug 15774: (follow-up) Address QA issues
[koha.git] / t / template_filters.t
index 3c9c5a9..b479faa 100644 (file)
@@ -16,7 +16,7 @@
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
-use Test::More tests => 5;
+use Test::More tests => 7;
 use t::lib::QA::TemplateFilters;
 
 subtest 'Asset must use raw' => sub {
@@ -33,9 +33,9 @@ EXPECTED
 
     my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
     is( $new_content . "\n", $expected, );
-    my $missing_filters = t::lib::QA::TemplateFilters::search_missing_filters($input);
+    my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
     is_deeply(
-        $missing_filters,
+        \@missing_filters,
         [
             {
                 error       => "asset_must_be_raw",
@@ -79,9 +79,9 @@ EXPECTED
 
     my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
     is( $new_content . "\n", $expected, );
-    my $missing_filters = t::lib::QA::TemplateFilters::search_missing_filters($input);
+    my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
     is_deeply(
-        $missing_filters,
+        \@missing_filters,
         [{
                 error => "missing_filter",
                 line => "        [% just_a_var %]",
@@ -163,15 +163,15 @@ EXPECTED
 
     my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
     is( $new_content . "\n", $expected, );
-    my $missing_filters = t::lib::QA::TemplateFilters::search_missing_filters($input);
+    my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
     is_deeply(
-        $missing_filters,[],);
+        \@missing_filters,[],);
 };
 
 subtest 'Preserve pre/post chomps' => sub {
     plan tests => 1;
     my $input = <<INPUT;
-[% USE raw %]
+[%- USE raw -%]
 [%- var -%]
 [% - var - %]
 [%~ var ~%]
@@ -182,7 +182,7 @@ subtest 'Preserve pre/post chomps' => sub {
 [%~ var | uri ~%]
 INPUT
     my $expected = <<EXPECTED;
-[% USE raw %]
+[%- USE raw -%]
 [%- var | html -%]
 [%- var | html -%]
 [%~ var | html ~%]
@@ -198,7 +198,7 @@ EXPECTED
 };
 
 subtest 'Use uri filter if needed' => sub {
-    plan tests => 3;
+    plan tests => 4;
     my $input = <<INPUT;
 <a href="tel:[% patron.phone %]">[% patron.phone %]</a>
 <a href="mailto:[% patron.emailpro %]" title="[% patron.emailpro %]">[% patron.emailpro %]</a>
@@ -208,6 +208,9 @@ subtest 'Use uri filter if needed' => sub {
 <a href="[% myuri | uri %]" title="[% myuri %]">[% myuri %]</a>
 <a href="[% myurl | html %]" title="[% myurl %]">[% myurl %]</a>
 <a href="[% myurl | url %]" title="[% myurl %]">[% myurl %]</a>
+<a href="[% myurl | html_entity %]" title="[% myurl %]">[% myurl %]</a>
+<a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid %]&amp;basketno=[% basketno %]">[% another_var %]</a>
+<a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid %]&amp;basketno=[% basketno | html %]" title="[% a_title %]>[% another_var %]</a>
 INPUT
 
     # Note: [% myurl %] will be uri escaped, we cannot know url should be used
@@ -220,6 +223,9 @@ INPUT
 <a href="[% myuri | uri %]" title="[% myuri | html %]">[% myuri | html %]</a>
 <a href="[% myurl | uri %]" title="[% myurl | html %]">[% myurl | html %]</a>
 <a href="[% myurl | url %]" title="[% myurl | html %]">[% myurl | html %]</a>
+<a href="[% myurl | html_entity %]" title="[% myurl | html %]">[% myurl | html %]</a>
+<a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid | uri %]&amp;basketno=[% basketno | uri %]">[% another_var | html %]</a>
+<a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid | uri %]&amp;basketno=[% basketno | uri %]" title="[% a_title | html %]>[% another_var | html %]</a>
 EXPECTED
 
     my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
@@ -228,9 +234,9 @@ EXPECTED
     $input = <<INPUT;
 <a href="[% wrong_filter | html %]">[% var | html %]</a>
 INPUT
-    my $missing_filters = t::lib::QA::TemplateFilters::search_missing_filters($input);
+    my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
     is_deeply(
-        $missing_filters,
+        \@missing_filters,
         [
             {
                 error => "wrong_html_filter",
@@ -245,6 +251,72 @@ INPUT
     $input = <<INPUT;
 <a href="[% good_raw_filter | \$raw %]">[% var | html %]</a>
 INPUT
-    $missing_filters = t::lib::QA::TemplateFilters::search_missing_filters($input);
-    is_deeply( $missing_filters, [], );
+    @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
+    is_deeply( \@missing_filters, [], );
+
+    $input = <<INPUT;
+<a href="[% good_filter | html_entity %]">[% var | html %]</a>
+INPUT
+    @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
+    is_deeply( \@missing_filters, [], 'html_entity is a valid filter for href' );
+};
+
+subtest 'Do not escape KohaDates|Prices|HtmlTags output' => sub {
+    plan tests => 2;
+    my $input = <<INPUT;
+[% var | \$KohaDates %]
+[% var | \$KohaDates with_hours => 1 %]
+[% var | \$KohaDates | html %]
+[% var | \$KohaDates with_hours => 1 | html %]
+[% var | \$Price %]
+[% var | \$HtmlTags %]
+INPUT
+
+    my $expected = <<EXPECTED;
+[% var | \$KohaDates %]
+[% var | \$KohaDates with_hours => 1 %]
+[% var | \$KohaDates %]
+[% var | \$KohaDates with_hours => 1 %]
+[% var | \$Price %]
+[% var | \$HtmlTags %]
+EXPECTED
+
+    my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
+    is( $new_content . "\n", $expected, );
+
+
+    my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
+    is_deeply(
+        \@missing_filters,
+        [
+            {
+                error       => "extra_filter_not_needed",
+                line        => "[% var | \$KohaDates | html %]",
+                line_number => 3,
+            },
+            {
+                error       => "extra_filter_not_needed",
+                line        => "[% var | \$KohaDates with_hours => 1 | html %]",
+                line_number => 4,
+            }
+        ]
+    );
+};
+
+subtest 'Do not escape TT methods' => sub {
+    plan tests => 2;
+    my $input = <<INPUT;
+[% my_array.push(a_var) %]
+INPUT
+
+    my $expected = <<EXPECTED;
+[% my_array.push(a_var) %]
+EXPECTED
+
+    my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
+    is( $new_content . "\n", $expected, );
+
+
+    my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
+    is_deeply(\@missing_filters, []);
 };