From 4dda6136de822d1f6ea807253498fe115beb3def Mon Sep 17 00:00:00 2001 From: Taras Machyshyn Date: Tue, 1 Apr 2014 15:49:43 +0300 Subject: [PATCH] added exception for create and update actions, fix metadata 'delete' --- application/Espo/Core/Utils/FieldManager.php | 12 ++++ application/Espo/Core/Utils/Metadata.php | 2 + tests/Api/FieldManagerTest.php | 22 +++++++ tests/Espo/Core/Utils/FieldManagerTest.php | 64 +++++++++++++++++++- 4 files changed, 99 insertions(+), 1 deletion(-) diff --git a/application/Espo/Core/Utils/FieldManager.php b/application/Espo/Core/Utils/FieldManager.php index e6e2319dbb..7f3dbeaad1 100644 --- a/application/Espo/Core/Utils/FieldManager.php +++ b/application/Espo/Core/Utils/FieldManager.php @@ -22,6 +22,8 @@ namespace Espo\Core\Utils; +use \Espo\Core\Exceptions\Error; + class FieldManager { private $metadata; @@ -49,11 +51,21 @@ class FieldManager public function create($name, $fieldDef, $scope) { + $existingField = $this->read($name, $scope); + if (isset($existingField)) { + throw new Error('Field ['.$name.'] exists in '.$scope); + } + return $this->update($name, $fieldDef, $scope); } public function update($name, $fieldDef, $scope) { + $existingField = $this->read($name, $scope); + if (isset($existingField) && (!isset($existingField['isCustom']) || !$existingField['isCustom'])) { + throw new Error('Core field ['.$name.'] cannot be changed in '.$scope); + } + /*Add option to metadata that identify the custom field*/ if (!isset($fieldDef[$this->customOptionName]) || !$fieldDef[$this->customOptionName]) { $fieldDef[$this->customOptionName] = true; diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index 98588c0c2d..f037e8ac9f 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -269,6 +269,8 @@ class Metadata $GLOBALS['log']->warning('Metadata unsets available only for custom code.'); } + $this->init(true); + return $result; } diff --git a/tests/Api/FieldManagerTest.php b/tests/Api/FieldManagerTest.php index 7631b341c1..991fe14a90 100644 --- a/tests/Api/FieldManagerTest.php +++ b/tests/Api/FieldManagerTest.php @@ -39,6 +39,17 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($result, $this->fixture->getResponse($input)['response']); } + public function testCreateSameField() + { + $this->fixture->setType('POST'); + + $this->fixture->setUrl('/Admin/fieldManager/CustomEntity'); + + $input = '{"name":"customField","type":"varchar","maxLength":50}'; + + $this->assertEquals(500, $this->fixture->getResponse($input)['code']); + } + public function testReadAfterCreate() { @@ -61,6 +72,17 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($result, $this->fixture->getResponse($input)['response']); } + public function testUpdateCoreField() + { + $this->fixture->setType('PUT'); + + $this->fixture->setUrl('/Admin/fieldManager/Account/name'); + + $input = '{"type":"varchar","maxLength":50,"default":"this is a test"}'; + + $this->assertEquals(500, $this->fixture->getResponse($input)['code']); + } + public function testReadAfterUpdate() { $this->fixture->setType('GET'); diff --git a/tests/Espo/Core/Utils/FieldManagerTest.php b/tests/Espo/Core/Utils/FieldManagerTest.php index 18d54c5997..824d10994b 100644 --- a/tests/Espo/Core/Utils/FieldManagerTest.php +++ b/tests/Espo/Core/Utils/FieldManagerTest.php @@ -4,7 +4,6 @@ namespace tests\Espo\Core\Utils; use tests\ReflectionHelper; - class FieldManagerTest extends \PHPUnit_Framework_TestCase { protected $object; @@ -28,6 +27,61 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase $this->object = NULL; } + public function testCreateExistingField() + { + $this->setExpectedException('\Espo\Core\Exceptions\Error'); + + $data = array( + "type" => "varchar", + "maxLength" => "50", + ); + + $this->objects['metadata'] + ->expects($this->once()) + ->method('get') + ->will($this->returnValue($data)); + + $this->object->create('varName', $data, 'CustomEntity'); + } + + public function testUpdateCoreField() + { + $this->setExpectedException('\Espo\Core\Exceptions\Error'); + + $data = array( + "type" => "varchar", + "maxLength" => "50", + ); + + $this->objects['metadata'] + ->expects($this->once()) + ->method('get') + ->will($this->returnValue($data)); + + $this->object->update('name', $data, 'Account'); + } + + public function testUpdateCustomField() + { + $data = array( + "type" => "varchar", + "maxLength" => "50", + "isCustom" => true, + ); + + $this->objects['metadata'] + ->expects($this->once()) + ->method('get') + ->will($this->returnValue($data)); + + $this->objects['metadata'] + ->expects($this->once()) + ->method('get') + ->will($this->returnValue(true)); + + $this->object->update('varName', $data, 'CustomEntity'); + } + public function testRead() { @@ -63,6 +117,14 @@ class FieldManagerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($result, $this->reflection->invokeMethod('normalizeDefs', array($input1, $input2))); } + public function testDeleteTestFile() + { + $file = 'custom/Espo/Custom/Resources/metadata/entityDefs/CustomEntity.json'; + if (file_exists($file)) { + @unlink($file); + } + } +