Merge branch 'MDL-63702-master' of git://github.com/mihailges/moodle
[moodle.git] / lib / tests / outputcomponents_test.php
blobde5ec1ebd4bf8345fd25668b4f38be3fede37866
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.
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.
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
26 defined('MOODLE_INTERNAL') || die();
28 global $CFG;
29 require_once($CFG->libdir . '/outputcomponents.php');
31 /**
32 * Unit tests for the user_picture class.
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;
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";
60 $this->assertContains($expected, $returned, "Expected pattern '$expected' not returned");
62 $this->assertContains("custom1 AS prefixcustom1", $returned, "Expected pattern 'custom1 AS prefixcustom1' not returned");
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";
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});
86 $this->assertSame('Value of custom1', $returned->custom1);
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";
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});
112 $this->assertSame('Value of custom1', $returned->custom1);
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));
283 public function test_empty_menu() {
284 $emptymenu = new custom_menu();
285 $this->assertInstanceOf('custom_menu', $emptymenu);
286 $this->assertFalse($emptymenu->has_children());
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());
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!');
391 * Support function that takes a custom_menu_item and converts it to a string.
393 * @param custom_menu_item $item
394 * @param int $depth
395 * @return string
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);
408 return $str;
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>',
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>',
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);