Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

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