public function test_timezones() {
        global $CFG, $USER;

        // The timezones used in this test are chosen because they do not use DST - that would break the test.

        $currenttimezonephp = date_default_timezone_get();
        $currenttimezonecfg = null;
        if (!empty($CFG->timezone)) {
            $currenttimezonecfg = $CFG->timezone;
        }
        $userstimezone = null;
        if (!empty($USER->timezone)) {
            $userstimezone = $USER->timezone;
        }

        // We are testing a difference between $CFG->timezone and the php.ini timezone.
        // GMT+8.
        date_default_timezone_set('Australia/Perth');
        // GMT-04:30.
        $CFG->timezone = 'America/Caracas';

        $testclass = new \core\task\scheduled_test_task();

        // Scheduled tasks should always use servertime - so this is 03:30 GMT.
        $testclass->set_hour('1');
        $testclass->set_minute('0');

        // Next valid time should be 1am of the next day.
        $nexttime = $testclass->get_next_scheduled_time();

        // GMT+05:45.
        $USER->timezone = 'Asia/Kathmandu';
        $userdate = userdate($nexttime);

        // Should be displayed in user timezone.
        // I used http://www.timeanddate.com/worldclock/fixedtime.html?msg=Moodle+Test&iso=20140314T01&p1=58
        // to verify this time.
        $this->assertContains('11:15 AM', core_text::strtoupper($userdate));

        $CFG->timezone = $currenttimezonecfg;
        date_default_timezone_set($currenttimezonephp);
    }
 /**
  * Tests the use of 'R' syntax in time fields of tasks to get
  * tasks be configured with a non-uniform time.
  */
 public function test_random_time_specification()
 {
     // Testing non-deterministic things in a unit test is not really
     // wise, so we just test the values have changed within allowed bounds.
     $testclass = new \core\task\scheduled_test_task();
     // The test task defaults to '*'.
     $this->assertInternalType('string', $testclass->get_minute());
     $this->assertInternalType('string', $testclass->get_hour());
     // Set a random value.
     $testclass->set_minute('R');
     $testclass->set_hour('R');
     // Verify the minute has changed within allowed bounds.
     $minute = $testclass->get_minute();
     $this->assertInternalType('int', $minute);
     $this->assertGreaterThanOrEqual(0, $minute);
     $this->assertLessThanOrEqual(59, $minute);
     // Verify the hour has changed within allowed bounds.
     $hour = $testclass->get_hour();
     $this->assertInternalType('int', $hour);
     $this->assertGreaterThanOrEqual(0, $hour);
     $this->assertLessThanOrEqual(23, $hour);
 }