MDL-36206 - Unit Test - Create unit test for pagination bar
[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 outputcomponents_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/'.$context1->id.'/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/'.$context1->id.'/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/'.$context1->id.'/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/'.$context1->id.'/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/'.$context1->id.'/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/'.$context1->id.'/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     }
279     public function test_empty_menu() {
280         $emptymenu = new custom_menu();
281         $this->assertTrue($emptymenu instanceof custom_menu);
282         $this->assertFalse($emptymenu->has_children());
283     }
285     public function test_basic_syntax() {
286         $definition = <<<EOF
287 Moodle community|http://moodle.org
288 -Moodle free support|http://moodle.org/support
289 -Moodle development|http://moodle.org/development
290 --Moodle Tracker|http://tracker.moodle.org
291 --Moodle Docs|http://docs.moodle.org
292 -Moodle News|http://moodle.org/news
293 Moodle company
294 -Hosting|http://moodle.com/hosting|Commercial hosting
295 -Support|http://moodle.com/support|Commercial support
296 EOF;
298         $menu = new custom_menu($definition);
299         $this->assertTrue($menu instanceof custom_menu);
300         $this->assertTrue($menu->has_children());
301         $firstlevel = $menu->get_children();
302         $this->assertTrue(is_array($firstlevel));
303         $this->assertEquals(2, count($firstlevel));
305         $item = array_shift($firstlevel);
306         $this->assertTrue($item instanceof custom_menu_item);
307         $this->assertTrue($item->has_children());
308         $this->assertEquals(3, count($item->get_children()));
309         $this->assertEquals('Moodle community', $item->get_text());
310         $itemurl = $item->get_url();
311         $this->assertTrue($itemurl instanceof moodle_url);
312         $this->assertEquals('http://moodle.org', $itemurl->out());
313         $this->assertEquals($item->get_text(), $item->get_title()); // implicit title
315         $item = array_shift($firstlevel);
316         $this->assertTrue($item->has_children());
317         $this->assertEquals(2, count($item->get_children()));
318         $this->assertEquals('Moodle company', $item->get_text());
319         $this->assertTrue(is_null($item->get_url()));
321         $children = $item->get_children();
322         $subitem = array_shift($children);
323         $this->assertFalse($subitem->has_children());
324         $this->assertEquals('Hosting', $subitem->get_text());
325         $this->assertEquals('Commercial hosting', $subitem->get_title());
326     }
328     public function test_multilang_support() {
329         $definition = <<<EOF
330 Start|http://school.info
331 Info
332 -English|http://school.info/en|Information in English|en
333 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids
334 EOF;
336         // the menu without multilang support
337         $menu = new custom_menu($definition);
338         $this->assertTrue($menu->has_children());
339         $this->assertEquals(2, count($menu->get_children()));
341         $children = $menu->get_children();
342         $infomenu = array_pop($children);
343         $this->assertTrue($infomenu->has_children());
344         $children = $infomenu->get_children();
345         $this->assertEquals(2, count($children));
347         $children = $infomenu->get_children();
348         $langspecinfo = array_shift($children);
349         $this->assertEquals('Information in English', $langspecinfo->get_title());
351         // same menu for English language selected
352         $menu = new custom_menu($definition, 'en');
353         $this->assertTrue($menu->has_children());
354         $this->assertEquals(2, count($menu->get_children()));
356         $children = $menu->get_children();
357         $infomenu = array_pop($children);
358         $this->assertTrue($infomenu->has_children());
359         $this->assertEquals(1, count($infomenu->get_children()));
361         $children = $infomenu->get_children();
362         $langspecinfo = array_shift($children);
363         $this->assertEquals('Information in English', $langspecinfo->get_title());
365         // same menu for German (de_du) language selected
366         $menu = new custom_menu($definition, 'de_du');
367         $this->assertTrue($menu->has_children());
368         $this->assertEquals(2, count($menu->get_children()));
370         $children = $menu->get_children();
371         $infomenu = array_pop($children);
372         $this->assertTrue($infomenu->has_children());
373         $this->assertEquals(1, count($infomenu->get_children()));
375         $children = $infomenu->get_children();
376         $langspecinfo = array_shift($children);
377         $this->assertEquals('Informationen in deutscher Sprache', $langspecinfo->get_title());
379         // same menu for Czech language selected
380         $menu = new custom_menu($definition, 'cs');
381         $this->assertTrue($menu->has_children());
382         $this->assertEquals(2, count($menu->get_children()));
384         $children = $infomenu->get_children();
385         $infomenu = array_pop( $children);
386         $this->assertFalse($infomenu->has_children());
387     }
388     
389     public function test_prepare() {
390         $expecteda = array('1',
391                            '<a href="index.php?page=1">2</a>',
392                            '<a href="index.php?page=2">3</a>',
393                            '<a href="index.php?page=3">4</a>',
394                            '<a href="index.php?page=4">5</a>',
395                            '<a href="index.php?page=5">6</a>',
396                            '<a href="index.php?page=6">7</a>',
397                            '<a href="index.php?page=7">8</a>',
398                            );
399         $expectedb = array('<a href="page?page=3">4</a>',
400                            '<a href="page?page=4">5</a>',
401                            '6',
402                            '<a href="page?page=6">7</a>',
403                            '<a href="page?page=7">8</a>',
404                            );
406         $mpage = new moodle_page();
407         $rbase = new renderer_base($mpage, "/");
408         $pbara = new paging_bar(40, 0, 5, 'index.php');
409         $pbara->prepare($rbase,$mpage, "/");
410         $pbarb = new paging_bar(100, 5, 5, 'page');
411         $pbarb->maxdisplay = 5;
412         $pbarb->prepare($rbase,$mpage,"/");
414         $this->assertEquals($pbara->pagelinks, $expecteda);
415         $this->assertEquals($pbarb->pagelinks, $expectedb);
416     }