weekly release 3.0.3+
[moodle.git] / lib / tests / outputcomponents_test.php
blob1c180efdf5b76a22df5b52de447af52ef332da90
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('clean', $CFG->theme);
125 $this->assertEquals(1, $CFG->slasharguments);
126 $this->assertEquals(1, $CFG->themerev);
127 $this->assertEquals(0, $CFG->themedesignermode);
128 $this->assertSame('http://www.example.com/moodle', $CFG->wwwroot);
129 $this->assertSame($CFG->wwwroot, $CFG->httpswwwroot);
130 $this->assertEquals(0, $CFG->enablegravatar);
131 $this->assertSame('mm', $CFG->gravatardefaulturl);
133 // Create some users.
134 $page = new moodle_page();
135 $page->set_url('/user/profile.php');
136 $page->set_context(context_system::instance());
137 $renderer = $page->get_renderer('core');
139 $user1 = $this->getDataGenerator()->create_user(array('picture'=>11, 'email'=>'user1@example.com'));
140 $context1 = context_user::instance($user1->id);
141 $user2 = $this->getDataGenerator()->create_user(array('picture'=>0, 'email'=>'user2@example.com'));
142 $context2 = context_user::instance($user2->id);
144 $user3 = $this->getDataGenerator()->create_user(array('picture'=>1, 'deleted'=>1, 'email'=>'user3@example.com'));
145 $context3 = context_user::instance($user3->id, IGNORE_MISSING);
146 $this->assertEquals(0, $user3->picture);
147 $this->assertNotEquals('user3@example.com', $user3->email);
148 $this->assertFalse($context3);
150 // Try legacy picture == 1.
151 $user1->picture = 1;
152 $up1 = new user_picture($user1);
153 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/clean/f2?rev=1', $up1->get_url($page, $renderer)->out(false));
154 $user1->picture = 11;
156 // Try valid user with picture when user context is not cached - 1 query expected.
157 context_helper::reset_caches();
158 $reads = $DB->perf_get_reads();
159 $up1 = new user_picture($user1);
160 $this->assertEquals($reads, $DB->perf_get_reads());
161 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/clean/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
162 $this->assertEquals($reads+1, $DB->perf_get_reads());
164 // Try valid user with contextid hint - no queries expected.
165 $user1->contextid = $context1->id;
166 context_helper::reset_caches();
167 $reads = $DB->perf_get_reads();
168 $up1 = new user_picture($user1);
169 $this->assertEquals($reads, $DB->perf_get_reads());
170 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/clean/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
171 $this->assertEquals($reads, $DB->perf_get_reads());
173 // Try valid user without image - no queries expected.
174 context_helper::reset_caches();
175 $reads = $DB->perf_get_reads();
176 $up2 = new user_picture($user2);
177 $this->assertEquals($reads, $DB->perf_get_reads());
178 $this->assertSame($CFG->wwwroot.'/theme/image.php/clean/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
179 $this->assertEquals($reads, $DB->perf_get_reads());
181 // Try guessing of deleted users - no queries expected.
182 unset($user3->deleted);
183 context_helper::reset_caches();
184 $reads = $DB->perf_get_reads();
185 $up3 = new user_picture($user3);
186 $this->assertEquals($reads, $DB->perf_get_reads());
187 $this->assertSame($CFG->wwwroot.'/theme/image.php/clean/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
188 $this->assertEquals($reads, $DB->perf_get_reads());
190 // Try incorrectly deleted users (with valid email and pciture flag) - some DB reads expected.
191 $user3->email = 'user3@example.com';
192 $user3->picture = 1;
193 $reads = $DB->perf_get_reads();
194 $up3 = new user_picture($user3);
195 $this->assertEquals($reads, $DB->perf_get_reads());
196 $this->assertSame($CFG->wwwroot.'/theme/image.php/clean/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
197 $this->assertGreaterThan($reads, $DB->perf_get_reads());
199 // Test gravatar.
200 set_config('enablegravatar', 1);
202 // Deleted user can not have gravatar.
203 $user3->email = 'deleted';
204 $user3->picture = 0;
205 $up3 = new user_picture($user3);
206 $this->assertSame($CFG->wwwroot.'/theme/image.php/clean/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
208 // Verify defaults to misteryman (mm).
209 $up2 = new user_picture($user2);
210 $this->assertSame('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=mm', $up2->get_url($page, $renderer)->out(false));
212 // Without gravatardefaulturl, verify we pick own file.
213 set_config('gravatardefaulturl', '');
214 $up2 = new user_picture($user2);
215 $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));
216 // Uploaded image takes precedence before gravatar.
217 $up1 = new user_picture($user1);
218 $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/clean/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
220 // Https version.
221 $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
223 $up1 = new user_picture($user1);
224 $this->assertSame($CFG->httpswwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/clean/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
226 $up3 = new user_picture($user3);
227 $this->assertSame($CFG->httpswwwroot.'/theme/image.php/clean/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
229 $up2 = new user_picture($user2);
230 $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));
232 // TODO MDL-44792 Rewrite those tests to use a fixture.
233 // Now test gravatar with one theme having own images (afterburner).
234 // $CFG->httpswwwroot = $CFG->wwwroot;
235 // $this->assertFileExists("$CFG->dirroot/theme/afterburner/config.php");
236 // set_config('theme', 'afterburner');
237 // $page = new moodle_page();
238 // $page->set_url('/user/profile.php');
239 // $page->set_context(context_system::instance());
240 // $renderer = $page->get_renderer('core');
242 // $up2 = new user_picture($user2);
243 // $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));
245 // // Https version.
246 // $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
248 // $up2 = new user_picture($user2);
249 // $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));
250 // End of gravatar tests.
252 // Test themed images.
253 // set_config('enablegravatar', 0);
254 // $this->assertFileExists("$CFG->dirroot/theme/formal_white/config.php"); // Use any other theme.
255 // set_config('theme', 'formal_white');
256 // $CFG->httpswwwroot = $CFG->wwwroot;
257 // $page = new moodle_page();
258 // $page->set_url('/user/profile.php');
259 // $page->set_context(context_system::instance());
260 // $renderer = $page->get_renderer('core');
262 // $up1 = new user_picture($user1);
263 // $this->assertSame($CFG->wwwroot.'/pluginfile.php/'.$context1->id.'/user/icon/formal_white/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
265 // $up2 = new user_picture($user2);
266 // $this->assertSame($CFG->wwwroot.'/theme/image.php/formal_white/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
268 // Test non-slashargument images.
269 set_config('theme', 'clean');
270 $CFG->httpswwwroot = $CFG->wwwroot;
271 $CFG->slasharguments = 0;
272 $page = new moodle_page();
273 $page->set_url('/user/profile.php');
274 $page->set_context(context_system::instance());
275 $renderer = $page->get_renderer('core');
277 $up3 = new user_picture($user3);
278 $this->assertSame($CFG->wwwroot.'/theme/image.php?theme=clean&component=core&rev=1&image=u%2Ff2', $up3->get_url($page, $renderer)->out(false));
281 public function test_empty_menu() {
282 $emptymenu = new custom_menu();
283 $this->assertInstanceOf('custom_menu', $emptymenu);
284 $this->assertFalse($emptymenu->has_children());
287 public function test_basic_syntax() {
288 $definition = <<<EOF
289 Moodle community|http://moodle.org
290 -Moodle free support|http://moodle.org/support
291 -Moodle development|http://moodle.org/development
292 --Moodle Tracker|http://tracker.moodle.org
293 --Moodle Docs|http://docs.moodle.org
294 -Moodle News|http://moodle.org/news
295 Moodle company||Moodle trust pty
296 -Hosting|http://moodle.com/hosting|Commercial hosting
297 -Support|http://moodle.com/support|Commercial support
298 EOF;
300 $menu = new custom_menu($definition);
301 $this->assertInstanceOf('custom_menu', $menu);
302 $this->assertTrue($menu->has_children());
303 $firstlevel = $menu->get_children();
304 $this->assertTrue(is_array($firstlevel));
305 $this->assertCount(2, $firstlevel);
307 $item = array_shift($firstlevel);
308 $this->assertInstanceOf('custom_menu_item', $item);
309 $this->assertTrue($item->has_children());
310 $this->assertCount(3, $item->get_children());
311 $this->assertEquals('Moodle community', $item->get_text());
312 $itemurl = $item->get_url();
313 $this->assertTrue($itemurl instanceof moodle_url);
314 $this->assertEquals('http://moodle.org', $itemurl->out());
315 $this->assertEquals($item->get_text(), $item->get_title()); // Implicit title.
317 /** @var custom_menu_item $item */
318 $item = array_shift($firstlevel);
319 $this->assertTrue($item->has_children());
320 $this->assertCount(2, $item->get_children());
321 $this->assertSame('Moodle company', $item->get_text());
322 $this->assertNull($item->get_url());
323 $this->assertSame('Moodle trust pty', $item->get_title());
325 $children = $item->get_children();
326 $subitem = array_shift($children);
327 $this->assertFalse($subitem->has_children());
328 $this->assertSame('Hosting', $subitem->get_text());
329 $this->assertSame('Commercial hosting', $subitem->get_title());
332 public function test_custommenu_mulitlang() {
333 $definition = <<<EOF
334 Start|http://school.info
335 Info
336 -English|http://school.info/en|Information in English|en
337 --Nested under English
338 --I will be lost|||de
339 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
340 --Nested under Deutsch
341 --I will be lost|||en
342 kontaktieren Sie uns|contactus.php||de
343 Contact us|contactus.php||en
344 EOF;
345 $definitionen = <<<EOF
346 Start|http://school.info
347 Info
348 -English|http://school.info/en|Information in English|en
349 --Nested under English
350 Contact us|contactus.php||en
351 EOF;
352 $definitionde = <<<EOF
353 Start|http://school.info
354 Info
355 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
356 --Nested under Deutsch
357 kontaktieren Sie uns|contactus.php||de
358 EOF;
360 $definitiondedu = <<<EOF
361 Start|http://school.info
362 Info
363 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
364 --Nested under Deutsch
365 EOF;
367 $parsed = $this->custommenu_out(new custom_menu($definition));
368 $parseden = $this->custommenu_out(new custom_menu($definition, 'en'));
369 $parsedde = $this->custommenu_out(new custom_menu($definition, 'de'));
370 $parseddedu = $this->custommenu_out(new custom_menu($definition, 'de_du'));
372 $actualen = $this->custommenu_out(new custom_menu($definitionen, 'en'));
373 $actualde = $this->custommenu_out(new custom_menu($definitionde, 'de'));
374 $actualdedu = $this->custommenu_out(new custom_menu($definitiondedu, 'de_du'));
376 $this->assertSame($actualen, $parseden, 'The parsed English menu does not match the expected English menu');
377 $this->assertSame($actualde, $parsedde, 'The parsed German menu does not match the expected German menu');
378 $this->assertSame($actualdedu, $parseddedu, 'The parsed German [Du] menu does not match the expected German [Du] menu');
380 $this->assertNotSame($parsed, $parsedde, 'The menu without language is the same as the German menu. They should differ!');
381 $this->assertNotSame($parsed, $parseden, 'The menu without language is the same as the English menu. They should differ!');
382 $this->assertNotSame($parsed, $parseddedu, 'The menu without language is the same as the German [Du] menu. They should differ!');
383 $this->assertNotSame($parseden, $parsedde, 'The English menu is the same as the German menu. They should differ!');
384 $this->assertNotSame($parseden, $parseddedu, 'The English menu is the same as the German [Du] menu. They should differ!');
385 $this->assertNotSame($parseddedu, $parsedde, 'The German [Du] menu is the same as the German menu. They should differ!');
389 * Support function that takes a custom_menu_item and converts it to a string.
391 * @param custom_menu_item $item
392 * @param int $depth
393 * @return string
395 protected function custommenu_out(custom_menu_item $item, $depth = 0) {
396 $str = str_repeat('-', $depth);
397 $str .= $item->get_text();
398 $str .= '|' . $item->get_url();
399 $str .= '|' . $item->get_title();
400 if ($item->has_children()) {
401 $str .= '|' . count($item->get_children());
402 foreach ($item->get_children() as $child) {
403 $str .= "\n" . $this->custommenu_out($child, $depth + 1);
406 return $str;
409 public function test_prepare() {
410 $expecteda = array('<span class="current-page">1</span>',
411 '<a href="index.php?page=1">2</a>',
412 '<a href="index.php?page=2">3</a>',
413 '<a href="index.php?page=3">4</a>',
414 '<a href="index.php?page=4">5</a>',
415 '<a href="index.php?page=5">6</a>',
416 '<a href="index.php?page=6">7</a>',
417 '<a href="index.php?page=7">8</a>',
419 $expectedb = array('<a href="page?page=3">4</a>',
420 '<a href="page?page=4">5</a>',
421 '<span class="current-page">6</span>',
422 '<a href="page?page=6">7</a>',
423 '<a href="page?page=7">8</a>',
426 $mpage = new moodle_page();
427 $rbase = new renderer_base($mpage, "/");
428 $pbara = new paging_bar(40, 0, 5, 'index.php');
429 $pbara->prepare($rbase, $mpage, "/");
430 $pbarb = new paging_bar(100, 5, 5, 'page');
431 $pbarb->maxdisplay = 5;
432 $pbarb->prepare($rbase, $mpage, "/");
434 $this->assertEquals($expecteda, $pbara->pagelinks);
435 $this->assertEquals($expectedb, $pbarb->pagelinks);