MDL-66849 output: Remove custom menu title when not specified
[moodle.git] / lib / tests / outputcomponents_test.php
CommitLineData
a3d5830a
PS
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/outputcomponents.php.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2011 David Mudrak <david@moodle.com>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27
28global $CFG;
29require_once($CFG->libdir . '/outputcomponents.php');
30
a3d5830a 31/**
18c43230 32 * Unit tests for the user_picture class.
a3d5830a 33 */
18c43230 34class core_outputcomponents_testcase extends advanced_testcase {
a3d5830a 35
80705c10 36 public function test_fields_aliasing() {
a3d5830a
PS
37 $fields = user_picture::fields();
38 $fields = array_map('trim', explode(',', $fields));
39 $this->assertTrue(in_array('id', $fields));
40
41 $aliased = array();
42 foreach ($fields as $field) {
43 if ($field === 'id') {
44 $aliased['id'] = 'aliasedid';
45 } else {
46 $aliased[$field] = 'prefix'.$field;
47 }
48 }
49
50 $returned = user_picture::fields('', array('custom1', 'id'), 'aliasedid', 'prefix');
51 $returned = array_map('trim', explode(',', $returned));
18c43230 52 $this->assertEquals(count($returned), count($fields) + 1); // Only one extra field added.
a3d5830a
PS
53
54 foreach ($fields as $field) {
55 if ($field === 'id') {
56 $expected = "id AS aliasedid";
57 } else {
58 $expected = "$field AS prefix$field";
59 }
18c43230 60 $this->assertContains($expected, $returned, "Expected pattern '$expected' not returned");
a3d5830a 61 }
18c43230 62 $this->assertContains("custom1 AS prefixcustom1", $returned, "Expected pattern 'custom1 AS prefixcustom1' not returned");
a3d5830a
PS
63 }
64
80705c10 65 public function test_fields_unaliasing() {
a3d5830a
PS
66 $fields = user_picture::fields();
67 $fields = array_map('trim', explode(',', $fields));
68
69 $fakerecord = new stdClass();
70 $fakerecord->aliasedid = 42;
71 foreach ($fields as $field) {
72 if ($field !== 'id') {
73 $fakerecord->{'prefix'.$field} = "Value of $field";
74 }
75 }
76 $fakerecord->prefixcustom1 = 'Value of custom1';
77
78 $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
79
18c43230 80 $this->assertEquals(42, $returned->id);
a3d5830a
PS
81 foreach ($fields as $field) {
82 if ($field !== 'id') {
18c43230 83 $this->assertSame("Value of $field", $returned->{$field});
a3d5830a
PS
84 }
85 }
18c43230 86 $this->assertSame('Value of custom1', $returned->custom1);
a3d5830a
PS
87 }
88
80705c10 89 public function test_fields_unaliasing_null() {
a3d5830a
PS
90 $fields = user_picture::fields();
91 $fields = array_map('trim', explode(',', $fields));
92
93 $fakerecord = new stdClass();
94 $fakerecord->aliasedid = 42;
95 foreach ($fields as $field) {
96 if ($field !== 'id') {
97 $fakerecord->{'prefix'.$field} = "Value of $field";
98 }
99 }
100 $fakerecord->prefixcustom1 = 'Value of custom1';
101 $fakerecord->prefiximagealt = null;
102
103 $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
104
18c43230
PS
105 $this->assertEquals(42, $returned->id);
106 $this->assertNull($returned->imagealt);
a3d5830a
PS
107 foreach ($fields as $field) {
108 if ($field !== 'id' and $field !== 'imagealt') {
18c43230 109 $this->assertSame("Value of $field", $returned->{$field});
a3d5830a
PS
110 }
111 }
18c43230 112 $this->assertSame('Value of custom1', $returned->custom1);
a3d5830a 113 }
80705c10
PS
114
115 public function test_get_url() {
26e778d1 116 global $DB, $CFG, $USER;
80705c10
PS
117
118 $this->resetAfterTest();
119
c2e899a4
SH
120 // Force SVG on so that we have predictable URL's.
121 $CFG->svgicons = true;
122
18c43230 123 // Verify new install contains expected defaults.
e56b58e3 124 $this->assertSame(theme_config::DEFAULT_THEME, $CFG->theme);
3db67789 125 $this->assertEquals(1, $CFG->slasharguments);
80705c10
PS
126 $this->assertEquals(1, $CFG->themerev);
127 $this->assertEquals(0, $CFG->themedesignermode);
55946a89 128 $this->assertSame('https://www.example.com/moodle', $CFG->wwwroot);
80705c10 129 $this->assertEquals(0, $CFG->enablegravatar);
18c43230 130 $this->assertSame('mm', $CFG->gravatardefaulturl);
80705c10 131
18c43230 132 // Create some users.
80705c10
PS
133 $page = new moodle_page();
134 $page->set_url('/user/profile.php');
135 $page->set_context(context_system::instance());
136 $renderer = $page->get_renderer('core');
137
4d254790 138 $user1 = $this->getDataGenerator()->create_user(array('picture'=>11, 'email'=>'user1@example.com'));
80705c10
PS
139 $context1 = context_user::instance($user1->id);
140 $user2 = $this->getDataGenerator()->create_user(array('picture'=>0, 'email'=>'user2@example.com'));
141 $context2 = context_user::instance($user2->id);
142
143 $user3 = $this->getDataGenerator()->create_user(array('picture'=>1, 'deleted'=>1, 'email'=>'user3@example.com'));
144 $context3 = context_user::instance($user3->id, IGNORE_MISSING);
18c43230
PS
145 $this->assertEquals(0, $user3->picture);
146 $this->assertNotEquals('user3@example.com', $user3->email);
80705c10
PS
147 $this->assertFalse($context3);
148
18c43230 149 // Try legacy picture == 1.
4d254790
PS
150 $user1->picture = 1;
151 $up1 = new user_picture($user1);
e56b58e3 152 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=1', $up1->get_url($page, $renderer)->out(false));
4d254790
PS
153 $user1->picture = 11;
154
18c43230 155 // Try valid user with picture when user context is not cached - 1 query expected.
80705c10
PS
156 context_helper::reset_caches();
157 $reads = $DB->perf_get_reads();
158 $up1 = new user_picture($user1);
159 $this->assertEquals($reads, $DB->perf_get_reads());
e56b58e3 160 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
80705c10
PS
161 $this->assertEquals($reads+1, $DB->perf_get_reads());
162
18c43230 163 // Try valid user with contextid hint - no queries expected.
80705c10
PS
164 $user1->contextid = $context1->id;
165 context_helper::reset_caches();
166 $reads = $DB->perf_get_reads();
167 $up1 = new user_picture($user1);
168 $this->assertEquals($reads, $DB->perf_get_reads());
e56b58e3 169 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
80705c10
PS
170 $this->assertEquals($reads, $DB->perf_get_reads());
171
18c43230 172 // Try valid user without image - no queries expected.
80705c10
PS
173 context_helper::reset_caches();
174 $reads = $DB->perf_get_reads();
175 $up2 = new user_picture($user2);
176 $this->assertEquals($reads, $DB->perf_get_reads());
e56b58e3 177 $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
80705c10
PS
178 $this->assertEquals($reads, $DB->perf_get_reads());
179
18c43230 180 // Try guessing of deleted users - no queries expected.
80705c10
PS
181 unset($user3->deleted);
182 context_helper::reset_caches();
183 $reads = $DB->perf_get_reads();
184 $up3 = new user_picture($user3);
185 $this->assertEquals($reads, $DB->perf_get_reads());
e56b58e3 186 $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
80705c10
PS
187 $this->assertEquals($reads, $DB->perf_get_reads());
188
18c43230 189 // Try incorrectly deleted users (with valid email and pciture flag) - some DB reads expected.
80705c10
PS
190 $user3->email = 'user3@example.com';
191 $user3->picture = 1;
192 $reads = $DB->perf_get_reads();
193 $up3 = new user_picture($user3);
194 $this->assertEquals($reads, $DB->perf_get_reads());
e56b58e3 195 $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
18c43230 196 $this->assertGreaterThan($reads, $DB->perf_get_reads());
80705c10 197
18c43230 198 // Test gravatar.
80705c10
PS
199 set_config('enablegravatar', 1);
200
18c43230 201 // Deleted user can not have gravatar.
80705c10
PS
202 $user3->email = 'deleted';
203 $user3->picture = 0;
204 $up3 = new user_picture($user3);
e56b58e3 205 $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
80705c10 206
55946a89
EL
207 // Http version.
208 $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
55946a89 209
18c43230 210 // Verify defaults to misteryman (mm).
1ef501ca 211 $up2 = new user_picture($user2);
18c43230 212 $this->assertSame('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=mm', $up2->get_url($page, $renderer)->out(false));
1ef501ca 213
18c43230 214 // Without gravatardefaulturl, verify we pick own file.
1ef501ca
EL
215 set_config('gravatardefaulturl', '');
216 $up2 = new user_picture($user2);
18c43230
PS
217 $this->assertSame('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=http%3A%2F%2Fwww.example.com%2Fmoodle%2Fpix%2Fu%2Ff2.png', $up2->get_url($page, $renderer)->out(false));
218 // Uploaded image takes precedence before gravatar.
1ef501ca 219 $up1 = new user_picture($user1);
e56b58e3 220 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
80705c10 221
26e778d1
JL
222 // Uploaded image with token-based access for current user.
223 $up1 = new user_picture($user1);
224 $up1->includetoken = true;
225 $token = get_user_key('core_files', $USER->id);
226 $this->assertSame($CFG->wwwroot.'/tokenpluginfile.php/'.$token.'/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
227
228 // Uploaded image with token-based access for other user.
229 $up1 = new user_picture($user1);
230 $up1->includetoken = $user2->id;
231 $token = get_user_key('core_files', $user2->id);
232 $this->assertSame($CFG->wwwroot.'/tokenpluginfile.php/'.$token.'/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
233
18c43230 234 // Https version.
672f4836 235 $CFG->wwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
80705c10
PS
236
237 $up1 = new user_picture($user1);
672f4836 238 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
80705c10
PS
239
240 $up3 = new user_picture($user3);
672f4836 241 $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
80705c10
PS
242
243 $up2 = new user_picture($user2);
18c43230 244 $this->assertSame('https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=https%3A%2F%2Fwww.example.com%2Fmoodle%2Fpix%2Fu%2Ff2.png', $up2->get_url($page, $renderer)->out(false));
1ef501ca 245
e80981bf 246 // TODO MDL-44792 Rewrite those tests to use a fixture.
18c43230 247 // Now test gravatar with one theme having own images (afterburner).
e80981bf
FM
248 // $this->assertFileExists("$CFG->dirroot/theme/afterburner/config.php");
249 // set_config('theme', 'afterburner');
250 // $page = new moodle_page();
251 // $page->set_url('/user/profile.php');
252 // $page->set_context(context_system::instance());
253 // $renderer = $page->get_renderer('core');
254
255 // $up2 = new user_picture($user2);
256 // $this->assertEquals('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=http%3A%2F%2Fwww.example.com%2Fmoodle%2Ftheme%2Fafterburner%2Fpix_core%2Fu%2Ff2.png', $up2->get_url($page, $renderer)->out(false));
257
e80981bf
FM
258 // $up2 = new user_picture($user2);
259 // $this->assertSame('https://secure.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=https%3A%2F%2Fwww.example.com%2Fmoodle%2Ftheme%2Fafterburner%2Fpix_core%2Fu%2Ff2.png', $up2->get_url($page, $renderer)->out(false));
18c43230 260 // End of gravatar tests.
80705c10 261
18c43230 262 // Test themed images.
e80981bf
FM
263 // set_config('enablegravatar', 0);
264 // $this->assertFileExists("$CFG->dirroot/theme/formal_white/config.php"); // Use any other theme.
265 // set_config('theme', 'formal_white');
e80981bf
FM
266 // $page = new moodle_page();
267 // $page->set_url('/user/profile.php');
268 // $page->set_context(context_system::instance());
269 // $renderer = $page->get_renderer('core');
270
271 // $up1 = new user_picture($user1);
272 // $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/formal_white/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
273
274 // $up2 = new user_picture($user2);
275 // $this->assertSame($CFG->wwwroot.'/theme/image.php/formal_white/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
fcc4f23d 276
18c43230 277 // Test non-slashargument images.
e00f1c66 278 set_config('theme', 'classic');
672f4836 279 $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
fcc4f23d
PS
280 $CFG->slasharguments = 0;
281 $page = new moodle_page();
282 $page->set_url('/user/profile.php');
283 $page->set_context(context_system::instance());
284 $renderer = $page->get_renderer('core');
285
286 $up3 = new user_picture($user3);
e00f1c66 287 $this->assertSame($CFG->wwwroot.'/theme/image.php?theme=classic&component=core&rev=1&image=u%2Ff2', $up3->get_url($page, $renderer)->out(false));
80705c10 288 }
a3d5830a
PS
289
290 public function test_empty_menu() {
291 $emptymenu = new custom_menu();
18c43230 292 $this->assertInstanceOf('custom_menu', $emptymenu);
a3d5830a
PS
293 $this->assertFalse($emptymenu->has_children());
294 }
295
296 public function test_basic_syntax() {
297 $definition = <<<EOF
298Moodle community|http://moodle.org
299-Moodle free support|http://moodle.org/support
300-Moodle development|http://moodle.org/development
301--Moodle Tracker|http://tracker.moodle.org
302--Moodle Docs|http://docs.moodle.org
303-Moodle News|http://moodle.org/news
14aa5a40 304Moodle company||Moodle trust pty
a3d5830a
PS
305-Hosting|http://moodle.com/hosting|Commercial hosting
306-Support|http://moodle.com/support|Commercial support
307EOF;
308
309 $menu = new custom_menu($definition);
18c43230 310 $this->assertInstanceOf('custom_menu', $menu);
a3d5830a
PS
311 $this->assertTrue($menu->has_children());
312 $firstlevel = $menu->get_children();
313 $this->assertTrue(is_array($firstlevel));
18c43230 314 $this->assertCount(2, $firstlevel);
a3d5830a
PS
315
316 $item = array_shift($firstlevel);
18c43230 317 $this->assertInstanceOf('custom_menu_item', $item);
a3d5830a 318 $this->assertTrue($item->has_children());
18c43230 319 $this->assertCount(3, $item->get_children());
a3d5830a
PS
320 $this->assertEquals('Moodle community', $item->get_text());
321 $itemurl = $item->get_url();
322 $this->assertTrue($itemurl instanceof moodle_url);
323 $this->assertEquals('http://moodle.org', $itemurl->out());
8056397b 324 $this->assertNull($item->get_title()); // Implicit title.
a3d5830a 325
14aa5a40 326 /** @var custom_menu_item $item */
a3d5830a
PS
327 $item = array_shift($firstlevel);
328 $this->assertTrue($item->has_children());
18c43230
PS
329 $this->assertCount(2, $item->get_children());
330 $this->assertSame('Moodle company', $item->get_text());
331 $this->assertNull($item->get_url());
14aa5a40 332 $this->assertSame('Moodle trust pty', $item->get_title());
a3d5830a
PS
333
334 $children = $item->get_children();
335 $subitem = array_shift($children);
336 $this->assertFalse($subitem->has_children());
18c43230
PS
337 $this->assertSame('Hosting', $subitem->get_text());
338 $this->assertSame('Commercial hosting', $subitem->get_title());
a3d5830a
PS
339 }
340
14aa5a40 341 public function test_custommenu_mulitlang() {
a3d5830a
PS
342 $definition = <<<EOF
343Start|http://school.info
344Info
345-English|http://school.info/en|Information in English|en
62ffeb4f 346--Nested under English
14aa5a40 347--I will be lost|||de
a3d5830a 348-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
62ffeb4f 349--Nested under Deutsch
14aa5a40
SH
350--I will be lost|||en
351kontaktieren Sie uns|contactus.php||de
352Contact us|contactus.php||en
353EOF;
354 $definitionen = <<<EOF
355Start|http://school.info
356Info
357-English|http://school.info/en|Information in English|en
358--Nested under English
359Contact us|contactus.php||en
360EOF;
361 $definitionde = <<<EOF
362Start|http://school.info
363Info
364-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
365--Nested under Deutsch
366kontaktieren Sie uns|contactus.php||de
a3d5830a
PS
367EOF;
368
14aa5a40
SH
369 $definitiondedu = <<<EOF
370Start|http://school.info
371Info
372-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
373--Nested under Deutsch
374EOF;
a3d5830a 375
14aa5a40
SH
376 $parsed = $this->custommenu_out(new custom_menu($definition));
377 $parseden = $this->custommenu_out(new custom_menu($definition, 'en'));
378 $parsedde = $this->custommenu_out(new custom_menu($definition, 'de'));
379 $parseddedu = $this->custommenu_out(new custom_menu($definition, 'de_du'));
380
381 $actualen = $this->custommenu_out(new custom_menu($definitionen, 'en'));
382 $actualde = $this->custommenu_out(new custom_menu($definitionde, 'de'));
383 $actualdedu = $this->custommenu_out(new custom_menu($definitiondedu, 'de_du'));
384
385 $this->assertSame($actualen, $parseden, 'The parsed English menu does not match the expected English menu');
386 $this->assertSame($actualde, $parsedde, 'The parsed German menu does not match the expected German menu');
387 $this->assertSame($actualdedu, $parseddedu, 'The parsed German [Du] menu does not match the expected German [Du] menu');
388
389 $this->assertNotSame($parsed, $parsedde, 'The menu without language is the same as the German menu. They should differ!');
390 $this->assertNotSame($parsed, $parseden, 'The menu without language is the same as the English menu. They should differ!');
391 $this->assertNotSame($parsed, $parseddedu, 'The menu without language is the same as the German [Du] menu. They should differ!');
392 $this->assertNotSame($parseden, $parsedde, 'The English menu is the same as the German menu. They should differ!');
393 $this->assertNotSame($parseden, $parseddedu, 'The English menu is the same as the German [Du] menu. They should differ!');
394 $this->assertNotSame($parseddedu, $parsedde, 'The German [Du] menu is the same as the German menu. They should differ!');
395 }
a3d5830a 396
14aa5a40
SH
397 /**
398 * Support function that takes a custom_menu_item and converts it to a string.
399 *
400 * @param custom_menu_item $item
401 * @param int $depth
402 * @return string
403 */
404 protected function custommenu_out(custom_menu_item $item, $depth = 0) {
405 $str = str_repeat('-', $depth);
406 $str .= $item->get_text();
407 $str .= '|' . $item->get_url();
408 $str .= '|' . $item->get_title();
409 if ($item->has_children()) {
410 $str .= '|' . count($item->get_children());
411 foreach ($item->get_children() as $child) {
412 $str .= "\n" . $this->custommenu_out($child, $depth + 1);
413 }
414 }
415 return $str;
a3d5830a 416 }
4c5dc0e3 417
0dbae598 418 public function test_prepare() {
1c77e2aa 419 $expecteda = array('<span class="current-page">1</span>',
0dbae598
JF
420 '<a href="index.php?page=1">2</a>',
421 '<a href="index.php?page=2">3</a>',
422 '<a href="index.php?page=3">4</a>',
423 '<a href="index.php?page=4">5</a>',
424 '<a href="index.php?page=5">6</a>',
425 '<a href="index.php?page=6">7</a>',
426 '<a href="index.php?page=7">8</a>',
427 );
428 $expectedb = array('<a href="page?page=3">4</a>',
429 '<a href="page?page=4">5</a>',
1c77e2aa 430 '<span class="current-page">6</span>',
0dbae598
JF
431 '<a href="page?page=6">7</a>',
432 '<a href="page?page=7">8</a>',
433 );
434
435 $mpage = new moodle_page();
436 $rbase = new renderer_base($mpage, "/");
437 $pbara = new paging_bar(40, 0, 5, 'index.php');
18c43230 438 $pbara->prepare($rbase, $mpage, "/");
0dbae598
JF
439 $pbarb = new paging_bar(100, 5, 5, 'page');
440 $pbarb->maxdisplay = 5;
18c43230 441 $pbarb->prepare($rbase, $mpage, "/");
0dbae598 442
18c43230
PS
443 $this->assertEquals($expecteda, $pbara->pagelinks);
444 $this->assertEquals($expectedb, $pbarb->pagelinks);
0dbae598 445 }
ef96eb02
DW
446
447 public function test_pix_icon() {
448 $this->resetAfterTest();
449
450 $page = new moodle_page();
451
452 set_config('theme', 'boost');
453 // Need to reset after changing theme.
454 $page->reset_theme_and_output();
455 $renderer = $page->get_renderer('core');
456
457 $reason = 'An icon with no alt text is hidden from screenreaders.';
458 $this->assertContains('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
459
460 $reason = 'An icon with alt text is not hidden from screenreaders.';
461 $this->assertNotContains('aria-hidden="true"', $renderer->pix_icon('t/print', 'Print'), $reason);
462
463 // Test another theme with a different icon system.
e00f1c66 464 set_config('theme', 'classic');
ef96eb02
DW
465 // Need to reset after changing theme.
466 $page->reset_theme_and_output();
467 $renderer = $page->get_renderer('core');
468
469 $reason = 'An icon with no alt text is hidden from screenreaders.';
470 $this->assertContains('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
471
472 $reason = 'An icon with alt text is not hidden from screenreaders.';
473 $this->assertNotContains('aria-hidden="true"', $renderer->pix_icon('t/print', 'Print'), $reason);
474 }
92c28d07
JP
475
476 /**
477 * Test for checking the template context data for the single_select element.
478 */
479 public function test_single_select() {
480 global $PAGE;
481
482 $fakename = 'fakename';
483 $fakeclass = 'fakeclass';
484 $faketitle = 'faketitle';
485 $fakedisabled = true;
486 $fakefor = 'fakefor';
487
488 $someid = 'someid';
489 $realname = 'realname';
490 $realclass = 'realclass';
491 $realtitle = 'realtitle';
492 $realdisabled = false;
493 $reallabel = 'Some cool label';
494 $labelclass = 'somelabelclass';
495 $labelstyle = 'font-weight: bold';
496
497 $dataaction = 'actiondata';
498 $dataother = 'otherdata';
499
500 $attributes = [
501 'id' => $someid,
502 'class' => $fakeclass,
503 'title' => $faketitle,
504 'disabled' => $fakedisabled,
505 'name' => $fakename,
506 'data-action' => $dataaction,
507 'data-other' => $dataother,
508 ];
509 $labelattributes = [
510 'for' => $fakefor,
511 'class' => $labelclass,
512 'style' => $labelstyle
513 ];
514
515 $options = [ "Option A", "Option B", "Option C" ];
516 $nothing = ['' => 'choosedots'];
517
518 $url = new moodle_url('/');
519
520 $singleselect = new single_select($url, $realname, $options, null, $nothing, 'someformid');
521 $singleselect->class = $realclass;
522 $singleselect->tooltip = $realtitle;
523 $singleselect->disabled = $realdisabled;
524 $singleselect->attributes = $attributes;
525 $singleselect->label = $reallabel;
526 $singleselect->labelattributes = $labelattributes;
527
528 $renderer = $PAGE->get_renderer('core');
529 $data = $singleselect->export_for_template($renderer);
530
531 $this->assertEquals($realtitle, $data->title);
532 $this->assertEquals($singleselect->class, $data->classes);
533 $this->assertEquals($realname, $data->name);
534 $this->assertEquals($reallabel, $data->label);
535 $this->assertEquals($realdisabled, $data->disabled);
536 $this->assertEquals($someid, $data->id);
537
538 // Validate attributes array.
539 // The following should not be included: id, class, name, disabled.
540 $this->assertFalse(in_array(['name' => 'id', 'value' => $someid], $data->attributes));
541 $this->assertFalse(in_array(['name' => 'class', 'value' => $fakeclass], $data->attributes));
542 $this->assertFalse(in_array(['name' => 'name', 'value' => $fakeclass], $data->attributes));
543 $this->assertFalse(in_array(['name' => 'disabled', 'value' => $fakedisabled], $data->attributes));
544 // The rest should be fine.
545 $this->assertTrue(in_array(['name' => 'data-action', 'value' => $dataaction], $data->attributes));
546 $this->assertTrue(in_array(['name' => 'data-other', 'value' => $dataother], $data->attributes));
547
548 // Validate label attributes.
549 // The for attribute should not be included.
550 $this->assertFalse(in_array(['name' => 'for', 'value' => $someid], $data->labelattributes));
551 // The rest should be fine.
552 $this->assertTrue(in_array(['name' => 'class', 'value' => $labelclass], $data->labelattributes));
553 $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
554 }
555
556 /**
557 * Test for checking the template context data for the url_select element.
558 */
559 public function test_url_select() {
560 global $PAGE;
561
562 $fakename = 'fakename';
563 $fakeclass = 'fakeclass';
564 $faketitle = 'faketitle';
565 $fakedisabled = true;
566 $fakefor = 'fakefor';
567
568 $someid = 'someid';
569 $realclass = 'realclass';
570 $realtitle = 'realtitle';
571 $realdisabled = false;
572 $reallabel = 'Some cool label';
573 $labelclass = 'somelabelclass';
574 $labelstyle = 'font-weight: bold';
575
576 $dataaction = 'actiondata';
577 $dataother = 'otherdata';
578
579 $attributes = [
580 'id' => $someid,
581 'class' => $fakeclass,
582 'title' => $faketitle,
583 'disabled' => $fakedisabled,
584 'name' => $fakename,
585 'data-action' => $dataaction,
586 'data-other' => $dataother,
587 ];
588 $labelattributes = [
589 'for' => $fakefor,
590 'class' => $labelclass,
591 'style' => $labelstyle
592 ];
593
594 $url1 = new moodle_url("/#a");
595 $url2 = new moodle_url("/#b");
596 $url3 = new moodle_url("/#c");
597
598 $urls = [
599 $url1->out() => 'A',
600 $url2->out() => 'B',
601 $url3->out() => 'C',
602 ];
603 $nothing = ['' => 'choosedots'];
604
605 $urlselect = new url_select($urls, null, $nothing, 'someformid');
606 $urlselect->class = $realclass;
607 $urlselect->tooltip = $realtitle;
608 $urlselect->disabled = $realdisabled;
609 $urlselect->attributes = $attributes;
610 $urlselect->label = $reallabel;
611 $urlselect->labelattributes = $labelattributes;
612
613 $renderer = $PAGE->get_renderer('core');
614 $data = $urlselect->export_for_template($renderer);
615
616 $this->assertEquals($realtitle, $data->title);
617 $this->assertEquals($urlselect->class, $data->classes);
618 $this->assertEquals($reallabel, $data->label);
619 $this->assertEquals($realdisabled, $data->disabled);
620 $this->assertEquals($someid, $data->id);
621
622 // Validate attributes array.
623 // The following should not be included: id, class, name, disabled.
624 $this->assertFalse(in_array(['name' => 'id', 'value' => $someid], $data->attributes));
625 $this->assertFalse(in_array(['name' => 'class', 'value' => $fakeclass], $data->attributes));
626 $this->assertFalse(in_array(['name' => 'name', 'value' => $fakeclass], $data->attributes));
627 $this->assertFalse(in_array(['name' => 'disabled', 'value' => $fakedisabled], $data->attributes));
628 // The rest should be fine.
629 $this->assertTrue(in_array(['name' => 'data-action', 'value' => $dataaction], $data->attributes));
630 $this->assertTrue(in_array(['name' => 'data-other', 'value' => $dataother], $data->attributes));
631
632 // Validate label attributes.
633 // The for attribute should not be included.
634 $this->assertFalse(in_array(['name' => 'for', 'value' => $someid], $data->labelattributes));
635 // The rest should be fine.
636 $this->assertTrue(in_array(['name' => 'class', 'value' => $labelclass], $data->labelattributes));
637 $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
638 }
4c5dc0e3 639}