19declare(strict_types=1);
21use PHPUnit\Framework\Attributes\RunInSeparateProcess;
22use PHPUnit\Framework\Attributes\DataProvider;
31 protected function setUp(): void
35 class_exists(
'ilDateTime');
39 $logger = $this->getMockBuilder(ilLogger::class)
40 ->disableOriginalConstructor()
43 $logger_factory = $this->getMockBuilder(ilLoggerFactory::class)
44 ->disableOriginalConstructor()
45 ->onlyMethods([
'getComponentLogger'])
47 $logger_factory->method(
'getComponentLogger')->willReturn($logger);
51 $this->current_time = time();
64 [
'2001-01-01',
'1. Jan 2001'],
65 [
'2001-01-01 00:00:00',
'1. Jan 2001'],
66 [978307200,
'1. Jan 2001'],
72 #[DataProvider('dataProviderFormatDateWithDateFormat')]
76 $this->assertEquals($output, $helper->formatDate($input));
83 [
'2001-01-01 00:00:00',
'1. Jan 2001, 01:00'],
84 [978307200,
'1. Jan 2001, 01:00'],
90 #[DataProvider('dataProviderFormatDateTimeWithDateTimeFormat')]
94 $this->assertEquals($output, $helper->formatDateTime($input));
103 $helper->formatDate(
'2001-01-01');
107 $helper->formatDate(
'2001-01-01');
111 $helper->formatDate(
'2001-01-01');
121 class_exists(
'ilDateTime');
124 'date: unix-int ok' => [
'date', $ts,
IL_CAL_UNIX,
null,
'unix timestamp (int) accepted for date'],
125 'date: unix-string ok' => [
130 'unix timestamp (string) accepted for date'
132 'datetime: unix-int ok' => [
137 'unix timestamp (int) accepted for datetime'
139 'datetime: unix-string ok' => [
144 'unix timestamp (string) accepted for datetime'
147 'date: negative-unix ok' => [
'date', -1,
IL_CAL_UNIX,
null,
'negative unix timestamp accepted as int'],
148 'datetime: negative-unix ok' => [
153 'negative unix timestamp accepted as int'
156 'date: date ok' => [
'date',
'2025-09-12',
IL_CAL_DATE,
null,
'valid date string'],
157 'datetime: datetime ok' => [
159 '2025-09-12 20:30:00',
162 'valid date-time string'
164 'date: YYYYMMDD ok' => [
'date',
'20250912',
IL_CAL_DATE,
null,
'numeric YYYYMMDD is not treated as unix'],
166 'date: unix-string with DATE throws' => [
170 InvalidArgumentException::class,
171 'unix-like string with DATE format should be rejected'
173 'datetime: unix-string with DATETIME throws' => [
177 InvalidArgumentException::class,
178 'unix-like string with DATETIME format should be rejected'
180 'date: date with UNIX throws' => [
184 InvalidArgumentException::class,
185 'non-numeric with UNIX should be rejected'
187 'datetime: datetime with UNIX throws' => [
189 '2025-09-12 20:30:00',
191 InvalidArgumentException::class,
192 'non-numeric with UNIX should be rejected'
201 #[DataProvider('provideExplicitFormatCases')]
206 ?
string $expected_exception,
211 $cb =
static function () use ($helper, $method, $input, $format) {
212 return $method ===
'date'
214 : $helper->formatDateTime($input,
null, $format);
217 if ($expected_exception ===
null) {
230 'formatDateTime' => [
'formatDateTime'],
231 'formatDate' => [
'formatDate'],
235 #[DataProvider('dateFormattingMethodsProvider')]
239 $this->expectExceptionMessage(
'Cannot parse date: invalid-date');
240 $helper->$method(
'invalid-date');
243 #[RunInSeparateProcess]
246 $this->mockUserLanguageGerman();
249 $this->assertEquals(
'1. Mai 2001', $helper->formatDate(
'2001-05-01 01:30:59', $this->getUserMock()));
252 #[RunInSeparateProcess]
255 $this->mockUserLanguageGerman();
258 $this->assertEquals(
'1. Mai 2001, 03:30', $helper->formatDateTime(
'2001-05-01 01:30:59', $this->getUserMock()));
263 $user = $this->getMockBuilder(ilObjUser::class)
264 ->disableOriginalConstructor()
265 ->onlyMethods([
'getTimeFormat',
'getLanguage',
'getTimeZone'])
268 $user->method(
'getLanguage')->willReturn(
'de');
269 $user->method(
'getTimeZone')->willReturn(self::USER_TIME_ZONE);
276 $lng = $this->getMockBuilder(ilLanguage::class)
277 ->onlyMethods([
'txt',
'loadLanguageModule'])
278 ->disableOriginalConstructor()
280 $lng->method(
'txt')->willReturnCallback(
function (
string $topic):
string {
281 return match ($topic) {
282 'month_01_short' =>
'Jan',
283 'month_01_long' =>
'January',
284 'month_02_short' =>
'Feb',
285 'month_03_short' =>
'Mar',
286 'month_04_short' =>
'Apr',
287 'month_05_short' =>
'May',
288 'month_06_short' =>
'Jun',
289 'month_07_short' =>
'Jul',
290 'month_08_short' =>
'Aug',
291 'month_09_short' =>
'Sep',
292 'month_10_short' =>
'Oct',
293 'month_11_short' =>
'Nov',
294 'month_12_short' =>
'Dec',
295 'no_date' =>
'No date',
297 'yesterday' =>
'Yesterday',
298 'tomorrow' =>
'Tomorrow',
299 default =>
'-' . $topic .
'-'
308 if (!defined(
'ILIAS_LOG_ENABLED')) {
309 define(
'ILIAS_LOG_ENABLED',
false);
311 if (!defined(
'ILIAS_ABSOLUTE_PATH')) {
312 define(
'ILIAS_ABSOLUTE_PATH', dirname(__FILE__, 5));
315 $ilClientIniFile = $this->getMockBuilder(ilIniFile::class)
316 ->disableOriginalConstructor()
320 $ilDB = $this->createMock(ilDBInterface::class);
322 $statement = $this->createMock(ilDBStatement::class);
324 if (str_contains($query,
'SELECT * FROM lng_modules')) {
325 $statement->method(
'numRows')->willReturn(1);
326 $statement->method(
'fetchRow')->willReturn([
327 'lang_array' => serialize([
328 'month_01_short' =>
'Jan',
329 'month_01_long' =>
'Januar',
330 'month_02_short' =>
'Feb',
331 'month_03_short' =>
'Mär',
332 'month_04_short' =>
'Apr',
333 'month_05_short' =>
'Mai',
334 'month_06_short' =>
'Jun',
335 'month_07_short' =>
'Jul',
336 'month_08_short' =>
'Aug',
337 'month_09_short' =>
'Sep',
338 'month_10_short' =>
'Okt',
339 'month_11_short' =>
'Nov',
340 'month_12_short' =>
'Dez',
341 'no_date' =>
'Kein Datum',
343 'yesterday' =>
'Gestern',
344 'tomorrow' =>
'Morgen'
348 $statement->method(
'numRows')->willReturn(0);
349 $statement->method(
'fetchRow')->willReturn(
false);
assertThrows(callable $cb, ?string $expected_class=null, ?string $expected_message=null)
@template T of Throwable
assertDoesNotThrow(callable $cb, string $message='')
setGlobalVariable(string $name, mixed $value)
const string DEFAULT_TIME_ZONE
static provideExplicitFormatCases()
static dataProviderFormatDateTimeWithDateTimeFormat()
testExplicitFormatsWorkAsExpected(string $method, $input, int $format, ?string $expected_exception, string $why)
testFormatDateTimeWithUserLanguage()
testFormatDateWithUserLanguage()
static dataProviderFormatDateWithDateFormat()
testHelperDoesNotChangeUseRelativeDates()
static dateFormattingMethodsProvider()
testFormatDateWithDefaultFormat($input, $output)
testCannotFormatNonDateString(string $method)
const string USER_TIME_ZONE
testFormatDateTimeWithDefaultFormat($input, $output)
formatDate($raw_date_input, ?ilObjUser $user=null, ?int $date_format=null)
static useRelativeDates()
static setUseRelativeDates(bool $a_status)
set use relative dates
static _restoreDefaultTimeZone()
static _setDefaultTimeZone(string $a_tz)
setGlobalVariable(string $name, mixed $value)