Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [Versions 39 and 310]
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 * PHPUnit integration tests 19 * 20 * @package core 21 * @category phpunit 22 * @copyright 2012 Petr Skoda {@link http://skodak.org} 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 * Test advanced_testcase extra features. 31 * 32 * @package core 33 * @category phpunit 34 * @copyright 2012 Petr Skoda {@link http://skodak.org} 35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 36 */ 37 class core_phpunit_advanced_testcase extends advanced_testcase { 38 39 public function test_debugging() { 40 global $CFG; 41 $this->resetAfterTest(); 42 43 debugging('hokus'); 44 $this->assertDebuggingCalled(); 45 debugging('pokus'); 46 $this->assertDebuggingCalled('pokus'); 47 debugging('pokus', DEBUG_MINIMAL); 48 $this->assertDebuggingCalled('pokus', DEBUG_MINIMAL); 49 $this->assertDebuggingNotCalled(); 50 51 debugging('a'); 52 debugging('b', DEBUG_MINIMAL); 53 debugging('c', DEBUG_DEVELOPER); 54 $debuggings = $this->getDebuggingMessages(); 55 $this->assertCount(3, $debuggings); 56 $this->assertSame('a', $debuggings[0]->message); 57 $this->assertSame(DEBUG_NORMAL, $debuggings[0]->level); 58 $this->assertSame('b', $debuggings[1]->message); 59 $this->assertSame(DEBUG_MINIMAL, $debuggings[1]->level); 60 $this->assertSame('c', $debuggings[2]->message); 61 $this->assertSame(DEBUG_DEVELOPER, $debuggings[2]->level); 62 63 $this->resetDebugging(); 64 $this->assertDebuggingNotCalled(); 65 $debuggings = $this->getDebuggingMessages(); 66 $this->assertCount(0, $debuggings); 67 68 set_debugging(DEBUG_NONE); 69 debugging('hokus'); 70 $this->assertDebuggingNotCalled(); 71 set_debugging(DEBUG_DEVELOPER); 72 } 73 74 /** 75 * @test 76 * 77 * Annotations are a valid PHPUnit method for running tests. Debugging needs to support them. 78 */ 79 public function debugging_called_with_annotation() { 80 debugging('pokus', DEBUG_MINIMAL); 81 $this->assertDebuggingCalled('pokus', DEBUG_MINIMAL); 82 } 83 84 public function test_set_user() { 85 global $USER, $DB, $SESSION; 86 87 $this->resetAfterTest(); 88 89 $this->assertEquals(0, $USER->id); 90 $this->assertSame($_SESSION['USER'], $USER); 91 $this->assertSame($GLOBALS['USER'], $USER); 92 93 $user = $DB->get_record('user', array('id'=>2)); 94 $this->assertNotEmpty($user); 95 $this->setUser($user); 96 $this->assertEquals(2, $USER->id); 97 $this->assertEquals(2, $_SESSION['USER']->id); 98 $this->assertSame($_SESSION['USER'], $USER); 99 $this->assertSame($GLOBALS['USER'], $USER); 100 101 $USER->id = 3; 102 $this->assertEquals(3, $USER->id); 103 $this->assertEquals(3, $_SESSION['USER']->id); 104 $this->assertSame($_SESSION['USER'], $USER); 105 $this->assertSame($GLOBALS['USER'], $USER); 106 107 \core\session\manager::set_user($user); 108 $this->assertEquals(2, $USER->id); 109 $this->assertEquals(2, $_SESSION['USER']->id); 110 $this->assertSame($_SESSION['USER'], $USER); 111 $this->assertSame($GLOBALS['USER'], $USER); 112 113 $USER = $DB->get_record('user', array('id'=>1)); 114 $this->assertNotEmpty($USER); 115 $this->assertEquals(1, $USER->id); 116 $this->assertEquals(1, $_SESSION['USER']->id); 117 $this->assertSame($_SESSION['USER'], $USER); 118 $this->assertSame($GLOBALS['USER'], $USER); 119 120 $this->setUser(null); 121 $this->assertEquals(0, $USER->id); 122 $this->assertSame($_SESSION['USER'], $USER); 123 $this->assertSame($GLOBALS['USER'], $USER); 124 125 // Ensure session is reset after setUser, as it may contain extra info. 126 $SESSION->sometestvalue = true; 127 $this->setUser($user); 128 $this->assertObjectNotHasAttribute('sometestvalue', $SESSION); 129 } 130 131 public function test_set_admin_user() { 132 global $USER; 133 134 $this->resetAfterTest(); 135 136 $this->setAdminUser(); 137 $this->assertEquals($USER->id, 2); 138 $this->assertTrue(is_siteadmin()); 139 } 140 141 public function test_set_guest_user() { 142 global $USER; 143 144 $this->resetAfterTest(); 145 146 $this->setGuestUser(); 147 $this->assertEquals($USER->id, 1); 148 $this->assertTrue(isguestuser()); 149 } 150 151 public function test_database_reset() { 152 global $DB; 153 154 $this->resetAfterTest(); 155 156 $this->preventResetByRollback(); 157 158 $this->assertEquals(1, $DB->count_records('course')); // Only frontpage in new site. 159 160 // This is weird table - id is NOT a sequence here. 161 $this->assertEquals(0, $DB->count_records('context_temp')); 162 $DB->import_record('context_temp', array('id'=>5, 'path'=>'/1/2', 'depth'=>2)); 163 $record = $DB->get_record('context_temp', array()); 164 $this->assertEquals(5, $record->id); 165 166 $this->assertEquals(0, $DB->count_records('user_preferences')); 167 $originaldisplayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x')); 168 $this->assertEquals(1, $DB->count_records('user_preferences')); 169 170 $numcourses = $DB->count_records('course'); 171 $course = $this->getDataGenerator()->create_course(); 172 $this->assertEquals($numcourses + 1, $DB->count_records('course')); 173 174 $this->assertEquals(2, $DB->count_records('user')); 175 $DB->delete_records('user', array('id'=>1)); 176 $this->assertEquals(1, $DB->count_records('user')); 177 178 $this->resetAllData(); 179 180 $this->assertEquals(1, $DB->count_records('course')); // Only frontpage in new site. 181 $this->assertEquals(0, $DB->count_records('context_temp')); // Only frontpage in new site. 182 183 $numcourses = $DB->count_records('course'); 184 $course = $this->getDataGenerator()->create_course(); 185 $this->assertEquals($numcourses + 1, $DB->count_records('course')); 186 187 $displayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x')); 188 $this->assertEquals($originaldisplayid, $displayid); 189 190 $this->assertEquals(2, $DB->count_records('user')); 191 $DB->delete_records('user', array('id'=>2)); 192 $user = $this->getDataGenerator()->create_user(); 193 $this->assertEquals(2, $DB->count_records('user')); 194 $this->assertGreaterThan(2, $user->id); 195 196 $this->resetAllData(); 197 198 $numcourses = $DB->count_records('course'); 199 $course = $this->getDataGenerator()->create_course(); 200 $this->assertEquals($numcourses + 1, $DB->count_records('course')); 201 202 $this->assertEquals(2, $DB->count_records('user')); 203 $DB->delete_records('user', array('id'=>2)); 204 205 $this->resetAllData(); 206 207 $numcourses = $DB->count_records('course'); 208 $course = $this->getDataGenerator()->create_course(); 209 $this->assertEquals($numcourses + 1, $DB->count_records('course')); 210 211 $this->assertEquals(2, $DB->count_records('user')); 212 } 213 214 public function test_change_detection() { 215 global $DB, $CFG, $COURSE, $SITE, $USER; 216 217 $this->preventResetByRollback(); 218 self::resetAllData(true); 219 220 // Database change. 221 $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2))); 222 $DB->set_field('user', 'confirmed', 0, array('id'=>2)); 223 try { 224 self::resetAllData(true); 225 } catch (Exception $e) { 226 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); 227 } 228 $this->assertEquals(1, $DB->get_field('user', 'confirmed', array('id'=>2))); 229 230 // Config change. 231 $CFG->xx = 'yy'; 232 unset($CFG->admin); 233 $CFG->rolesactive = 0; 234 try { 235 self::resetAllData(true); 236 } catch (Exception $e) { 237 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); 238 $this->assertStringContainsString('xx', $e->getMessage()); 239 $this->assertStringContainsString('admin', $e->getMessage()); 240 $this->assertStringContainsString('rolesactive', $e->getMessage()); 241 } 242 $this->assertFalse(isset($CFG->xx)); 243 $this->assertTrue(isset($CFG->admin)); 244 $this->assertEquals(1, $CFG->rolesactive); 245 246 // _GET change. 247 $_GET['__somethingthatwillnotnormallybepresent__'] = 'yy'; 248 self::resetAllData(true); 249 250 $this->assertEquals(array(), $_GET); 251 252 // _POST change. 253 $_POST['__somethingthatwillnotnormallybepresent2__'] = 'yy'; 254 self::resetAllData(true); 255 $this->assertEquals(array(), $_POST); 256 257 // _FILES change. 258 $_FILES['__somethingthatwillnotnormallybepresent3__'] = 'yy'; 259 self::resetAllData(true); 260 $this->assertEquals(array(), $_FILES); 261 262 // _REQUEST change. 263 $_REQUEST['__somethingthatwillnotnormallybepresent4__'] = 'yy'; 264 self::resetAllData(true); 265 $this->assertEquals(array(), $_REQUEST); 266 267 // Silent changes. 268 $_SERVER['xx'] = 'yy'; 269 self::resetAllData(true); 270 $this->assertFalse(isset($_SERVER['xx'])); 271 272 // COURSE change. 273 $SITE->id = 10; 274 $COURSE = new stdClass(); 275 $COURSE->id = 7; 276 try { 277 self::resetAllData(true); 278 } catch (Exception $e) { 279 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); 280 $this->assertEquals(1, $SITE->id); 281 $this->assertSame($SITE, $COURSE); 282 $this->assertSame($SITE, $COURSE); 283 } 284 285 // USER change. 286 $this->setUser(2); 287 try { 288 self::resetAllData(true); 289 } catch (Exception $e) { 290 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); 291 $this->assertEquals(0, $USER->id); 292 } 293 } 294 295 public function test_getDataGenerator() { 296 $generator = $this->getDataGenerator(); 297 $this->assertInstanceOf('testing_data_generator', $generator); 298 } 299 300 public function test_database_mock1() { 301 global $DB; 302 303 try { 304 $DB->get_record('pokus', array()); 305 $this->fail('Exception expected when accessing non existent table'); 306 } catch (moodle_exception $e) { 307 $this->assertInstanceOf('dml_exception', $e); 308 } 309 $DB = $this->createMock(get_class($DB)); 310 $this->assertNull($DB->get_record('pokus', array())); 311 // Rest continues after reset. 312 } 313 314 public function test_database_mock2() { 315 global $DB; 316 317 // Now the database should be back to normal. 318 $this->assertFalse($DB->get_record('user', array('id'=>9999))); 319 } 320 321 public function test_load_data_dataset_xml() { 322 global $DB; 323 324 $this->resetAfterTest(); 325 326 $this->assertFalse($DB->record_exists('user', array('id' => 5))); 327 $this->assertFalse($DB->record_exists('user', array('id' => 7))); 328 $dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml'); 329 $this->assertDebuggingCalled('createXMLDataSet() is deprecated. Please use dataset_from_files() instead.'); 330 $this->loadDataSet($dataset); 331 $this->assertDebuggingCalled('loadDataSet() is deprecated. Please use dataset->to_database() instead.'); 332 $this->assertTrue($DB->record_exists('user', array('id' => 5))); 333 $this->assertTrue($DB->record_exists('user', array('id' => 7))); 334 $user5 = $DB->get_record('user', array('id' => 5)); 335 $user7 = $DB->get_record('user', array('id' => 7)); 336 $this->assertSame('bozka.novakova', $user5->username); 337 $this->assertSame('pepa.novak', $user7->username); 338 339 } 340 341 public function test_load_dataset_csv() { 342 global $DB; 343 344 $this->resetAfterTest(); 345 346 $this->assertFalse($DB->record_exists('user', array('id' => 8))); 347 $this->assertFalse($DB->record_exists('user', array('id' => 9))); 348 $dataset = $this->createCsvDataSet(array('user' => __DIR__.'/fixtures/sample_dataset.csv')); 349 $this->assertDebuggingCalled('createCsvDataSet() is deprecated. Please use dataset_from_files() instead.'); 350 $this->loadDataSet($dataset); 351 $this->assertDebuggingCalled('loadDataSet() is deprecated. Please use dataset->to_database() instead.'); 352 $this->assertEquals(5, $DB->get_field('user', 'id', array('username' => 'bozka.novakova'))); 353 $this->assertEquals(7, $DB->get_field('user', 'id', array('username' => 'pepa.novak'))); 354 355 } 356 357 public function test_load_dataset_array() { 358 global $DB; 359 360 $this->resetAfterTest(); 361 362 $data = array( 363 'user' => array( 364 array('username', 'email'), 365 array('top.secret', 'top@example.com'), 366 array('low.secret', 'low@example.com'), 367 ), 368 ); 369 370 $this->assertFalse($DB->record_exists('user', array('email' => 'top@example.com'))); 371 $this->assertFalse($DB->record_exists('user', array('email' => 'low@example.com'))); 372 $dataset = $this->createArrayDataSet($data); 373 $this->assertDebuggingCalled('createArrayDataSet() is deprecated. Please use dataset_from_array() instead.'); 374 $this->loadDataSet($dataset); 375 $this->assertDebuggingCalled('loadDataSet() is deprecated. Please use dataset->to_database() instead.'); 376 $this->assertTrue($DB->record_exists('user', array('email' => 'top@example.com'))); 377 $this->assertTrue($DB->record_exists('user', array('email' => 'low@example.com'))); 378 379 $data = array( 380 'user' => array( 381 array('username' => 'noidea', 'email' => 'noidea@example.com'), 382 array('username' => 'onemore', 'email' => 'onemore@example.com'), 383 ), 384 ); 385 $dataset = $this->createArrayDataSet($data); 386 $this->assertDebuggingCalled('createArrayDataSet() is deprecated. Please use dataset_from_array() instead.'); 387 $this->loadDataSet($dataset); 388 $this->assertDebuggingCalled('loadDataSet() is deprecated. Please use dataset->to_database() instead.'); 389 $this->assertTrue($DB->record_exists('user', array('username' => 'noidea'))); 390 $this->assertTrue($DB->record_exists('user', array('username' => 'onemore'))); 391 } 392 393 public function test_assert_time_current() { 394 $this->assertTimeCurrent(time()); 395 396 $this->setCurrentTimeStart(); 397 $this->assertTimeCurrent(time()); 398 $this->waitForSecond(); 399 $this->assertTimeCurrent(time()); 400 $this->assertTimeCurrent(time()-1); 401 402 try { 403 $this->setCurrentTimeStart(); 404 $this->assertTimeCurrent(time()+10); 405 $this->fail('Failed assert expected'); 406 } catch (Exception $e) { 407 $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e); 408 } 409 410 try { 411 $this->setCurrentTimeStart(); 412 $this->assertTimeCurrent(time()-10); 413 $this->fail('Failed assert expected'); 414 } catch (Exception $e) { 415 $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e); 416 } 417 } 418 419 public function test_message_processors_reset() { 420 global $DB; 421 422 $this->resetAfterTest(true); 423 424 // Get all processors first. 425 $processors1 = get_message_processors(); 426 427 // Add a new message processor and get all processors again. 428 $processor = new stdClass(); 429 $processor->name = 'test_processor'; 430 $processor->enabled = 1; 431 $DB->insert_record('message_processors', $processor); 432 433 $processors2 = get_message_processors(); 434 435 // Assert that new processor still haven't been added to the list. 436 $this->assertSame($processors1, $processors2); 437 438 // Reset message processors data. 439 $processors3 = get_message_processors(false, true); 440 // Now, list of processors should not be the same any more, 441 // And we should have one more message processor in the list. 442 $this->assertNotSame($processors1, $processors3); 443 $this->assertEquals(count($processors1) + 1, count($processors3)); 444 } 445 446 public function test_message_redirection() { 447 $this->preventResetByRollback(); // Messaging is not compatible with transactions... 448 $this->resetAfterTest(false); 449 450 $user1 = $this->getDataGenerator()->create_user(); 451 $user2 = $this->getDataGenerator()->create_user(); 452 453 // Any core message will do here. 454 $message1 = new \core\message\message(); 455 $message1->courseid = 1; 456 $message1->component = 'moodle'; 457 $message1->name = 'instantmessage'; 458 $message1->userfrom = $user1; 459 $message1->userto = $user2; 460 $message1->subject = 'message subject 1'; 461 $message1->fullmessage = 'message body'; 462 $message1->fullmessageformat = FORMAT_MARKDOWN; 463 $message1->fullmessagehtml = '<p>message body</p>'; 464 $message1->smallmessage = 'small message'; 465 $message1->notification = 0; 466 467 $message2 = new \core\message\message(); 468 $message2->courseid = 1; 469 $message2->component = 'moodle'; 470 $message2->name = 'instantmessage'; 471 $message2->userfrom = $user2; 472 $message2->userto = $user1; 473 $message2->subject = 'message subject 2'; 474 $message2->fullmessage = 'message body'; 475 $message2->fullmessageformat = FORMAT_MARKDOWN; 476 $message2->fullmessagehtml = '<p>message body</p>'; 477 $message2->smallmessage = 'small message'; 478 $message2->notification = 0; 479 480 // There should be debugging message without redirection. 481 $mailsink = $this->redirectEmails(); 482 $mailsink->close(); 483 message_send($message1); 484 $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.'); 485 486 // Sink should catch messages. 487 $sink = $this->redirectMessages(); 488 $mid1 = message_send($message1); 489 $mid2 = message_send($message2); 490 491 $this->assertDebuggingNotCalled('message redirection must prevent debug messages from the message_send()'); 492 $this->assertEquals(2, $sink->count()); 493 $this->assertGreaterThanOrEqual(1, $mid1); 494 $this->assertGreaterThanOrEqual($mid1, $mid2); 495 496 $messages = $sink->get_messages(); 497 $this->assertIsArray($messages); 498 $this->assertCount(2, $messages); 499 $this->assertEquals($mid1, $messages[0]->id); 500 $this->assertEquals($message1->userto->id, $messages[0]->useridto); 501 $this->assertEquals($message1->userfrom->id, $messages[0]->useridfrom); 502 $this->assertEquals($message1->smallmessage, $messages[0]->smallmessage); 503 $this->assertEquals($mid2, $messages[1]->id); 504 $this->assertEquals($message2->userto->id, $messages[1]->useridto); 505 $this->assertEquals($message2->userfrom->id, $messages[1]->useridfrom); 506 $this->assertEquals($message2->smallmessage, $messages[1]->smallmessage); 507 508 // Test resetting. 509 $sink->clear(); 510 $messages = $sink->get_messages(); 511 $this->assertIsArray($messages); 512 $this->assertCount(0, $messages); 513 514 message_send($message1); 515 $messages = $sink->get_messages(); 516 $this->assertIsArray($messages); 517 $this->assertCount(1, $messages); 518 519 // Test closing. 520 $sink->close(); 521 $messages = $sink->get_messages(); 522 $this->assertIsArray($messages); 523 $this->assertCount(1, $messages, 'Messages in sink are supposed to stay there after close'); 524 525 // Test debugging is enabled again. 526 message_send($message1); 527 $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.'); 528 529 // Test invalid names and components. 530 531 $sink = $this->redirectMessages(); 532 533 $message3 = new \core\message\message(); 534 $message3->courseid = 1; 535 $message3->component = 'xxxx_yyyyy'; 536 $message3->name = 'instantmessage'; 537 $message3->userfrom = $user2; 538 $message3->userto = $user1; 539 $message3->subject = 'message subject 2'; 540 $message3->fullmessage = 'message body'; 541 $message3->fullmessageformat = FORMAT_MARKDOWN; 542 $message3->fullmessagehtml = '<p>message body</p>'; 543 $message3->smallmessage = 'small message'; 544 $message3->notification = 0; 545 546 $this->assertFalse(message_send($message3)); 547 $this->assertDebuggingCalled('Attempt to send msg from a provider xxxx_yyyyy/instantmessage '. 548 'that is inactive or not allowed for the user id='.$user1->id); 549 550 $message3->component = 'moodle'; 551 $message3->name = 'yyyyyy'; 552 553 $this->assertFalse(message_send($message3)); 554 $this->assertDebuggingCalled('Attempt to send msg from a provider moodle/yyyyyy '. 555 'that is inactive or not allowed for the user id='.$user1->id); 556 557 message_send($message1); 558 $this->assertEquals(1, $sink->count()); 559 560 // Test if sink can be carried over to next test. 561 $this->assertTrue(phpunit_util::is_redirecting_messages()); 562 return $sink; 563 } 564 565 /** 566 * @depends test_message_redirection 567 */ 568 public function test_message_redirection_noreset($sink) { 569 if ($this->isInIsolation()) { 570 $this->markTestSkipped('State cannot be carried over between tests in isolated tests'); 571 } 572 573 $this->preventResetByRollback(); // Messaging is not compatible with transactions... 574 $this->resetAfterTest(); 575 576 $this->assertTrue(phpunit_util::is_redirecting_messages()); 577 $this->assertEquals(1, $sink->count()); 578 579 $message = new \core\message\message(); 580 $message->courseid = 1; 581 $message->component = 'moodle'; 582 $message->name = 'instantmessage'; 583 $message->userfrom = get_admin(); 584 $message->userto = get_admin(); 585 $message->subject = 'message subject 1'; 586 $message->fullmessage = 'message body'; 587 $message->fullmessageformat = FORMAT_MARKDOWN; 588 $message->fullmessagehtml = '<p>message body</p>'; 589 $message->smallmessage = 'small message'; 590 $message->notification = 0; 591 592 message_send($message); 593 $this->assertEquals(2, $sink->count()); 594 } 595 596 /** 597 * @depends test_message_redirection_noreset 598 */ 599 public function test_message_redirection_reset() { 600 $this->assertFalse(phpunit_util::is_redirecting_messages(), 'Test reset must stop message redirection.'); 601 } 602 603 public function test_set_timezone() { 604 global $CFG; 605 $this->resetAfterTest(); 606 607 $this->assertSame('Australia/Perth', $CFG->timezone); 608 $this->assertSame('Australia/Perth', date_default_timezone_get()); 609 610 $this->setTimezone('Pacific/Auckland', 'Europe/Prague'); 611 $this->assertSame('Pacific/Auckland', $CFG->timezone); 612 $this->assertSame('Pacific/Auckland', date_default_timezone_get()); 613 614 $this->setTimezone('99', 'Europe/Prague'); 615 $this->assertSame('99', $CFG->timezone); 616 $this->assertSame('Europe/Prague', date_default_timezone_get()); 617 618 $this->setTimezone('xxx', 'Europe/Prague'); 619 $this->assertSame('xxx', $CFG->timezone); 620 $this->assertSame('Europe/Prague', date_default_timezone_get()); 621 622 $this->setTimezone(); 623 $this->assertSame('Australia/Perth', $CFG->timezone); 624 $this->assertSame('Australia/Perth', date_default_timezone_get()); 625 626 try { 627 $this->setTimezone('Pacific/Auckland', ''); 628 } catch (Exception $e) { 629 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); 630 } 631 632 try { 633 $this->setTimezone('Pacific/Auckland', 'xxxx'); 634 } catch (Exception $e) { 635 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); 636 } 637 638 try { 639 $this->setTimezone('Pacific/Auckland', null); 640 } catch (Exception $e) { 641 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); 642 } 643 644 } 645 646 public function test_locale_reset() { 647 global $CFG; 648 649 $this->resetAfterTest(); 650 651 // If this fails self::resetAllData(); must be updated. 652 $this->assertSame('en_AU.UTF-8', get_string('locale', 'langconfig')); 653 $this->assertSame('English_Australia.1252', get_string('localewin', 'langconfig')); 654 655 if ($CFG->ostype === 'WINDOWS') { 656 $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0)); 657 setlocale(LC_TIME, 'English_USA.1252'); 658 } else { 659 $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0)); 660 setlocale(LC_TIME, 'en_US.UTF-8'); 661 } 662 663 try { 664 self::resetAllData(true); 665 } catch (Exception $e) { 666 $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e); 667 } 668 669 if ($CFG->ostype === 'WINDOWS') { 670 $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0)); 671 } else { 672 $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0)); 673 } 674 675 if ($CFG->ostype === 'WINDOWS') { 676 $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0)); 677 setlocale(LC_TIME, 'English_USA.1252'); 678 } else { 679 $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0)); 680 setlocale(LC_TIME, 'en_US.UTF-8'); 681 } 682 683 self::resetAllData(false); 684 685 if ($CFG->ostype === 'WINDOWS') { 686 $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0)); 687 } else { 688 $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0)); 689 } 690 } 691 692 /** 693 * This test sets a user agent and makes sure that it is cleared when the test is reset. 694 */ 695 public function test_it_resets_useragent_after_test() { 696 $this->resetAfterTest(); 697 $fakeagent = 'New user agent set.'; 698 699 // Sanity check: it should not be set when test begins. 700 self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set at first.'); 701 702 // Set a fake useragent and check it was set properly. 703 core_useragent::instance(true, $fakeagent); 704 self::assertSame($fakeagent, core_useragent::get_user_agent_string(), 'It should be the forced agent.'); 705 706 // Reset test data and ansure the useragent was cleaned. 707 self::resetAllData(false); 708 self::assertFalse(core_useragent::get_user_agent_string(), 'It should not be set again, data was reset.'); 709 } 710 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body