|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00003 // This file is part of Moodle - http://moodle.org/ 00004 // 00005 // Moodle is free software: you can redistribute it and/or modify 00006 // it under the terms of the GNU General Public License as published by 00007 // the Free Software Foundation, either version 3 of the License, or 00008 // (at your option) any later version. 00009 // 00010 // Moodle is distributed in the hope that it will be useful, 00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 // GNU General Public License for more details. 00014 // 00015 // You should have received a copy of the GNU General Public License 00016 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 00017 00026 defined('MOODLE_INTERNAL') || die(); 00027 require_once($CFG->libdir . '/outputcomponents.php'); 00028 00032 class user_picture_test extends UnitTestCase { 00033 00034 public static $includecoverage = array('lib/outputcomponents.php'); 00035 00036 public function test_user_picture_fields_aliasing() { 00037 $fields = user_picture::fields(); 00038 $fields = array_map('trim', explode(',', $fields)); 00039 $this->assertTrue(in_array('id', $fields)); 00040 00041 $aliased = array(); 00042 foreach ($fields as $field) { 00043 if ($field === 'id') { 00044 $aliased['id'] = 'aliasedid'; 00045 } else { 00046 $aliased[$field] = 'prefix'.$field; 00047 } 00048 } 00049 00050 $returned = user_picture::fields('', array('custom1', 'id'), 'aliasedid', 'prefix'); 00051 $returned = array_map('trim', explode(',', $returned)); 00052 $this->assertEqual(count($returned), count($fields) + 1); // only one extra field added 00053 00054 foreach ($fields as $field) { 00055 if ($field === 'id') { 00056 $expected = "id AS aliasedid"; 00057 } else { 00058 $expected = "$field AS prefix$field"; 00059 } 00060 $this->assertTrue(in_array($expected, $returned), "Expected pattern '$expected' not returned"); 00061 } 00062 $this->assertTrue(in_array("custom1 AS prefixcustom1", $returned), "Expected pattern 'custom1 AS prefixcustom1' not returned"); 00063 } 00064 00065 public function test_user_picture_fields_unaliasing() { 00066 $fields = user_picture::fields(); 00067 $fields = array_map('trim', explode(',', $fields)); 00068 00069 $fakerecord = new stdClass(); 00070 $fakerecord->aliasedid = 42; 00071 foreach ($fields as $field) { 00072 if ($field !== 'id') { 00073 $fakerecord->{'prefix'.$field} = "Value of $field"; 00074 } 00075 } 00076 $fakerecord->prefixcustom1 = 'Value of custom1'; 00077 00078 $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix'); 00079 00080 $this->assertEqual($returned->id, 42); 00081 foreach ($fields as $field) { 00082 if ($field !== 'id') { 00083 $this->assertEqual($returned->{$field}, "Value of $field"); 00084 } 00085 } 00086 $this->assertEqual($returned->custom1, 'Value of custom1'); 00087 } 00088 00089 public function test_user_picture_fields_unaliasing_null() { 00090 $fields = user_picture::fields(); 00091 $fields = array_map('trim', explode(',', $fields)); 00092 00093 $fakerecord = new stdClass(); 00094 $fakerecord->aliasedid = 42; 00095 foreach ($fields as $field) { 00096 if ($field !== 'id') { 00097 $fakerecord->{'prefix'.$field} = "Value of $field"; 00098 } 00099 } 00100 $fakerecord->prefixcustom1 = 'Value of custom1'; 00101 $fakerecord->prefiximagealt = null; 00102 00103 $returned = user_picture::unalias($fakerecord, array('custom1'), 'aliasedid', 'prefix'); 00104 00105 $this->assertEqual($returned->id, 42); 00106 $this->assertEqual($returned->imagealt, null); 00107 foreach ($fields as $field) { 00108 if ($field !== 'id' and $field !== 'imagealt') { 00109 $this->assertEqual($returned->{$field}, "Value of $field"); 00110 } 00111 } 00112 $this->assertEqual($returned->custom1, 'Value of custom1'); 00113 } 00114 } 00115 00116 00120 class custom_menu_test extends UnitTestCase { 00121 00122 public function test_empty_menu() { 00123 $emptymenu = new custom_menu(); 00124 $this->assertTrue($emptymenu instanceof custom_menu); 00125 $this->assertFalse($emptymenu->has_children()); 00126 } 00127 00128 public function test_basic_syntax() { 00129 $definition = <<<EOF 00130 Moodle community|http://moodle.org 00131 -Moodle free support|http://moodle.org/support 00132 -Moodle development|http://moodle.org/development 00133 --Moodle Tracker|http://tracker.moodle.org 00134 --Moodle Docs|http://docs.moodle.org 00135 -Moodle News|http://moodle.org/news 00136 Moodle company 00137 -Hosting|http://moodle.com/hosting|Commercial hosting 00138 -Support|http://moodle.com/support|Commercial support 00139 EOF; 00140 00141 $menu = new custom_menu($definition); 00142 $this->assertTrue($menu instanceof custom_menu); 00143 $this->assertTrue($menu->has_children()); 00144 $firstlevel = $menu->get_children(); 00145 $this->assertIsA($firstlevel, 'array'); 00146 $this->assertEqual(2, count($firstlevel)); 00147 00148 $item = array_shift($firstlevel); 00149 $this->assertTrue($item instanceof custom_menu_item); 00150 $this->assertTrue($item->has_children()); 00151 $this->assertEqual(3, count($item->get_children())); 00152 $this->assertEqual('Moodle community', $item->get_text()); 00153 $itemurl = $item->get_url(); 00154 $this->assertTrue($itemurl instanceof moodle_url); 00155 $this->assertEqual('http://moodle.org', $itemurl->out()); 00156 $this->assertEqual($item->get_text(), $item->get_title()); // implicit title 00157 00158 $item = array_shift($firstlevel); 00159 $this->assertTrue($item->has_children()); 00160 $this->assertEqual(2, count($item->get_children())); 00161 $this->assertEqual('Moodle company', $item->get_text()); 00162 $this->assertTrue(is_null($item->get_url())); 00163 00164 $subitem = array_shift($item->get_children()); 00165 $this->assertFalse($subitem->has_children()); 00166 $this->assertEqual('Hosting', $subitem->get_text()); 00167 $this->assertEqual('Commercial hosting', $subitem->get_title()); 00168 } 00169 00170 public function test_multilang_support() { 00171 $definition = <<<EOF 00172 Start|http://school.info 00173 Info 00174 -English|http://school.info/en|Information in English|en 00175 -Deutsch|http://school.info/de|Informationen in deutscher Sprache|de,de_du,de_kids 00176 EOF; 00177 00178 // the menu without multilang support 00179 $menu = new custom_menu($definition); 00180 $this->assertTrue($menu->has_children()); 00181 $this->assertEqual(2, count($menu->get_children())); 00182 00183 $infomenu = array_pop($menu->get_children()); 00184 $this->assertTrue($infomenu->has_children()); 00185 $this->assertEqual(2, count($infomenu->get_children())); 00186 00187 $langspecinfo = array_shift($infomenu->get_children()); 00188 $this->assertEqual('Information in English', $langspecinfo->get_title()); 00189 00190 // same menu for English language selected 00191 $menu = new custom_menu($definition, 'en'); 00192 $this->assertTrue($menu->has_children()); 00193 $this->assertEqual(2, count($menu->get_children())); 00194 00195 $infomenu = array_pop($menu->get_children()); 00196 $this->assertTrue($infomenu->has_children()); 00197 $this->assertEqual(1, count($infomenu->get_children())); 00198 00199 $langspecinfo = array_shift($infomenu->get_children()); 00200 $this->assertEqual('Information in English', $langspecinfo->get_title()); 00201 00202 // same menu for German (de_du) language selected 00203 $menu = new custom_menu($definition, 'de_du'); 00204 $this->assertTrue($menu->has_children()); 00205 $this->assertEqual(2, count($menu->get_children())); 00206 00207 $infomenu = array_pop($menu->get_children()); 00208 $this->assertTrue($infomenu->has_children()); 00209 $this->assertEqual(1, count($infomenu->get_children())); 00210 00211 $langspecinfo = array_shift($infomenu->get_children()); 00212 $this->assertEqual('Informationen in deutscher Sprache', $langspecinfo->get_title()); 00213 00214 // same menu for Czech language selected 00215 $menu = new custom_menu($definition, 'cs'); 00216 $this->assertTrue($menu->has_children()); 00217 $this->assertEqual(2, count($menu->get_children())); 00218 00219 $infomenu = array_pop($menu->get_children()); 00220 $this->assertFalse($infomenu->has_children()); 00221 } 00222 }