MDL-65081 core_output: Unit tests for single_select and url_select
[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;
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         // Https version.
223         $CFG->wwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
225         $up1 = new user_picture($user1);
226         $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/boost/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
228         $up3 = new user_picture($user3);
229         $this->assertSame($CFG->wwwroot.'/theme/image.php/boost/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
231         $up2 = new user_picture($user2);
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));
234         // TODO MDL-44792 Rewrite those tests to use a fixture.
235         // Now test gravatar with one theme having own images (afterburner).
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');
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));
247         // // Https version.
248         // $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
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));
252         // End of gravatar tests.
254         // Test themed images.
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');
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));
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));
270         // Test non-slashargument images.
271         set_config('theme', 'clean');
272         $CFG->wwwroot = str_replace('https:', 'http:', $CFG->wwwroot);
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');
279         $up3 = new user_picture($user3);
280         $this->assertSame($CFG->wwwroot.'/theme/image.php?theme=clean&component=core&rev=1&image=u%2Ff2', $up3->get_url($page, $renderer)->out(false));
281     }
283     public function test_empty_menu() {
284         $emptymenu = new custom_menu();
285         $this->assertInstanceOf('custom_menu', $emptymenu);
286         $this->assertFalse($emptymenu->has_children());
287     }
289     public function test_basic_syntax() {
290         $definition = <<<EOF
291 Moodle 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
297 Moodle company||Moodle trust pty
298 -Hosting|http://moodle.com/hosting|Commercial hosting
299 -Support|http://moodle.com/support|Commercial support
300 EOF;
302         $menu = new custom_menu($definition);
303         $this->assertInstanceOf('custom_menu', $menu);
304         $this->assertTrue($menu->has_children());
305         $firstlevel = $menu->get_children();
306         $this->assertTrue(is_array($firstlevel));
307         $this->assertCount(2, $firstlevel);
309         $item = array_shift($firstlevel);
310         $this->assertInstanceOf('custom_menu_item', $item);
311         $this->assertTrue($item->has_children());
312         $this->assertCount(3, $item->get_children());
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());
317         $this->assertEquals($item->get_text(), $item->get_title()); // Implicit title.
319         /** @var custom_menu_item $item */
320         $item = array_shift($firstlevel);
321         $this->assertTrue($item->has_children());
322         $this->assertCount(2, $item->get_children());
323         $this->assertSame('Moodle company', $item->get_text());
324         $this->assertNull($item->get_url());
325         $this->assertSame('Moodle trust pty', $item->get_title());
327         $children = $item->get_children();
328         $subitem = array_shift($children);
329         $this->assertFalse($subitem->has_children());
330         $this->assertSame('Hosting', $subitem->get_text());
331         $this->assertSame('Commercial hosting', $subitem->get_title());
332     }
334     public function test_custommenu_mulitlang() {
335         $definition = <<<EOF
336 Start|http://school.info
337 Info
338 -English|http://school.info/en|Information in English|en
339 --Nested under English
340 --I will be lost|||de
341 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
342 --Nested under Deutsch
343 --I will be lost|||en
344 kontaktieren Sie uns|contactus.php||de
345 Contact us|contactus.php||en
346 EOF;
347         $definitionen = <<<EOF
348 Start|http://school.info
349 Info
350 -English|http://school.info/en|Information in English|en
351 --Nested under English
352 Contact us|contactus.php||en
353 EOF;
354         $definitionde = <<<EOF
355 Start|http://school.info
356 Info
357 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
358 --Nested under Deutsch
359 kontaktieren Sie uns|contactus.php||de
360 EOF;
362         $definitiondedu = <<<EOF
363 Start|http://school.info
364 Info
365 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
366 --Nested under Deutsch
367 EOF;
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'));
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'));
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');
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     }
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;
409     }
411     public function test_prepare() {
412         $expecteda = array('<span class="current-page">1</span>',
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>',
423                            '<span class="current-page">6</span>',
424                            '<a href="page?page=6">7</a>',
425                            '<a href="page?page=7">8</a>',
426                            );
428         $mpage = new moodle_page();
429         $rbase = new renderer_base($mpage, "/");
430         $pbara = new paging_bar(40, 0, 5, 'index.php');
431         $pbara->prepare($rbase, $mpage, "/");
432         $pbarb = new paging_bar(100, 5, 5, 'page');
433         $pbarb->maxdisplay = 5;
434         $pbarb->prepare($rbase, $mpage, "/");
436         $this->assertEquals($expecteda, $pbara->pagelinks);
437         $this->assertEquals($expectedb, $pbarb->pagelinks);
438     }
440     public function test_pix_icon() {
441         $this->resetAfterTest();
443         $page = new moodle_page();
445         set_config('theme', 'boost');
446         // Need to reset after changing theme.
447         $page->reset_theme_and_output();
448         $renderer = $page->get_renderer('core');
450         $reason = 'An icon with no alt text is hidden from screenreaders.';
451         $this->assertContains('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
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);
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');
462         $reason = 'An icon with no alt text is hidden from screenreaders.';
463         $this->assertContains('aria-hidden="true"', $renderer->pix_icon('t/print', ''), $reason);
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     }
469     /**
470      * Test for checking the template context data for the single_select element.
471      */
472     public function test_single_select() {
473         global $PAGE;
475         $fakename = 'fakename';
476         $fakeclass = 'fakeclass';
477         $faketitle = 'faketitle';
478         $fakedisabled = true;
479         $fakefor = 'fakefor';
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';
490         $dataaction = 'actiondata';
491         $dataother = 'otherdata';
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         ];
508         $options = [ "Option A", "Option B", "Option C" ];
509         $nothing = ['' => 'choosedots'];
511         $url = new moodle_url('/');
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;
521         $renderer = $PAGE->get_renderer('core');
522         $data = $singleselect->export_for_template($renderer);
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);
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));
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     }
549     /**
550      * Test for checking the template context data for the url_select element.
551      */
552     public function test_url_select() {
553         global $PAGE;
555         $fakename = 'fakename';
556         $fakeclass = 'fakeclass';
557         $faketitle = 'faketitle';
558         $fakedisabled = true;
559         $fakefor = 'fakefor';
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';
569         $dataaction = 'actiondata';
570         $dataother = 'otherdata';
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         ];
587         $url1 = new moodle_url("/#a");
588         $url2 = new moodle_url("/#b");
589         $url3 = new moodle_url("/#c");
591         $urls = [
592             $url1->out() => 'A',
593             $url2->out() => 'B',
594             $url3->out() => 'C',
595         ];
596         $nothing = ['' => 'choosedots'];
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;
606         $renderer = $PAGE->get_renderer('core');
607         $data = $urlselect->export_for_template($renderer);
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);
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));
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     }