2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 namespace core\output
;
20 * Unit tests for the Mustache source loader class.
22 * Unit tests for lib/classes/output/mustache_template_source_loader.php
25 * @copyright 2018 Ryan Wyllie <ryan@moodle.com>
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 class mustache_template_source_loader_test
extends \advanced_testcase
{
30 * Ensure that stripping comments from templates does not mutilate the template body.
32 public function test_strip_template_comments() {
34 $templatebody = <<<'TBD'
35 <h1>{{# str }} pluginname, mod_lemmings {{/ str }}</h1>
37 <div>{{{unescapedtest}}}</div>
41 {{> mod_lemmings/lemmingprofile }}
42 {{# pix }} t/edit, core, Edit Lemming {{/ pix }}
45 {{^lemmings}}Sorry, no lemmings today{{/lemmings}}
46 <div id="{{ uniqid }}-tab-container">
48 <ul role="tablist" class="nav nav-tabs">
57 <div class="tab-content">
60 {{> core/notification_info}}
67 require(['jquery','core/tabs'], function($, tabs) {
69 var container = $("#{{ uniqid }}-tab-container");
70 tabs.create(container);
74 $templatewithcomment = <<<TBC
76 This file is part of Moodle - http://moodle.org/
78 Moodle is free software: you can redistribute it and/or modify
79 it under the terms of the GNU General Public License as published by
80 the Free Software Foundation, either version 3 of the License, or
81 (at your option) any later version.
83 Moodle is distributed in the hope that it will be useful,
84 but WITHOUT ANY WARRANTY; without even the implied warranty of
85 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
86 GNU General Public License for more details.
88 You should have received a copy of the GNU General Public License
89 along with Moodle. If not, see <http://www.gnu.org/licenses/>.
92 @template mod_lemmings/lemmings
96 The purpose of this template is to render a lot of lemmings.
98 Classes required for JS:
101 Data attributes required for JS:
104 Context variables required for this template:
105 * attributes Array of name / value pairs.
107 Example context (json):
110 { "name": "Lemmy Winks", "age" : 1, "size" : "big" },
111 { "name": "Rocky", "age" : 2, "size" : "small" }
118 Here's some more comment text
119 Note, there is no need to test bracketed variables inside comments as gherkin does not support that!
120 See this issue: https://github.com/mustache/spec/issues/8
124 $loader = new mustache_template_source_loader();
125 $actual = \phpunit_util
::call_internal_method(
127 'strip_template_comments',
128 [$templatewithcomment],
129 \core\output\mustache_template_source_loader
::class
131 $this->assertEquals(trim($templatebody), trim($actual));
135 * Data provider for the test_load function.
137 public function test_load_test_cases() {
140 'test' => '{{! a comment }}The rest of the template'
143 $loader = $this->build_loader_from_static_cache($cache);
148 'component' => 'core',
150 'includecomments' => true,
151 'expected' => '{{! a comment }}The rest of the template'
153 'without comments' => [
155 'component' => 'core',
157 'includecomments' => false,
158 'expected' => 'The rest of the template'
164 * Test the load function.
166 * @dataProvider test_load_test_cases()
167 * @param mustache_template_source_loader $loader The loader
168 * @param string $component The moodle component
169 * @param string $name The template name
170 * @param bool $includecomments Whether to strip comments
171 * @param string $expected The expected output
173 public function test_load($loader, $component, $name, $includecomments, $expected) {
174 $this->assertEquals($expected, $loader->load($component, $name, 'boost', $includecomments));
178 * Data provider for the load_with_dependencies function.
180 public function test_load_with_dependencies_test_cases() {
181 // Create a bunch of templates that include one another in various ways. There is
182 // multiple instances of recursive inclusions to test that the code doensn't get
183 // stuck in an infinite loop.
184 $foo = '{{! a comment }}{{> core/bar }}{{< test/bop }}{{/ test/bop}}{{#str}} help, core {{/str}}';
185 $foo2 = '{{! a comment }}hello';
186 $bar = '{{! a comment }}{{> core/baz }}';
187 $baz = '{{! a comment }}{{#str}} hide, core {{/str}}';
188 $bop = '{{! a comment }}{{< test/bim }}{{/ test/bim }}{{> core/foo }}';
189 $bim = '{{! a comment }}{{< core/foo }}{{/ core/foo}}{{> test/foo }}';
190 $foonocomment = '{{> core/bar }}{{< test/bop }}{{/ test/bop}}{{#str}} help, core {{/str}}';
191 $foo2nocomment = 'hello';
192 $barnocomment = '{{> core/baz }}';
193 $baznocomment = '{{#str}} hide, core {{/str}}';
194 $bopnocomment = '{{< test/bim }}{{/ test/bim }}{{> core/foo }}';
195 $bimnocomment = '{{< core/foo }}{{/ core/foo}}{{> test/foo }}';
208 $loader = $this->build_loader_from_static_cache($cache);
211 'no template includes w comments' => [
213 'component' => 'test',
215 'includecomments' => true,
225 'no template includes w/o comments' => [
227 'component' => 'test',
229 'includecomments' => false,
233 'foo' => $foo2nocomment
239 'no template includes with string w comments' => [
241 'component' => 'core',
243 'includecomments' => true,
257 'no template includes with string w/o comments' => [
259 'component' => 'core',
261 'includecomments' => false,
265 'baz' => $baznocomment
275 'full with comments' => [
277 'component' => 'core',
279 'includecomments' => true,
301 'full without comments' => [
303 'component' => 'core',
305 'includecomments' => false,
309 'foo' => $foonocomment,
310 'bar' => $barnocomment,
311 'baz' => $baznocomment
314 'foo' => $foo2nocomment,
315 'bop' => $bopnocomment,
316 'bim' => $bimnocomment
331 * Test the load_with_dependencies function.
333 * @dataProvider test_load_with_dependencies_test_cases()
334 * @param mustache_template_source_loader $loader The loader
335 * @param string $component The moodle component
336 * @param string $name The template name
337 * @param bool $includecomments Whether to strip comments
338 * @param string $expected The expected output
340 public function test_load_with_dependencies($loader, $component, $name, $includecomments, $expected) {
341 $actual = $loader->load_with_dependencies($component, $name, 'boost', $includecomments);
342 $this->assertEquals($expected, $actual);
345 * Data provider for the test_load function.
347 public function test_scan_template_source_for_dependencies_test_cases() {
348 $foo = '{{! a comment }}{{> core/bar }}{{< test/bop }}{{/ test/bop}}{{#str}} help, core {{/str}}';
349 $bar = '{{! a comment }}{{> core/baz }}';
350 $baz = '{{! a comment }}{{#str}} hide, core {{/str}}';
351 $bop = '{{! a comment }}hello';
352 $multiline1 = <<<TEMPLATE
353 {{! a comment }}{{#str}} authorreplyingprivatelytoauthor,
356 $multiline2 = <<<TEMPLATE
357 {{! a comment }}{{#str}}
358 authorreplyingprivatelytoauthor,
361 $multiline3 = <<<TEMPLATE
362 {{! a comment }}{{#str}}
363 authorreplyingprivatelytoauthor,
367 $multiline4 = <<<TEMPLATE
368 {{! a comment }}{{#str}}
369 authorreplyingprivatelytoauthor, mod_forum
372 $multiline5 = <<<TEMPLATE
373 {{! a comment }}{{#str}}
384 'multiline1' => $multiline1,
385 'multiline2' => $multiline2,
386 'multiline3' => $multiline3,
387 'multiline4' => $multiline4,
388 'multiline5' => $multiline5,
391 $loader = $this->build_loader_from_static_cache($cache);
394 'single template include' => [
404 'single string include' => [
435 'string: component on new line' => [
437 'source' => $multiline1,
441 'mod_forum' => ['authorreplyingprivatelytoauthor']
445 'string: identifier on own line' => [
447 'source' => $multiline2,
451 'mod_forum' => ['authorreplyingprivatelytoauthor']
455 'string: all parts on new lines' => [
457 'source' => $multiline3,
461 'mod_forum' => ['authorreplyingprivatelytoauthor']
465 'string: id and component on own line' => [
467 'source' => $multiline4,
471 'mod_forum' => ['authorreplyingprivatelytoauthor']
475 'string: no component' => [
477 'source' => $multiline5,
489 * Test the scan_template_source_for_dependencies function.
491 * @dataProvider test_scan_template_source_for_dependencies_test_cases()
492 * @param mustache_template_source_loader $loader The loader
493 * @param string $source The template to test
494 * @param string $expected The expected output
496 public function test_scan_template_source_for_dependencies($loader, $source, $expected) {
497 $actual = \phpunit_util
::call_internal_method(
499 'scan_template_source_for_dependencies',
501 \core\output\mustache_template_source_loader
::class
503 $this->assertEquals($expected, $actual);
507 * Create an instance of mustache_template_source_loader which loads its templates
508 * from the given cache rather than disk.
510 * @param array $cache A cache of templates
511 * @return mustache_template_source_loader
513 private function build_loader_from_static_cache(array $cache) : mustache_template_source_loader
{
514 return new mustache_template_source_loader(function($component, $name, $themename) use ($cache) {
515 return $cache[$component][$name];