MDL-57636 frontpage: Fix display enrolled courses
[moodle.git] / lib / tests / output_external_test.php
CommitLineData
bd87693f 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/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
22 */
23
24defined('MOODLE_INTERNAL') || die();
25
26use core\output\external;
27
28require_once(__DIR__.'/../../lib/externallib.php');
29require_once(__DIR__.'/../../lib/mustache/src/Mustache/Tokenizer.php');
30require_once(__DIR__.'/../../lib/mustache/src/Mustache/Parser.php');
31
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
38 */
39class core_output_external_testcase extends base_testcase {
40
41 /**
42 * Ensure that stripping comments from templates does not mutilate the template body.
43 */
44 public function test_strip_template_comments() {
45
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) {
80
81 var container = $("#{{ uniqid }}-tab-container");
82 tabs.create(container);
83 });
84 {{/js}}
85TBD;
86 $templatewithcomment = <<<TBC
87 {{!
88 This file is part of Moodle - http://moodle.org/
89
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.
94
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.
99
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/>.
102 }}
103 {{!
104 @template mod_lemmings/lemmings
105
106 Lemmings template.
107
108 The purpose of this template is to render a lot of lemmings.
109
110 Classes required for JS:
111 * none
112
113 Data attributes required for JS:
114 * none
115
116 Context variables required for this template:
117 * attributes Array of name / value pairs.
118
119 Example context (json):
120 {
121 "lemmings": [
122 { "name": "Lemmy Winks", "age" : 1, "size" : "big" },
123 { "name": "Rocky", "age" : 2, "size" : "small" }
124 ]
125 }
126
127 }}
128 $templatebody
129 {{!
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
133 }}
134TBC;
135
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));
140
141 $tokenizer = new Mustache_Tokenizer();
142 $tokens = $tokenizer->scan($templatebody);
143 $parser = new Mustache_Parser();
144 $tree = $parser->parse($tokens);
145 $this->assertNotEmpty($tree);
146 }
147}