Skip to content

Commit 13e276c

Browse files
committed
Merge branch 'ACP2E-3501' of https://github.com/adobe-commerce-tier-4/magento2ce into PR-Tier4-VK-2025-01-09
2 parents e4a46e9 + e466720 commit 13e276c

File tree

3 files changed

+133
-4
lines changed

3 files changed

+133
-4
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright 2024 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Customer\Model\Validator;
9+
10+
use DateTimeZone;
11+
use Magento\Customer\Model\Customer;
12+
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
13+
use Magento\Framework\Validator\AbstractValidator;
14+
use Magento\Store\Model\ScopeInterface;
15+
16+
/**
17+
* Customer dob field validator.
18+
*/
19+
class Dob extends AbstractValidator
20+
{
21+
/**
22+
* @var TimezoneInterface
23+
*/
24+
private $timezone;
25+
26+
/**
27+
* @param TimezoneInterface $timezone
28+
*/
29+
public function __construct(TimezoneInterface $timezone)
30+
{
31+
$this->timezone = $timezone;
32+
}
33+
34+
/**
35+
* Validate dob field.
36+
*
37+
* @param Customer $customer
38+
* @return bool
39+
*/
40+
public function isValid($customer): bool
41+
{
42+
$storeId = (int)$customer->getStoreId();
43+
$timezone = new DateTimeZone($this->timezone->getConfigTimezone(ScopeInterface::SCOPE_STORE, $storeId));
44+
45+
if (!$this->isValidDob($customer->getDob(), $timezone)) {
46+
$this->_addMessages([['dob' => 'The Date of Birth should not be greater than today.']]);
47+
}
48+
49+
return count($this->_messages) === 0;
50+
}
51+
52+
/**
53+
* Check if specified dob is not in the future
54+
*
55+
* @param string|null $dobValue
56+
* @param DateTimeZone $timezone
57+
* @return bool
58+
*/
59+
private function isValidDob(?string $dobValue, DateTimeZone $timezone): bool
60+
{
61+
if ($dobValue) {
62+
63+
// Get the date of birth and set the time to 00:00:00
64+
$dobDate = new \DateTime($dobValue, $timezone);
65+
$dobDate->setTime(0, 0, 0);
66+
67+
// Get the timestamp of the date of birth and the current date
68+
$dobTimestamp = $dobDate->getTimestamp();
69+
$currentTimestamp = time();
70+
71+
// If the date's of birth first minute is in the future, return false - the day has not started yet
72+
return ($dobTimestamp <= $currentTimestamp);
73+
}
74+
75+
return true;
76+
}
77+
}

app/code/Magento/Customer/etc/validation.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
/**
4-
* Copyright © Magento, Inc. All rights reserved.
5-
* See COPYING.txt for license details.
4+
* Copyright 2012 Adobe
5+
* All Rights Reserved.
66
*/
77
-->
88
<validation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Validator/etc/validation.xsd">
@@ -23,12 +23,18 @@
2323
<constraint alias="name_validator" class="Magento\Customer\Model\Validator\Name" />
2424
</entity_constraints>
2525
</rule>
26+
<rule name="check_dob">
27+
<entity_constraints>
28+
<constraint alias="dob_validator" class="Magento\Customer\Model\Validator\Dob" />
29+
</entity_constraints>
30+
</rule>
2631
</rules>
2732
<groups>
2833
<group name="save">
2934
<uses>
3035
<use rule="check_eav"/>
3136
<use rule="check_name"/>
37+
<use rule="check_dob"/>
3238
</uses>
3339
</group>
3440
<group name="form">

dev/tests/api-functional/testsuite/Magento/Customer/Api/AccountManagementTest.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* Copyright © Magento, Inc. All rights reserved.
4-
* See COPYING.txt for license details.
3+
* Copyright 2015 Adobe
4+
* All Rights Reserved.
55
*/
66

77
namespace Magento\Customer\Api;
@@ -223,6 +223,52 @@ public function testCreateCustomerWithErrors()
223223
}
224224
}
225225

226+
public function testCreateCustomerWithDateOfBirthInFuture()
227+
{
228+
$serviceInfo = [
229+
'rest' => [
230+
'resourcePath' => self::RESOURCE_PATH,
231+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, ],
232+
'soap' => [
233+
'service' => self::SERVICE_NAME,
234+
'serviceVersion' => self::SERVICE_VERSION,
235+
'operation' => self::SERVICE_NAME . 'CreateAccount',
236+
],
237+
];
238+
239+
$customerDataArray = $this->dataObjectProcessor->buildOutputDataArray(
240+
$this->customerHelper->createSampleCustomerDataObject(),
241+
\Magento\Customer\Api\Data\CustomerInterface::class
242+
);
243+
$date = new \DateTime();
244+
$date->modify('+1 month');
245+
$futureDob = $date->format('Y-m-d');
246+
$customerDataArray['dob'] = $futureDob;
247+
$requestData = ['customer' => $customerDataArray, 'password' => CustomerHelper::PASSWORD];
248+
try {
249+
$this->_webApiCall($serviceInfo, $requestData);
250+
$this->fail('Expected exception did not occur.');
251+
} catch (\Exception $e) {
252+
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
253+
$expectedException = new InputException();
254+
$expectedException->addError(__('The Date of Birth should not be greater than today.'));
255+
$this->assertInstanceOf('SoapFault', $e);
256+
$this->checkSoapFault(
257+
$e,
258+
$expectedException->getRawMessage(),
259+
'env:Sender',
260+
$expectedException->getParameters() // expected error parameters
261+
);
262+
} else {
263+
$this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
264+
$exceptionData = $this->processRestExceptionResult($e);
265+
$expectedExceptionData = [
266+
'message' => 'The Date of Birth should not be greater than today.',
267+
];
268+
$this->assertEquals($expectedExceptionData, $exceptionData);
269+
}
270+
}
271+
}
226272
public function testCreateCustomerWithoutOptionalFields()
227273
{
228274
$serviceInfo = [

0 commit comments

Comments
 (0)