Merge branch 'MDL-64012' of https://github.com/timhunt/moodle
[moodle.git] / lib / tests / output_external_test.php
blob35acb97cff1f0a17fa1654f592145e3c4c526434
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.
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 /**
18 * Unit tests for lib/classes/output/external.php
19 * @author Guy Thomas <gthomas@moodlerooms.com>
20 * @copyright Copyright (c) 2017 Blackboard Inc.
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 defined('MOODLE_INTERNAL') || die();
26 use core\output\external;
28 require_once(__DIR__.'/../../lib/externallib.php');
29 require_once(__DIR__.'/../../lib/mustache/src/Mustache/Tokenizer.php');
30 require_once(__DIR__.'/../../lib/mustache/src/Mustache/Parser.php');
32 /**
33 * Class core_output_external_testcase - test \core\output\external class.
34 * @package core
35 * @author Guy Thomas <gthomas@moodlerooms.com>
36 * @copyright Copyright (c) 2017 Blackboard Inc.
37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39 class core_output_external_testcase extends base_testcase {
41 /**
42 * Ensure that stripping comments from templates does not mutilate the template body.
44 public function test_strip_template_comments() {
46 $templatebody = <<<'TBD'
47 <h1>{{# str }} pluginname, mod_lemmings {{/ str }}</h1>
48 <div>{{test}}</div>
49 <div>{{{unescapedtest}}}</div>
50 {{#lemmings}}
51 <div>
52 <h2>{{name}}</h2>
53 {{> mod_lemmings/lemmingprofile }}
54 {{# pix }} t/edit, core, Edit Lemming {{/ pix }}
55 </div>
56 {{/lemmings}}
57 {{^lemmings}}Sorry, no lemmings today{{/lemmings}}
58 <div id="{{ uniqid }}-tab-container">
59 {{# tabheader }}
60 <ul role="tablist" class="nav nav-tabs">
61 {{# iconlist }}
62 {{# icons }}
63 {{> core/pix_icon }}
64 {{/ icons }}
65 {{/ iconlist }}
66 </ul>
67 {{/ tabheader }}
68 {{# tabbody }}
69 <div class="tab-content">
70 {{# tabcontent }}
71 {{# tabs }}
72 {{> core/notification_info}}
73 {{/ tabs }}
74 {{/ tabcontent }}
75 </div>
76 {{/ tabbody }}
77 </div>
78 {{#js}}
79 require(['jquery','core/tabs'], function($, tabs) {
81 var container = $("#{{ uniqid }}-tab-container");
82 tabs.create(container);
83 });
84 {{/js}}
85 TBD;
86 $templatewithcomment = <<<TBC
87 {{!
88 This file is part of Moodle - http://moodle.org/
90 Moodle is free software: you can redistribute it and/or modify
91 it under the terms of the GNU General Public License as published by
92 the Free Software Foundation, either version 3 of the License, or
93 (at your option) any later version.
95 Moodle is distributed in the hope that it will be useful,
96 but WITHOUT ANY WARRANTY; without even the implied warranty of
97 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
98 GNU General Public License for more details.
100 You should have received a copy of the GNU General Public License
101 along with Moodle. If not, see <http://www.gnu.org/licenses/>.
104 @template mod_lemmings/lemmings
106 Lemmings template.
108 The purpose of this template is to render a lot of lemmings.
110 Classes required for JS:
111 * none
113 Data attributes required for JS:
114 * none
116 Context variables required for this template:
117 * attributes Array of name / value pairs.
119 Example context (json):
121 "lemmings": [
122 { "name": "Lemmy Winks", "age" : 1, "size" : "big" },
123 { "name": "Rocky", "age" : 2, "size" : "small" }
128 $templatebody
130 Here's some more comment text
131 Note, there is no need to test bracketed variables inside comments as gherkin does not support that!
132 See this issue: https://github.com/mustache/spec/issues/8
134 TBC;
136 // Ensure that the template when stripped of comments just includes the body.
137 $stripped = phpunit_util::call_internal_method(null, 'strip_template_comments',
138 [$templatewithcomment], 'core\output\external');
139 $this->assertEquals(trim($templatebody), trim($stripped));
141 $tokenizer = new Mustache_Tokenizer();
142 $tokens = $tokenizer->scan($templatebody);
143 $parser = new Mustache_Parser();
144 $tree = $parser->parse($tokens);
145 $this->assertNotEmpty($tree);