Skip to content

Commit

Permalink
Merge pull request #916 from phalcon/3.4.x
Browse files Browse the repository at this point in the history
v3.4.4
  • Loading branch information
sergeyklay authored Aug 19, 2019
2 parents 08ab1a9 + 78bdf9f commit 8efbaa0
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 18 deletions.
28 changes: 10 additions & 18 deletions Library/Phalcon/Validation/Validator/MongoId.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@
| to [email protected] so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Anton Kornilov <[email protected]> |
| Maintainer: Wajdi Jurry <[email protected]>
+------------------------------------------------------------------------+
*/

namespace Phalcon\Validation\Validator;

use MongoId as Id;
use MongoDB\BSON\ObjectId;
use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Message;
use Phalcon\Validation\Exception as ValidationException;

/**
* MongoId validator
Expand All @@ -36,30 +36,22 @@ class MongoId extends Validator
* @param Validation $validation
* @param string $attribute
* @return bool
* @throws ValidationException
*/
public function validate(Validation $validation, $attribute)
{
if (!extension_loaded('mongo')) {
throw new ValidationException('Mongo extension is not available');
}

$value = $validation->getValue($attribute);
$allowEmpty = $this->hasOption('allowEmpty');
$result = ($allowEmpty && empty($value)) ? true : Id::isValid($value);

if (!$result) {
$message = ($this->hasOption('message')) ? $this->getOption('message') : 'MongoId is not valid';
if ($allowEmpty && empty($value)) {
return true;
}

$validation->appendMessage(
new Message(
$message,
$attribute,
'MongoId'
)
);
if ($value instanceof ObjectId || preg_match('/^[a-f\d]{24}$/i', $value)) {
return true;
}

return $result;
$message = $this->hasOption('message') ? $this->getOption('message') : 'MongoId is not valid';
$validation->appendMessage(new Message($message, $attribute, 'MongoId'));
return false;
}
}
61 changes: 61 additions & 0 deletions tests/unit/Validation/Validator/MongoIdTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Phalcon\Test\Validation\Validator;


use MongoDB\BSON\ObjectId;
use Phalcon\Test\Test\UnitTestCaseTest as Test;
use Phalcon\Validation;
use Phalcon\Validation\Validator\MongoId;

/**
* \Phalcon\Test\Validation\Validator\MongoIdTest
* Tests for Phalcon\Validation\Validator\MongoId component
*
* @copyright (c) 2011-2019 Phalcon Team
* @link http://www.phalconphp.com
* @author Wajdi Jurry <[email protected]>
* @package Phalcon\Validation\Validator
* @group Validation
*
* The contents of this file are subject to the New BSD License that is
* bundled with this package in the file docs/LICENSE.txt
*
* If you did not receive a copy of the license and are unable to obtain it
* through the world-wide-web, please send an email to [email protected]
* so that we can send you a copy immediately.
*/
class MongoIdTest extends Test
{
private $validator;

public function setUp()
{
$this->validator = new Validation();
$this->validator->add(
'id',
new MongoId([
'allowEmpty' => true
])
);
return parent::setUp(); // TODO: Change the autogenerated stub
}

public function testValidMongoIds()
{
$stringMongoId = "5d4a0496e4895300110e3272";
$objectId = new ObjectId("5d4a0496e4895300110e3272");
$emptyId = '';

$this->assertCount(0, $this->validator->validate(['id' => $stringMongoId]));
$this->assertCount(0, $this->validator->validate(['id' => $objectId]));
$this->assertCount(0, $this->validator->validate(['id' => $emptyId]));
}

public function testInvalidMongoId()
{
$invalidMongoId = "12345";

$this->assertCount(1, $this->validator->validate(['id' => $invalidMongoId]));
}
}

0 comments on commit 8efbaa0

Please sign in to comment.