[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
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/>. 16 17 /** 18 * Unit tests for setuplib.php 19 * 20 * @package core 21 * @category phpunit 22 * @copyright 2012 The Open University 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 29 /** 30 * Unit tests for setuplib.php 31 * 32 * @copyright 2012 The Open University 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class core_setuplib_testcase extends advanced_testcase { 36 37 /** 38 * Test get_docs_url_standard in the normal case when we should link to Moodle docs. 39 */ 40 public function test_get_docs_url_standard() { 41 global $CFG; 42 if (empty($CFG->docroot)) { 43 $docroot = 'http://docs.moodle.org/'; 44 } else { 45 $docroot = $CFG->docroot; 46 } 47 $this->assertRegExp('~^' . preg_quote($docroot, '') . '/2\d/' . current_language() . '/course/editing$~', 48 get_docs_url('course/editing')); 49 } 50 51 /** 52 * Test get_docs_url_standard in the special case of an absolute HTTP URL. 53 */ 54 public function test_get_docs_url_http() { 55 $url = 'http://moodle.org/'; 56 $this->assertEquals($url, get_docs_url($url)); 57 } 58 59 /** 60 * Test get_docs_url_standard in the special case of an absolute HTTPS URL. 61 */ 62 public function test_get_docs_url_https() { 63 $url = 'https://moodle.org/'; 64 $this->assertEquals($url, get_docs_url($url)); 65 } 66 67 /** 68 * Test get_docs_url_standard in the special case of a link relative to wwwroot. 69 */ 70 public function test_get_docs_url_wwwroot() { 71 global $CFG; 72 $this->assertSame($CFG->wwwroot . '/lib/tests/setuplib_test.php', 73 get_docs_url('%%WWWROOT%%/lib/tests/setuplib_test.php')); 74 } 75 76 public function test_is_web_crawler() { 77 $browsers = array( 78 'Mozilla/5.0 (Windows; U; MSIE 9.0; WIndows NT 9.0; en-US))', 79 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:18.0) Gecko/18.0 Firefox/18.0', 80 'Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/412 (KHTML, like Gecko) Safari/412', 81 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.215 Safari/534.10', 82 'Opera/9.0 (Windows NT 5.1; U; en)', 83 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 –Nexus', 84 'Mozilla/5.0 (iPad; U; CPU OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148 Safari/6533.18.5', 85 ); 86 $crawlers = array( 87 // Google. 88 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)', 89 'Googlebot/2.1 (+http://www.googlebot.com/bot.html)', 90 'Googlebot-Image/1.0', 91 // Yahoo. 92 'Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)', 93 // Bing. 94 'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)', 95 'Mozilla/5.0 (compatible; bingbot/2.0 +http://www.bing.com/bingbot.htm)', 96 // MSN. 97 'msnbot/2.1', 98 // Yandex. 99 'Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots)', 100 'Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots)', 101 // AltaVista. 102 'AltaVista V2.0B [email protected]', 103 // ZoomSpider. 104 'ZoomSpider - wrensoft.com [ZSEBOT]', 105 // Baidu. 106 'Baiduspider+(+http://www.baidu.com/search/spider_jp.html)', 107 'Baiduspider+(+http://www.baidu.com/search/spider.htm)', 108 'BaiDuSpider', 109 // Ask.com. 110 'User-Agent: Mozilla/2.0 (compatible; Ask Jeeves/Teoma)', 111 ); 112 113 foreach ($browsers as $agent) { 114 $_SERVER['HTTP_USER_AGENT'] = $agent; 115 $this->assertFalse(is_web_crawler()); 116 } 117 foreach ($crawlers as $agent) { 118 $_SERVER['HTTP_USER_AGENT'] = $agent; 119 $this->assertTrue(is_web_crawler(), "$agent should be considered a search engine"); 120 } 121 } 122 123 /** 124 * Test if get_exception_info() removes file system paths. 125 */ 126 public function test_exception_info_removes_serverpaths() { 127 global $CFG; 128 129 // This doesn't test them all possible ones, but these are set for unit tests. 130 $cfgnames = array('dataroot', 'dirroot', 'tempdir', 'cachedir', 'localcachedir'); 131 132 $fixture = ''; 133 $expected = ''; 134 foreach ($cfgnames as $cfgname) { 135 if (!empty($CFG->$cfgname)) { 136 $fixture .= $CFG->$cfgname.' '; 137 $expected .= "[$cfgname] "; 138 } 139 } 140 $exception = new moodle_exception('generalexceptionmessage', 'error', '', $fixture, $fixture); 141 $exceptioninfo = get_exception_info($exception); 142 143 $this->assertContains($expected, $exceptioninfo->message, 'Exception message does not contain system paths'); 144 $this->assertContains($expected, $exceptioninfo->debuginfo, 'Exception debug info does not contain system paths'); 145 } 146 147 public function test_localcachedir() { 148 global $CFG; 149 150 $this->resetAfterTest(true); 151 152 // Test default location - can not be modified in phpunit tests because we override everything in config.php. 153 $this->assertSame("$CFG->dataroot/localcache", $CFG->localcachedir); 154 155 $this->setCurrentTimeStart(); 156 $timestampfile = "$CFG->localcachedir/.lastpurged"; 157 158 // Delete existing localcache directory, as this is testing first call 159 // to make_localcache_directory. 160 remove_dir($CFG->localcachedir, true); 161 $dir = make_localcache_directory('', false); 162 $this->assertSame($CFG->localcachedir, $dir); 163 $this->assertFileNotExists("$CFG->localcachedir/.htaccess"); 164 $this->assertFileExists($timestampfile); 165 $this->assertTimeCurrent(filemtime($timestampfile)); 166 167 $dir = make_localcache_directory('test/test', false); 168 $this->assertSame("$CFG->localcachedir/test/test", $dir); 169 170 // Test custom location. 171 $CFG->localcachedir = "$CFG->dataroot/testlocalcache"; 172 $this->setCurrentTimeStart(); 173 $timestampfile = "$CFG->localcachedir/.lastpurged"; 174 $this->assertFileNotExists($timestampfile); 175 176 $dir = make_localcache_directory('', false); 177 $this->assertSame($CFG->localcachedir, $dir); 178 $this->assertFileExists("$CFG->localcachedir/.htaccess"); 179 $this->assertFileExists($timestampfile); 180 $this->assertTimeCurrent(filemtime($timestampfile)); 181 182 $dir = make_localcache_directory('test', false); 183 $this->assertSame("$CFG->localcachedir/test", $dir); 184 185 $prevtime = filemtime($timestampfile); 186 $dir = make_localcache_directory('pokus', false); 187 $this->assertSame("$CFG->localcachedir/pokus", $dir); 188 $this->assertSame($prevtime, filemtime($timestampfile)); 189 190 // Test purging. 191 $testfile = "$CFG->localcachedir/test/test.txt"; 192 $this->assertTrue(touch($testfile)); 193 194 $now = $this->setCurrentTimeStart(); 195 set_config('localcachedirpurged', $now - 2); 196 purge_all_caches(); 197 $this->assertFileNotExists($testfile); 198 $this->assertFileNotExists(dirname($testfile)); 199 $this->assertFileExists($timestampfile); 200 $this->assertTimeCurrent(filemtime($timestampfile)); 201 $this->assertTimeCurrent($CFG->localcachedirpurged); 202 203 // Simulates purge_all_caches() on another server node. 204 make_localcache_directory('test', false); 205 $this->assertTrue(touch($testfile)); 206 set_config('localcachedirpurged', $now - 1); 207 $this->assertTrue(touch($timestampfile, $now - 2)); 208 clearstatcache(); 209 $this->assertSame($now - 2, filemtime($timestampfile)); 210 211 $this->setCurrentTimeStart(); 212 $dir = make_localcache_directory('', false); 213 $this->assertSame("$CFG->localcachedir", $dir); 214 $this->assertFileNotExists($testfile); 215 $this->assertFileNotExists(dirname($testfile)); 216 $this->assertFileExists($timestampfile); 217 $this->assertTimeCurrent(filemtime($timestampfile)); 218 } 219 220 public function test_merge_query_params() { 221 $original = array( 222 'id' => '1', 223 'course' => '2', 224 'action' => 'delete', 225 'grade' => array( 226 0 => 'a', 227 1 => 'b', 228 2 => 'c', 229 ), 230 'items' => array( 231 'a' => 'aa', 232 'b' => 'bb', 233 ), 234 'mix' => array( 235 0 => '2', 236 ), 237 'numerical' => array( 238 '2' => array('a' => 'b'), 239 '1' => '2', 240 ), 241 ); 242 243 $chunk = array( 244 'numerical' => array( 245 '0' => 'z', 246 '2' => array('d' => 'e'), 247 ), 248 'action' => 'create', 249 'next' => '2', 250 'grade' => array( 251 0 => 'e', 252 1 => 'f', 253 2 => 'g', 254 ), 255 'mix' => 'mix', 256 ); 257 258 $expected = array( 259 'id' => '1', 260 'course' => '2', 261 'action' => 'create', 262 'grade' => array( 263 0 => 'a', 264 1 => 'b', 265 2 => 'c', 266 3 => 'e', 267 4 => 'f', 268 5 => 'g', 269 ), 270 'items' => array( 271 'a' => 'aa', 272 'b' => 'bb', 273 ), 274 'mix' => 'mix', 275 'numerical' => array( 276 '2' => array('a' => 'b', 'd' => 'e'), 277 '1' => '2', 278 '0' => 'z', 279 ), 280 'next' => '2', 281 ); 282 283 $array = $original; 284 merge_query_params($array, $chunk); 285 286 $this->assertSame($expected, $array); 287 $this->assertNotSame($original, $array); 288 289 $query = "id=1&course=2&action=create&grade%5B%5D=a&grade%5B%5D=b&grade%5B%5D=c&grade%5B%5D=e&grade%5B%5D=f&grade%5B%5D=g&items%5Ba%5D=aa&items%5Bb%5D=bb&mix=mix&numerical%5B2%5D%5Ba%5D=b&numerical%5B2%5D%5Bd%5D=e&numerical%5B1%5D=2&numerical%5B0%5D=z&next=2"; 290 $decoded = array(); 291 parse_str($query, $decoded); 292 $this->assertSame($expected, $decoded); 293 294 // Prove that we cannot use array_merge_recursive() instead. 295 $this->assertNotSame($expected, array_merge_recursive($original, $chunk)); 296 } 297 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |