Skip to content

Commit 9136564

Browse files
committed
finish validation; add tests (#22833)
1 parent 6e2043e commit 9136564

File tree

5 files changed

+113
-6
lines changed

5 files changed

+113
-6
lines changed

app/code/Magento/User/Model/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ protected function _getValidationRulesBeforeSave()
323323
}
324324
}
325325

326-
if ($this->hasExpiresAt()) {
326+
if (!empty($this->getExpiresAt())) {
327327
$this->validationRules->addExpiresAtRule($validator);
328328
}
329329
return $validator;

app/code/Magento/User/Model/UserValidationRules.php

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
namespace Magento\User\Model;
88

9+
use Magento\User\Model\Validator\ExpiresAt;
910
use Magento\Framework\Validator\EmailAddress;
1011
use Magento\Framework\Validator\NotEmpty;
1112
use Magento\Framework\Validator\Regex;
1213
use Magento\Framework\Validator\StringLength;
14+
use Magento\Framework\App\ObjectManager;
1315

1416
/**
1517
* Class for adding validation rules to an Admin user
@@ -23,6 +25,20 @@ class UserValidationRules
2325
* Minimum length of admin password
2426
*/
2527
const MIN_PASSWORD_LENGTH = 7;
28+
/**
29+
* @var Validator\ExpiresAt|null
30+
*/
31+
private $expiresValiator;
32+
33+
/**
34+
* UserValidationRules constructor.
35+
* @param Validator\ExpiresAt|null $expiresValiator
36+
*/
37+
public function __construct(?ExpiresAt $expiresValiator = null)
38+
{
39+
$this->expiresValiator = $expiresValiator
40+
?: ObjectManager::getInstance()->get(ExpiresAt::class);
41+
}
2642

2743
/**
2844
* Adds validation rule for user first name, last name, username and email
@@ -130,27 +146,30 @@ public function addPasswordConfirmationRule(
130146
* Adds validation rule for expiration date.
131147
* @param \Magento\Framework\Validator\DataObject $validator
132148
* @return \Magento\Framework\Validator\DataObject
149+
* @throws \Zend_Validate_Exception
133150
*/
134151
public function addExpiresAtRule(\Magento\Framework\Validator\DataObject $validator)
135152
{
136-
$expiresValidator = new \Zend_Validate_Date(
153+
$dateValidator = new \Zend_Validate_Date(
137154
[
138155
'format' => \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT,
139156
]
140157
);
141-
$expiresValidator->setMessage(
158+
$dateValidator->setMessage(
142159
__('"Expiration date" invalid type entered.'),
143160
\Zend_Validate_Date::INVALID
144161
);
145-
$expiresValidator->setMessage(
162+
$dateValidator->setMessage(
146163
__('"Expiration date" is not a valid date.'),
147164
\Zend_Validate_Date::INVALID_DATE
148165
);
149-
$expiresValidator->setMessage(
166+
$dateValidator->setMessage(
150167
__('"Expiration date" does not fit the required date format.'),
151168
\Zend_Validate_Date::FALSEFORMAT
152169
);
153-
$validator->addRule($expiresValidator, 'expires_at');
170+
$validator->addRule($dateValidator, 'expires_at');
171+
$validator->addRule($this->expiresValiator, 'expires_at');
172+
154173
return $validator;
155174
}
156175
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Model\Validator;
8+
9+
use Magento\Framework\Validator\AbstractValidator;
10+
11+
/**
12+
* Class ExpiresAt
13+
* @package Magento\User\Model\Validator
14+
*/
15+
class ExpiresAt extends AbstractValidator
16+
{
17+
18+
/**
19+
* Ensure that the given date is later than the current date.
20+
* @param String $value
21+
* @return bool
22+
* @throws \Exception
23+
*/
24+
public function isValid($value)
25+
{
26+
$currentTime = new \DateTime();
27+
$expiresAt = new \DateTime($value);
28+
29+
if ($expiresAt < $currentTime) {
30+
$message = __('The expiration date must be later than the current date.');
31+
$this->_addMessages([$message]);
32+
}
33+
34+
return !$this->hasMessages();
35+
}
36+
}

app/code/Magento/User/Test/Unit/Model/UserValidationRulesTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,10 @@ public function testAddPasswordConfirmationRule()
4242
$this->validator->expects($this->once())->method('addRule')->willReturn($this->validator);
4343
$this->assertSame($this->validator, $this->rules->addPasswordConfirmationRule($this->validator, ''));
4444
}
45+
46+
public function testAddExpiresAtRule()
47+
{
48+
$this->validator->expects($this->once())->method('addRule')->willReturn($this->validator);
49+
$this->assertSame($this->validator, $this->rules->addExpiresAtRule($this->validator));
50+
}
4551
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\User\Test\Unit\Model\Validator;
8+
9+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
10+
11+
/**
12+
* Class ExpiresAtValidatorTest
13+
* @package Magento\User\Test\Unit\Model
14+
*/
15+
class ExpiresAtTest extends \PHPUnit\Framework\TestCase
16+
{
17+
18+
/** @var \Magento\User\Model\Validator\ExpiresAt */
19+
protected $validator;
20+
21+
protected function setUp()
22+
{
23+
$objectManager = new ObjectManager($this);
24+
25+
$this->validator = $objectManager->getObject(
26+
\Magento\User\Model\Validator\ExpiresAt::class
27+
);
28+
}
29+
30+
public function testIsValidWhenInvalid()
31+
{
32+
static::assertFalse($this->validator->isValid('2018-01-01 00:00:00'));
33+
static::assertContains(
34+
'The expiration date must be later than the current date.',
35+
$this->validator->getMessages()
36+
);
37+
}
38+
39+
public function testIsValidWhenValid()
40+
{
41+
$futureDate = new \DateTime();
42+
$futureDate->modify('+1 days');
43+
static::assertTrue($this->validator->isValid($futureDate->format('Y-m-d H:i:s')));
44+
static::assertEquals([], $this->validator->getMessages());
45+
}
46+
}

0 commit comments

Comments
 (0)