Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions system/I18n/TimeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -1237,8 +1237,6 @@ public function __isset($name): bool
}

/**
* This is called when we unserialize the Time object.
*
* @param array<mixed, mixed> $data
*/
public function __unserialize(array $data): void
Expand All @@ -1247,6 +1245,16 @@ public function __unserialize(array $data): void
$timezone = $data['timezone'] ?? null;
assert(is_string($date) && is_string($timezone));

parent::__construct($date, new DateTimeZone($timezone));
$locale = $data["\0*\0locale"] ?? null;
$toStringFormat = $data["\0*\0toStringFormat"] ?? null;

$this->timezone = new DateTimeZone($timezone);
$this->locale = is_string($locale) ? $locale : Locale::getDefault();

if (is_string($toStringFormat)) {
$this->toStringFormat = $toStringFormat;
}

parent::__construct($date, $this->timezone);
}
}
9 changes: 9 additions & 0 deletions tests/system/I18n/TimeLegacyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,15 @@ public function testUnserializeTimeObject(): void
$this->assertNotSame($time1, $time2);
}

public function testUnserializeRestoresTimezoneAndLocale(): void
{
$time = unserialize(serialize(TimeLegacy::parse('2024-03-01 10:00:00', 'Europe/Paris', 'fr_FR')));

$this->assertSame('Europe/Paris', $time->getTimezoneName());
$this->assertSame('mars', $time->toLocalizedString('MMMM'));
$this->assertSame(1, $time->difference(TimeLegacy::parse('2024-03-02 10:00:00', 'Europe/Paris'))->getDays());
}

public function testSetTestNowWithFaLocale(): void
{
Locale::setDefault('fa');
Expand Down
23 changes: 23 additions & 0 deletions tests/system/I18n/TimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1358,6 +1358,29 @@ public function testUnserializeTimeObject(): void
$this->assertNotSame($time1, $time2);
}

public function testUnserializeRestoresTimezoneAndLocale(): void
{
$time = unserialize(serialize(Time::parse('2024-03-01 10:00:00', 'Europe/Paris', 'fr_FR')));

$this->assertSame('Europe/Paris', $time->getTimezoneName());
$this->assertSame('mars', $time->toLocalizedString('MMMM'));
$this->assertSame(1, $time->difference(Time::parse('2024-03-02 10:00:00', 'Europe/Paris'))->getDays());
}

public function testUnserializeFallsBackWithoutTraitProperties(): void
{
$payload = sprintf(
'O:%d:"%s":3:{s:4:"date";s:26:"2024-03-01 10:00:00.000000";s:13:"timezone_type";i:3;s:8:"timezone";s:12:"Europe/Paris";}',
strlen(Time::class),
Time::class,
);
$time = unserialize($payload);

$this->assertSame('Europe/Paris', $time->getTimezoneName());
$this->assertSame('March', $time->toLocalizedString('MMMM'));
$this->assertSame('2024-03-01 10:00:00', (string) $time);
}

public function testSetTestNowWithTimeZone(): void
{
Time::setTestNow('2017/03/10 12:00', 'Asia/Tokyo');
Expand Down
1 change: 1 addition & 0 deletions user_guide_src/source/changelogs/v4.7.6.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Bugs Fixed
- **HTTP:** Fixed a bug where ``Negotiate::media()`` threw an ``ErrorException`` when the ``Accept`` header contained a value without a subtype, such as ``json`` or an empty list element. Such values are now ignored.
- **HTTP:** Fixed a bug where ``Negotiate`` could select a value that the request header excluded with ``q=0`` through a wildcard or a broader range. A ``q=0`` range now excludes every value it matches, such as ``text/html`` and ``text/html;charset=utf-8`` for ``text/*;q=0``, or ``en-US`` for ``en;q=0``, unless a more specific range in the header accepts the value. For example, ``Accept-Encoding: gzip;q=0, *`` no longer selects ``gzip``.
- **HTTP:** Fixed stale ``$_REQUEST`` values after ``SiteURIFactory`` normalizes GET parameters, so ``Validation::withRequest()`` reads the normalized values while unrelated REQUEST entries are preserved.
- **I18n:** Fixed a regression in v4.7.1 where ``Time`` and ``TimeLegacy`` lost their timezone, locale and ``__toString()`` format after ``unserialize()``, so methods such as ``difference()`` and ``getTimezoneName()`` threw an ``Error``.
- **Security:** Fixed a bug where a valid JSON request body that is not an object (for example a JSON array) was corrupted after CSRF verification when the token was sent via the ``X-CSRF-TOKEN`` header.

See the repo's
Expand Down
Loading