Skip to content

Commit

Permalink
finish validation; add tests (#22833)
Browse files Browse the repository at this point in the history
  • Loading branch information
lfolco committed May 12, 2019
1 parent 6e2043e commit 9136564
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/code/Magento/User/Model/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ protected function _getValidationRulesBeforeSave()
}
}

if ($this->hasExpiresAt()) {
if (!empty($this->getExpiresAt())) {
$this->validationRules->addExpiresAtRule($validator);
}
return $validator;
Expand Down
29 changes: 24 additions & 5 deletions app/code/Magento/User/Model/UserValidationRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

namespace Magento\User\Model;

use Magento\User\Model\Validator\ExpiresAt;
use Magento\Framework\Validator\EmailAddress;
use Magento\Framework\Validator\NotEmpty;
use Magento\Framework\Validator\Regex;
use Magento\Framework\Validator\StringLength;
use Magento\Framework\App\ObjectManager;

/**
* Class for adding validation rules to an Admin user
Expand All @@ -23,6 +25,20 @@ class UserValidationRules
* Minimum length of admin password
*/
const MIN_PASSWORD_LENGTH = 7;
/**
* @var Validator\ExpiresAt|null
*/
private $expiresValiator;

/**
* UserValidationRules constructor.
* @param Validator\ExpiresAt|null $expiresValiator
*/
public function __construct(?ExpiresAt $expiresValiator = null)
{
$this->expiresValiator = $expiresValiator
?: ObjectManager::getInstance()->get(ExpiresAt::class);
}

/**
* Adds validation rule for user first name, last name, username and email
Expand Down Expand Up @@ -130,27 +146,30 @@ public function addPasswordConfirmationRule(
* Adds validation rule for expiration date.
* @param \Magento\Framework\Validator\DataObject $validator
* @return \Magento\Framework\Validator\DataObject
* @throws \Zend_Validate_Exception
*/
public function addExpiresAtRule(\Magento\Framework\Validator\DataObject $validator)
{
$expiresValidator = new \Zend_Validate_Date(
$dateValidator = new \Zend_Validate_Date(
[
'format' => \Magento\Framework\Stdlib\DateTime::DATETIME_INTERNAL_FORMAT,
]
);
$expiresValidator->setMessage(
$dateValidator->setMessage(
__('"Expiration date" invalid type entered.'),
\Zend_Validate_Date::INVALID
);
$expiresValidator->setMessage(
$dateValidator->setMessage(
__('"Expiration date" is not a valid date.'),
\Zend_Validate_Date::INVALID_DATE
);
$expiresValidator->setMessage(
$dateValidator->setMessage(
__('"Expiration date" does not fit the required date format.'),
\Zend_Validate_Date::FALSEFORMAT
);
$validator->addRule($expiresValidator, 'expires_at');
$validator->addRule($dateValidator, 'expires_at');
$validator->addRule($this->expiresValiator, 'expires_at');

return $validator;
}
}
36 changes: 36 additions & 0 deletions app/code/Magento/User/Model/Validator/ExpiresAt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\User\Model\Validator;

use Magento\Framework\Validator\AbstractValidator;

/**
* Class ExpiresAt
* @package Magento\User\Model\Validator
*/
class ExpiresAt extends AbstractValidator
{

/**
* Ensure that the given date is later than the current date.
* @param String $value
* @return bool
* @throws \Exception
*/
public function isValid($value)
{
$currentTime = new \DateTime();
$expiresAt = new \DateTime($value);

if ($expiresAt < $currentTime) {
$message = __('The expiration date must be later than the current date.');
$this->_addMessages([$message]);
}

return !$this->hasMessages();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,10 @@ public function testAddPasswordConfirmationRule()
$this->validator->expects($this->once())->method('addRule')->willReturn($this->validator);
$this->assertSame($this->validator, $this->rules->addPasswordConfirmationRule($this->validator, ''));
}

public function testAddExpiresAtRule()
{
$this->validator->expects($this->once())->method('addRule')->willReturn($this->validator);
$this->assertSame($this->validator, $this->rules->addExpiresAtRule($this->validator));
}
}
46 changes: 46 additions & 0 deletions app/code/Magento/User/Test/Unit/Model/Validator/ExpiresAtTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\User\Test\Unit\Model\Validator;

use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;

/**
* Class ExpiresAtValidatorTest
* @package Magento\User\Test\Unit\Model
*/
class ExpiresAtTest extends \PHPUnit\Framework\TestCase
{

/** @var \Magento\User\Model\Validator\ExpiresAt */
protected $validator;

protected function setUp()
{
$objectManager = new ObjectManager($this);

$this->validator = $objectManager->getObject(
\Magento\User\Model\Validator\ExpiresAt::class
);
}

public function testIsValidWhenInvalid()
{
static::assertFalse($this->validator->isValid('2018-01-01 00:00:00'));
static::assertContains(
'The expiration date must be later than the current date.',
$this->validator->getMessages()
);
}

public function testIsValidWhenValid()
{
$futureDate = new \DateTime();
$futureDate->modify('+1 days');
static::assertTrue($this->validator->isValid($futureDate->format('Y-m-d H:i:s')));
static::assertEquals([], $this->validator->getMessages());
}
}

0 comments on commit 9136564

Please sign in to comment.