Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 and 403]

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