MDL-66849 output: Remove custom menu title when not specified
[moodle.git] / lib / tests / outputcomponents_test.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/outputcomponents.php');
31 /**
32  * Unit tests for the user_picture class.
33  */
34 class core_outputcomponents_testcase extends advanced_testcase {
36     public function test_fields_aliasing() {
37         $fields = user_picture::fields();
38         $fields = array_map('trim', explode(',', $fields));
39         $this->assertTrue(in_array('id', $fields));
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         }
50         $returned = user_picture::fields('', array('custom1', 'id'), 'aliasedid', 'prefix');
51         $returned = array_map('trim', explode(',', $returned));
52         $this->assertEquals(count($returned), count($fields) + 1); // Only one extra field added.
54         foreach ($fields as $field) {
55             if ($field === 'id') {
56                 $expected = "id AS aliasedid";
57             } else {
58                 $expected = "$field AS prefix$field";
59             }
60             $this->assertContains($expected, $returned, "Expected pattern '$expected' not returned");
61         }
62         $this->assertContains("custom1 AS prefixcustom1", $returned, "Expected pattern 'custom1 AS prefixcustom1' not returned");
63     }
65     public function test_fields_unaliasing() {
66         $fields = user_picture::fields();
67         $fields = array_map('trim', explode(',', $fields));
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';
78         $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
80         $this->assertEquals(42, $returned->id);
81         foreach ($fields as $field) {
82             if ($field !== 'id') {
83                 $this->assertSame("Value of $field", $returned->{$field});
84             }
85         }
86         $this->assertSame('Value of custom1', $returned->custom1);
87     }
89     public function test_fields_unaliasing_null() {
90         $fields = user_picture::fields();
91         $fields = array_map('trim', explode(',', $fields));
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;
103         $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
105         $this->assertEquals(42, $returned->id);
106         $this->assertNull($returned->imagealt);
107         foreach ($fields as $field) {
108             if ($field !== 'id' and $field !== 'imagealt') {
109                 $this->assertSame("Value of $field", $returned->{$field});
110             }
111         }
112         $this->assertSame('Value of custom1', $returned->custom1);
113     }
115     public function test_get_url() {
116         global $DB, $CFG, $USER;
118         $this->resetAfterTest();
120         // Force SVG on so that we have predictable URL's.
121         $CFG->svgicons = true;
123         // Verify new install contains expected defaults.
124         $this->assertSame(theme_config::DEFAULT_THEME, $CFG->theme);
125         $this->assertEquals(1, $CFG->slasharguments);
126         $this->assertEquals(1, $CFG->themerev);
127         $this->assertEquals(0, $CFG->themedesignermode);
128         $this->assertSame('https://www.example.com/moodle', $CFG->wwwroot);
129         $this->assertEquals(0, $CFG->enablegravatar);
130         $this->assertSame('mm', $CFG->gravatardefaulturl);
132         // Create some users.
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');
138         $user1 = $this->getDataGenerator()->create_user(array('picture'=>11, 'email'=>'user1@example.com'));
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);
143         $user3 = $this->getDataGenerator()->create_user(array('picture'=>1, 'deleted'=>1, 'email'=>'user3@example.com'));
144         $context3 = context_user::instance($user3->id, IGNORE_MISSING);
145         $this->assertEquals(0, $user3->picture);
146         $this->assertNotEquals('user3@example.com', $user3->email);
147         $this->assertFalse($context3);
149         // Try legacy picture == 1.
150         $user1->picture = 1;
151         $up1 = new user_picture($user1);
152         $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=1', $up1->get_url($page, $renderer)->out(false));
153         $user1->picture = 11;
155         // Try valid user with picture when user context is not cached - 1 query expected.
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());
160         $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
161         $this->assertEquals($reads+1, $DB->perf_get_reads());
163         // Try valid user with contextid hint - no queries expected.
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());
169         $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
170         $this->assertEquals($reads, $DB->perf_get_reads());
172         // Try valid user without image - no queries expected.
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());
177         $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
178         $this->assertEquals($reads, $DB->perf_get_reads());
180         // Try guessing of deleted users - no queries expected.
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());
186         $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
187         $this->assertEquals($reads, $DB->perf_get_reads());
189         // Try incorrectly deleted users (with valid email and pciture flag) - some DB reads expected.
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());
195         $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
196         $this->assertGreaterThan($reads, $DB->perf_get_reads());
198         // Test gravatar.
199         set_config('enablegravatar', 1);
201         // Deleted user can not have gravatar.
202         $user3->email = 'deleted';
203         $user3->picture = 0;
204         $up3 = new user_picture($user3);
205         $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
207         // Http version.
208         $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
210         // Verify defaults to misteryman (mm).
211         $up2 = new user_picture($user2);
212         $this->assertSame('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=mm', $up2->get_url($page, $renderer)->out(false));
214         // Without gravatardefaulturl, verify we pick own file.
215         set_config('gravatardefaulturl', '');
216         $up2 = new user_picture($user2);
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.
219         $up1 = new user_picture($user1);
220         $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
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));
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));
234         // Https version.
235         $CFG->wwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
237         $up1 = new user_picture($user1);
238         $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
240         $up3 = new user_picture($user3);
241         $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
243         $up2 = new user_picture($user2);
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));
246         // TODO MDL-44792 Rewrite those tests to use a fixture.
247         // Now test gravatar with one theme having own images (afterburner).
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');
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));
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));
260         // End of gravatar tests.
262         // Test themed images.
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');
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');
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));
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));
277         // Test non-slashargument images.
278         set_config('theme', 'classic');
279         $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
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');
286         $up3 = new user_picture($user3);
287         $this->assertSame($CFG->wwwroot.'/theme/image.php?theme=classic&component=core&rev=1&image=u%2Ff2', $up3->get_url($page, $renderer)->out(false));
288     }
290     public function test_empty_menu() {
291         $emptymenu = new custom_menu();
292         $this->assertInstanceOf('custom_menu', $emptymenu);
293         $this->assertFalse($emptymenu->has_children());
294     }
296     public function test_basic_syntax() {
297         $definition = <<<EOF
298 Moodle 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
304 Moodle company||Moodle trust pty
305 -Hosting|http://moodle.com/hosting|Commercial hosting
306 -Support|http://moodle.com/support|Commercial support
307 EOF;
309         $menu = new custom_menu($definition);
310         $this->assertInstanceOf('custom_menu', $menu);
311         $this->assertTrue($menu->has_children());
312         $firstlevel = $menu->get_children();
313         $this->assertTrue(is_array($firstlevel));
314         $this->assertCount(2, $firstlevel);
316         $item = array_shift($firstlevel);
317         $this->assertInstanceOf('custom_menu_item', $item);
318         $this->assertTrue($item->has_children());
319         $this->assertCount(3, $item->get_children());
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());
324         $this->assertNull($item->get_title()); // Implicit title.
326         /** @var custom_menu_item $item */
327         $item = array_shift($firstlevel);
328         $this->assertTrue($item->has_children());
329         $this->assertCount(2, $item->get_children());
330         $this->assertSame('Moodle company', $item->get_text());
331         $this->assertNull($item->get_url());
332         $this->assertSame('Moodle trust pty', $item->get_title());
334         $children = $item->get_children();
335         $subitem = array_shift($children);
336         $this->assertFalse($subitem->has_children());
337         $this->assertSame('Hosting', $subitem->get_text());
338         $this->assertSame('Commercial hosting', $subitem->get_title());
339     }
341     public function test_custommenu_mulitlang() {
342         $definition = <<<EOF
343 Start|http://school.info
344 Info
345 -English|http://school.info/en|Information in English|en
346 --Nested under English
347 --I will be lost|||de
348 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
349 --Nested under Deutsch
350 --I will be lost|||en
351 kontaktieren Sie uns|contactus.php||de
352 Contact us|contactus.php||en
353 EOF;
354         $definitionen = <<<EOF
355 Start|http://school.info
356 Info
357 -English|http://school.info/en|Information in English|en
358 --Nested under English
359 Contact us|contactus.php||en
360 EOF;
361         $definitionde = <<<EOF
362 Start|http://school.info
363 Info
364 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
365 --Nested under Deutsch
366 kontaktieren Sie uns|contactus.php||de
367 EOF;
369         $definitiondedu = <<<EOF
370 Start|http://school.info
371 Info
372 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
373 --Nested under Deutsch
374 EOF;
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'));
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'));
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');
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     }
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;
416     }
418     public function test_prepare() {
419         $expecteda = array('<span class="current-page">1</span>',
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>',
430                            '<span class="current-page">6</span>',
431                            '<a href="page?page=6">7</a>',
432                            '<a href="page?page=7">8</a>',
433                            );
435         $mpage = new moodle_page();
436         $rbase = new renderer_base($mpage, "/");
437         $pbara = new paging_bar(40, 0, 5, 'index.php');
438         $pbara->prepare($rbase, $mpage, "/");
439         $pbarb = new paging_bar(100, 5, 5, 'page');
440         $pbarb->maxdisplay = 5;
441         $pbarb->prepare($rbase, $mpage, "/");
443         $this->assertEquals($expecteda, $pbara->pagelinks);
444         $this->assertEquals($expectedb, $pbarb->pagelinks);
445     }
447     public function test_pix_icon() {
448         $this->resetAfterTest();
450         $page = new moodle_page();
452         set_config('theme', 'boost');
453         // Need to reset after changing theme.
454         $page->reset_theme_and_output();
455         $renderer = $page->get_renderer('core');
457         $reason = 'An icon with no alt text is hidden from screenreaders.';
458         $this->assertContains('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
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);
463         // Test another theme with a different icon system.
464         set_config('theme', 'classic');
465         // Need to reset after changing theme.
466         $page->reset_theme_and_output();
467         $renderer = $page->get_renderer('core');
469         $reason = 'An icon with no alt text is hidden from screenreaders.';
470         $this->assertContains('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
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     }
476     /**
477      * Test for checking the template context data for the single_select element.
478      */
479     public function test_single_select() {
480         global $PAGE;
482         $fakename = 'fakename';
483         $fakeclass = 'fakeclass';
484         $faketitle = 'faketitle';
485         $fakedisabled = true;
486         $fakefor = 'fakefor';
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';
497         $dataaction = 'actiondata';
498         $dataother = 'otherdata';
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         ];
515         $options = [ "Option A", "Option B", "Option C" ];
516         $nothing = ['' => 'choosedots'];
518         $url = new moodle_url('/');
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;
528         $renderer = $PAGE->get_renderer('core');
529         $data = $singleselect->export_for_template($renderer);
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);
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));
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     }
556     /**
557      * Test for checking the template context data for the url_select element.
558      */
559     public function test_url_select() {
560         global $PAGE;
562         $fakename = 'fakename';
563         $fakeclass = 'fakeclass';
564         $faketitle = 'faketitle';
565         $fakedisabled = true;
566         $fakefor = 'fakefor';
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';
576         $dataaction = 'actiondata';
577         $dataother = 'otherdata';
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         ];
594         $url1 = new moodle_url("/#a");
595         $url2 = new moodle_url("/#b");
596         $url3 = new moodle_url("/#c");
598         $urls = [
599             $url1->out() => 'A',
600             $url2->out() => 'B',
601             $url3->out() => 'C',
602         ];
603         $nothing = ['' => 'choosedots'];
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;
613         $renderer = $PAGE->get_renderer('core');
614         $data = $urlselect->export_for_template($renderer);
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);
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));
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     }