MDL-65081 core_output: Unit tests for single_select and url_select
[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() {
116 global $DB, $CFG;
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
18c43230 222 // Https version.
672f4836 223 $CFG->wwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
80705c10
PS
224
225 $up1 = new user_picture($user1);
672f4836 226 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
80705c10
PS
227
228 $up3 = new user_picture($user3);
672f4836 229 $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
80705c10
PS
230
231 $up2 = new user_picture($user2);
18c43230 232 $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 233
e80981bf 234 // TODO MDL-44792 Rewrite those tests to use a fixture.
18c43230 235 // Now test gravatar with one theme having own images (afterburner).
e80981bf
FM
236 // $CFG->httpswwwroot = $CFG->wwwroot;
237 // $this->assertFileExists("$CFG->dirroot/theme/afterburner/config.php");
238 // set_config('theme', 'afterburner');
239 // $page = new moodle_page();
240 // $page->set_url('/user/profile.php');
241 // $page->set_context(context_system::instance());
242 // $renderer = $page->get_renderer('core');
243
244 // $up2 = new user_picture($user2);
245 // $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));
246
247 // // Https version.
248 // $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
249
250 // $up2 = new user_picture($user2);
251 // $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 252 // End of gravatar tests.
80705c10 253
18c43230 254 // Test themed images.
e80981bf
FM
255 // set_config('enablegravatar', 0);
256 // $this->assertFileExists("$CFG->dirroot/theme/formal_white/config.php"); // Use any other theme.
257 // set_config('theme', 'formal_white');
258 // $CFG->httpswwwroot = $CFG->wwwroot;
259 // $page = new moodle_page();
260 // $page->set_url('/user/profile.php');
261 // $page->set_context(context_system::instance());
262 // $renderer = $page->get_renderer('core');
263
264 // $up1 = new user_picture($user1);
265 // $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/formal_white/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
266
267 // $up2 = new user_picture($user2);
268 // $this->assertSame($CFG->wwwroot.'/theme/image.php/formal_white/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
fcc4f23d 269
18c43230 270 // Test non-slashargument images.
a4604e1e 271 set_config('theme', 'clean');
672f4836 272 $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
fcc4f23d
PS
273 $CFG->slasharguments = 0;
274 $page = new moodle_page();
275 $page->set_url('/user/profile.php');
276 $page->set_context(context_system::instance());
277 $renderer = $page->get_renderer('core');
278
279 $up3 = new user_picture($user3);
a4604e1e 280 $this->assertSame($CFG->wwwroot.'/theme/image.php?theme=clean&component=core&rev=1&image=u%2Ff2', $up3->get_url($page, $renderer)->out(false));
80705c10 281 }
a3d5830a
PS
282
283 public function test_empty_menu() {
284 $emptymenu = new custom_menu();
18c43230 285 $this->assertInstanceOf('custom_menu', $emptymenu);
a3d5830a
PS
286 $this->assertFalse($emptymenu->has_children());
287 }
288
289 public function test_basic_syntax() {
290 $definition = <<<EOF
291Moodle community|http://moodle.org
292-Moodle free support|http://moodle.org/support
293-Moodle development|http://moodle.org/development
294--Moodle Tracker|http://tracker.moodle.org
295--Moodle Docs|http://docs.moodle.org
296-Moodle News|http://moodle.org/news
14aa5a40 297Moodle company||Moodle trust pty
a3d5830a
PS
298-Hosting|http://moodle.com/hosting|Commercial hosting
299-Support|http://moodle.com/support|Commercial support
300EOF;
301
302 $menu = new custom_menu($definition);
18c43230 303 $this->assertInstanceOf('custom_menu', $menu);
a3d5830a
PS
304 $this->assertTrue($menu->has_children());
305 $firstlevel = $menu->get_children();
306 $this->assertTrue(is_array($firstlevel));
18c43230 307 $this->assertCount(2, $firstlevel);
a3d5830a
PS
308
309 $item = array_shift($firstlevel);
18c43230 310 $this->assertInstanceOf('custom_menu_item', $item);
a3d5830a 311 $this->assertTrue($item->has_children());
18c43230 312 $this->assertCount(3, $item->get_children());
a3d5830a
PS
313 $this->assertEquals('Moodle community', $item->get_text());
314 $itemurl = $item->get_url();
315 $this->assertTrue($itemurl instanceof moodle_url);
316 $this->assertEquals('http://moodle.org', $itemurl->out());
18c43230 317 $this->assertEquals($item->get_text(), $item->get_title()); // Implicit title.
a3d5830a 318
14aa5a40 319 /** @var custom_menu_item $item */
a3d5830a
PS
320 $item = array_shift($firstlevel);
321 $this->assertTrue($item->has_children());
18c43230
PS
322 $this->assertCount(2, $item->get_children());
323 $this->assertSame('Moodle company', $item->get_text());
324 $this->assertNull($item->get_url());
14aa5a40 325 $this->assertSame('Moodle trust pty', $item->get_title());
a3d5830a
PS
326
327 $children = $item->get_children();
328 $subitem = array_shift($children);
329 $this->assertFalse($subitem->has_children());
18c43230
PS
330 $this->assertSame('Hosting', $subitem->get_text());
331 $this->assertSame('Commercial hosting', $subitem->get_title());
a3d5830a
PS
332 }
333
14aa5a40 334 public function test_custommenu_mulitlang() {
a3d5830a
PS
335 $definition = <<<EOF
336Start|http://school.info
337Info
338-English|http://school.info/en|Information in English|en
62ffeb4f 339--Nested under English
14aa5a40 340--I will be lost|||de
a3d5830a 341-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
62ffeb4f 342--Nested under Deutsch
14aa5a40
SH
343--I will be lost|||en
344kontaktieren Sie uns|contactus.php||de
345Contact us|contactus.php||en
346EOF;
347 $definitionen = <<<EOF
348Start|http://school.info
349Info
350-English|http://school.info/en|Information in English|en
351--Nested under English
352Contact us|contactus.php||en
353EOF;
354 $definitionde = <<<EOF
355Start|http://school.info
356Info
357-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
358--Nested under Deutsch
359kontaktieren Sie uns|contactus.php||de
a3d5830a
PS
360EOF;
361
14aa5a40
SH
362 $definitiondedu = <<<EOF
363Start|http://school.info
364Info
365-Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
366--Nested under Deutsch
367EOF;
a3d5830a 368
14aa5a40
SH
369 $parsed = $this->custommenu_out(new custom_menu($definition));
370 $parseden = $this->custommenu_out(new custom_menu($definition, 'en'));
371 $parsedde = $this->custommenu_out(new custom_menu($definition, 'de'));
372 $parseddedu = $this->custommenu_out(new custom_menu($definition, 'de_du'));
373
374 $actualen = $this->custommenu_out(new custom_menu($definitionen, 'en'));
375 $actualde = $this->custommenu_out(new custom_menu($definitionde, 'de'));
376 $actualdedu = $this->custommenu_out(new custom_menu($definitiondedu, 'de_du'));
377
378 $this->assertSame($actualen, $parseden, 'The parsed English menu does not match the expected English menu');
379 $this->assertSame($actualde, $parsedde, 'The parsed German menu does not match the expected German menu');
380 $this->assertSame($actualdedu, $parseddedu, 'The parsed German [Du] menu does not match the expected German [Du] menu');
381
382 $this->assertNotSame($parsed, $parsedde, 'The menu without language is the same as the German menu. They should differ!');
383 $this->assertNotSame($parsed, $parseden, 'The menu without language is the same as the English menu. They should differ!');
384 $this->assertNotSame($parsed, $parseddedu, 'The menu without language is the same as the German [Du] menu. They should differ!');
385 $this->assertNotSame($parseden, $parsedde, 'The English menu is the same as the German menu. They should differ!');
386 $this->assertNotSame($parseden, $parseddedu, 'The English menu is the same as the German [Du] menu. They should differ!');
387 $this->assertNotSame($parseddedu, $parsedde, 'The German [Du] menu is the same as the German menu. They should differ!');
388 }
a3d5830a 389
14aa5a40
SH
390 /**
391 * Support function that takes a custom_menu_item and converts it to a string.
392 *
393 * @param custom_menu_item $item
394 * @param int $depth
395 * @return string
396 */
397 protected function custommenu_out(custom_menu_item $item, $depth = 0) {
398 $str = str_repeat('-', $depth);
399 $str .= $item->get_text();
400 $str .= '|' . $item->get_url();
401 $str .= '|' . $item->get_title();
402 if ($item->has_children()) {
403 $str .= '|' . count($item->get_children());
404 foreach ($item->get_children() as $child) {
405 $str .= "\n" . $this->custommenu_out($child, $depth + 1);
406 }
407 }
408 return $str;
a3d5830a 409 }
4c5dc0e3 410
0dbae598 411 public function test_prepare() {
1c77e2aa 412 $expecteda = array('<span class="current-page">1</span>',
0dbae598
JF
413 '<a href="index.php?page=1">2</a>',
414 '<a href="index.php?page=2">3</a>',
415 '<a href="index.php?page=3">4</a>',
416 '<a href="index.php?page=4">5</a>',
417 '<a href="index.php?page=5">6</a>',
418 '<a href="index.php?page=6">7</a>',
419 '<a href="index.php?page=7">8</a>',
420 );
421 $expectedb = array('<a href="page?page=3">4</a>',
422 '<a href="page?page=4">5</a>',
1c77e2aa 423 '<span class="current-page">6</span>',
0dbae598
JF
424 '<a href="page?page=6">7</a>',
425 '<a href="page?page=7">8</a>',
426 );
427
428 $mpage = new moodle_page();
429 $rbase = new renderer_base($mpage, "/");
430 $pbara = new paging_bar(40, 0, 5, 'index.php');
18c43230 431 $pbara->prepare($rbase, $mpage, "/");
0dbae598
JF
432 $pbarb = new paging_bar(100, 5, 5, 'page');
433 $pbarb->maxdisplay = 5;
18c43230 434 $pbarb->prepare($rbase, $mpage, "/");
0dbae598 435
18c43230
PS
436 $this->assertEquals($expecteda, $pbara->pagelinks);
437 $this->assertEquals($expectedb, $pbarb->pagelinks);
0dbae598 438 }
ef96eb02
DW
439
440 public function test_pix_icon() {
441 $this->resetAfterTest();
442
443 $page = new moodle_page();
444
445 set_config('theme', 'boost');
446 // Need to reset after changing theme.
447 $page->reset_theme_and_output();
448 $renderer = $page->get_renderer('core');
449
450 $reason = 'An icon with no alt text is hidden from screenreaders.';
451 $this->assertContains('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
452
453 $reason = 'An icon with alt text is not hidden from screenreaders.';
454 $this->assertNotContains('aria-hidden="true"', $renderer->pix_icon('t/print', 'Print'), $reason);
455
456 // Test another theme with a different icon system.
457 set_config('theme', 'clean');
458 // Need to reset after changing theme.
459 $page->reset_theme_and_output();
460 $renderer = $page->get_renderer('core');
461
462 $reason = 'An icon with no alt text is hidden from screenreaders.';
463 $this->assertContains('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
464
465 $reason = 'An icon with alt text is not hidden from screenreaders.';
466 $this->assertNotContains('aria-hidden="true"', $renderer->pix_icon('t/print', 'Print'), $reason);
467 }
92c28d07
JP
468
469 /**
470 * Test for checking the template context data for the single_select element.
471 */
472 public function test_single_select() {
473 global $PAGE;
474
475 $fakename = 'fakename';
476 $fakeclass = 'fakeclass';
477 $faketitle = 'faketitle';
478 $fakedisabled = true;
479 $fakefor = 'fakefor';
480
481 $someid = 'someid';
482 $realname = 'realname';
483 $realclass = 'realclass';
484 $realtitle = 'realtitle';
485 $realdisabled = false;
486 $reallabel = 'Some cool label';
487 $labelclass = 'somelabelclass';
488 $labelstyle = 'font-weight: bold';
489
490 $dataaction = 'actiondata';
491 $dataother = 'otherdata';
492
493 $attributes = [
494 'id' => $someid,
495 'class' => $fakeclass,
496 'title' => $faketitle,
497 'disabled' => $fakedisabled,
498 'name' => $fakename,
499 'data-action' => $dataaction,
500 'data-other' => $dataother,
501 ];
502 $labelattributes = [
503 'for' => $fakefor,
504 'class' => $labelclass,
505 'style' => $labelstyle
506 ];
507
508 $options = [ "Option A", "Option B", "Option C" ];
509 $nothing = ['' => 'choosedots'];
510
511 $url = new moodle_url('/');
512
513 $singleselect = new single_select($url, $realname, $options, null, $nothing, 'someformid');
514 $singleselect->class = $realclass;
515 $singleselect->tooltip = $realtitle;
516 $singleselect->disabled = $realdisabled;
517 $singleselect->attributes = $attributes;
518 $singleselect->label = $reallabel;
519 $singleselect->labelattributes = $labelattributes;
520
521 $renderer = $PAGE->get_renderer('core');
522 $data = $singleselect->export_for_template($renderer);
523
524 $this->assertEquals($realtitle, $data->title);
525 $this->assertEquals($singleselect->class, $data->classes);
526 $this->assertEquals($realname, $data->name);
527 $this->assertEquals($reallabel, $data->label);
528 $this->assertEquals($realdisabled, $data->disabled);
529 $this->assertEquals($someid, $data->id);
530
531 // Validate attributes array.
532 // The following should not be included: id, class, name, disabled.
533 $this->assertFalse(in_array(['name' => 'id', 'value' => $someid], $data->attributes));
534 $this->assertFalse(in_array(['name' => 'class', 'value' => $fakeclass], $data->attributes));
535 $this->assertFalse(in_array(['name' => 'name', 'value' => $fakeclass], $data->attributes));
536 $this->assertFalse(in_array(['name' => 'disabled', 'value' => $fakedisabled], $data->attributes));
537 // The rest should be fine.
538 $this->assertTrue(in_array(['name' => 'data-action', 'value' => $dataaction], $data->attributes));
539 $this->assertTrue(in_array(['name' => 'data-other', 'value' => $dataother], $data->attributes));
540
541 // Validate label attributes.
542 // The for attribute should not be included.
543 $this->assertFalse(in_array(['name' => 'for', 'value' => $someid], $data->labelattributes));
544 // The rest should be fine.
545 $this->assertTrue(in_array(['name' => 'class', 'value' => $labelclass], $data->labelattributes));
546 $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
547 }
548
549 /**
550 * Test for checking the template context data for the url_select element.
551 */
552 public function test_url_select() {
553 global $PAGE;
554
555 $fakename = 'fakename';
556 $fakeclass = 'fakeclass';
557 $faketitle = 'faketitle';
558 $fakedisabled = true;
559 $fakefor = 'fakefor';
560
561 $someid = 'someid';
562 $realclass = 'realclass';
563 $realtitle = 'realtitle';
564 $realdisabled = false;
565 $reallabel = 'Some cool label';
566 $labelclass = 'somelabelclass';
567 $labelstyle = 'font-weight: bold';
568
569 $dataaction = 'actiondata';
570 $dataother = 'otherdata';
571
572 $attributes = [
573 'id' => $someid,
574 'class' => $fakeclass,
575 'title' => $faketitle,
576 'disabled' => $fakedisabled,
577 'name' => $fakename,
578 'data-action' => $dataaction,
579 'data-other' => $dataother,
580 ];
581 $labelattributes = [
582 'for' => $fakefor,
583 'class' => $labelclass,
584 'style' => $labelstyle
585 ];
586
587 $url1 = new moodle_url("/#a");
588 $url2 = new moodle_url("/#b");
589 $url3 = new moodle_url("/#c");
590
591 $urls = [
592 $url1->out() => 'A',
593 $url2->out() => 'B',
594 $url3->out() => 'C',
595 ];
596 $nothing = ['' => 'choosedots'];
597
598 $urlselect = new url_select($urls, null, $nothing, 'someformid');
599 $urlselect->class = $realclass;
600 $urlselect->tooltip = $realtitle;
601 $urlselect->disabled = $realdisabled;
602 $urlselect->attributes = $attributes;
603 $urlselect->label = $reallabel;
604 $urlselect->labelattributes = $labelattributes;
605
606 $renderer = $PAGE->get_renderer('core');
607 $data = $urlselect->export_for_template($renderer);
608
609 $this->assertEquals($realtitle, $data->title);
610 $this->assertEquals($urlselect->class, $data->classes);
611 $this->assertEquals($reallabel, $data->label);
612 $this->assertEquals($realdisabled, $data->disabled);
613 $this->assertEquals($someid, $data->id);
614
615 // Validate attributes array.
616 // The following should not be included: id, class, name, disabled.
617 $this->assertFalse(in_array(['name' => 'id', 'value' => $someid], $data->attributes));
618 $this->assertFalse(in_array(['name' => 'class', 'value' => $fakeclass], $data->attributes));
619 $this->assertFalse(in_array(['name' => 'name', 'value' => $fakeclass], $data->attributes));
620 $this->assertFalse(in_array(['name' => 'disabled', 'value' => $fakedisabled], $data->attributes));
621 // The rest should be fine.
622 $this->assertTrue(in_array(['name' => 'data-action', 'value' => $dataaction], $data->attributes));
623 $this->assertTrue(in_array(['name' => 'data-other', 'value' => $dataother], $data->attributes));
624
625 // Validate label attributes.
626 // The for attribute should not be included.
627 $this->assertFalse(in_array(['name' => 'for', 'value' => $someid], $data->labelattributes));
628 // The rest should be fine.
629 $this->assertTrue(in_array(['name' => 'class', 'value' => $labelclass], $data->labelattributes));
630 $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
631 }
4c5dc0e3 632}