1 # Copyright 2018 Koha Development Team
3 # This file is part of Koha.
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.
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.
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>.
19 use Test::More tests => 7;
20 use t::lib::QA::TemplateFilters;
22 subtest 'Asset must use raw' => sub {
25 [% Asset.css("css/one.css") %]
26 [% Asset.css("js/two.js") %]
28 my $expected = <<EXPECTED;
30 [% Asset.css("css/one.css") | \$raw %]
31 [% Asset.css("js/two.js") | \$raw %]
34 my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
35 is( $new_content . "\n", $expected, );
36 my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
41 error => "asset_must_be_raw",
42 line => '[% Asset.css("css/one.css") %]',
46 error => "asset_must_be_raw",
47 line => '[% Asset.css("js/two.js") %]',
55 subtest 'Variables must be html escaped' => sub {
59 <title>Koha › Patrons ›
60 [% UNLESS blocking_error %]
62 [% just_a_var %] A N D [% another_one_on_same_line %]
64 [% IF ( patron.othernames ) %]“[% patron.othernames %]”[% END %]
66 [% patron_message.get_column('manager_surname') %]
69 my $expected = <<EXPECTED;
70 <title>Koha › Patrons ›
71 [% UNLESS blocking_error %]
72 [% just_a_var | html %]
73 [% just_a_var | html %] A N D [% another_one_on_same_line | html %]
75 [% IF ( patron.othernames ) %]“[% patron.othernames | html %]”[% END %]
77 [% patron_message.get_column('manager_surname') | html %]
80 my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
81 is( $new_content . "\n", $expected, );
82 my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
86 error => "missing_filter",
87 line => " [% just_a_var %]",
91 error => "missing_filter",
92 line => " [% just_a_var %] A N D [% another_one_on_same_line %]",
96 error => "missing_filter",
97 line => " [% just_a_var %] A N D [% another_one_on_same_line %]",
101 error => "missing_filter",
102 line => " [% IF ( patron.othernames ) %]“[% patron.othernames %]”[% END %]",
106 error => "missing_filter",
107 line => "[% patron_message.get_column('manager_surname') %]",
115 subtest 'TT directives, assignments and already filtered variables must not be escaped' => sub {
119 [% INCLUDE 'doc-head-open.inc' %]
127 [%- CASE 'foo' -%]foo
134 [% just_a_var_filtered|html %]
135 [% just_a_var_filtered |html %]
136 [% just_a_var_filtered| html %]
137 [% just_a_var_filtered | html %]
140 my $expected = <<EXPECTED;
142 [% INCLUDE 'doc-head-open.inc' %]
150 [%- CASE 'foo' -%]foo
157 [% just_a_var_filtered|html %]
158 [% just_a_var_filtered |html %]
159 [% just_a_var_filtered| html %]
160 [% just_a_var_filtered | html %]
164 my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
165 is( $new_content . "\n", $expected, );
166 my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
168 \@missing_filters,[],);
171 subtest 'Preserve pre/post chomps' => sub {
184 my $expected = <<EXPECTED;
196 my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
197 is( $new_content . "\n", $expected, );
200 subtest 'Use uri filter if needed' => sub {
203 <a href="tel:[% patron.phone %]">[% patron.phone %]</a>
204 <a href="mailto:[% patron.emailpro %]" title="[% patron.emailpro %]">[% patron.emailpro %]</a>
205 <a href="mailto:[% patron.emailpro | html %]" title="[% patron.emailpro %]">[% patron.emailpro %]</a>
206 <a href="mailto:[% patron.emailpro | uri %]" title="[% patron.emailpro %]">[% patron.emailpro %]</a>
207 <a href="[% myuri %]" title="[% myuri %]">[% myuri %]</a>
208 <a href="[% myuri | uri %]" title="[% myuri %]">[% myuri %]</a>
209 <a href="[% myurl | html %]" title="[% myurl %]">[% myurl %]</a>
210 <a href="[% myurl | url %]" title="[% myurl %]">[% myurl %]</a>
211 <a href="[% myurl | html_entity %]" title="[% myurl %]">[% myurl %]</a>
212 <a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid %]&basketno=[% basketno %]">[% another_var %]</a>
213 <a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid %]&basketno=[% basketno | html %]" title="[% a_title %]>[% another_var %]</a>
216 # Note: [% myurl %] will be uri escaped, we cannot know url should be used
217 my $expected = <<EXPECTED;
218 <a href="tel:[% patron.phone | uri %]">[% patron.phone | html %]</a>
219 <a href="mailto:[% patron.emailpro | uri %]" title="[% patron.emailpro | html %]">[% patron.emailpro | html %]</a>
220 <a href="mailto:[% patron.emailpro | uri %]" title="[% patron.emailpro | html %]">[% patron.emailpro | html %]</a>
221 <a href="mailto:[% patron.emailpro | uri %]" title="[% patron.emailpro | html %]">[% patron.emailpro | html %]</a>
222 <a href="[% myuri | uri %]" title="[% myuri | html %]">[% myuri | html %]</a>
223 <a href="[% myuri | uri %]" title="[% myuri | html %]">[% myuri | html %]</a>
224 <a href="[% myurl | uri %]" title="[% myurl | html %]">[% myurl | html %]</a>
225 <a href="[% myurl | url %]" title="[% myurl | html %]">[% myurl | html %]</a>
226 <a href="[% myurl | html_entity %]" title="[% myurl | html %]">[% myurl | html %]</a>
227 <a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid | uri %]&basketno=[% basketno | uri %]">[% another_var | html %]</a>
228 <a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid | uri %]&basketno=[% basketno | uri %]" title="[% a_title | html %]>[% another_var | html %]</a>
231 my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
232 is( $new_content . "\n", $expected, );
235 <a href="[% wrong_filter | html %]">[% var | html %]</a>
237 my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
242 error => "wrong_html_filter",
244 '<a href="[% wrong_filter | html %]">[% var | html %]</a>',
252 <a href="[% good_raw_filter | \$raw %]">[% var | html %]</a>
254 @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
255 is_deeply( \@missing_filters, [], );
258 <a href="[% good_filter | html_entity %]">[% var | html %]</a>
260 @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
261 is_deeply( \@missing_filters, [], 'html_entity is a valid filter for href' );
264 subtest 'Do not escape KohaDates|Prices|HtmlTags output' => sub {
267 [% var | \$KohaDates %]
268 [% var | \$KohaDates with_hours => 1 %]
269 [% var | \$KohaDates | html %]
270 [% var | \$KohaDates with_hours => 1 | html %]
272 [% var | \$HtmlTags %]
275 my $expected = <<EXPECTED;
276 [% var | \$KohaDates %]
277 [% var | \$KohaDates with_hours => 1 %]
278 [% var | \$KohaDates %]
279 [% var | \$KohaDates with_hours => 1 %]
281 [% var | \$HtmlTags %]
284 my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
285 is( $new_content . "\n", $expected, );
288 my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
293 error => "extra_filter_not_needed",
294 line => "[% var | \$KohaDates | html %]",
298 error => "extra_filter_not_needed",
299 line => "[% var | \$KohaDates with_hours => 1 | html %]",
306 subtest 'Do not escape TT methods' => sub {
309 [% my_array.push(a_var) %]
312 my $expected = <<EXPECTED;
313 [% my_array.push(a_var) %]
316 my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
317 is( $new_content . "\n", $expected, );
320 my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
321 is_deeply(\@missing_filters, []);