Bug 23004: Unit test
[koha.git] / t / template_filters.t
1 # Copyright 2018 Koha Development Team
2 #
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19 use Test::More tests => 7;
20 use t::lib::QA::TemplateFilters;
21
22 subtest 'Asset must use raw' => sub {
23     plan tests => 2;
24     my $input = <<INPUT;
25 [% Asset.css("css/one.css") %]
26 [% Asset.css("js/two.js") %]
27 INPUT
28     my $expected = <<EXPECTED;
29 [% USE raw %]
30 [% Asset.css("css/one.css") | \$raw %]
31 [% Asset.css("js/two.js") | \$raw %]
32 EXPECTED
33
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);
37     is_deeply(
38         \@missing_filters,
39         [
40             {
41                 error       => "asset_must_be_raw",
42                 line        => '[% Asset.css("css/one.css") %]',
43                 line_number => 1,
44             },
45             {
46                 error       => "asset_must_be_raw",
47                 line        => '[% Asset.css("js/two.js") %]',
48                 line_number => 2,
49             }
50
51         ],
52     );
53 };
54
55 subtest 'Variables must be html escaped' => sub {
56     plan tests => 2;
57
58     my $input = <<INPUT;
59 <title>Koha &rsaquo; Patrons &rsaquo;
60     [% UNLESS blocking_error %]
61         [% just_a_var %]
62         [% just_a_var %] A N D [% another_one_on_same_line %]
63     [% END %]
64     [% IF ( patron.othernames ) %]&ldquo;[% patron.othernames %]&rdquo;[% END %]
65 </title>
66 [% patron_message.get_column('manager_surname') %]
67 INPUT
68
69     my $expected = <<EXPECTED;
70 <title>Koha &rsaquo; Patrons &rsaquo;
71     [% UNLESS blocking_error %]
72         [% just_a_var | html %]
73         [% just_a_var | html %] A N D [% another_one_on_same_line | html %]
74     [% END %]
75     [% IF ( patron.othernames ) %]&ldquo;[% patron.othernames | html %]&rdquo;[% END %]
76 </title>
77 [% patron_message.get_column('manager_surname') | html %]
78 EXPECTED
79
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);
83     is_deeply(
84         \@missing_filters,
85         [{
86                 error => "missing_filter",
87                 line => "        [% just_a_var %]",
88                 line_number => 3,
89             },
90             {
91                 error => "missing_filter",
92                 line => "        [% just_a_var %] A N D [% another_one_on_same_line %]",
93                 line_number => 4,
94             },
95             {
96                 error => "missing_filter",
97                 line => "        [% just_a_var %] A N D [% another_one_on_same_line %]",
98                 line_number => 4,
99             },
100             {
101                 error => "missing_filter",
102                 line => "    [% IF ( patron.othernames ) %]&ldquo;[% patron.othernames %]&rdquo;[% END %]",
103                 line_number => 6,
104             },
105             {
106                 error => "missing_filter",
107                 line  => "[% patron_message.get_column('manager_surname') %]",
108                 line_number => 8
109             }
110         ],
111
112     );
113 };
114
115 subtest 'TT directives, assignments and already filtered variables must not be escaped' => sub {
116     plan tests => 2;
117     my $input = <<INPUT;
118 #[% USE Asset %]
119 [% INCLUDE 'doc-head-open.inc' %]
120 [%# do_nothing %]
121 [% # do_nothing %]
122 [% SWITCH var %]
123 [% CASE 'foo' %]foo
124 [% CASE %]
125 [% END %]
126 [%- SWITCH var -%]
127 [%- CASE 'foo' -%]foo
128 [%- CASE -%]
129 [%- END -%]
130 [% foo UNLESS bar %]
131 [% SET var = val %]
132 [% var = val %]
133 [% var | \$Price %]
134 [% just_a_var_filtered|html %]
135 [% just_a_var_filtered |html %]
136 [% just_a_var_filtered| html %]
137 [% just_a_var_filtered | html %]
138 [%END%]
139 INPUT
140     my $expected = <<EXPECTED;
141 #[% USE Asset %]
142 [% INCLUDE 'doc-head-open.inc' %]
143 [%# do_nothing %]
144 [% # do_nothing %]
145 [% SWITCH var %]
146 [% CASE 'foo' %]foo
147 [% CASE %]
148 [% END %]
149 [%- SWITCH var -%]
150 [%- CASE 'foo' -%]foo
151 [%- CASE -%]
152 [%- END -%]
153 [% foo UNLESS bar %]
154 [% SET var = val %]
155 [% var = val %]
156 [% var | \$Price %]
157 [% just_a_var_filtered|html %]
158 [% just_a_var_filtered |html %]
159 [% just_a_var_filtered| html %]
160 [% just_a_var_filtered | html %]
161 [%END%]
162 EXPECTED
163
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);
167     is_deeply(
168         \@missing_filters,[],);
169 };
170
171 subtest 'Preserve pre/post chomps' => sub {
172     plan tests => 1;
173     my $input = <<INPUT;
174 [%- USE raw -%]
175 [%- var -%]
176 [% - var - %]
177 [%~ var ~%]
178 [% ~ var ~ %]
179 [%- var | html -%]
180 [%~ var | html ~%]
181 [%- var | uri -%]
182 [%~ var | uri ~%]
183 INPUT
184     my $expected = <<EXPECTED;
185 [%- USE raw -%]
186 [%- var | html -%]
187 [%- var | html -%]
188 [%~ var | html ~%]
189 [%~ var | html ~%]
190 [%- var | html -%]
191 [%~ var | html ~%]
192 [%- var | uri -%]
193 [%~ var | uri ~%]
194 EXPECTED
195
196     my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
197     is( $new_content . "\n", $expected, );
198 };
199
200 subtest 'Use uri filter if needed' => sub {
201     plan tests => 4;
202     my $input = <<INPUT;
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 %]&amp;basketno=[% basketno %]">[% another_var %]</a>
213 <a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid %]&amp;basketno=[% basketno | html %]" title="[% a_title %]>[% another_var %]</a>
214 INPUT
215
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 %]&amp;basketno=[% basketno | uri %]">[% another_var | html %]</a>
228 <a href="/cgi-bin/koha/acqui/newordersuggestion.pl?booksellerid=[% booksellerid | uri %]&amp;basketno=[% basketno | uri %]" title="[% a_title | html %]>[% another_var | html %]</a>
229 EXPECTED
230
231     my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
232     is( $new_content . "\n", $expected, );
233
234     $input = <<INPUT;
235 <a href="[% wrong_filter | html %]">[% var | html %]</a>
236 INPUT
237     my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
238     is_deeply(
239         \@missing_filters,
240         [
241             {
242                 error => "wrong_html_filter",
243                 line =>
244                   '<a href="[% wrong_filter | html %]">[% var | html %]</a>',
245                 line_number => 1
246             }
247
248         ],
249     );
250
251     $input = <<INPUT;
252 <a href="[% good_raw_filter | \$raw %]">[% var | html %]</a>
253 INPUT
254     @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
255     is_deeply( \@missing_filters, [], );
256
257     $input = <<INPUT;
258 <a href="[% good_filter | html_entity %]">[% var | html %]</a>
259 INPUT
260     @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
261     is_deeply( \@missing_filters, [], 'html_entity is a valid filter for href' );
262 };
263
264 subtest 'Do not escape KohaDates|Prices|HtmlTags output' => sub {
265     plan tests => 2;
266     my $input = <<INPUT;
267 [% var | \$KohaDates %]
268 [% var | \$KohaDates with_hours => 1 %]
269 [% var | \$KohaDates | html %]
270 [% var | \$KohaDates with_hours => 1 | html %]
271 [% var | \$Price %]
272 [% var | \$HtmlTags %]
273 INPUT
274
275     my $expected = <<EXPECTED;
276 [% var | \$KohaDates %]
277 [% var | \$KohaDates with_hours => 1 %]
278 [% var | \$KohaDates %]
279 [% var | \$KohaDates with_hours => 1 %]
280 [% var | \$Price %]
281 [% var | \$HtmlTags %]
282 EXPECTED
283
284     my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
285     is( $new_content . "\n", $expected, );
286
287
288     my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
289     is_deeply(
290         \@missing_filters,
291         [
292             {
293                 error       => "extra_filter_not_needed",
294                 line        => "[% var | \$KohaDates | html %]",
295                 line_number => 3,
296             },
297             {
298                 error       => "extra_filter_not_needed",
299                 line        => "[% var | \$KohaDates with_hours => 1 | html %]",
300                 line_number => 4,
301             }
302         ]
303     );
304 };
305
306 subtest 'Do not escape TT methods' => sub {
307     plan tests => 2;
308     my $input = <<INPUT;
309 [% my_array.push(a_var) %]
310 INPUT
311
312     my $expected = <<EXPECTED;
313 [% my_array.push(a_var) %]
314 EXPECTED
315
316     my $new_content = t::lib::QA::TemplateFilters::fix_filters($input);
317     is( $new_content . "\n", $expected, );
318
319
320     my @missing_filters = t::lib::QA::TemplateFilters::missing_filters($input);
321     is_deeply(\@missing_filters, []);
322 };