added exception for create and update actions, fix metadata 'delete'

This commit is contained in:
Taras Machyshyn
2014-04-01 15:49:43 +03:00
parent 9bef04259e
commit 4dda6136de
4 changed files with 99 additions and 1 deletions
@@ -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;
+2
View File
@@ -269,6 +269,8 @@ class Metadata
$GLOBALS['log']->warning('Metadata unsets available only for custom code.');
}
$this->init(true);
return $result;
}
+22
View File
@@ -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');
+63 -1
View File
@@ -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);
}
}