MDL-35669 gravatar amend and complete gravatar tests.
[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');
32 /**
33  * Unit tests for the user_picture class
34  */
35 class user_picture_testcase extends advanced_testcase {
37     public function test_fields_aliasing() {
38         $fields = user_picture::fields();
39         $fields = array_map('trim', explode(',', $fields));
40         $this->assertTrue(in_array('id', $fields));
42         $aliased = array();
43         foreach ($fields as $field) {
44             if ($field === 'id') {
45                 $aliased['id'] = 'aliasedid';
46             } else {
47                 $aliased[$field] = 'prefix'.$field;
48             }
49         }
51         $returned = user_picture::fields('', array('custom1', 'id'), 'aliasedid', 'prefix');
52         $returned = array_map('trim', explode(',', $returned));
53         $this->assertEquals(count($returned), count($fields) + 1); // only one extra field added
55         foreach ($fields as $field) {
56             if ($field === 'id') {
57                 $expected = "id AS aliasedid";
58             } else {
59                 $expected = "$field AS prefix$field";
60             }
61             $this->assertTrue(in_array($expected, $returned), "Expected pattern '$expected' not returned");
62         }
63         $this->assertTrue(in_array("custom1 AS prefixcustom1", $returned), "Expected pattern 'custom1 AS prefixcustom1' not returned");
64     }
66     public function test_fields_unaliasing() {
67         $fields = user_picture::fields();
68         $fields = array_map('trim', explode(',', $fields));
70         $fakerecord = new stdClass();
71         $fakerecord->aliasedid = 42;
72         foreach ($fields as $field) {
73             if ($field !== 'id') {
74                 $fakerecord->{'prefix'.$field} = "Value of $field";
75             }
76         }
77         $fakerecord->prefixcustom1 = 'Value of custom1';
79         $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
81         $this->assertEquals($returned->id, 42);
82         foreach ($fields as $field) {
83             if ($field !== 'id') {
84                 $this->assertEquals($returned->{$field}, "Value of $field");
85             }
86         }
87         $this->assertEquals($returned->custom1, 'Value of custom1');
88     }
90     public function test_fields_unaliasing_null() {
91         $fields = user_picture::fields();
92         $fields = array_map('trim', explode(',', $fields));
94         $fakerecord = new stdClass();
95         $fakerecord->aliasedid = 42;
96         foreach ($fields as $field) {
97             if ($field !== 'id') {
98                 $fakerecord->{'prefix'.$field} = "Value of $field";
99             }
100         }
101         $fakerecord->prefixcustom1 = 'Value of custom1';
102         $fakerecord->prefiximagealt = null;
104         $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix');
106         $this->assertEquals($returned->id, 42);
107         $this->assertEquals($returned->imagealt, null);
108         foreach ($fields as $field) {
109             if ($field !== 'id' and $field !== 'imagealt') {
110                 $this->assertEquals($returned->{$field}, "Value of $field");
111             }
112         }
113         $this->assertEquals($returned->custom1, 'Value of custom1');
114     }
116     public function test_get_url() {
117         global $DB, $CFG;
119         $this->resetAfterTest();
121         // verify new install contains expected defaults
122         $this->assertEquals('standard', $CFG->theme);
123         $this->assertEquals(1, $CFG->slasharguments);
124         $this->assertEquals(1, $CFG->themerev);
125         $this->assertEquals(0, $CFG->themedesignermode);
126         $this->assertEquals('http://www.example.com/moodle', $CFG->wwwroot);
127         $this->assertEquals($CFG->wwwroot, $CFG->httpswwwroot);
128         $this->assertEquals(0, $CFG->enablegravatar);
129         $this->assertEquals('mm', $CFG->gravatardefaulturl);
131         // create some users
132         $page = new moodle_page();
133         $page->set_url('/user/profile.php');
134         $page->set_context(context_system::instance());
135         $renderer = $page->get_renderer('core');
137         $user1 = $this->getDataGenerator()->create_user(array('picture'=>11, 'email'=>'user1@example.com'));
138         $context1 = context_user::instance($user1->id);
139         $user2 = $this->getDataGenerator()->create_user(array('picture'=>0, 'email'=>'user2@example.com'));
140         $context2 = context_user::instance($user2->id);
142         $user3 = $this->getDataGenerator()->create_user(array('picture'=>1, 'deleted'=>1, 'email'=>'user3@example.com'));
143         $context3 = context_user::instance($user3->id, IGNORE_MISSING);
144         $this->assertEquals($user3->picture, 0);
145         $this->assertNotEquals($user3->email, 'user3@example.com');
146         $this->assertFalse($context3);
148         // try legacy picture == 1
149         $user1->picture = 1;
150         $up1 = new user_picture($user1);
151         $this->assertEquals($CFG->wwwroot.'/pluginfile.php/15/user/icon/standard/f2?rev=1', $up1->get_url($page, $renderer)->out(false));
152         $user1->picture = 11;
154         // try valid user with picture when user context is not cached - 1 query expected
155         context_helper::reset_caches();
156         $reads = $DB->perf_get_reads();
157         $up1 = new user_picture($user1);
158         $this->assertEquals($reads, $DB->perf_get_reads());
159         $this->assertEquals($CFG->wwwroot.'/pluginfile.php/15/user/icon/standard/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
160         $this->assertEquals($reads+1, $DB->perf_get_reads());
162         // try valid user with contextid hint - no queries expected
163         $user1->contextid = $context1->id;
164         context_helper::reset_caches();
165         $reads = $DB->perf_get_reads();
166         $up1 = new user_picture($user1);
167         $this->assertEquals($reads, $DB->perf_get_reads());
168         $this->assertEquals($CFG->wwwroot.'/pluginfile.php/15/user/icon/standard/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
169         $this->assertEquals($reads, $DB->perf_get_reads());
171         // try valid user without image - no queries expected
172         context_helper::reset_caches();
173         $reads = $DB->perf_get_reads();
174         $up2 = new user_picture($user2);
175         $this->assertEquals($reads, $DB->perf_get_reads());
176         $this->assertEquals($CFG->wwwroot.'/theme/image.php/standard/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
177         $this->assertEquals($reads, $DB->perf_get_reads());
179         // try guessing of deleted users - no queries expected
180         unset($user3->deleted);
181         context_helper::reset_caches();
182         $reads = $DB->perf_get_reads();
183         $up3 = new user_picture($user3);
184         $this->assertEquals($reads, $DB->perf_get_reads());
185         $this->assertEquals($CFG->wwwroot.'/theme/image.php/standard/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
186         $this->assertEquals($reads, $DB->perf_get_reads());
188         // try incorrectly deleted users (with valid email and pciture flag) - some DB reads expected
189         $user3->email = 'user3@example.com';
190         $user3->picture = 1;
191         $reads = $DB->perf_get_reads();
192         $up3 = new user_picture($user3);
193         $this->assertEquals($reads, $DB->perf_get_reads());
194         $this->assertEquals($CFG->wwwroot.'/theme/image.php/standard/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
195         $this->assertTrue($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->assertEquals($CFG->wwwroot.'/theme/image.php/standard/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
207         // verify defaults to misteryman (mm)
208         $up2 = new user_picture($user2);
209         $this->assertEquals('http://www.gravatar.com/avatar/ab53a2911ddf9b4817ac01ddcd3d975f?s=35&d=mm', $up2->get_url($page, $renderer)->out(false));
211         // without gravatardefaulturl, verify we pick own file
212         set_config('gravatardefaulturl', '');
213         $up2 = new user_picture($user2);
214         $this->assertEquals('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));
215         // uploaded image takes precedence before gravatar
216         $up1 = new user_picture($user1);
217         $this->assertEquals($CFG->wwwroot.'/pluginfile.php/15/user/icon/standard/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
219         // https version
220         $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
222         $up1 = new user_picture($user1);
223         $this->assertEquals($CFG->httpswwwroot.'/pluginfile.php/15/user/icon/standard/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
225         $up3 = new user_picture($user3);
226         $this->assertEquals($CFG->httpswwwroot.'/theme/image.php/standard/core/1/u/f2', $up3->get_url($page, $renderer)->out(false));
228         $up2 = new user_picture($user2);
229         $this->assertEquals('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));
231         // now test gravatar with one theme having own images (afterburner)
232         $CFG->httpswwwroot = $CFG->wwwroot;
233         $this->assertTrue(file_exists("$CFG->dirroot/theme/afterburner/config.php"));
234         set_config('theme', 'afterburner');
235         $page = new moodle_page();
236         $page->set_url('/user/profile.php');
237         $page->set_context(context_system::instance());
238         $renderer = $page->get_renderer('core');
240         $up2 = new user_picture($user2);
241         $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));
243         // https version
244         $CFG->httpswwwroot = str_replace('http:', 'https:', $CFG->wwwroot);
246         $up2 = new user_picture($user2);
247         $this->assertEquals('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));
248         // end of gravatar tests
250         // test themed images
251         set_config('enablegravatar', 0);
252         $this->assertTrue(file_exists("$CFG->dirroot/theme/formal_white/config.php")); // use any other theme
253         set_config('theme', 'formal_white');
254         $CFG->httpswwwroot = $CFG->wwwroot;
255         $page = new moodle_page();
256         $page->set_url('/user/profile.php');
257         $page->set_context(context_system::instance());
258         $renderer = $page->get_renderer('core');
260         $up1 = new user_picture($user1);
261         $this->assertEquals($CFG->wwwroot.'/pluginfile.php/15/user/icon/formal_white/f2?rev=11', $up1->get_url($page, $renderer)->out(false));
263         $up2 = new user_picture($user2);
264         $this->assertEquals($CFG->wwwroot.'/theme/image.php/formal_white/core/1/u/f2', $up2->get_url($page, $renderer)->out(false));
266         // test non-slashargument images
267         set_config('theme', 'standard');
268         $CFG->httpswwwroot = $CFG->wwwroot;
269         $CFG->slasharguments = 0;
270         $page = new moodle_page();
271         $page->set_url('/user/profile.php');
272         $page->set_context(context_system::instance());
273         $renderer = $page->get_renderer('core');
275         $up3 = new user_picture($user3);
276         $this->assertEquals($CFG->wwwroot.'/theme/image.php?theme=standard&component=core&rev=1&image=u%2Ff2', $up3->get_url($page, $renderer)->out(false));
277     }
281 /**
282  * Unit tests for the custom_menu class
283  */
284 class custom_menu_testcase extends basic_testcase {
286     public function test_empty_menu() {
287         $emptymenu = new custom_menu();
288         $this->assertTrue($emptymenu instanceof custom_menu);
289         $this->assertFalse($emptymenu->has_children());
290     }
292     public function test_basic_syntax() {
293         $definition = <<<EOF
294 Moodle community|http://moodle.org
295 -Moodle free support|http://moodle.org/support
296 -Moodle development|http://moodle.org/development
297 --Moodle Tracker|http://tracker.moodle.org
298 --Moodle Docs|http://docs.moodle.org
299 -Moodle News|http://moodle.org/news
300 Moodle company
301 -Hosting|http://moodle.com/hosting|Commercial hosting
302 -Support|http://moodle.com/support|Commercial support
303 EOF;
305         $menu = new custom_menu($definition);
306         $this->assertTrue($menu instanceof custom_menu);
307         $this->assertTrue($menu->has_children());
308         $firstlevel = $menu->get_children();
309         $this->assertTrue(is_array($firstlevel));
310         $this->assertEquals(2, count($firstlevel));
312         $item = array_shift($firstlevel);
313         $this->assertTrue($item instanceof custom_menu_item);
314         $this->assertTrue($item->has_children());
315         $this->assertEquals(3, count($item->get_children()));
316         $this->assertEquals('Moodle community', $item->get_text());
317         $itemurl = $item->get_url();
318         $this->assertTrue($itemurl instanceof moodle_url);
319         $this->assertEquals('http://moodle.org', $itemurl->out());
320         $this->assertEquals($item->get_text(), $item->get_title()); // implicit title
322         $item = array_shift($firstlevel);
323         $this->assertTrue($item->has_children());
324         $this->assertEquals(2, count($item->get_children()));
325         $this->assertEquals('Moodle company', $item->get_text());
326         $this->assertTrue(is_null($item->get_url()));
328         $children = $item->get_children();
329         $subitem = array_shift($children);
330         $this->assertFalse($subitem->has_children());
331         $this->assertEquals('Hosting', $subitem->get_text());
332         $this->assertEquals('Commercial hosting', $subitem->get_title());
333     }
335     public function test_multilang_support() {
336         $definition = <<<EOF
337 Start|http://school.info
338 Info
339 -English|http://school.info/en|Information in English|en
340 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
341 EOF;
343         // the menu without multilang support
344         $menu = new custom_menu($definition);
345         $this->assertTrue($menu->has_children());
346         $this->assertEquals(2, count($menu->get_children()));
348         $children = $menu->get_children();
349         $infomenu = array_pop($children);
350         $this->assertTrue($infomenu->has_children());
351         $children = $infomenu->get_children();
352         $this->assertEquals(2, count($children));
354         $children = $infomenu->get_children();
355         $langspecinfo = array_shift($children);
356         $this->assertEquals('Information in English', $langspecinfo->get_title());
358         // same menu for English language selected
359         $menu = new custom_menu($definition, 'en');
360         $this->assertTrue($menu->has_children());
361         $this->assertEquals(2, count($menu->get_children()));
363         $children = $menu->get_children();
364         $infomenu = array_pop($children);
365         $this->assertTrue($infomenu->has_children());
366         $this->assertEquals(1, count($infomenu->get_children()));
368         $children = $infomenu->get_children();
369         $langspecinfo = array_shift($children);
370         $this->assertEquals('Information in English', $langspecinfo->get_title());
372         // same menu for German (de_du) language selected
373         $menu = new custom_menu($definition, 'de_du');
374         $this->assertTrue($menu->has_children());
375         $this->assertEquals(2, count($menu->get_children()));
377         $children = $menu->get_children();
378         $infomenu = array_pop($children);
379         $this->assertTrue($infomenu->has_children());
380         $this->assertEquals(1, count($infomenu->get_children()));
382         $children = $infomenu->get_children();
383         $langspecinfo = array_shift($children);
384         $this->assertEquals('Informationen in deutscher Sprache', $langspecinfo->get_title());
386         // same menu for Czech language selected
387         $menu = new custom_menu($definition, 'cs');
388         $this->assertTrue($menu->has_children());
389         $this->assertEquals(2, count($menu->get_children()));
391         $children = $infomenu->get_children();
392         $infomenu = array_pop( $children);
393         $this->assertFalse($infomenu->has_children());
394     }