MDL-69050 lang: Stop using the term blacklist in mustache output engine
[moodle.git] / lib / tests / output_mustache_helper_collection_test.php
CommitLineData
f9664b0c
RW
1<?php
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Unit tests for lib/classes/output/mustache_helper_collection
19 *
20 * @copyright 2019 Ryan Wyllie <ryan@moodle.com>
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
26use core\output\mustache_helper_collection;
27
28/**
29 * Unit tests for the mustache_helper_collection class.
30 */
31class core_output_mustache_helper_collection_testcase extends advanced_testcase {
32 /**
972340b6 33 * Test cases to confirm that disallowed helpers are stripped from the source
f9664b0c
RW
34 * text by the helper before being passed to other another helper. This prevents
35 * nested calls to helpers.
36 */
972340b6 37 public function get_strip_disallowed_helpers_testcases() {
f9664b0c 38 return [
972340b6
DM
39 'no disallowed' => [
40 'disallowed' => [],
f9664b0c
RW
41 'input' => 'core, move, {{#js}} some nasty JS {{/js}}',
42 'expected' => 'core, move, {{#js}} some nasty JS {{/js}}'
43 ],
972340b6
DM
44 'disallowed no match' => [
45 'disallowed' => ['foo'],
f9664b0c
RW
46 'input' => 'core, move, {{#js}} some nasty JS {{/js}}',
47 'expected' => 'core, move, {{#js}} some nasty JS {{/js}}'
48 ],
972340b6
DM
49 'disallowed partial match 1' => [
50 'disallowed' => ['js'],
f9664b0c
RW
51 'input' => 'core, move, {{#json}} some nasty JS {{/json}}',
52 'expected' => 'core, move, {{#json}} some nasty JS {{/json}}'
53 ],
972340b6
DM
54 'disallowed partial match 2' => [
55 'disallowed' => ['js'],
f9664b0c
RW
56 'input' => 'core, move, {{#onjs}} some nasty JS {{/onjs}}',
57 'expected' => 'core, move, {{#onjs}} some nasty JS {{/onjs}}'
58 ],
972340b6
DM
59 'single disallowed 1' => [
60 'disallowed' => ['js'],
f9664b0c
RW
61 'input' => 'core, move, {{#js}} some nasty JS {{/js}}',
62 'expected' => 'core, move, {{}}'
63 ],
972340b6
DM
64 'single disallowed 2' => [
65 'disallowed' => ['js'],
f9664b0c
RW
66 'input' => 'core, move, {{ # js }} some nasty JS {{ / js }}',
67 'expected' => 'core, move, {{}}'
68 ],
972340b6
DM
69 'single disallowed 3' => [
70 'disallowed' => ['js'],
f9664b0c
RW
71 'input' => 'core, {{#js}} some nasty JS {{/js}}, test',
72 'expected' => 'core, {{}}, test'
73 ],
972340b6
DM
74 'single disallowed 3' => [
75 'disallowed' => ['js'],
f9664b0c
RW
76 'input' => 'core, {{#ok}} this is ok {{/ok}}, {{#js}} some nasty JS {{/js}}',
77 'expected' => 'core, {{#ok}} this is ok {{/ok}}, {{}}'
78 ],
972340b6
DM
79 'single disallowed multiple matches 1' => [
80 'disallowed' => ['js'],
f9664b0c
RW
81 'input' => 'core, {{#js}} some nasty JS {{/js}}, {{#js}} some nasty JS {{/js}}',
82 'expected' => 'core, {{}}'
83 ],
972340b6
DM
84 'single disallowed multiple matches 2' => [
85 'disallowed' => ['js'],
f9664b0c
RW
86 'input' => 'core, {{ # js }} some nasty JS {{ / js }}, {{ # js }} some nasty JS {{ / js }}',
87 'expected' => 'core, {{}}'
88 ],
972340b6
DM
89 'single disallowed multiple matches nested 1' => [
90 'disallowed' => ['js'],
f9664b0c
RW
91 'input' => 'core, move, {{#js}} some nasty JS {{#js}} some nasty JS {{/js}} {{/js}}',
92 'expected' => 'core, move, {{}}'
93 ],
972340b6
DM
94 'single disallowed multiple matches nested 2' => [
95 'disallowed' => ['js'],
f9664b0c
RW
96 'input' => 'core, move, {{ # js }} some nasty JS {{ # js }} some nasty JS {{ / js }}{{ / js }}',
97 'expected' => 'core, move, {{}}'
98 ],
972340b6
DM
99 'multiple disallowed 1' => [
100 'disallowed' => ['js', 'foo'],
f9664b0c
RW
101 'input' => 'core, move, {{#js}} some nasty JS {{/js}}',
102 'expected' => 'core, move, {{}}'
103 ],
972340b6
DM
104 'multiple disallowed 2' => [
105 'disallowed' => ['js', 'foo'],
f9664b0c
RW
106 'input' => 'core, {{#foo}} blah {{/foo}}, {{#js}} js {{/js}}',
107 'expected' => 'core, {{}}, {{}}'
108 ],
972340b6
DM
109 'multiple disallowed 3' => [
110 'disallowed' => ['js', 'foo'],
f9664b0c
RW
111 'input' => '{{#foo}} blah {{/foo}}, {{#foo}} blah {{/foo}}, {{#js}} js {{/js}}',
112 'expected' => '{{}}, {{}}'
113 ],
972340b6
DM
114 'multiple disallowed 4' => [
115 'disallowed' => ['js', 'foo'],
f9664b0c
RW
116 'input' => '{{#foo}} blah {{/foo}}, {{#js}} js {{/js}}, {{#foo}} blah {{/foo}}',
117 'expected' => '{{}}'
118 ],
972340b6
DM
119 'multiple disallowed 4' => [
120 'disallowed' => ['js', 'foo'],
f9664b0c
RW
121 'input' => 'core, move, {{#js}} JS {{#foo}} blah {{/foo}} {{/js}}',
122 'expected' => 'core, move, {{}}'
123 ],
124 ];
125 }
126
127 /**
128 * Test that the mustache_helper_collection class correctly strips
972340b6
DM
129 * @dataProvider get_strip_disallowed_helpers_testcases()
130 * @param string[] $disallowed The list of helpers to strip
f9664b0c 131 * @param string $input The input string for the helper
972340b6 132 * @param string $expected The expected output of the string after disallowed strip
f9664b0c 133 */
972340b6
DM
134 public function test_strip_disallowed_helpers($disallowed, $input, $expected) {
135 $collection = new mustache_helper_collection(null, $disallowed);
136 $this->assertEquals($expected, $collection->strip_disallowed_helpers($disallowed, $input));
f9664b0c
RW
137 }
138
139 /**
972340b6 140 * Test that the disallowed helpers are disabled during the execution of other
f9664b0c
RW
141 * helpers.
142 *
972340b6 143 * Any allowed helper should still be available to call during the
f9664b0c
RW
144 * execution of a helper.
145 */
972340b6 146 public function test_disallowed_helpers_disabled_during_execution() {
f9664b0c
RW
147 $engine = new \Mustache_Engine();
148 $context = new \Mustache_Context();
149 $lambdahelper = new \Mustache_LambdaHelper($engine, $context);
972340b6
DM
150 $disallowed = ['bad'];
151 $collection = new mustache_helper_collection(null, $disallowed);
f9664b0c
RW
152 $badcalled = false;
153 $goodcalled = false;
154
155 $badhelper = function() use (&$badcalled) {
156 $badcalled = true;
157 return '';
158 };
159 $goodhelper = function() use (&$goodcalled) {
160 $goodcalled = true;
161 return '';
162 };
163 // A test helper that just returns the text without modifying it.
164 $testhelper = function($text, $lambda) use ($collection) {
165 $collection->get('good')($text, $lambda);
166 $collection->get('bad')($text, $lambda);
167 return $text;
168 };
169 $collection->add('bad', $badhelper);
170 $collection->add('good', $goodhelper);
171 $collection->add('test', $testhelper);
172
173 $this->assertEquals('success output', $collection->get('test')('success output', $lambdahelper));
174 $this->assertTrue($goodcalled);
175 $this->assertFalse($badcalled);
176 }
972340b6
DM
177
178 /**
179 * Test that calling deprecated method strip_blacklisted_helpers() still works and shows developer debugging.
180 */
181 public function test_deprecated_strip_blacklisted_helpers() {
182
183 $collection = new mustache_helper_collection(null, ['js']);
184 $stripped = $collection->strip_blacklisted_helpers(['js'], '{{#js}} JS {{/js}}');
185 $this->assertEquals('{{}}', $stripped);
186 $this->assertDebuggingCalled('mustache_helper_collection::strip_blacklisted_helpers() is deprecated. ' .
187 'Please use mustache_helper_collection::strip_disallowed_helpers() instead.', DEBUG_DEVELOPER);
188 }
f9664b0c 189}