-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish validation; add tests (#22833)
- Loading branch information
Showing
5 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
app/code/Magento/User/Test/Unit/Model/Validator/ExpiresAtTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |