Skip to content

Commit 838a448

Browse files
committed
ACP2E-3501: VAPT: Business Logic Error - future date as customer date of birth
1 parent 078c387 commit 838a448

File tree

3 files changed

+138
-4
lines changed

3 files changed

+138
-4
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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 Magento\Customer\Model\Customer;
11+
use Magento\Framework\Validator\AbstractValidator;
12+
use Magento\Store\Model\StoreManagerInterface;
13+
14+
/**
15+
* Customer name fields validator.
16+
*/
17+
class Dob extends AbstractValidator
18+
{
19+
/**
20+
* @var \DateTime
21+
*/
22+
private $currentDate;
23+
24+
/**
25+
* @var StoreManagerInterface
26+
*/
27+
private $storeManager;
28+
29+
/**
30+
* @param StoreManagerInterface $storeManager
31+
*/
32+
public function __construct(StoreManagerInterface $storeManager)
33+
{
34+
$this->currentDate = new \DateTime();
35+
$this->storeManager = $storeManager;
36+
}
37+
38+
/**
39+
* Validate name fields.
40+
*
41+
* @param Customer $customer
42+
* @return bool
43+
*/
44+
public function isValid($customer)
45+
{
46+
if (!$this->isValidDob($customer->getDob(), $customer->getStoreId())) {
47+
parent::_addMessages([['dob' => 'The Date of Birth should not be greater than today.']]);
48+
}
49+
50+
return count($this->_messages) == 0;
51+
}
52+
53+
/**
54+
* Check if specified dob is not in the future
55+
*
56+
* @param string|null $dobValue
57+
* @param int $storeId
58+
* @return bool
59+
*/
60+
private function isValidDob($dobValue, $storeId)
61+
{
62+
if ($dobValue != null) {
63+
64+
// Get the timezone of the store
65+
$store = $this->storeManager->getStore($storeId);
66+
$timezone = $store->getConfig('general/locale/timezone');
67+
68+
// Get the date of birth and set the time to 00:00:00
69+
$dobDate = new \DateTime($dobValue, new \DateTimeZone($timezone));
70+
$dobDate->setTime(0, 0, 0);
71+
72+
// Get the timestamp of the date of birth and the current date
73+
$dobTimestamp = $dobDate->getTimestamp();
74+
$currentTimestamp = $this->currentDate->getTimestamp();
75+
76+
// If the date's of birth first minute is in the future, return false - the day has not started yet
77+
if ($dobTimestamp > $currentTimestamp) {
78+
return false;
79+
}
80+
}
81+
82+
return true;
83+
}
84+
}

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: 46 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,50 @@ 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+
$futureDob = '14-12-2044';
244+
$customerDataArray['dob'] = $futureDob;
245+
$requestData = ['customer' => $customerDataArray, 'password' => CustomerHelper::PASSWORD];
246+
try {
247+
$this->_webApiCall($serviceInfo, $requestData);
248+
$this->fail('Expected exception did not occur.');
249+
} catch (\Exception $e) {
250+
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
251+
$expectedException = new InputException();
252+
$expectedException->addError(__('The Date of Birth should not be greater than today.'));
253+
$this->assertInstanceOf('SoapFault', $e);
254+
$this->checkSoapFault(
255+
$e,
256+
$expectedException->getRawMessage(),
257+
'env:Sender',
258+
$expectedException->getParameters() // expected error parameters
259+
);
260+
} else {
261+
$this->assertEquals(HTTPExceptionCodes::HTTP_BAD_REQUEST, $e->getCode());
262+
$exceptionData = $this->processRestExceptionResult($e);
263+
$expectedExceptionData = [
264+
'message' => 'The Date of Birth should not be greater than today.',
265+
];
266+
$this->assertEquals($expectedExceptionData, $exceptionData);
267+
}
268+
}
269+
}
226270
public function testCreateCustomerWithoutOptionalFields()
227271
{
228272
$serviceInfo = [

0 commit comments

Comments
 (0)