Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 39 and 401] [Versions 401 and 402] [Versions 401 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  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_load_data_dataset_xml() {
 317          global $DB;
 318  
 319          $this->resetAfterTest();
 320  
 321          $this->assertFalse($DB->record_exists('user', array('id' => 5)));
 322          $this->assertFalse($DB->record_exists('user', array('id' => 7)));
 323          $dataset = $this->createXMLDataSet(__DIR__.'/fixtures/sample_dataset.xml');
 324          $this->assertDebuggingCalled('createXMLDataSet() is deprecated. Please use dataset_from_files() instead.');
 325          $this->loadDataSet($dataset);
 326          $this->assertDebuggingCalled('loadDataSet() is deprecated. Please use dataset->to_database() instead.');
 327          $this->assertTrue($DB->record_exists('user', array('id' => 5)));
 328          $this->assertTrue($DB->record_exists('user', array('id' => 7)));
 329          $user5 = $DB->get_record('user', array('id' => 5));
 330          $user7 = $DB->get_record('user', array('id' => 7));
 331          $this->assertSame('bozka.novakova', $user5->username);
 332          $this->assertSame('pepa.novak', $user7->username);
 333  
 334      }
 335  
 336      public function test_load_dataset_csv() {
 337          global $DB;
 338  
 339          $this->resetAfterTest();
 340  
 341          $this->assertFalse($DB->record_exists('user', array('id' => 8)));
 342          $this->assertFalse($DB->record_exists('user', array('id' => 9)));
 343          $dataset = $this->createCsvDataSet(array('user' => __DIR__.'/fixtures/sample_dataset.csv'));
 344          $this->assertDebuggingCalled('createCsvDataSet() is deprecated. Please use dataset_from_files() instead.');
 345          $this->loadDataSet($dataset);
 346          $this->assertDebuggingCalled('loadDataSet() is deprecated. Please use dataset->to_database() instead.');
 347          $this->assertEquals(5, $DB->get_field('user', 'id', array('username' => 'bozka.novakova')));
 348          $this->assertEquals(7, $DB->get_field('user', 'id', array('username' => 'pepa.novak')));
 349  
 350      }
 351  
 352      public function test_load_dataset_array() {
 353          global $DB;
 354  
 355          $this->resetAfterTest();
 356  
 357          $data = array(
 358              'user' => array(
 359                  array('username', 'email'),
 360                  array('top.secret', 'top@example.com'),
 361                  array('low.secret', 'low@example.com'),
 362              ),
 363          );
 364  
 365          $this->assertFalse($DB->record_exists('user', array('email' => 'top@example.com')));
 366          $this->assertFalse($DB->record_exists('user', array('email' => 'low@example.com')));
 367          $dataset = $this->createArrayDataSet($data);
 368          $this->assertDebuggingCalled('createArrayDataSet() is deprecated. Please use dataset_from_array() instead.');
 369          $this->loadDataSet($dataset);
 370          $this->assertDebuggingCalled('loadDataSet() is deprecated. Please use dataset->to_database() instead.');
 371          $this->assertTrue($DB->record_exists('user', array('email' => 'top@example.com')));
 372          $this->assertTrue($DB->record_exists('user', array('email' => 'low@example.com')));
 373  
 374          $data = array(
 375              'user' => array(
 376                  array('username' => 'noidea', 'email' => 'noidea@example.com'),
 377                  array('username' => 'onemore', 'email' => 'onemore@example.com'),
 378              ),
 379          );
 380          $dataset = $this->createArrayDataSet($data);
 381          $this->assertDebuggingCalled('createArrayDataSet() is deprecated. Please use dataset_from_array() instead.');
 382          $this->loadDataSet($dataset);
 383          $this->assertDebuggingCalled('loadDataSet() is deprecated. Please use dataset->to_database() instead.');
 384          $this->assertTrue($DB->record_exists('user', array('username' => 'noidea')));
 385          $this->assertTrue($DB->record_exists('user', array('username' => 'onemore')));
 386      }
 387  
 388      public function test_assert_time_current() {
 389          $this->assertTimeCurrent(time());
 390  
 391          $this->setCurrentTimeStart();
 392          $this->assertTimeCurrent(time());
 393          $this->waitForSecond();
 394          $this->assertTimeCurrent(time());
 395          $this->assertTimeCurrent(time()-1);
 396  
 397          try {
 398              $this->setCurrentTimeStart();
 399              $this->assertTimeCurrent(time()+10);
 400              $this->fail('Failed assert expected');
 401          } catch (\Exception $e) {
 402              $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
 403          }
 404  
 405          try {
 406              $this->setCurrentTimeStart();
 407              $this->assertTimeCurrent(time()-10);
 408              $this->fail('Failed assert expected');
 409          } catch (\Exception $e) {
 410              $this->assertInstanceOf('PHPUnit\Framework\ExpectationFailedException', $e);
 411          }
 412      }
 413  
 414      public function test_message_processors_reset() {
 415          global $DB;
 416  
 417          $this->resetAfterTest(true);
 418  
 419          // Get all processors first.
 420          $processors1 = get_message_processors();
 421  
 422          // Add a new message processor and get all processors again.
 423          $processor = new \stdClass();
 424          $processor->name = 'test_processor';
 425          $processor->enabled = 1;
 426          $DB->insert_record('message_processors', $processor);
 427  
 428          $processors2 = get_message_processors();
 429  
 430          // Assert that new processor still haven't been added to the list.
 431          $this->assertSame($processors1, $processors2);
 432  
 433          // Reset message processors data.
 434          $processors3 = get_message_processors(false, true);
 435          // Now, list of processors should not be the same any more,
 436          // And we should have one more message processor in the list.
 437          $this->assertNotSame($processors1, $processors3);
 438          $this->assertEquals(count($processors1) + 1, count($processors3));
 439      }
 440  
 441      public function test_message_redirection() {
 442          $this->preventResetByRollback(); // Messaging is not compatible with transactions...
 443          $this->resetAfterTest(false);
 444  
 445          $user1 = $this->getDataGenerator()->create_user();
 446          $user2 = $this->getDataGenerator()->create_user();
 447  
 448          // Any core message will do here.
 449          $message1 = new \core\message\message();
 450          $message1->courseid          = 1;
 451          $message1->component         = 'moodle';
 452          $message1->name              = 'instantmessage';
 453          $message1->userfrom          = $user1;
 454          $message1->userto            = $user2;
 455          $message1->subject           = 'message subject 1';
 456          $message1->fullmessage       = 'message body';
 457          $message1->fullmessageformat = FORMAT_MARKDOWN;
 458          $message1->fullmessagehtml   = '<p>message body</p>';
 459          $message1->smallmessage      = 'small message';
 460          $message1->notification      = 0;
 461  
 462          $message2 = new \core\message\message();
 463          $message2->courseid          = 1;
 464          $message2->component         = 'moodle';
 465          $message2->name              = 'instantmessage';
 466          $message2->userfrom          = $user2;
 467          $message2->userto            = $user1;
 468          $message2->subject           = 'message subject 2';
 469          $message2->fullmessage       = 'message body';
 470          $message2->fullmessageformat = FORMAT_MARKDOWN;
 471          $message2->fullmessagehtml   = '<p>message body</p>';
 472          $message2->smallmessage      = 'small message';
 473          $message2->notification      = 0;
 474  
 475          // There should be debugging message without redirection.
 476          $mailsink = $this->redirectEmails();
 477          $mailsink->close();
 478          message_send($message1);
 479          $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
 480  
 481          // Sink should catch messages.
 482          $sink = $this->redirectMessages();
 483          $mid1 = message_send($message1);
 484          $mid2 = message_send($message2);
 485  
 486          $this->assertDebuggingNotCalled('message redirection must prevent debug messages from the message_send()');
 487          $this->assertEquals(2, $sink->count());
 488          $this->assertGreaterThanOrEqual(1, $mid1);
 489          $this->assertGreaterThanOrEqual($mid1, $mid2);
 490  
 491          $messages = $sink->get_messages();
 492          $this->assertIsArray($messages);
 493          $this->assertCount(2, $messages);
 494          $this->assertEquals($mid1, $messages[0]->id);
 495          $this->assertEquals($message1->userto->id, $messages[0]->useridto);
 496          $this->assertEquals($message1->userfrom->id, $messages[0]->useridfrom);
 497          $this->assertEquals($message1->smallmessage, $messages[0]->smallmessage);
 498          $this->assertEquals($mid2, $messages[1]->id);
 499          $this->assertEquals($message2->userto->id, $messages[1]->useridto);
 500          $this->assertEquals($message2->userfrom->id, $messages[1]->useridfrom);
 501          $this->assertEquals($message2->smallmessage, $messages[1]->smallmessage);
 502  
 503          // Test resetting.
 504          $sink->clear();
 505          $messages = $sink->get_messages();
 506          $this->assertIsArray($messages);
 507          $this->assertCount(0, $messages);
 508  
 509          message_send($message1);
 510          $messages = $sink->get_messages();
 511          $this->assertIsArray($messages);
 512          $this->assertCount(1, $messages);
 513  
 514          // Test closing.
 515          $sink->close();
 516          $messages = $sink->get_messages();
 517          $this->assertIsArray($messages);
 518          $this->assertCount(1, $messages, 'Messages in sink are supposed to stay there after close');
 519  
 520          // Test debugging is enabled again.
 521          message_send($message1);
 522          $this->assertDebuggingCalled(null, null, 'message_send() must print debug message that messaging is disabled in phpunit tests.');
 523  
 524          // Test invalid names and components.
 525  
 526          $sink = $this->redirectMessages();
 527  
 528          $message3 = new \core\message\message();
 529          $message3->courseid          = 1;
 530          $message3->component         = 'xxxx_yyyyy';
 531          $message3->name              = 'instantmessage';
 532          $message3->userfrom          = $user2;
 533          $message3->userto            = $user1;
 534          $message3->subject           = 'message subject 2';
 535          $message3->fullmessage       = 'message body';
 536          $message3->fullmessageformat = FORMAT_MARKDOWN;
 537          $message3->fullmessagehtml   = '<p>message body</p>';
 538          $message3->smallmessage      = 'small message';
 539          $message3->notification      = 0;
 540  
 541          $this->assertFalse(message_send($message3));
 542          $this->assertDebuggingCalled('Attempt to send msg from a provider xxxx_yyyyy/instantmessage '.
 543              'that is inactive or not allowed for the user id='.$user1->id);
 544  
 545          $message3->component = 'moodle';
 546          $message3->name      = 'yyyyyy';
 547  
 548          $this->assertFalse(message_send($message3));
 549          $this->assertDebuggingCalled('Attempt to send msg from a provider moodle/yyyyyy '.
 550              'that is inactive or not allowed for the user id='.$user1->id);
 551  
 552          message_send($message1);
 553          $this->assertEquals(1, $sink->count());
 554  
 555          // Test if sink can be carried over to next test.
 556          $this->assertTrue(\phpunit_util::is_redirecting_messages());
 557          return $sink;
 558      }
 559  
 560      /**
 561       * @depends test_message_redirection
 562       */
 563      public function test_message_redirection_noreset($sink) {
 564          if ($this->isInIsolation()) {
 565              $this->markTestSkipped('State cannot be carried over between tests in isolated tests');
 566          }
 567  
 568          $this->preventResetByRollback(); // Messaging is not compatible with transactions...
 569          $this->resetAfterTest();
 570  
 571          $this->assertTrue(\phpunit_util::is_redirecting_messages());
 572          $this->assertEquals(1, $sink->count());
 573  
 574          $message = new \core\message\message();
 575          $message->courseid          = 1;
 576          $message->component         = 'moodle';
 577          $message->name              = 'instantmessage';
 578          $message->userfrom          = get_admin();
 579          $message->userto            = get_admin();
 580          $message->subject           = 'message subject 1';
 581          $message->fullmessage       = 'message body';
 582          $message->fullmessageformat = FORMAT_MARKDOWN;
 583          $message->fullmessagehtml   = '<p>message body</p>';
 584          $message->smallmessage      = 'small message';
 585          $message->notification      = 0;
 586  
 587          message_send($message);
 588          $this->assertEquals(2, $sink->count());
 589      }
 590  
 591      /**
 592       * @depends test_message_redirection_noreset
 593       */
 594      public function test_message_redirection_reset() {
 595          $this->assertFalse(\phpunit_util::is_redirecting_messages(), 'Test reset must stop message redirection.');
 596      }
 597  
 598      public function test_set_timezone() {
 599          global $CFG;
 600          $this->resetAfterTest();
 601  
 602          $this->assertSame('Australia/Perth', $CFG->timezone);
 603          $this->assertSame('Australia/Perth', date_default_timezone_get());
 604  
 605          $this->setTimezone('Pacific/Auckland', 'Europe/Prague');
 606          $this->assertSame('Pacific/Auckland', $CFG->timezone);
 607          $this->assertSame('Pacific/Auckland', date_default_timezone_get());
 608  
 609          $this->setTimezone('99', 'Europe/Prague');
 610          $this->assertSame('99', $CFG->timezone);
 611          $this->assertSame('Europe/Prague', date_default_timezone_get());
 612  
 613          $this->setTimezone('xxx', 'Europe/Prague');
 614          $this->assertSame('xxx', $CFG->timezone);
 615          $this->assertSame('Europe/Prague', date_default_timezone_get());
 616  
 617          $this->setTimezone();
 618          $this->assertSame('Australia/Perth', $CFG->timezone);
 619          $this->assertSame('Australia/Perth', date_default_timezone_get());
 620  
 621          try {
 622              $this->setTimezone('Pacific/Auckland', '');
 623          } catch (\Exception $e) {
 624              $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
 625          }
 626  
 627          try {
 628              $this->setTimezone('Pacific/Auckland', 'xxxx');
 629          } catch (\Exception $e) {
 630              $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
 631          }
 632  
 633          try {
 634              $this->setTimezone('Pacific/Auckland', null);
 635          } catch (\Exception $e) {
 636              $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
 637          }
 638  
 639      }
 640  
 641      public function test_locale_reset() {
 642          global $CFG;
 643  
 644          $this->resetAfterTest();
 645  
 646          // If this fails self::resetAllData(); must be updated.
 647          $this->assertSame('en_AU.UTF-8', get_string('locale', 'langconfig'));
 648          $this->assertSame('English_Australia.1252', get_string('localewin', 'langconfig'));
 649  
 650          if ($CFG->ostype === 'WINDOWS') {
 651              $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
 652              setlocale(LC_TIME, 'English_USA.1252');
 653          } else {
 654              $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
 655              setlocale(LC_TIME, 'en_US.UTF-8');
 656          }
 657  
 658          try {
 659              self::resetAllData(true);
 660          } catch (\Exception $e) {
 661              $this->assertInstanceOf('PHPUnit\Framework\Error\Warning', $e);
 662          }
 663  
 664          if ($CFG->ostype === 'WINDOWS') {
 665              $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
 666          } else {
 667              $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
 668          }
 669  
 670          if ($CFG->ostype === 'WINDOWS') {
 671              $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
 672              setlocale(LC_TIME, 'English_USA.1252');
 673          } else {
 674              $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
 675              setlocale(LC_TIME, 'en_US.UTF-8');
 676          }
 677  
 678          self::resetAllData(false);
 679  
 680          if ($CFG->ostype === 'WINDOWS') {
 681              $this->assertSame('English_Australia.1252', setlocale(LC_TIME, 0));
 682          } else {
 683              $this->assertSame('en_AU.UTF-8', setlocale(LC_TIME, 0));
 684          }
 685      }
 686  
 687      /**
 688       * This test sets a user agent and makes sure that it is cleared when the test is reset.
 689       */
 690      public function test_it_resets_useragent_after_test() {
 691          $this->resetAfterTest();
 692          $fakeagent = 'New user agent set.';
 693  
 694          // Sanity check: it should not be set when test begins.
 695          self::assertFalse(\core_useragent::get_user_agent_string(), 'It should not be set at first.');
 696  
 697          // Set a fake useragent and check it was set properly.
 698          \core_useragent::instance(true, $fakeagent);
 699          self::assertSame($fakeagent, \core_useragent::get_user_agent_string(), 'It should be the forced agent.');
 700  
 701          // Reset test data and ansure the useragent was cleaned.
 702          self::resetAllData(false);
 703          self::assertFalse(\core_useragent::get_user_agent_string(), 'It should not be set again, data was reset.');
 704      }
 705  
 706      /**
 707       * @covers ::runAdhocTasks
 708       */
 709      public function test_runadhoctasks_no_tasks_queued(): void {
 710          $this->runAdhocTasks();
 711          $this->expectOutputRegex('/^$/');
 712      }
 713  
 714      /**
 715       * @covers ::runAdhocTasks
 716       */
 717      public function test_runadhoctasks_tasks_queued(): void {
 718          $this->resetAfterTest(true);
 719          $admin = get_admin();
 720          \core\task\manager::queue_adhoc_task(new \core_phpunit\adhoc_test_task());
 721          $this->runAdhocTasks();
 722          $this->expectOutputRegex("/Task was run as {$admin->id}/");
 723      }
 724  
 725      /**
 726       * @covers ::runAdhocTasks
 727       */
 728      public function test_runadhoctasks_with_existing_user_change(): void {
 729          $this->resetAfterTest(true);
 730          $admin = get_admin();
 731  
 732          $this->setGuestUser();
 733          \core\task\manager::queue_adhoc_task(new \core_phpunit\adhoc_test_task());
 734          $this->runAdhocTasks();
 735          $this->expectOutputRegex("/Task was run as {$admin->id}/");
 736      }
 737  
 738      /**
 739       * @covers ::runAdhocTasks
 740       */
 741      public function test_runadhoctasks_with_existing_user_change_and_specified(): void {
 742          global $USER;
 743  
 744          $this->resetAfterTest(true);
 745          $user = $this->getDataGenerator()->create_user();
 746  
 747          $this->setGuestUser();
 748          $task = new \core_phpunit\adhoc_test_task();
 749          $task->set_userid($user->id);
 750          \core\task\manager::queue_adhoc_task($task);
 751          $this->runAdhocTasks();
 752          $this->expectOutputRegex("/Task was run as {$user->id}/");
 753      }
 754  }