Lines 16-43
Link Here
|
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
17 |
|
18 |
use Modern::Perl; |
18 |
use Modern::Perl; |
19 |
use Test::More tests => 1; |
19 |
use Test::More tests => 5; |
20 |
use t::lib::QA::TemplateFilters; |
20 |
use t::lib::QA::TemplateFilters; |
21 |
|
21 |
|
22 |
my $input = <<INPUT; |
22 |
subtest 'Asset must use raw' => sub { |
23 |
[% USE Asset %] |
23 |
plan tests => 2; |
24 |
[% INCLUDE 'doc-head-open.inc' %] |
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::search_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; |
25 |
<title>Koha › Patrons › |
59 |
<title>Koha › Patrons › |
26 |
[% UNLESS blocking_error %] |
60 |
[% UNLESS blocking_error %] |
27 |
Patron details for [% INCLUDE 'patron-title.inc' no_html = 1 %] |
61 |
[% just_a_var %] |
28 |
[% just_a_var %] A N D [% another_one_on_same_line %] |
62 |
[% just_a_var %] A N D [% another_one_on_same_line %] |
29 |
[% just_a_var_filtered|html %] |
|
|
30 |
[% just_a_var_filtered |html %] |
31 |
[% just_a_var_filtered| html %] |
32 |
[% just_a_var_filtered | html %] |
33 |
[% END %] |
63 |
[% END %] |
34 |
[% IF ( patron.othernames | html ) %]“[% patron.othernames %]”[% END %] |
64 |
[% IF ( patron.othernames ) %]“[% patron.othernames %]”[% END %] |
35 |
[% Asset.css("css/datatables.css").raw %] |
|
|
36 |
[% Asset.css("css/datatables.css") | \$raw %] |
37 |
</title> |
65 |
</title> |
38 |
<a href="tel:[% patron.phone %]">[% patron.phone %]</a> |
|
|
39 |
<a title="[% patron.emailpro %]" href="mailto:[% patron.emailpro | uri %]">[% patron.emailpro %]</a> |
40 |
[% patron_message.get_column('manager_surname') %] |
66 |
[% patron_message.get_column('manager_surname') %] |
|
|
67 |
INPUT |
68 |
|
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 %] |
74 |
[% END %] |
75 |
[% IF ( patron.othernames ) %]“[% patron.othernames | html %]”[% 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::search_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 ) %]“[% patron.othernames %]”[% 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' %] |
41 |
[%# do_nothing %] |
143 |
[%# do_nothing %] |
42 |
[% # do_nothing %] |
144 |
[% # do_nothing %] |
43 |
[% SWITCH var %] |
145 |
[% SWITCH var %] |
Lines 48-137
my $input = <<INPUT;
Link Here
|
48 |
[%- CASE 'foo' -%]foo |
150 |
[%- CASE 'foo' -%]foo |
49 |
[%- CASE -%] |
151 |
[%- CASE -%] |
50 |
[%- END -%] |
152 |
[%- END -%] |
51 |
[%- var -%] |
|
|
52 |
[% - var - %] |
53 |
[%~ var ~%] |
54 |
[% ~ var ~ %] |
55 |
[% var | \$raw %] |
56 |
[% foo UNLESS bar %] |
153 |
[% foo UNLESS bar %] |
57 |
[% SET var = val %] |
154 |
[% SET var = val %] |
58 |
[% var = val %] |
155 |
[% var = val %] |
59 |
[% var | \$Price %] |
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 %] |
60 |
[%END%] |
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::search_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 => 3; |
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> |
61 |
INPUT |
211 |
INPUT |
62 |
|
212 |
|
63 |
my @expected_errors = ( |
213 |
# Note: [% myurl %] will be uri escaped, we cannot know url should be used |
64 |
{ |
214 |
my $expected = <<EXPECTED; |
65 |
error => q{missing_filter}, |
215 |
<a href="tel:[% patron.phone | uri %]">[% patron.phone | html %]</a> |
66 |
line => |
216 |
<a href="mailto:[% patron.emailpro | uri %]" title="[% patron.emailpro | html %]">[% patron.emailpro | html %]</a> |
67 |
q{ [% just_a_var %] A N D [% another_one_on_same_line %]}, |
217 |
<a href="mailto:[% patron.emailpro | uri %]" title="[% patron.emailpro | html %]">[% patron.emailpro | html %]</a> |
68 |
line_number => 6, |
218 |
<a href="mailto:[% patron.emailpro | uri %]" title="[% patron.emailpro | html %]">[% patron.emailpro | html %]</a> |
69 |
}, |
219 |
<a href="[% myuri | uri %]" title="[% myuri | html %]">[% myuri | html %]</a> |
70 |
{ |
220 |
<a href="[% myuri | uri %]" title="[% myuri | html %]">[% myuri | html %]</a> |
71 |
error => q{missing_filter}, |
221 |
<a href="[% myurl | uri %]" title="[% myurl | html %]">[% myurl | html %]</a> |
72 |
line => |
222 |
<a href="[% myurl | url %]" title="[% myurl | html %]">[% myurl | html %]</a> |
73 |
q{ [% just_a_var %] A N D [% another_one_on_same_line %]}, |
223 |
EXPECTED |
74 |
line_number => 6, |
224 |
|
75 |
}, |
225 |
my $new_content = t::lib::QA::TemplateFilters::fix_filters($input); |
76 |
{ |
226 |
is( $new_content . "\n", $expected, ); |
77 |
error => q{missing_filter}, |
227 |
|
78 |
line => |
228 |
$input = <<INPUT; |
79 |
q{ [% IF ( patron.othernames | html ) %]“[% patron.othernames %]”[% END %]}, |
229 |
<a href="[% wrong_filter | html %]">[% var | html %]</a> |
80 |
line_number => 12, |
230 |
INPUT |
81 |
}, |
231 |
my $missing_filters = t::lib::QA::TemplateFilters::search_missing_filters($input); |
82 |
{ |
232 |
is_deeply( |
83 |
error => q{asset_must_be_raw}, |
233 |
$missing_filters, |
84 |
line => q{ [% Asset.css("css/datatables.css").raw %]}, |
234 |
[ |
85 |
line_number => 13, |
235 |
{ |
86 |
}, |
236 |
error => "wrong_html_filter", |
87 |
{ |
237 |
line => |
88 |
error => q{missing_filter}, |
238 |
'<a href="[% wrong_filter | html %]">[% var | html %]</a>', |
89 |
line => q{<a href="tel:[% patron.phone %]">[% patron.phone %]</a>}, |
239 |
line_number => 1 |
90 |
line_number => 16, |
240 |
} |
91 |
}, |
241 |
|
92 |
{ |
242 |
], |
93 |
error => q{missing_filter}, |
243 |
); |
94 |
line => q{<a href="tel:[% patron.phone %]">[% patron.phone %]</a>}, |
244 |
|
95 |
line_number => 16, |
245 |
$input = <<INPUT; |
96 |
}, |
246 |
<a href="[% good_raw_filter | \$raw %]">[% var | html %]</a> |
97 |
{ |
247 |
INPUT |
98 |
error => q{missing_filter}, |
248 |
$missing_filters = t::lib::QA::TemplateFilters::search_missing_filters($input); |
99 |
line => |
249 |
is_deeply( $missing_filters, [], ); |
100 |
q{<a title="[% patron.emailpro %]" href="mailto:[% patron.emailpro | uri %]">[% patron.emailpro %]</a>}, |
250 |
}; |
101 |
line_number => 17, |
|
|
102 |
}, |
103 |
{ |
104 |
error => q{missing_filter}, |
105 |
line => |
106 |
q{<a title="[% patron.emailpro %]" href="mailto:[% patron.emailpro | uri %]">[% patron.emailpro %]</a>}, |
107 |
line_number => 17, |
108 |
}, |
109 |
{ |
110 |
error => q{missing_filter}, |
111 |
line => q{[% patron_message.get_column('manager_surname') %]}, |
112 |
line_number => 18, |
113 |
}, |
114 |
{ |
115 |
error => q{missing_filter}, |
116 |
line => q{[%- var -%]}, |
117 |
line_number => 29, |
118 |
}, |
119 |
{ |
120 |
error => q{missing_filter}, |
121 |
line => q{[% - var - %]}, |
122 |
line_number => 30, |
123 |
}, |
124 |
{ |
125 |
error => q{missing_filter}, |
126 |
line => q{[%~ var ~%]}, |
127 |
line_number => 31, |
128 |
}, |
129 |
{ |
130 |
error => q{missing_filter}, |
131 |
line => q{[% ~ var ~ %]}, |
132 |
line_number => 32, |
133 |
} |
134 |
); |
135 |
|
136 |
my @get = t::lib::QA::TemplateFilters::missing_filters($input); |
137 |
is_deeply( \@get, \@expected_errors); |