[ 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 /lib/filelib.php. 19 * 20 * @package core_files 21 * @category phpunit 22 * @copyright 2009 Jerome Mouneyrac 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->libdir . '/filelib.php'); 30 require_once($CFG->dirroot . '/repository/lib.php'); 31 32 class core_filelib_testcase extends advanced_testcase { 33 public function test_format_postdata_for_curlcall() { 34 35 // POST params with just simple types. 36 $postdatatoconvert = array( 'userid' => 1, 'roleid' => 22, 'name' => 'john'); 37 $expectedresult = "userid=1&roleid=22&name=john"; 38 $postdata = format_postdata_for_curlcall($postdatatoconvert); 39 $this->assertEquals($expectedresult, $postdata); 40 41 // POST params with a string containing & character. 42 $postdatatoconvert = array( 'name' => 'john&emilie', 'roleid' => 22); 43 $expectedresult = "name=john%26emilie&roleid=22"; // Urlencode: '%26' => '&'. 44 $postdata = format_postdata_for_curlcall($postdatatoconvert); 45 $this->assertEquals($expectedresult, $postdata); 46 47 // POST params with an empty value. 48 $postdatatoconvert = array( 'name' => null, 'roleid' => 22); 49 $expectedresult = "name=&roleid=22"; 50 $postdata = format_postdata_for_curlcall($postdatatoconvert); 51 $this->assertEquals($expectedresult, $postdata); 52 53 // POST params with complex types. 54 $postdatatoconvert = array( 'users' => array( 55 array( 56 'id' => 2, 57 'customfields' => array( 58 array 59 ( 60 'type' => 'Color', 61 'value' => 'violet' 62 ) 63 ) 64 ) 65 ) 66 ); 67 $expectedresult = "users[0][id]=2&users[0][customfields][0][type]=Color&users[0][customfields][0][value]=violet"; 68 $postdata = format_postdata_for_curlcall($postdatatoconvert); 69 $this->assertEquals($expectedresult, $postdata); 70 71 // POST params with other complex types. 72 $postdatatoconvert = array ('members' => 73 array( 74 array('groupid' => 1, 'userid' => 1) 75 , array('groupid' => 1, 'userid' => 2) 76 ) 77 ); 78 $expectedresult = "members[0][groupid]=1&members[0][userid]=1&members[1][groupid]=1&members[1][userid]=2"; 79 $postdata = format_postdata_for_curlcall($postdatatoconvert); 80 $this->assertEquals($expectedresult, $postdata); 81 } 82 83 public function test_download_file_content() { 84 global $CFG; 85 86 // Test http success first. 87 $testhtml = $this->getExternalTestFileUrl('/test.html'); 88 89 $contents = download_file_content($testhtml); 90 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); 91 92 $tofile = "$CFG->tempdir/test.html"; 93 @unlink($tofile); 94 $result = download_file_content($testhtml, null, null, false, 300, 20, false, $tofile); 95 $this->assertTrue($result); 96 $this->assertFileExists($tofile); 97 $this->assertSame(file_get_contents($tofile), $contents); 98 @unlink($tofile); 99 100 $result = download_file_content($testhtml, null, null, false, 300, 20, false, null, true); 101 $this->assertSame($contents, $result); 102 103 $response = download_file_content($testhtml, null, null, true); 104 $this->assertInstanceOf('stdClass', $response); 105 $this->assertSame('200', $response->status); 106 $this->assertTrue(is_array($response->headers)); 107 $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code)); 108 $this->assertSame($contents, $response->results); 109 $this->assertSame('', $response->error); 110 111 // Test https success. 112 $testhtml = $this->getExternalTestFileUrl('/test.html', true); 113 114 $contents = download_file_content($testhtml, null, null, false, 300, 20, true); 115 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); 116 117 $contents = download_file_content($testhtml); 118 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); 119 120 // Now 404. 121 $testhtml = $this->getExternalTestFileUrl('/test.html_nonexistent'); 122 123 $contents = download_file_content($testhtml); 124 $this->assertFalse($contents); 125 $this->assertDebuggingCalled(); 126 127 $response = download_file_content($testhtml, null, null, true); 128 $this->assertInstanceOf('stdClass', $response); 129 $this->assertSame('404', $response->status); 130 $this->assertTrue(is_array($response->headers)); 131 $this->assertRegExp('|^HTTP/1\.[01] 404 Not Found$|', rtrim($response->response_code)); 132 // Do not test the response starts with DOCTYPE here because some servers may return different headers. 133 $this->assertSame('', $response->error); 134 135 // Invalid url. 136 $testhtml = $this->getExternalTestFileUrl('/test.html'); 137 $testhtml = str_replace('http://', 'ftp://', $testhtml); 138 139 $contents = download_file_content($testhtml); 140 $this->assertFalse($contents); 141 142 // Test standard redirects. 143 $testurl = $this->getExternalTestFileUrl('/test_redir.php'); 144 145 $contents = download_file_content("$testurl?redir=2"); 146 $this->assertSame('done', $contents); 147 148 $response = download_file_content("$testurl?redir=2", null, null, true); 149 $this->assertInstanceOf('stdClass', $response); 150 $this->assertSame('200', $response->status); 151 $this->assertTrue(is_array($response->headers)); 152 $this->assertRegExp('|^HTTP/1\.[01] 200 OK$|', rtrim($response->response_code)); 153 $this->assertSame('done', $response->results); 154 $this->assertSame('', $response->error); 155 156 // Commented out this block if there are performance problems. 157 /* 158 $contents = download_file_content("$testurl?redir=6"); 159 $this->assertFalse(false, $contents); 160 $this->assertDebuggingCalled(); 161 $response = download_file_content("$testurl?redir=6", null, null, true); 162 $this->assertInstanceOf('stdClass', $response); 163 $this->assertSame('0', $response->status); 164 $this->assertTrue(is_array($response->headers)); 165 $this->assertFalse($response->results); 166 $this->assertNotEmpty($response->error); 167 */ 168 169 // Test relative redirects. 170 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php'); 171 172 $contents = download_file_content("$testurl"); 173 $this->assertSame('done', $contents); 174 175 $contents = download_file_content("$testurl?unused=xxx"); 176 $this->assertSame('done', $contents); 177 } 178 179 /** 180 * Test curl basics. 181 */ 182 public function test_curl_basics() { 183 global $CFG; 184 185 // Test HTTP success. 186 $testhtml = $this->getExternalTestFileUrl('/test.html'); 187 188 $curl = new curl(); 189 $contents = $curl->get($testhtml); 190 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); 191 $this->assertSame(0, $curl->get_errno()); 192 193 $curl = new curl(); 194 $tofile = "$CFG->tempdir/test.html"; 195 @unlink($tofile); 196 $fp = fopen($tofile, 'w'); 197 $result = $curl->get($testhtml, array(), array('CURLOPT_FILE'=>$fp)); 198 $this->assertTrue($result); 199 fclose($fp); 200 $this->assertFileExists($tofile); 201 $this->assertSame($contents, file_get_contents($tofile)); 202 @unlink($tofile); 203 204 $curl = new curl(); 205 $tofile = "$CFG->tempdir/test.html"; 206 @unlink($tofile); 207 $result = $curl->download_one($testhtml, array(), array('filepath'=>$tofile)); 208 $this->assertTrue($result); 209 $this->assertFileExists($tofile); 210 $this->assertSame($contents, file_get_contents($tofile)); 211 @unlink($tofile); 212 213 // Test 404 request. 214 $curl = new curl(); 215 $contents = $curl->get($this->getExternalTestFileUrl('/i.do.not.exist')); 216 $response = $curl->getResponse(); 217 $this->assertSame('404 Not Found', reset($response)); 218 $this->assertSame(0, $curl->get_errno()); 219 } 220 221 public function test_curl_redirects() { 222 global $CFG; 223 224 // Test full URL redirects. 225 $testurl = $this->getExternalTestFileUrl('/test_redir.php'); 226 227 $curl = new curl(); 228 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2)); 229 $response = $curl->getResponse(); 230 $this->assertSame('200 OK', reset($response)); 231 $this->assertSame(0, $curl->get_errno()); 232 $this->assertSame(2, $curl->info['redirect_count']); 233 $this->assertSame('done', $contents); 234 235 $curl = new curl(); 236 $curl->emulateredirects = true; 237 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>2)); 238 $response = $curl->getResponse(); 239 $this->assertSame('200 OK', reset($response)); 240 $this->assertSame(0, $curl->get_errno()); 241 $this->assertSame(2, $curl->info['redirect_count']); 242 $this->assertSame('done', $contents); 243 244 // This test was failing for people behind Squid proxies. Squid does not 245 // fully support HTTP 1.1, so converts things to HTTP 1.0, where the name 246 // of the status code is different. 247 reset($response); 248 if (key($response) === 'HTTP/1.0') { 249 $responsecode302 = '302 Moved Temporarily'; 250 } else { 251 $responsecode302 = '302 Found'; 252 } 253 254 $curl = new curl(); 255 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0)); 256 $response = $curl->getResponse(); 257 $this->assertSame($responsecode302, reset($response)); 258 $this->assertSame(0, $curl->get_errno()); 259 $this->assertSame(302, $curl->info['http_code']); 260 $this->assertSame('', $contents); 261 262 $curl = new curl(); 263 $curl->emulateredirects = true; 264 $contents = $curl->get("$testurl?redir=3", array(), array('CURLOPT_FOLLOWLOCATION'=>0)); 265 $response = $curl->getResponse(); 266 $this->assertSame($responsecode302, reset($response)); 267 $this->assertSame(0, $curl->get_errno()); 268 $this->assertSame(302, $curl->info['http_code']); 269 $this->assertSame('', $contents); 270 271 $curl = new curl(); 272 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1)); 273 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno()); 274 $this->assertNotEmpty($contents); 275 276 $curl = new curl(); 277 $curl->emulateredirects = true; 278 $contents = $curl->get("$testurl?redir=2", array(), array('CURLOPT_MAXREDIRS'=>1)); 279 $this->assertSame(CURLE_TOO_MANY_REDIRECTS, $curl->get_errno()); 280 $this->assertNotEmpty($contents); 281 282 $curl = new curl(); 283 $tofile = "$CFG->tempdir/test.html"; 284 @unlink($tofile); 285 $fp = fopen($tofile, 'w'); 286 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp)); 287 $this->assertTrue($result); 288 fclose($fp); 289 $this->assertFileExists($tofile); 290 $this->assertSame('done', file_get_contents($tofile)); 291 @unlink($tofile); 292 293 $curl = new curl(); 294 $curl->emulateredirects = true; 295 $tofile = "$CFG->tempdir/test.html"; 296 @unlink($tofile); 297 $fp = fopen($tofile, 'w'); 298 $result = $curl->get("$testurl?redir=1", array(), array('CURLOPT_FILE'=>$fp)); 299 $this->assertTrue($result); 300 fclose($fp); 301 $this->assertFileExists($tofile); 302 $this->assertSame('done', file_get_contents($tofile)); 303 @unlink($tofile); 304 305 $curl = new curl(); 306 $tofile = "$CFG->tempdir/test.html"; 307 @unlink($tofile); 308 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile)); 309 $this->assertTrue($result); 310 $this->assertFileExists($tofile); 311 $this->assertSame('done', file_get_contents($tofile)); 312 @unlink($tofile); 313 314 $curl = new curl(); 315 $curl->emulateredirects = true; 316 $tofile = "$CFG->tempdir/test.html"; 317 @unlink($tofile); 318 $result = $curl->download_one("$testurl?redir=1", array(), array('filepath'=>$tofile)); 319 $this->assertTrue($result); 320 $this->assertFileExists($tofile); 321 $this->assertSame('done', file_get_contents($tofile)); 322 @unlink($tofile); 323 } 324 325 public function test_curl_relative_redirects() { 326 // Test relative location redirects. 327 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php'); 328 329 $curl = new curl(); 330 $contents = $curl->get($testurl); 331 $response = $curl->getResponse(); 332 $this->assertSame('200 OK', reset($response)); 333 $this->assertSame(0, $curl->get_errno()); 334 $this->assertSame(1, $curl->info['redirect_count']); 335 $this->assertSame('done', $contents); 336 337 $curl = new curl(); 338 $curl->emulateredirects = true; 339 $contents = $curl->get($testurl); 340 $response = $curl->getResponse(); 341 $this->assertSame('200 OK', reset($response)); 342 $this->assertSame(0, $curl->get_errno()); 343 $this->assertSame(1, $curl->info['redirect_count']); 344 $this->assertSame('done', $contents); 345 346 // Test different redirect types. 347 $testurl = $this->getExternalTestFileUrl('/test_relative_redir.php'); 348 349 $curl = new curl(); 350 $contents = $curl->get("$testurl?type=301"); 351 $response = $curl->getResponse(); 352 $this->assertSame('200 OK', reset($response)); 353 $this->assertSame(0, $curl->get_errno()); 354 $this->assertSame(1, $curl->info['redirect_count']); 355 $this->assertSame('done', $contents); 356 357 $curl = new curl(); 358 $curl->emulateredirects = true; 359 $contents = $curl->get("$testurl?type=301"); 360 $response = $curl->getResponse(); 361 $this->assertSame('200 OK', reset($response)); 362 $this->assertSame(0, $curl->get_errno()); 363 $this->assertSame(1, $curl->info['redirect_count']); 364 $this->assertSame('done', $contents); 365 366 $curl = new curl(); 367 $contents = $curl->get("$testurl?type=302"); 368 $response = $curl->getResponse(); 369 $this->assertSame('200 OK', reset($response)); 370 $this->assertSame(0, $curl->get_errno()); 371 $this->assertSame(1, $curl->info['redirect_count']); 372 $this->assertSame('done', $contents); 373 374 $curl = new curl(); 375 $curl->emulateredirects = true; 376 $contents = $curl->get("$testurl?type=302"); 377 $response = $curl->getResponse(); 378 $this->assertSame('200 OK', reset($response)); 379 $this->assertSame(0, $curl->get_errno()); 380 $this->assertSame(1, $curl->info['redirect_count']); 381 $this->assertSame('done', $contents); 382 383 $curl = new curl(); 384 $contents = $curl->get("$testurl?type=303"); 385 $response = $curl->getResponse(); 386 $this->assertSame('200 OK', reset($response)); 387 $this->assertSame(0, $curl->get_errno()); 388 $this->assertSame(1, $curl->info['redirect_count']); 389 $this->assertSame('done', $contents); 390 391 $curl = new curl(); 392 $curl->emulateredirects = true; 393 $contents = $curl->get("$testurl?type=303"); 394 $response = $curl->getResponse(); 395 $this->assertSame('200 OK', reset($response)); 396 $this->assertSame(0, $curl->get_errno()); 397 $this->assertSame(1, $curl->info['redirect_count']); 398 $this->assertSame('done', $contents); 399 400 $curl = new curl(); 401 $contents = $curl->get("$testurl?type=307"); 402 $response = $curl->getResponse(); 403 $this->assertSame('200 OK', reset($response)); 404 $this->assertSame(0, $curl->get_errno()); 405 $this->assertSame(1, $curl->info['redirect_count']); 406 $this->assertSame('done', $contents); 407 408 $curl = new curl(); 409 $curl->emulateredirects = true; 410 $contents = $curl->get("$testurl?type=307"); 411 $response = $curl->getResponse(); 412 $this->assertSame('200 OK', reset($response)); 413 $this->assertSame(0, $curl->get_errno()); 414 $this->assertSame(1, $curl->info['redirect_count']); 415 $this->assertSame('done', $contents); 416 417 $curl = new curl(); 418 $contents = $curl->get("$testurl?type=308"); 419 $response = $curl->getResponse(); 420 $this->assertSame('200 OK', reset($response)); 421 $this->assertSame(0, $curl->get_errno()); 422 $this->assertSame(1, $curl->info['redirect_count']); 423 $this->assertSame('done', $contents); 424 425 $curl = new curl(); 426 $curl->emulateredirects = true; 427 $contents = $curl->get("$testurl?type=308"); 428 $response = $curl->getResponse(); 429 $this->assertSame('200 OK', reset($response)); 430 $this->assertSame(0, $curl->get_errno()); 431 $this->assertSame(1, $curl->info['redirect_count']); 432 $this->assertSame('done', $contents); 433 434 } 435 436 public function test_curl_proxybypass() { 437 global $CFG; 438 $testurl = $this->getExternalTestFileUrl('/test.html'); 439 440 $oldproxy = $CFG->proxyhost; 441 $oldproxybypass = $CFG->proxybypass; 442 443 // Test without proxy bypass and inaccessible proxy. 444 $CFG->proxyhost = 'i.do.not.exist'; 445 $CFG->proxybypass = ''; 446 $curl = new curl(); 447 $contents = $curl->get($testurl); 448 $this->assertNotEquals(0, $curl->get_errno()); 449 $this->assertNotEquals('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); 450 451 // Test with proxy bypass. 452 $testurlhost = parse_url($testurl, PHP_URL_HOST); 453 $CFG->proxybypass = $testurlhost; 454 $curl = new curl(); 455 $contents = $curl->get($testurl); 456 $this->assertSame(0, $curl->get_errno()); 457 $this->assertSame('47250a973d1b88d9445f94db4ef2c97a', md5($contents)); 458 459 $CFG->proxyhost = $oldproxy; 460 $CFG->proxybypass = $oldproxybypass; 461 } 462 463 public function test_curl_post() { 464 $testurl = $this->getExternalTestFileUrl('/test_post.php'); 465 466 // Test post request. 467 $curl = new curl(); 468 $contents = $curl->post($testurl, 'data=moodletest'); 469 $response = $curl->getResponse(); 470 $this->assertSame('200 OK', reset($response)); 471 $this->assertSame(0, $curl->get_errno()); 472 $this->assertSame('OK', $contents); 473 474 // Test 100 requests. 475 $curl = new curl(); 476 $curl->setHeader('Expect: 100-continue'); 477 $contents = $curl->post($testurl, 'data=moodletest'); 478 $response = $curl->getResponse(); 479 $this->assertSame('200 OK', reset($response)); 480 $this->assertSame(0, $curl->get_errno()); 481 $this->assertSame('OK', $contents); 482 } 483 484 public function test_curl_file() { 485 $this->resetAfterTest(); 486 $testurl = $this->getExternalTestFileUrl('/test_file.php'); 487 488 $fs = get_file_storage(); 489 $filerecord = array( 490 'contextid' => context_system::instance()->id, 491 'component' => 'test', 492 'filearea' => 'curl_post', 493 'itemid' => 0, 494 'filepath' => '/', 495 'filename' => 'test.txt' 496 ); 497 $teststring = 'moodletest'; 498 $testfile = $fs->create_file_from_string($filerecord, $teststring); 499 500 // Test post with file. 501 $data = array('testfile' => $testfile); 502 $curl = new curl(); 503 $contents = $curl->post($testurl, $data); 504 $this->assertSame('OK', $contents); 505 } 506 507 /** 508 * Testing prepare draft area 509 * 510 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org} 511 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 512 */ 513 public function test_prepare_draft_area() { 514 global $USER, $DB; 515 516 $this->resetAfterTest(true); 517 518 $generator = $this->getDataGenerator(); 519 $user = $generator->create_user(); 520 $usercontext = context_user::instance($user->id); 521 $USER = $DB->get_record('user', array('id'=>$user->id)); 522 523 $repositorypluginname = 'user'; 524 525 $args = array(); 526 $args['type'] = $repositorypluginname; 527 $repos = repository::get_instances($args); 528 $userrepository = reset($repos); 529 $this->assertInstanceOf('repository', $userrepository); 530 531 $fs = get_file_storage(); 532 533 $syscontext = context_system::instance(); 534 $component = 'core'; 535 $filearea = 'unittest'; 536 $itemid = 0; 537 $filepath = '/'; 538 $filename = 'test.txt'; 539 $sourcefield = 'Copyright stuff'; 540 541 $filerecord = array( 542 'contextid' => $syscontext->id, 543 'component' => $component, 544 'filearea' => $filearea, 545 'itemid' => $itemid, 546 'filepath' => $filepath, 547 'filename' => $filename, 548 'source' => $sourcefield, 549 ); 550 $ref = $fs->pack_reference($filerecord); 551 $originalfile = $fs->create_file_from_string($filerecord, 'Test content'); 552 $fileid = $originalfile->get_id(); 553 $this->assertInstanceOf('stored_file', $originalfile); 554 555 // Create a user private file. 556 $userfilerecord = new stdClass; 557 $userfilerecord->contextid = $usercontext->id; 558 $userfilerecord->component = 'user'; 559 $userfilerecord->filearea = 'private'; 560 $userfilerecord->itemid = 0; 561 $userfilerecord->filepath = '/'; 562 $userfilerecord->filename = 'userfile.txt'; 563 $userfilerecord->source = 'test'; 564 $userfile = $fs->create_file_from_string($userfilerecord, 'User file content'); 565 $userfileref = $fs->pack_reference($userfilerecord); 566 567 $filerefrecord = clone((object)$filerecord); 568 $filerefrecord->filename = 'testref.txt'; 569 570 // Create a file reference. 571 $fileref = $fs->create_file_from_reference($filerefrecord, $userrepository->id, $userfileref); 572 $this->assertInstanceOf('stored_file', $fileref); 573 $this->assertEquals($userrepository->id, $fileref->get_repository_id()); 574 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash()); 575 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize()); 576 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details()); 577 578 $draftitemid = 0; 579 file_prepare_draft_area($draftitemid, $syscontext->id, $component, $filearea, $itemid); 580 581 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid); 582 $this->assertCount(3, $draftfiles); 583 584 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filename); 585 $source = unserialize($draftfile->get_source()); 586 $this->assertSame($ref, $source->original); 587 $this->assertSame($sourcefield, $source->source); 588 589 $draftfileref = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $filepath, $filerefrecord->filename); 590 $this->assertInstanceOf('stored_file', $draftfileref); 591 $this->assertTrue($draftfileref->is_external_file()); 592 593 // Change some information. 594 $author = 'Dongsheng Cai'; 595 $draftfile->set_author($author); 596 $newsourcefield = 'Get from Flickr'; 597 $license = 'GPLv3'; 598 $draftfile->set_license($license); 599 // If you want to really just change source field, do this. 600 $source = unserialize($draftfile->get_source()); 601 $newsourcefield = 'From flickr'; 602 $source->source = $newsourcefield; 603 $draftfile->set_source(serialize($source)); 604 605 // Save changed file. 606 file_save_draft_area_files($draftitemid, $syscontext->id, $component, $filearea, $itemid); 607 608 $file = $fs->get_file($syscontext->id, $component, $filearea, $itemid, $filepath, $filename); 609 610 // Make sure it's the original file id. 611 $this->assertEquals($fileid, $file->get_id()); 612 $this->assertInstanceOf('stored_file', $file); 613 $this->assertSame($author, $file->get_author()); 614 $this->assertSame($license, $file->get_license()); 615 $this->assertEquals($newsourcefield, $file->get_source()); 616 } 617 618 /** 619 * Testing deleting original files. 620 * 621 * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org} 622 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 623 */ 624 public function test_delete_original_file_from_draft() { 625 global $USER, $DB; 626 627 $this->resetAfterTest(true); 628 629 $generator = $this->getDataGenerator(); 630 $user = $generator->create_user(); 631 $usercontext = context_user::instance($user->id); 632 $USER = $DB->get_record('user', array('id'=>$user->id)); 633 634 $repositorypluginname = 'user'; 635 636 $args = array(); 637 $args['type'] = $repositorypluginname; 638 $repos = repository::get_instances($args); 639 $userrepository = reset($repos); 640 $this->assertInstanceOf('repository', $userrepository); 641 642 $fs = get_file_storage(); 643 $syscontext = context_system::instance(); 644 645 $filecontent = 'User file content'; 646 647 // Create a user private file. 648 $userfilerecord = new stdClass; 649 $userfilerecord->contextid = $usercontext->id; 650 $userfilerecord->component = 'user'; 651 $userfilerecord->filearea = 'private'; 652 $userfilerecord->itemid = 0; 653 $userfilerecord->filepath = '/'; 654 $userfilerecord->filename = 'userfile.txt'; 655 $userfilerecord->source = 'test'; 656 $userfile = $fs->create_file_from_string($userfilerecord, $filecontent); 657 $userfileref = $fs->pack_reference($userfilerecord); 658 $contenthash = $userfile->get_contenthash(); 659 660 $filerecord = array( 661 'contextid' => $syscontext->id, 662 'component' => 'core', 663 'filearea' => 'phpunit', 664 'itemid' => 0, 665 'filepath' => '/', 666 'filename' => 'test.txt', 667 ); 668 // Create a file reference. 669 $fileref = $fs->create_file_from_reference($filerecord, $userrepository->id, $userfileref); 670 $this->assertInstanceOf('stored_file', $fileref); 671 $this->assertEquals($userrepository->id, $fileref->get_repository_id()); 672 $this->assertSame($userfile->get_contenthash(), $fileref->get_contenthash()); 673 $this->assertEquals($userfile->get_filesize(), $fileref->get_filesize()); 674 $this->assertRegExp('#' . $userfile->get_filename(). '$#', $fileref->get_reference_details()); 675 676 $draftitemid = 0; 677 file_prepare_draft_area($draftitemid, $usercontext->id, 'user', 'private', 0); 678 $draftfiles = $fs->get_area_files($usercontext->id, 'user', 'draft', $draftitemid); 679 $this->assertCount(2, $draftfiles); 680 $draftfile = $fs->get_file($usercontext->id, 'user', 'draft', $draftitemid, $userfilerecord->filepath, $userfilerecord->filename); 681 $draftfile->delete(); 682 // Save changed file. 683 file_save_draft_area_files($draftitemid, $usercontext->id, 'user', 'private', 0); 684 685 // The file reference should be a regular moodle file now. 686 $fileref = $fs->get_file($syscontext->id, 'core', 'phpunit', 0, '/', 'test.txt'); 687 $this->assertFalse($fileref->is_external_file()); 688 $this->assertSame($contenthash, $fileref->get_contenthash()); 689 $this->assertEquals($filecontent, $fileref->get_content()); 690 } 691 692 /** 693 * Tests the strip_double_headers function in the curl class. 694 */ 695 public function test_curl_strip_double_headers() { 696 // Example from issue tracker. 697 $mdl30648example = <<<EOF 698 HTTP/1.0 407 Proxy Authentication Required 699 Server: squid/2.7.STABLE9 700 Date: Thu, 08 Dec 2011 14:44:33 GMT 701 Content-Type: text/html 702 Content-Length: 1275 703 X-Squid-Error: ERR_CACHE_ACCESS_DENIED 0 704 Proxy-Authenticate: Basic realm="Squid proxy-caching web server" 705 X-Cache: MISS from homer.lancs.ac.uk 706 X-Cache-Lookup: NONE from homer.lancs.ac.uk:3128 707 Via: 1.0 homer.lancs.ac.uk:3128 (squid/2.7.STABLE9) 708 Connection: close 709 710 HTTP/1.0 200 OK 711 Server: Apache 712 X-Lb-Nocache: true 713 Cache-Control: private, max-age=15, no-transform 714 ETag: "4d69af5d8ba873ea9192c489e151bd7b" 715 Content-Type: text/html 716 Date: Thu, 08 Dec 2011 14:44:53 GMT 717 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk 718 X-Cache-Action: MISS 719 X-Cache-Age: 0 720 Vary: Cookie,X-Country,X-Ip-is-uk-combined,X-Ip-is-advertise-combined,X-Ip_is_uk_combined,X-Ip_is_advertise_combined, X-GeoIP 721 X-Cache: MISS from ww 722 723 <html>... 724 EOF; 725 $mdl30648expected = <<<EOF 726 HTTP/1.0 200 OK 727 Server: Apache 728 X-Lb-Nocache: true 729 Cache-Control: private, max-age=15, no-transform 730 ETag: "4d69af5d8ba873ea9192c489e151bd7b" 731 Content-Type: text/html 732 Date: Thu, 08 Dec 2011 14:44:53 GMT 733 Set-Cookie: BBC-UID=c4de2e109c8df6a51de627cee11b214bd4fb6054a030222488317afb31b343360MoodleBot/1.0; expires=Mon, 07-Dec-15 14:44:53 GMT; path=/; domain=bbc.co.uk 734 X-Cache-Action: MISS 735 X-Cache-Age: 0 736 Vary: Cookie,X-Country,X-Ip-is-uk-combined,X-Ip-is-advertise-combined,X-Ip_is_uk_combined,X-Ip_is_advertise_combined, X-GeoIP 737 X-Cache: MISS from ww 738 739 <html>... 740 EOF; 741 // For HTTP, replace the \n with \r\n. 742 $mdl30648example = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648example); 743 $mdl30648expected = preg_replace("~(?!<\r)\n~", "\r\n", $mdl30648expected); 744 745 // Test stripping works OK. 746 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648example)); 747 // Test it does nothing to the 'plain' data. 748 $this->assertSame($mdl30648expected, curl::strip_double_headers($mdl30648expected)); 749 750 // Example from OU proxy. 751 $httpsexample = <<<EOF 752 HTTP/1.0 200 Connection established 753 754 HTTP/1.1 200 OK 755 Date: Fri, 22 Feb 2013 17:14:23 GMT 756 Server: Apache/2 757 X-Powered-By: PHP/5.3.3-7+squeeze14 758 Content-Type: text/xml 759 Connection: close 760 Content-Encoding: gzip 761 Transfer-Encoding: chunked 762 763 <?xml version="1.0" encoding="ISO-8859-1" ?> 764 <rss version="2.0">... 765 EOF; 766 $httpsexpected = <<<EOF 767 HTTP/1.1 200 OK 768 Date: Fri, 22 Feb 2013 17:14:23 GMT 769 Server: Apache/2 770 X-Powered-By: PHP/5.3.3-7+squeeze14 771 Content-Type: text/xml 772 Connection: close 773 Content-Encoding: gzip 774 Transfer-Encoding: chunked 775 776 <?xml version="1.0" encoding="ISO-8859-1" ?> 777 <rss version="2.0">... 778 EOF; 779 // For HTTP, replace the \n with \r\n. 780 $httpsexample = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexample); 781 $httpsexpected = preg_replace("~(?!<\r)\n~", "\r\n", $httpsexpected); 782 783 // Test stripping works OK. 784 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexample)); 785 // Test it does nothing to the 'plain' data. 786 $this->assertSame($httpsexpected, curl::strip_double_headers($httpsexpected)); 787 } 788 }
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 |