field validation additions and tests
This commit is contained in:
@@ -31,4 +31,11 @@ namespace Espo\Core\FieldValidators;
|
||||
|
||||
class CurrencyType extends FloatType
|
||||
{
|
||||
protected function isNotEmpty(\Espo\ORM\Entity $entity, $field)
|
||||
{
|
||||
return
|
||||
$entity->has($field) && $entity->get($field) !== null &&
|
||||
$entity->has($field . 'Currency') && $entity->get($field . 'Currency') !== null &&
|
||||
$entity->get($field . 'Currency') !== '';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ namespace Espo\Core\FieldValidators;
|
||||
class DateType extends BaseType
|
||||
{
|
||||
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
return $this->isNotEmpty($entity, $field);
|
||||
}
|
||||
|
||||
protected function isNotEmpty(\Espo\ORM\Entity $entity, $field)
|
||||
{
|
||||
return $entity->has($field) && $entity->get($field) !== null;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ namespace Espo\Core\FieldValidators;
|
||||
class DatetimeOptionalType extends DatetimeType
|
||||
{
|
||||
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
return $this->isNotEmpty($entity, $field);
|
||||
}
|
||||
|
||||
protected function isNotEmpty(\Espo\ORM\Entity $entity, $field)
|
||||
{
|
||||
if ($entity->has($field) && $entity->get($field) !== null) return true;
|
||||
if ($entity->has($field . 'Date') && $entity->get($field . 'Date') !== null) return true;
|
||||
|
||||
@@ -33,7 +33,7 @@ class EmailType extends BaseType
|
||||
{
|
||||
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
if ($entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null) return true;
|
||||
if ($this->isNotEmpty($entity, $field)) return true;
|
||||
|
||||
$dataList = $entity->get($field . 'Data');
|
||||
if (!is_array($dataList)) return false;
|
||||
@@ -47,7 +47,7 @@ class EmailType extends BaseType
|
||||
|
||||
public function checkEmailAddress(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
if ($entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null) {
|
||||
if ($this->isNotEmpty($entity, $field)) {
|
||||
$address = $entity->get($field);
|
||||
if (!filter_var($address, FILTER_VALIDATE_EMAIL)) {
|
||||
return false;
|
||||
@@ -67,4 +67,9 @@ class EmailType extends BaseType
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function isNotEmpty(\Espo\ORM\Entity $entity, $field)
|
||||
{
|
||||
return $entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
|
||||
* Website: http://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\FieldValidators;
|
||||
|
||||
class EnumType extends BaseType
|
||||
{
|
||||
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
return $this->isNotEmpty($entity, $field);
|
||||
}
|
||||
|
||||
protected function isNotEmpty(\Espo\ORM\Entity $entity, $field)
|
||||
{
|
||||
return $entity->has($field) && $entity->get($field) !== null;
|
||||
}
|
||||
}
|
||||
@@ -33,20 +33,25 @@ class IntType extends BaseType
|
||||
{
|
||||
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
return $entity->has($field) && $entity->get($field) !== null;
|
||||
return $this->isNotEmpty($entity, $field);
|
||||
}
|
||||
|
||||
public function checkMax(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
if (!$entity->has($field) || $entity->get($field) === null) return true;
|
||||
if (!$this->isNotEmpty($entity, $field)) return true;
|
||||
if ($entity->get($field) > $validationValue) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
public function checkMin(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
if (!$entity->has($field) || $entity->get($field) === null) return true;
|
||||
if (!$this->isNotEmpty($entity, $field)) return true;
|
||||
if ($entity->get($field) < $validationValue) return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function isNotEmpty(\Espo\ORM\Entity $entity, $field)
|
||||
{
|
||||
return $entity->has($field) && $entity->get($field) !== null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ class PhoneType extends BaseType
|
||||
{
|
||||
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
if ($entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null) return true;
|
||||
if ($this->isNotEmpty($entity, $field)) return true;
|
||||
|
||||
$dataList = $entity->get($field . 'Data');
|
||||
if (!is_array($dataList)) return false;
|
||||
@@ -44,4 +44,9 @@ class PhoneType extends BaseType
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
protected function isNotEmpty(\Espo\ORM\Entity $entity, $field)
|
||||
{
|
||||
return $entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,12 +33,12 @@ class VarcharType extends BaseType
|
||||
{
|
||||
public function checkRequired(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
return $entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null;
|
||||
return $this->isNotEmpty($entity, $field);
|
||||
}
|
||||
|
||||
public function checkMaxLength(\Espo\ORM\Entity $entity, string $field, $validationValue, $data) : bool
|
||||
{
|
||||
if ($entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null) {
|
||||
if ($this->isNotEmpty($entity, $field)) {
|
||||
$value = $entity->get($field);
|
||||
if (mb_strlen($value) > $validationValue) {
|
||||
return false;
|
||||
@@ -46,4 +46,9 @@ class VarcharType extends BaseType
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function isNotEmpty(\Espo\ORM\Entity $entity, $field)
|
||||
{
|
||||
return $entity->has($field) && $entity->get($field) !== '' && $entity->get($field) !== null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -42,6 +42,9 @@
|
||||
"type":"bool"
|
||||
}
|
||||
],
|
||||
"validationList": [
|
||||
"required"
|
||||
],
|
||||
"filter": true,
|
||||
"fieldDefs":{
|
||||
"type":"varchar"
|
||||
|
||||
@@ -929,6 +929,8 @@ class Record extends \Espo\Core\Services\Base
|
||||
|
||||
$entity->set($data);
|
||||
|
||||
$this->processValidation($entity, $data);
|
||||
|
||||
$this->beforeUpdateEntity($entity, $data);
|
||||
|
||||
if (!$this->checkAssignment($entity)) {
|
||||
@@ -1587,6 +1589,11 @@ class Record extends \Espo\Core\Services\Base
|
||||
$entity = $this->getEntity($id);
|
||||
if ($this->getAcl()->check($entity, 'edit') && $this->checkEntityForMassUpdate($entity, $data)) {
|
||||
$entity->set($data);
|
||||
try {
|
||||
$this->processValidation($entity, $data);
|
||||
} catch (\Exception $e) {
|
||||
continue;
|
||||
}
|
||||
if ($this->checkAssignment($entity)) {
|
||||
if ($repository->save($entity, ['massUpdate' => true])) {
|
||||
$idsUpdated[] = $entity->id;
|
||||
@@ -1638,14 +1645,14 @@ class Record extends \Espo\Core\Services\Base
|
||||
|
||||
$this->afterMassUpdate($idsUpdated, $data);
|
||||
|
||||
return [
|
||||
return (object) [
|
||||
'count' => $count
|
||||
];
|
||||
}
|
||||
|
||||
$this->afterMassUpdate($idsUpdated, $data);
|
||||
|
||||
return [
|
||||
return (object) [
|
||||
'count' => $count,
|
||||
'ids' => $idsUpdated
|
||||
];
|
||||
|
||||
@@ -0,0 +1,346 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2018 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
|
||||
* Website: http://www.espocrm.com
|
||||
*
|
||||
* EspoCRM is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* EspoCRM is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with EspoCRM. If not, see http://www.gnu.org/licenses/.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace tests\integration\Espo\Record;
|
||||
|
||||
class FieldValidationTest extends \tests\integration\Core\BaseTestCase
|
||||
{
|
||||
private function setFieldsDefs($app, $entityType, $data)
|
||||
{
|
||||
$metadata = $app->getContainer()->get('metadata');
|
||||
$metadata->set('entityDefs', $entityType, [
|
||||
'fields' => $data
|
||||
]);
|
||||
$metadata->save();
|
||||
}
|
||||
|
||||
public function testRequiredVarchar1()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'name' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public function testUpdateRequiredVarchar1()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'name' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$entity = $app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => 'test'
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->update($entity->id, (object) [
|
||||
'name' => ''
|
||||
]);
|
||||
}
|
||||
|
||||
public function testMassUpdateRequiredVarchar1()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'name' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$entity = $app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => 'test'
|
||||
]);
|
||||
|
||||
$result = $app->getContainer()->get('serviceFactory')->create('Account')->massUpdate((object) [
|
||||
'name' => ''
|
||||
], [
|
||||
'ids' => [$entity->id]
|
||||
]);
|
||||
|
||||
$this->assertEquals(0, $result->count);
|
||||
}
|
||||
|
||||
public function testMassUpdateRequiredVarchar2()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'name' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$entity = $app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => 'test'
|
||||
]);
|
||||
|
||||
$result = $app->getContainer()->get('serviceFactory')->create('Account')->massUpdate((object) [
|
||||
'name' => 'hello'
|
||||
], [
|
||||
'ids' => [$entity->id]
|
||||
]);
|
||||
|
||||
$this->assertEquals(1, $result->count);
|
||||
}
|
||||
|
||||
public function testRequiredVarchar2()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'name' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRequiredVarchar3()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'name' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => 'test'
|
||||
]);
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function testMaxLength1()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'name' => [
|
||||
'required' => true,
|
||||
'maxLength' => 5
|
||||
]
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => '123456'
|
||||
]);
|
||||
}
|
||||
|
||||
public function testMaxLength2()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'name' => [
|
||||
'required' => true,
|
||||
'maxLength' => 5
|
||||
]
|
||||
]);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => '12345'
|
||||
]);
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function testRequiredLink1()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'assignedUser' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => 'test',
|
||||
'assignedUserId' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRequiredLink2()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'assignedUser' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => 'test',
|
||||
'assignedUserId' => '1',
|
||||
]);
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function testRequiredLinkMultiple1()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Account', [
|
||||
'teams' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Account')->create((object) [
|
||||
'name' => 'test',
|
||||
'teamsIds' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRequiredCurrency1()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Lead', [
|
||||
'opportunityAmount' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Lead')->create((object) [
|
||||
'lastName' => 'test',
|
||||
'opportunityAmount' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRequiredCurrency2()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Lead', [
|
||||
'opportunityAmount' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Lead')->create((object) [
|
||||
'lastName' => 'test',
|
||||
'opportunityAmount' => 100,
|
||||
'opportunityAmountCurrency' => null,
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRequiredCurrency3()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Lead', [
|
||||
'opportunityAmount' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Lead')->create((object) [
|
||||
'lastName' => 'test',
|
||||
'opportunityAmount' => 100,
|
||||
'opportunityAmountCurrency' => 'USD',
|
||||
]);
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
|
||||
public function testRequiredEnum1()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Lead', [
|
||||
'status' => [
|
||||
'required' => true,
|
||||
'default' => null
|
||||
]
|
||||
]);
|
||||
$app = $this->createApplication();
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$e = $app->getContainer()->get('serviceFactory')->create('Lead')->create((object) [
|
||||
'lastName' => 'test'
|
||||
]);
|
||||
|
||||
$this->assertTrue($status === null);
|
||||
}
|
||||
|
||||
public function testRequiredEnum2()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Lead', [
|
||||
'status' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$this->expectException(\Espo\Core\Exceptions\BadRequest::class);
|
||||
|
||||
$app->getContainer()->get('serviceFactory')->create('Lead')->create((object) [
|
||||
'lastName' => 'test',
|
||||
'status' => null
|
||||
]);
|
||||
}
|
||||
|
||||
public function testRequiredEnum3()
|
||||
{
|
||||
$app = $this->createApplication();
|
||||
$this->setFieldsDefs($app, 'Lead', [
|
||||
'status' => [
|
||||
'required' => true
|
||||
]
|
||||
]);
|
||||
|
||||
$e = $app->getContainer()->get('serviceFactory')->create('Lead')->create((object) [
|
||||
'lastName' => 'test',
|
||||
'status' => 'New'
|
||||
]);
|
||||
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user