mirror of
https://github.com/moodle/moodle.git
synced 2025-08-05 17:06:53 +02:00

Applied the following changes to various testcase classes: - Namespaced with component[\level2-API] - Moved to level2-API subdirectory when required. - Fixed incorrect use statements with leading backslash. - Remove file phpdoc block - Remove MOODLE_INTERNAL if not needed. - Changed code to point to global scope when needed. - Fix some relative paths and comments here and there. - All them passing individually. - Complete runs passing too. Special mention to: - The following task tests have been moved within the level2 directory: - \core\adhoc_task_test => \core\task\adhoc_task_test - \core\scheduled_task_test => \core\task\scheduled_task_test - \core\calendar_cron_task_test => \core\task\calendar_cron_task_test - \core\h5p_get_content_types_task_test => \core\task\h5p_get_content_types_task_test - \core\task_database_logger_test => \core\task\database_logger_test - \core\task_logging_test => \core\task\logging_test - The following event tests have been moved within level2 directory: - \core\event_context_locked_test => \core\event\context_locked_test - \core\event_deprecated_test => \core\event\deprecated_test - \core\event_grade_deleted_test => \core\event\grade_deleted_test - \core\event_profile_field_test => \core\event\profile_field_test - \core\event_unknown_logged_test => \core\event\unknown_logged_test - \core\event_user_graded_test => \core\event\user_graded_test - \core\event_user_password_updated_test => \core\event\user_password_updated_test - The following output tests have been moved within level2 directory: - \core\mustache_template_finder_test => \core\output\mustache_template_finder_test - \core\mustache_template_source_loader_test => \core\output\mustache_template_source_loader_test - \core\output_mustache_helper_collection_test => \core\output\mustache_helper_collection_test - The following tests have been moved to their correct tests directories: - lib/tests/time_splittings_test.php => analytics/tests/time_splittings_test.php - All the classes and tests under lib/filebrowser and lib/filestorage belong to core, not to core_files. Some day we should move them to their correct subsystem. - All the classes and tests under lib/grade belong to core, not to core_grades. Some day we should move them to their correct subsystem. - The core_grades_external class and its \core\grades_external_test unit test should belong to the grades subsystem or, alternatively, to \core\external, they both should be moved together. - The core_grading_external class and its \core\grading_external_test unit test should belong to the grading subsystem or, alternatively, to \core\external, they both should be moved together. - The \core\message\message and \core\message\inbound (may be others) classes, and their associated tests should go to the core_message subsystem. - The core_user class, and its associated tests should go to the core_user subsystem. - The \core\update namespace is plain wrong (update is not valid API) and needs action 1) create it or 2) move elsewhere.
339 lines
14 KiB
PHP
339 lines
14 KiB
PHP
<?php
|
|
// This file is part of Moodle - http://moodle.org/
|
|
//
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
namespace core;
|
|
|
|
/**
|
|
* Tests legacy Moodle date/time functions.
|
|
*
|
|
* @package core
|
|
* @copyright 2015 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
* @author Petr Skoda <petr.skoda@totaralms.com>
|
|
*/
|
|
class date_legacy_test extends \advanced_testcase {
|
|
public function test_settings() {
|
|
global $CFG;
|
|
$this->resetAfterTest();
|
|
|
|
$this->assertNotEmpty($CFG->timezone);
|
|
|
|
$this->assertSame('99', $CFG->forcetimezone);
|
|
|
|
$user = $this->getDataGenerator()->create_user();
|
|
$this->assertSame('99', $user->timezone);
|
|
}
|
|
|
|
public function test_get_user_timezone() {
|
|
global $CFG, $USER;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$user = $this->getDataGenerator()->create_user();
|
|
$this->setUser($user);
|
|
|
|
// All set to something.
|
|
|
|
$this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
|
|
$USER->timezone = 'Europe/Prague';
|
|
|
|
$tz = get_user_timezone();
|
|
$this->assertSame('Europe/Prague', $tz);
|
|
|
|
$tz = get_user_timezone(99);
|
|
$this->assertSame('Europe/Prague', $tz);
|
|
$tz = get_user_timezone('99');
|
|
$this->assertSame('Europe/Prague', $tz);
|
|
|
|
$tz = get_user_timezone('Europe/Berlin');
|
|
$this->assertSame('Europe/Berlin', $tz);
|
|
|
|
// User timezone not set.
|
|
|
|
$this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
|
|
$USER->timezone = '99';
|
|
|
|
$tz = get_user_timezone();
|
|
$this->assertSame('Pacific/Auckland', $tz);
|
|
|
|
$tz = get_user_timezone(99);
|
|
$this->assertSame('Pacific/Auckland', $tz);
|
|
$tz = get_user_timezone('99');
|
|
$this->assertSame('Pacific/Auckland', $tz);
|
|
|
|
$tz = get_user_timezone('Europe/Berlin');
|
|
$this->assertSame('Europe/Berlin', $tz);
|
|
|
|
// Server timezone not set.
|
|
|
|
$this->setTimezone('99', 'Pacific/Auckland');
|
|
$USER->timezone = 'Europe/Prague';
|
|
|
|
$tz = get_user_timezone();
|
|
$this->assertSame('Europe/Prague', $tz);
|
|
|
|
$tz = get_user_timezone(99);
|
|
$this->assertSame('Europe/Prague', $tz);
|
|
$tz = get_user_timezone('99');
|
|
$this->assertSame('Europe/Prague', $tz);
|
|
|
|
$tz = get_user_timezone('Europe/Berlin');
|
|
$this->assertSame('Europe/Berlin', $tz);
|
|
|
|
// Server and user timezone not set.
|
|
|
|
$this->setTimezone('99', 'Pacific/Auckland');
|
|
$USER->timezone = '99';
|
|
|
|
$tz = get_user_timezone();
|
|
$this->assertSame(99.0, $tz);
|
|
|
|
$tz = get_user_timezone(99);
|
|
$this->assertSame(99.0, $tz);
|
|
$tz = get_user_timezone('99');
|
|
$this->assertSame(99.0, $tz);
|
|
|
|
$tz = get_user_timezone('Europe/Berlin');
|
|
$this->assertSame('Europe/Berlin', $tz);
|
|
}
|
|
|
|
public function test_dst_offset_on() {
|
|
$time = gmmktime(1, 1, 1, 3, 1, 2015);
|
|
$this->assertSame(3600, dst_offset_on($time, 'Pacific/Auckland'));
|
|
$this->assertSame(0, dst_offset_on($time, 'Australia/Perth'));
|
|
$this->assertSame(1800, dst_offset_on($time, 'Australia/Lord_Howe'));
|
|
$this->assertSame(0, dst_offset_on($time, 'Europe/Prague'));
|
|
$this->assertSame(0, dst_offset_on($time, 'America/New_York'));
|
|
|
|
$time = gmmktime(1, 1, 1, 5, 1, 2015);
|
|
$this->assertSame(0, dst_offset_on($time, 'Pacific/Auckland'));
|
|
$this->assertSame(0, dst_offset_on($time, 'Australia/Perth'));
|
|
$this->assertSame(0, dst_offset_on($time, 'Australia/Lord_Howe'));
|
|
$this->assertSame(3600, dst_offset_on($time, 'Europe/Prague'));
|
|
$this->assertSame(3600, dst_offset_on($time, 'America/New_York'));
|
|
}
|
|
|
|
public function test_make_timestamp() {
|
|
global $CFG;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
// There are quite a lot of problems, let's pick some less problematic zones for now.
|
|
$timezones = array('Europe/Prague', 'Europe/London', 'Australia/Perth', 'Pacific/Auckland', 'America/New_York', '99');
|
|
|
|
$dates = array(
|
|
array(2, 1, 0, 40, 40),
|
|
array(4, 3, 0, 30, 22),
|
|
array(9, 5, 0, 20, 19),
|
|
array(11, 28, 0, 10, 45),
|
|
);
|
|
$years = array(1999, 2009, 2014, 2018);
|
|
|
|
$this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
|
|
foreach ($timezones as $tz) {
|
|
foreach ($years as $year) {
|
|
foreach ($dates as $date) {
|
|
$result = make_timestamp($year, $date[0], $date[1], $date[2], $date[3], $date[4], $tz, true);
|
|
$expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
|
|
$expected->setDate($year, $date[0], $date[1]);
|
|
$expected->setTime($date[2], $date[3], $date[4]);
|
|
$this->assertSame($expected->getTimestamp(), $result,
|
|
'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->setTimezone('99', 'Pacific/Auckland');
|
|
foreach ($timezones as $tz) {
|
|
foreach ($years as $year) {
|
|
foreach ($dates as $date) {
|
|
$result = make_timestamp($year, $date[0], $date[1], $date[2], $date[3], $date[4], $tz, true);
|
|
$expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
|
|
$expected->setDate($year, $date[0], $date[1]);
|
|
$expected->setTime($date[2], $date[3], $date[4]);
|
|
$this->assertSame($expected->getTimestamp(), $result,
|
|
'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_usergetdate() {
|
|
global $CFG;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
// There are quite a lot of problems, let's pick some less problematic zones for now.
|
|
$timezones = array('Europe/Prague', 'Europe/London', 'Australia/Perth', 'Pacific/Auckland', 'America/New_York', '99');
|
|
|
|
$dates = array(
|
|
array(2, 1, 0, 40, 40),
|
|
array(4, 3, 0, 30, 22),
|
|
array(9, 5, 0, 20, 19),
|
|
array(11, 28, 0, 10, 45),
|
|
);
|
|
$years = array(1999, 2009, 2014, 2018);
|
|
|
|
$this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
|
|
foreach ($timezones as $tz) {
|
|
foreach ($years as $year) {
|
|
foreach ($dates as $date) {
|
|
$expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
|
|
$expected->setDate($year, $date[0], $date[1]);
|
|
$expected->setTime($date[2], $date[3], $date[4]);
|
|
$result = usergetdate($expected->getTimestamp(), $tz);
|
|
unset($result[0]); // Extra introduced by getdate().
|
|
$ex = array(
|
|
'seconds' => $date[4],
|
|
'minutes' => $date[3],
|
|
'hours' => $date[2],
|
|
'mday' => $date[1],
|
|
'wday' => (int)$expected->format('w'),
|
|
'mon' => $date[0],
|
|
'year' => $year,
|
|
'yday' => (int)$expected->format('z'),
|
|
'weekday' => $expected->format('l'),
|
|
'month' => $expected->format('F'),
|
|
);
|
|
$this->assertSame($ex, $result,
|
|
'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->setTimezone('99', 'Pacific/Auckland');
|
|
foreach ($timezones as $tz) {
|
|
foreach ($years as $year) {
|
|
foreach ($dates as $date) {
|
|
$expected = new \DateTime('now', new \DateTimeZone(($tz == 99 ? 'Pacific/Auckland' : $tz)));
|
|
$expected->setDate($year, $date[0], $date[1]);
|
|
$expected->setTime($date[2], $date[3], $date[4]);
|
|
$result = usergetdate($expected->getTimestamp(), $tz);
|
|
unset($result[0]); // Extra introduced by getdate().
|
|
$ex = array(
|
|
'seconds' => $date[4],
|
|
'minutes' => $date[3],
|
|
'hours' => $date[2],
|
|
'mday' => $date[1],
|
|
'wday' => (int)$expected->format('w'),
|
|
'mon' => $date[0],
|
|
'year' => $year,
|
|
'yday' => (int)$expected->format('z'),
|
|
'weekday' => $expected->format('l'),
|
|
'month' => $expected->format('F'),
|
|
);
|
|
$this->assertSame($ex, $result,
|
|
'Incorrect result for data ' . $expected->format("D, d M Y H:i:s O") . ' ' . $tz);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_userdate() {
|
|
global $CFG;
|
|
|
|
$this->resetAfterTest();
|
|
|
|
$dates = array(
|
|
array(2, 1, 0, 40, 40),
|
|
array(4, 3, 0, 30, 22),
|
|
array(9, 5, 0, 20, 19),
|
|
array(11, 28, 0, 10, 45),
|
|
);
|
|
$years = array(1999, 2009, 2014, 2018);
|
|
|
|
$users = array();
|
|
$users[] = $this->getDataGenerator()->create_user(array('timezone' => 99));
|
|
$users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Europe/Prague'));
|
|
$users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Pacific/Auckland'));
|
|
$users[] = $this->getDataGenerator()->create_user(array('timezone' => 'Australia/Perth'));
|
|
$users[] = $this->getDataGenerator()->create_user(array('timezone' => 'America/New_York'));
|
|
|
|
$format = get_string('strftimedaydatetime', 'langconfig');
|
|
|
|
$this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
|
|
foreach ($years as $year) {
|
|
foreach ($dates as $date) {
|
|
$expected = new \DateTime('now', new \DateTimeZone('UTC'));
|
|
$expected->setDate($year, $date[0], $date[1]);
|
|
$expected->setTime($date[2], $date[3], $date[4]);
|
|
|
|
foreach ($users as $user) {
|
|
$this->setUser($user);
|
|
$expected->setTimezone(new \DateTimeZone(($user->timezone == 99 ? 'Pacific/Auckland' : $user->timezone)));
|
|
$result = userdate($expected->getTimestamp(), '', 99, false, false);
|
|
date_default_timezone_set($expected->getTimezone()->getName());
|
|
$ex = strftime($format, $expected->getTimestamp());
|
|
date_default_timezone_set($CFG->timezone);
|
|
$this->assertSame($ex, $result);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function test_usertime() {
|
|
// This is a useless bad hack, it needs to be completely eliminated.
|
|
|
|
$time = gmmktime(1, 1, 1, 3, 1, 2015);
|
|
$this->assertSame($time - (60 * 60 * 1), usertime($time, '1'));
|
|
$this->assertSame($time - (60 * 60 * -1), usertime($time, '-1'));
|
|
$this->assertSame($time - (60 * 60 * 1), usertime($time, 'Europe/Prague'));
|
|
$this->assertSame($time - (60 * 60 * 8), usertime($time, 'Australia/Perth'));
|
|
$this->assertSame($time - (60 * 60 * 12), usertime($time, 'Pacific/Auckland'));
|
|
$this->assertSame($time - (60 * 60 * -5), usertime($time, 'America/New_York'));
|
|
|
|
$time = gmmktime(1, 1, 1, 5, 1, 2015);
|
|
$this->assertSame($time - (60 * 60 * 1), usertime($time, '1'));
|
|
$this->assertSame($time - (60 * 60 * -1), usertime($time, '-1'));
|
|
$this->assertSame($time - (60 * 60 * 1), usertime($time, 'Europe/Prague'));
|
|
$this->assertSame($time - (60 * 60 * 8), usertime($time, 'Australia/Perth'));
|
|
$this->assertSame($time - (60 * 60 * 12), usertime($time, 'Pacific/Auckland'));
|
|
$this->assertSame($time - (60 * 60 * -5), usertime($time, 'America/New_York'));
|
|
}
|
|
|
|
public function test_usertimezone() {
|
|
global $USER;
|
|
$this->resetAfterTest();
|
|
|
|
$this->setTimezone('Pacific/Auckland', 'Pacific/Auckland');
|
|
|
|
$USER->timezone = 'Europe/Prague';
|
|
$this->assertSame('Europe/Prague', usertimezone());
|
|
|
|
$USER->timezone = '1';
|
|
$this->assertSame('UTC+1', usertimezone());
|
|
|
|
$USER->timezone = '0';
|
|
$this->assertSame('UTC', usertimezone());
|
|
|
|
$USER->timezone = '99';
|
|
$this->assertSame('Pacific/Auckland', usertimezone());
|
|
|
|
$USER->timezone = '99';
|
|
$this->assertSame('Europe/Berlin', usertimezone('Europe/Berlin'));
|
|
|
|
$USER->timezone = '99';
|
|
$this->assertSame('Pacific/Auckland', usertimezone('99'));
|
|
|
|
$USER->timezone = 'Europe/Prague';
|
|
$this->assertSame('Europe/Prague', usertimezone('99'));
|
|
|
|
// When passed an unknown non-whole hour TZ, verify we round to closest
|
|
// hour. (Possible for legacy reasons when old timezones go away).
|
|
$USER->timezone = '-9.23';
|
|
$this->assertSame('UTC-9', usertimezone('99'));
|
|
}
|
|
}
|