Skip to content

Commit 2be406c

Browse files
committed
ACP2E-4311: Error loading some locale
1 parent 1925cae commit 2be406c

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

app/code/Magento/Customer/Plugin/ValidateDobOnSave.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Magento\Framework\Exception\InputException;
1515
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
1616
use Magento\Framework\Stdlib\DateTime;
17+
use Magento\Framework\Locale\ResolverInterface;
1718

1819
class ValidateDobOnSave
1920
{
@@ -27,16 +28,24 @@ class ValidateDobOnSave
2728
*/
2829
private $json;
2930

31+
/**
32+
* @var ResolverInterface
33+
*/
34+
private $localeResolver;
35+
3036
/**
3137
* @param EavConfig $eavConfig
3238
* @param JsonSerializer $json
39+
* @param ResolverInterface $localeResolver
3340
*/
3441
public function __construct(
3542
EavConfig $eavConfig,
36-
JsonSerializer $json
43+
JsonSerializer $json,
44+
ResolverInterface $localeResolver
3745
) {
3846
$this->eavConfig = $eavConfig;
3947
$this->json = $json;
48+
$this->localeResolver = $localeResolver;
4049
}
4150

4251
/**
@@ -67,8 +76,9 @@ public function aroundSave(
6776
}
6877

6978
if ($dobDate) {
79+
$normalizedDob = $dobDate->format(DateTime::DATE_PHP_FORMAT);
80+
$customer->setDob($normalizedDob);
7081
$attr = $this->eavConfig->getAttribute('customer', 'dob');
71-
7282
$rules = $attr->getData('validate_rules');
7383
if (is_string($rules) && $rules !== '') {
7484
try {
@@ -127,9 +137,21 @@ private function parseDate($value): ?\DateTimeImmutable
127137
$seconds = ($intVal >= 10000000000) ? intdiv($intVal, 1000) : $intVal;
128138
return (new \DateTimeImmutable('@' . $seconds))->setTimezone(new \DateTimeZone('UTC'));
129139
}
140+
$stringValue = (string)$value;
141+
$locale = $this->localeResolver->getLocale();
142+
$formatter = new \IntlDateFormatter(
143+
$locale,
144+
\IntlDateFormatter::SHORT,
145+
\IntlDateFormatter::NONE
146+
);
147+
$formatter->setPattern(DateTime::DATE_INTERNAL_FORMAT);
148+
$timestamp = $formatter->parse($stringValue);
149+
if ($timestamp !== false) {
150+
return new \DateTimeImmutable('@' . $timestamp);
151+
}
130152

131153
try {
132-
return new \DateTimeImmutable((string)$value);
154+
return new \DateTimeImmutable($stringValue);
133155
} catch (\Exception $e) {
134156
return null;
135157
}

app/code/Magento/Customer/Test/Unit/Plugin/ValidateDobOnSaveTest.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Serialize\Serializer\Json as JsonSerializer;
1818
use PHPUnit\Framework\MockObject\MockObject;
1919
use PHPUnit\Framework\TestCase;
20+
use Magento\Framework\Locale\ResolverInterface;
2021

2122
/**
2223
* Unit test for validate date of birth plugin
@@ -42,10 +43,13 @@ protected function setUp(): void
4243
$this->eavConfig = $this->createMock(EavConfig::class);
4344
$this->json = $this->createMock(JsonSerializer::class);
4445
$this->repo = $this->createMock(CustomerRepositoryInterface::class);
46+
$localeResolver = $this->createMock(ResolverInterface::class);
47+
$localeResolver->method('getLocale')->willReturn('en_US');
4548

4649
$this->plugin = new ValidateDobOnSave(
4750
$this->eavConfig,
48-
$this->json
51+
$this->json,
52+
$localeResolver
4953
);
5054
}
5155

@@ -322,6 +326,21 @@ public function testDobZeroEpochInvalidThrows(): void
322326
$this->assertFalse($called);
323327
}
324328

329+
public function testDobIsNormalized(): void
330+
{
331+
$dob = '2005-12-01';
332+
$customer = $this->createMock(CustomerInterface::class);
333+
$customer->method('getDob')->willReturn($dob);
334+
$customer->expects($this->once())
335+
->method('setDob')
336+
->with('2005-12-01');
337+
$this->mockAttributeRulesArray(['date_range_min' => '1980-01-01', 'date_range_max' => '2010-12-31']);
338+
$called = false;
339+
$proceed = $this->proceedPlugin($called, $customer);
340+
$this->plugin->aroundSave($this->repo, $proceed, $customer);
341+
$this->assertTrue($called);
342+
}
343+
325344
/**
326345
* Create a proceed closure that marks $called and returns either the given $result or the original $customer.
327346
*

0 commit comments

Comments
 (0)