diff --git a/application/Espo/Core/Utils/Json.php b/application/Espo/Core/Utils/Json.php index 7abf5b26a1..b061762b35 100644 --- a/application/Espo/Core/Utils/Json.php +++ b/application/Espo/Core/Utils/Json.php @@ -84,11 +84,14 @@ class Json * @param string $json * @return bool */ - public static function isJSON($json){ - if ($json=='[]' || $json=='{}') { + public static function isJSON($json) + { + if ($json === '[]' || $json === '{}') { return true; - } - + } else if (is_array($json)) { + return false; + } + return static::decode($json) != null; } diff --git a/application/Espo/Core/Utils/Layout.php b/application/Espo/Core/Utils/Layout.php index 80846d6a85..20cffed2ac 100644 --- a/application/Espo/Core/Utils/Layout.php +++ b/application/Espo/Core/Utils/Layout.php @@ -4,12 +4,30 @@ namespace Espo\Core\Utils; class Layout { - - private $layoutConfig; - private $config; private $fileManager; - private $metadata; + private $metadata; + + /** + * @var string - uses for loading default values + */ + private $name = 'layout'; + + /** + * @var array - path to layout files + */ + private $paths = array( + 'corePath' => 'application/Espo/Resources/layouts', + 'modulePath' => 'application/Espo/Modules/{*}/Resources/layouts', + ); + + /** + * @var array - path to layout files in custom folder + */ + private $customPaths = array( + 'corePath' => 'application/Espo/Custom/Resources/layouts', + 'modulePath' => 'application/Espo/Custom/Modules/{*}/Resources/layouts', + ); public function __construct(\Espo\Core\Utils\Config $config, \Espo\Core\Utils\File\Manager $fileManager, \Espo\Core\Utils\Metadata $metadata) @@ -36,22 +54,24 @@ class Layout /** - * Get Layout context - * - * @param $controller - * @param $name - * - * @return json - */ + * Get Layout context + * + * @param $controller + * @param $name + * + * @return json + */ function get($controller, $name) - { - $fileFullPath = Util::concatPath($this->getLayoutPath($controller), $name.'.json'); - + { + $fileFullPath = Util::concatPath($this->getLayoutPath($controller, true), $name.'.json'); if (!file_exists($fileFullPath)) { + $fileFullPath = Util::concatPath($this->getLayoutPath($controller), $name.'.json'); + } + if (!file_exists($fileFullPath)) { //load defaults $defaultPath = $this->getConfig()->get('defaultsPath'); - $fileFullPath = Util::concatPath( Util::concatPath($defaultPath, $this->getLayoutConfig()->name), $name.'.json' ); + $fileFullPath = Util::concatPath( Util::concatPath($defaultPath, $this->name), $name.'.json' ); //END: load defaults if (!file_exists($fileFullPath)) { @@ -60,97 +80,81 @@ class Layout } return $this->getFileManager()->getContent($fileFullPath); - } - - + } + + /** - * Merge layout data - * Ex. $controller= Account, $name= detail then will be created a file layoutFolder/Account/detail.json - * - * @param JSON string $data - * @param string $controller - ex. Account - * @param string $name - detail - * - * @return bool - */ - function merge($data, $controller, $name) - { - $layoutPath = $this->getLayoutPath($controller); - - /*//merge data with defaults values - $defaults = $this->loadDefaultValues($name, $this->getLayoutConfig()->name); - - $decoded = $this->getArrayData($data); - $mergedValues= $this->merge($defaults, $decoded); - $data= $this->getObject('JSON')->encode($mergedValues); - //END: merge data with defaults values */ - - return $this->getFileManager()->mergeContent($data, $layoutPath, $name.'.json', true); - } - - - /** - * Set Layout data - * Ex. $controller= Account, $name= detail then will be created a file layoutFolder/Account/detail.json - * - * @param JSON string $data - * @param string $controller - ex. Account - * @param string $name - detail - * - * @return bool - */ + * Set Layout data + * Ex. $controller= Account, $name= detail then will be created a file layoutFolder/Account/detail.json + * + * @param JSON string $data + * @param string $controller - ex. Account + * @param string $name - detail + * + * @return bool + */ function set($data, $controller, $name) { if (empty($controller) || empty($name)) { return false; } - $layoutPath = $this->getLayoutPath($controller); + $layoutPath = $this->getLayoutPath($controller, true); + + if (!Json::isJSON($data)) { + $data = Json::encode($data); + } return $this->getFileManager()->setContent($data, $layoutPath, $name.'.json'); } - /** - * Get Layout path, ex. application/Modules/Crm/Layouts/Account - * - * @param string $entityName - * @param bool $delim - delimiter - * - * @return string - */ - public function getLayoutPath($entityName, $delim= '/') - { - $moduleName= $this->getMetadata()->getScopeModuleName($entityName); - - $path= $this->getLayoutConfig()->corePath; - if ($moduleName !== false) { - $path= str_replace('{*}', $moduleName, $this->getLayoutConfig()->customPath); - } - $path= Util::concatPath($path, $entityName); - - if ($delim!='/') { - $path = str_replace('/', $delim, $path); - } - - return $path; - } - /** - * Get settings for Layout - * - * @return object - */ - protected function getLayoutConfig() + * Merge layout data + * Ex. $controller= Account, $name= detail then will be created a file layoutFolder/Account/detail.json + * + * @param JSON string $data + * @param string $controller - ex. Account + * @param string $name - detail + * + * @return bool + */ + function merge($data, $controller, $name) { - if (isset($this->layoutConfig) && is_object($this->layoutConfig)) { - return $this->layoutConfig; - } + $prevData = $this->get($controller, $name); + + $prevDataArray= Json::getArrayData($prevData); + $dataArray= Json::getArrayData($data); - $this->layoutConfig = $this->getConfig()->get('layoutConfig'); + $data= Util::merge($prevDataArray, $dataArray); + $data= Json::encode($data); - return $this->layoutConfig; - } + return $this->set($data, $controller, $name); + } + + /** + * Get Layout path, ex. application/Modules/Crm/Layouts/Account + * + * @param string $entityName + * @param bool $isCustom - if need to check custom folder + * + * @return string + */ + public function getLayoutPath($entityName, $isCustom = false) + { + $paths = $isCustom ? $this->customPaths : $this->paths; + + $moduleName = $this->getMetadata()->getScopeModuleName($entityName); + + $path = $paths['corePath']; + if ($moduleName !== false) { + $path = str_replace('{*}', $moduleName, $paths['modulePath']); + } + $path = Util::concatPath($path, $entityName); + + return $path; + } + } diff --git a/application/Espo/Core/Utils/Metadata.php b/application/Espo/Core/Utils/Metadata.php index 177d657b54..ade46c2ccb 100644 --- a/application/Espo/Core/Utils/Metadata.php +++ b/application/Espo/Core/Utils/Metadata.php @@ -353,7 +353,7 @@ class Metadata { $moduleName = $this->getScopeModuleName($scopeName); - $path = $this->getConfig()->get('espoPath'); + $path = 'Espo'; if ($moduleName !== false) { $path = str_replace('{*}', $moduleName, $this->getConfig()->get('espoModulePath')); } diff --git a/application/Espo/Core/defaults/doctrine/metadata/Espo.Entities.User.php b/application/Espo/Core/defaults/doctrine/metadata/Espo.Entities.User.php deleted file mode 100644 index 4d5635c83d..0000000000 --- a/application/Espo/Core/defaults/doctrine/metadata/Espo.Entities.User.php +++ /dev/null @@ -1,33 +0,0 @@ - - array ( - 'table' => 'users', - 'type' => 'entity', - 'id' => - array ( - 'id' => - array ( - 'type' => 'string', - 'generator' => - array ( - 'strategy' => 'UUID', - ), - ), - ), - 'fields' => - array ( - 'username' => - array ( - 'type' => 'string(30)', - ), - 'password' => - array ( - 'type' => 'string(255)', - ), - ), - ), -); - -?> \ No newline at end of file diff --git a/application/Espo/Core/defaults/layouts/detail.json b/application/Espo/Core/defaults/layout/detail.json similarity index 100% rename from application/Espo/Core/defaults/layouts/detail.json rename to application/Espo/Core/defaults/layout/detail.json diff --git a/application/Espo/Core/defaults/layouts/detailSmall.json b/application/Espo/Core/defaults/layout/detailSmall.json similarity index 100% rename from application/Espo/Core/defaults/layouts/detailSmall.json rename to application/Espo/Core/defaults/layout/detailSmall.json diff --git a/application/Espo/Core/defaults/layouts/filters.json b/application/Espo/Core/defaults/layout/filters.json similarity index 100% rename from application/Espo/Core/defaults/layouts/filters.json rename to application/Espo/Core/defaults/layout/filters.json diff --git a/application/Espo/Core/defaults/layouts/filtersAdvanced.json b/application/Espo/Core/defaults/layout/filtersAdvanced.json similarity index 100% rename from application/Espo/Core/defaults/layouts/filtersAdvanced.json rename to application/Espo/Core/defaults/layout/filtersAdvanced.json diff --git a/application/Espo/Core/defaults/layouts/list.json b/application/Espo/Core/defaults/layout/list.json similarity index 100% rename from application/Espo/Core/defaults/layouts/list.json rename to application/Espo/Core/defaults/layout/list.json diff --git a/application/Espo/Core/defaults/layouts/listDashlet.json b/application/Espo/Core/defaults/layout/listDashlet.json similarity index 100% rename from application/Espo/Core/defaults/layouts/listDashlet.json rename to application/Espo/Core/defaults/layout/listDashlet.json diff --git a/application/Espo/Core/defaults/layouts/listSmall.json b/application/Espo/Core/defaults/layout/listSmall.json similarity index 100% rename from application/Espo/Core/defaults/layouts/listSmall.json rename to application/Espo/Core/defaults/layout/listSmall.json diff --git a/application/Espo/Core/defaults/layouts/massUpdate.json b/application/Espo/Core/defaults/layout/massUpdate.json similarity index 100% rename from application/Espo/Core/defaults/layouts/massUpdate.json rename to application/Espo/Core/defaults/layout/massUpdate.json diff --git a/application/Espo/Core/defaults/layouts/relationships.json b/application/Espo/Core/defaults/layout/relationships.json similarity index 100% rename from application/Espo/Core/defaults/layouts/relationships.json rename to application/Espo/Core/defaults/layout/relationships.json diff --git a/application/Espo/Core/defaults/systemConfig.php b/application/Espo/Core/defaults/systemConfig.php index 44a9d2ea49..f1b151d039 100644 --- a/application/Espo/Core/defaults/systemConfig.php +++ b/application/Espo/Core/defaults/systemConfig.php @@ -8,12 +8,9 @@ return array ( 'defaultsPath' => 'application/Espo/Core/defaults', 'unsetFileName' => 'unset.json', - 'espoPath' => 'Espo', 'espoModulePath' => 'Espo/Modules/{*}', 'espoCustomPath' => 'Espo/Custom', - 'controllerPath' => 'Controllers', //path for controllers in a module - 'metadataConfig' => array ( 'name' => 'metadata', @@ -22,13 +19,6 @@ return array ( 'customPath' => 'application/Espo/Modules/{*}/Resources/metadata', ), - 'layoutConfig' => - array ( - 'name' => 'layouts', - 'corePath' => 'application/Espo/Resources/layouts', - 'customPath' => 'application/Espo/Modules/{*}/Resources/layouts', - ), - 'languageConfig' => array ( 'name' => '{lang}', @@ -61,7 +51,6 @@ return array ( 'configPath', 'cachePath', 'metadataConfig', - 'layoutConfig', 'languageConfig', 'database', 'customPath', @@ -71,10 +60,8 @@ return array ( 'configCustomPathFull', 'crud', 'customDir', - 'espoPath', 'espoModulePath', 'espoCustomPath', - 'controllerPath', 'scopeModuleMap', ), 'adminItems' => diff --git a/tests/Api/LayoutTest.php b/tests/Api/LayoutTest.php index 342af5c659..3eea7e4586 100644 --- a/tests/Api/LayoutTest.php +++ b/tests/Api/LayoutTest.php @@ -2,9 +2,6 @@ namespace tests\Api; -require_once('tests/testBootstrap.php'); - - class LayoutTest extends \PHPUnit_Framework_TestCase { protected $fixture; @@ -29,34 +26,62 @@ class LayoutTest extends \PHPUnit_Framework_TestCase { $this->fixture->setType('PUT'); - $this->fixture->setUrl('/custom-test/layout/test-put'); + $this->fixture->setUrl('/CustomTest/layout/testPut'); $data= '["amount","account","closeDate","leadSource","stage","probability","assignedUser"]'; $this->assertTrue($this->fixture->isSuccess( $data )); //check if file exists - $this->assertTrue(file_exists('application/Espo/Resources/layouts/CustomTest/testPut.json')); + $file = 'application/Espo/Custom/Resources/layouts/CustomTest/testPut.json'; + + $fileExists = file_exists($file); + $this->assertTrue($fileExists); + + if ($fileExists) { + $content = file_get_contents($file); + + $this->assertEquals($data, $content); + + @unlink($file); + } } function testPatch() { $this->fixture->setType('PATCH'); - $this->fixture->setUrl('/custom-test/layout/test-patch'); + $this->fixture->setUrl('/CustomTest/layout/testPatch'); $data= '[{"label":"MyLabel"}]'; + $this->assertTrue($this->fixture->isSuccess( $data )); + + $data= '[{"isPathed":true}]'; $this->assertTrue($this->fixture->isSuccess( $data )); //check if file exists - $this->assertTrue(file_exists('application/Espo/Resources/layouts/CustomTest/testPatch.json')); + $file = 'application/Espo/Custom/Resources/layouts/CustomTest/testPatch.json'; + + $fileExists = file_exists($file); + $this->assertTrue($fileExists); + + if ($fileExists) { + + $content = file_get_contents($file); + + //$data= '[{"label":"MyLabel","isPathed":true}]'; + $data= '[{"isPathed":true}]'; //now PATCH works like PUT + $this->assertEquals($data, $content); + + @unlink($file); + } } function testGet() { $this->fixture->setType('GET'); - $this->fixture->setUrl('/custom-test/layout/detail'); + $this->fixture->setUrl('/CustomTest/layout/detail'); $this->assertTrue($this->fixture->isSuccess( )); - $this->fixture->setUrl('/need-to-be-not-real/layout/not-real'); + $this->fixture->setUrl('/needToBeNotReal/layout/notReal'); $response= $this->fixture->getResponse(); $this->assertEquals(404, $response['code']); } diff --git a/tests/Api/MetadataTest.php b/tests/Api/MetadataTest.php deleted file mode 100644 index b299c0202c..0000000000 --- a/tests/Api/MetadataTest.php +++ /dev/null @@ -1,59 +0,0 @@ -fixture = new RestTesterClass(); - - /****************************************/ - $this->fixture->setUrl('/metadata'); - /****************************************/ - } - - protected function tearDown() - { - $this->fixture = NULL; - } - - - function testGet() - { - $this->fixture->setType('GET'); - - $this->fixture->setUrl('/metadata'); - $this->assertTrue($this->fixture->isSuccess( )); - } - - /*function testPut() - { - $this->fixture->setType('PUT'); - - $this->fixture->setUrl('/metadata/custom-test/account'); - $data= '{"module":"Test"}'; - $this->assertTrue($this->fixture->isSuccess( $data )); - - //check if file exists - $this->assertTrue(file_exists('application/Espo/Modules/Crm/Resources/metadata/customTest/Account.json')); - - - $this->fixture->setUrl('/metadata/custom-test/custom-test'); - $data= '{"module":"Test","var1":{"subvar1":"NEWsubval1","subvar55":"subval55"}}'; - $this->assertTrue($this->fixture->isSuccess( $data )); - - //check if file exists - $this->assertTrue(file_exists('application/Espo/Resources/metadata/customTest/CustomTest.json')); - }*/ - - -} - -?> \ No newline at end of file diff --git a/tests/Api/RestTesterClass.php b/tests/Api/RestTesterClass.php index 8477d33018..3ea81d66a5 100644 --- a/tests/Api/RestTesterClass.php +++ b/tests/Api/RestTesterClass.php @@ -14,7 +14,7 @@ class RestTesterClass function __construct() { - $config = include('tests/config.php'); + $config = include('tests/Api/config.php'); $this->apiUrl = $config['apiUrl']; $this->username = $config['username']; diff --git a/tests/Api/SettingsTest.php b/tests/Api/SettingsTest.php deleted file mode 100644 index 8cc0a63b29..0000000000 --- a/tests/Api/SettingsTest.php +++ /dev/null @@ -1,49 +0,0 @@ -fixture = new RestTesterClass(); - $this->fixture->setUrl('/settings'); - } - - protected function tearDown() - { - $this->fixture = NULL; - } - - - function testGet() - { - $this->fixture->setType('GET'); - $this->assertTrue($this->fixture->isSuccess()); - } - - function testPatch() - { - $this->fixture->setType('PATCH'); - - $array= array( - "customTest"=> array("test"=> "success"), - ); - $json= json_encode($array); - $this->assertTrue( $this->fixture->isSuccess($json) ); - - //config get if the customTest item exists - $savedValue = $GLOBALS['app']->getContainer()->get('config')->get('customTest'); - $this->assertObjectHasAttribute('test', $savedValue); - } - - -} - -?> \ No newline at end of file diff --git a/tests/config.php b/tests/Api/config.php similarity index 58% rename from tests/config.php rename to tests/Api/config.php index 1a9b43dca4..459c0726ce 100644 --- a/tests/config.php +++ b/tests/Api/config.php @@ -1,7 +1,7 @@ 'http://172.20.0.1/espocrm-new/api', + 'apiUrl' => 'http://172.20.0.1/espocrm/api', 'username' => 'admin', 'password' => '1', ); diff --git a/tests/Espo/Core/Utils/Database/RelationsTest.php b/tests/Espo/Core/Utils/Database/RelationsTest.php deleted file mode 100644 index 4ef46ffcf1..0000000000 --- a/tests/Espo/Core/Utils/Database/RelationsTest.php +++ /dev/null @@ -1,535 +0,0 @@ -app = $GLOBALS['app']; - } - - - protected function setUp() - { - $this->object = new \Espo\Core\Utils\Database\Relations(); - } - - protected function tearDown() - { - $this->object = NULL; - } - - - public function invokeMethod($methodName, array $parameters = array()) - { - $reflection = new \ReflectionClass(get_class($this->object)); - $method = $reflection->getMethod($methodName); - $method->setAccessible(true); - - return $method->invokeArgs($this->object, $parameters); - } - //$this->invokeMethod('cryptPassword', array('passwordToCrypt')); - - - - function testGetSortEntities() - { - $input = array( - 'entity1' => 'ContactTest', - 'entity2' => 'CallTest', - ); - - $result = array( - 0 => 'callTest', - 1 => 'contactTest', - ); - - $this->assertEquals($result, $this->invokeMethod('getSortEntities', array($input['entity1'], $input['entity2']))); - } - - - function testGetJoinTable() - { - $input = array( - 'entity1' => 'ContactTest', - 'entity2' => 'CallTest', - ); - - $result = 'callTestContactTest'; - - $this->assertEquals( $result, $this->invokeMethod('getJoinTable', array($input['entity1'], $input['entity2'])) ); - } - - - function testManyMany() - { - $input = array( - 'params' => array ( - 'entityName' => 'Call', - 'link' => - array ( - 'name' => 'contacts', - 'params' => - array ( - 'type' => 'hasMany', - 'entity' => 'Contact', - 'foreign' => 'calls', - ), - ), - 'targetEntity' => 'Contact', - ), - - - 'foreignParams' => array ( - 'entityName' => 'Contact', - 'link' => - array ( - 'name' => 'calls', - 'params' => - array ( - 'type' => 'hasMany', - 'entity' => 'Call', - 'foreign' => 'contacts', - ), - ), - 'targetEntity' => 'Call', - ), - ); - - $result = array( - 'Call' => - array ( - 'relations' => - array ( - 'contacts' => - array ( - 'type' => 'manyMany', - 'entity' => 'Contact', - 'relationName' => 'callContact', - 'key' => 'id', - 'foreignKey' => 'id', - 'midKeys' => - array ( - 'callId', - 'contactId', - ), - ), - ), - ), - ); - - - $this->assertEquals( $result, $this->invokeMethod('manyMany', array($input['params'], $input['foreignParams'])) ); - - //test reverse: result should be an empty array - $result = array(); - $this->assertEquals( $result, $this->invokeMethod('manyMany', array($input['foreignParams'], $input['params'])) ); - } - - - function testHasMany() - { - $input = array( - 'params' => array ( - 'entityName' => 'Account', - 'link' => - array ( - 'name' => 'contacts', - 'params' => - array ( - 'type' => 'hasMany', - 'entity' => 'Contact', - 'foreign' => 'account', - ), - ), - 'targetEntity' => 'Contact', - ), - - 'foreignParams' => array ( - 'entityName' => 'Contact', - 'link' => - array ( - 'name' => 'account', - 'params' => - array ( - 'type' => 'belongsTo', - 'entity' => 'Account', - ), - ), - 'targetEntity' => 'Account', - ), - ); - - $result = array ( - 'Account' => - array ( - 'relations' => - array ( - 'contacts' => - array ( - 'type' => 'hasMany', - 'entity' => 'Contact', - 'foreignKey' => 'accountId', - ), - ), - ), - ); - - - $this->assertEquals( $result, $this->invokeMethod('hasMany', array($input['params'], $input['foreignParams'])) ); - } - - - function testBelongsTo() - { - $input = array( - 'params' => array ( - 'entityName' => 'Attachment', - 'link' => - array ( - 'name' => 'createdBy', - 'params' => - array ( - 'type' => 'belongsTo', - 'entity' => 'User', - ), - ), - 'targetEntity' => 'User', - ), - - 'foreignParams' => array ( - 'entityName' => 'User', - 'link' => false, - 'targetEntity' => 'Attachment', - ), - ); - - $result = array ( - 'Attachment' => - array ( - 'fields' => - array ( - 'createdByName' => - array ( - 'type' => 'foreign', - 'relation' => 'createdBy', - 'notStorable' => true, - ), - 'createdById' => - array ( - 'type' => 'foreignId', - ), - ), - 'relations' => - array ( - 'createdBy' => - array ( - 'type' => 'belongsTo', - 'entity' => 'User', - 'key' => 'createdById', - 'foreignKey' => 'id', - ), - ), - ), - ); - - - $this->assertEquals( $result, $this->invokeMethod('belongsTo', array($input['params'], $input['foreignParams'])) ); - } - - - function testHasChildren() - { - $input = array( - 'params' => array ( - 'entityName' => 'Post', - 'link' => - array ( - 'name' => 'notes', - 'params' => - array ( - "type" => "hasChildren", - "entity" => "Post", - "foreign" => "parent", - ), - ), - 'targetEntity' => 'Note', - ), - - 'foreignParams' => array ( - 'entityName' => 'Note', - 'link' => array ( - 'name' => 'parent', - 'params' => - array ( - "type" => "belongsToParent", - "entities" => array("Post", "Account", "Case"), - "foreign" => "notes" - ), - ), - 'targetEntity' => 'Post', - ), - ); - - $result = array ( - 'Post' => - array ( - 'relations' => - array ( - 'notes' => - array ( - 'type' => 'hasChildren', - 'entity' => 'Note', - 'foreignKey' => 'parentId', - 'foreignType' => 'parentType', - ), - ), - ), - - /*'Note' => array( - 'fields' => array( - 'parentId' => array( - 'type' => 'foreignId', - ), - 'parentType' => array( - 'type' => 'foreignType', - ), - 'parentName' => array( - 'type' => 'varchar', - 'notStorable' => true, - ), - ), - ),*/ - ); - - - $this->assertEquals( $result, $this->invokeMethod('hasChildren', array($input['params'], $input['foreignParams'])) ); - } - - - function testBelongsToParent_Single() - { - $input = array( - 'params' => array ( - 'entityName' => 'Note', - 'link' => - array ( - 'name' => 'parent', - 'params' => - array ( - 'type' => 'belongsToParent', - 'foreign' => 'notes', - ), - ), - 'targetEntity' => 'Note', - ), - - 'foreignParams' => array ( - 'entityName' => 'Note', - 'link' => - array ( - 'name' => 'attachments', - 'params' => - array ( - 'type' => 'hasChildren', - 'entity' => 'Attachment', - 'foreign' => 'parent', - ), - ), - 'targetEntity' => 'Note', - ), - ); - - $result = array ( - 'Note' => - array ( - 'fields' => - array ( - 'parentId' => - array ( - 'type' => 'foreignId', - ), - 'parentType' => - array ( - 'type' => 'foreignType', - ), - 'parentName' => - array ( - 'type' => 'varchar', - 'notStorable' => true, - ), - ), - ), - ); - - $this->assertEquals( $result, $this->invokeMethod('belongsToParent', array($input['params'], $input['foreignParams'])) ); - } - - - function testBelongsToParent() - { - $input = array( - 'params' => array ( - 'entityName' => 'Call', - 'link' => - array ( - 'name' => 'parent', - 'params' => - array ( - 'type' => 'belongsToParent', - 'entities' => - array ( - 'Account', - 'Opportunity', - 'Case', - ), - 'foreign' => 'calls', - ), - ), - 'targetEntity' => 'Call', - ), - - 'foreignParams' => array ( - 'entityName' => 'Call', - 'link' => false, - 'targetEntity' => 'Call', - ), - ); - - $result = array ( - 'Account' => - array ( - 'fields' => - array ( - 'parentId' => - array ( - 'type' => 'foreignId', - ), - 'parentType' => - array ( - 'type' => 'foreignType', - ), - 'parentName' => - array ( - 'type' => 'varchar', - 'notStorable' => true, - ), - ), - ), - 'Opportunity' => - array ( - 'fields' => - array ( - 'parentId' => - array ( - 'type' => 'foreignId', - ), - 'parentType' => - array ( - 'type' => 'foreignType', - ), - 'parentName' => - array ( - 'type' => 'varchar', - 'notStorable' => true, - ), - ), - ), - 'Case' => - array ( - 'fields' => - array ( - 'parentId' => - array ( - 'type' => 'foreignId', - ), - 'parentType' => - array ( - 'type' => 'foreignType', - ), - 'parentName' => - array ( - 'type' => 'varchar', - 'notStorable' => true, - ), - ), - ), - ); - - $this->assertEquals( $result, $this->invokeMethod('belongsToParent', array($input['params'], $input['foreignParams'])) ); - } - - - function testEntityEmailAddress() - { - $input = array( - 'params' => array ( - 'entityName' => 'User', - 'link' => - array ( - 'name' => 'email', - ), - 'targetEntity' => 'User', - ), - - 'foreignParams' => array ( - 'entityName' => 'User', - 'link' => - array ( - ), - 'targetEntity' => 'User', - ), - ); - - $result = array ( - 'User' => - array ( - 'relations' => - array ( - 'email' => - array ( - 'type' => 'manyMany', - 'entity' => 'EmailAddress', - 'relationName' => 'entityEmailAddress', - 'midKeys' => - array ( - 0 => 'entity_id', - 1 => 'email_address_id', - ), - 'conditions' => - array ( - 'entityType' => 'User', - ), - 'additionalColumns' => - array ( - 'primary' => - array ( - 'type' => 'bool', - 'default' => false, - ), - ), - ), - ), - ), - ); - - //todo: move to RelationManager file - $this->assertEquals( $result, $this->invokeMethod('entityEmailAddress', array($input['params'], $input['foreignParams'])) ); - } - - - - - - -} \ No newline at end of file diff --git a/tests/Espo/Core/Utils/Database/Schema/ConverterTest.php b/tests/Espo/Core/Utils/Database/Schema/ConverterTest.php deleted file mode 100644 index 3cf1b2dad9..0000000000 --- a/tests/Espo/Core/Utils/Database/Schema/ConverterTest.php +++ /dev/null @@ -1,83 +0,0 @@ -getMockBuilder('\\Espo\\Core\\Utils\\File\\Manager')->setConstructorArgs(array( - (object) array( - 'defaultPermissions' => (object) array ( - 'dir' => '0775', - 'file' => '0664', - 'user' => '', - 'group' => '', - ), - ) - ))->getMock(); */ - - $fileManager = $this->getMockBuilder('\\Espo\\Core\\Utils\\File\\Manager')->disableOriginalConstructor()->getMock(); - - $this->object = new \Espo\Core\Utils\Database\Schema\Converter($fileManager); - - $this->reflection = new \ReflectionClass(get_class($this->object)); - } - - protected function tearDown() - { - $this->object = NULL; - } - - protected function invokeMethod($methodName, array $parameters = array()) - { - $method = $this->reflection->getMethod($methodName); - $method->setAccessible(true); - - return $method->invokeArgs($this->object, $parameters); - } - - protected function getProperty($name) - { - $property = $this->reflection->getProperty($name); - $property->setAccessible(true); - return $property->getValue($this->object); - } - - protected function setProperty($name, $value) - { - $property = $this->reflection->getProperty($name); - $property->setAccessible(true); - $property->setValue($this->object, $value); - } - - - - public function testGetCustomTables() - { - $originalValue = $this->getProperty('customTablePath'); - $this->setProperty('customTablePath', 'tests/testData/Utils/Database/Schema/Tables'); - - - $file1 = include('tests/testData/Utils/Database/Schema/Tables/Subscription.php'); - $file2 = include('tests/testData/Utils/Database/Schema/Tables/Comment.php'); - - $result = array_merge($file1, $file2); - - //todo fix the mockObject 'fileManager' - //$this->assertEquals($result, $this->invokeMethod('getCustomTables', array())); - - $this->setProperty('customTablePath', $originalValue); - } - - - - - - - -} \ No newline at end of file diff --git a/tests/Espo/Core/Utils/File/ManagerTest.php b/tests/Espo/Core/Utils/File/ManagerTest.php deleted file mode 100644 index fc3717ff2e..0000000000 --- a/tests/Espo/Core/Utils/File/ManagerTest.php +++ /dev/null @@ -1,269 +0,0 @@ -object = new \Espo\Core\Utils\File\Manager( - (object) array( - 'defaultPermissions' => (object) array ( - 'dir' => '0775', - 'file' => '0664', - 'user' => '', - 'group' => '', - ), - ) - ); - } - - protected function tearDown() - { - $this->object = NULL; - } - - public function invokeMethod($methodName, array $parameters = array()) - { - $reflection = new \ReflectionClass(get_class($this->object)); - $method = $reflection->getMethod($methodName); - $method->setAccessible(true); - - return $method->invokeArgs($this->object, $parameters); - } - - - - function testGetFileList() - { - $result= array('Dir1', 'file1.json','file1.php',); //no recursive - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', false)); - - $result= array('Dir1'); //no recursive - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', false, '', 'dir')); - - $result= array('file1.json','file1.php',); //no recursive - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', false, '', 'file')); - - - $result= array( - 'Dir1' => array( - 'file2.json', - 'Dir2' => array ( - 'file3.json' - ), - ), - 'file1.json', - 'file1.php', - ); - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', true)); - } - - function testGetFileListWithFilter() - { - $result= array('file1.json'); //no recursive - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', false, '.*\.json$')); - - - $result= array( - 'Dir1' => array( - 'file2.json', - 'Dir2' => array ( - 'file3.json' - ), - ), - 'file1.json', - ); - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', true, '.*\.json$')); - - $result= array( - 'file1.php', - 'Dir1' => array( - 'Dir2' => array ( - ), - ), - ); - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', true, '.*\.php$')); - } - - function testGetFileListRecursively() - { - $result= array( - 'Dir1', - 'file1.json', - 'file1.php', - ); - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', false)); - - - $result= array( - 'Dir1' => array( - 'file2.json', - 'Dir2' => array ( - 'file3.json' - ), - ), - 'file1.json', - 'file1.php', - ); - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', true)); - - - $result= array( - 'Dir1' => array( - 'Dir2', - 'file2.json', - ), - 'file1.json', - 'file1.php', - ); - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', 1)); - - - $result= array( - 'Dir1' => array( - 'file2.json', - 'Dir2' => array ( - 'file3.json' - ), - ), - 'file1.json', - 'file1.php', - ); - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', 2)); - - - $result= array( - 'Dir1' => array( - 'file2.json', - 'Dir2' => array ( - 'file3.json' - ), - ), - 'file1.json', - 'file1.php', - ); - $this->assertEquals($result, $this->object->getFileList($this->filesPath.'/getFileList', 3)); - } - - function testGetContent() - { - $testPath= $this->filesPath.'/getContent'; - - $result= '{"testData":"Test"}'; - $this->assertEquals($result, $this->object->getContent($testPath, 'test.json')); - - $result= '{"testData":"Test"}'; - $this->assertEquals($result, $this->object->getContent($testPath.'/test.json')); - } - - /*function testSetContent() - { - $testPath= $this->filesPath.'/setContent'; - - $result= 'next value'; - $this->assertTrue($this->object->setContent($result, $testPath, 'test.json')); - //$this->assertEquals($result, $this->object->getContent($testPath, 'test.json')); - - //$this->assertTrue($this->object->setContent('initial value', $testPath.'/test.json')); - } */ - - /*function testMergeContent() - { - $testPath= $this->filesPath.'/setContent'; - - $initialVal= $this->object->getContent($testPath, 'test2.json'); - - $result= '{"var1":"val1","var2":"val2"}'; - $this->assertTrue($this->object->setContent($result, $testPath, 'test2.json')); - - $result= '{"var2":"new val"}'; - $this->assertTrue($this->object->mergeContent($result, $testPath, 'test2.json', true)); - - $result= '{"var1":"val1","var2":"new val"}'; - $this->assertEquals($result, $this->object->getContent($testPath, 'test2.json')); - } */ - - function testGetCurrentPermission() - { - $this->assertEquals(4, strlen($this->object->getCurrentPermission($this->filesPath.'/setContent/test.json'))); - $this->assertStringMatchesFormat('%d', $this->object->getCurrentPermission($this->filesPath.'/setContent/test.json')); - } - - /*function testCheckCreateFile() - { - $filePath= $this->filesPath.'/setContent/test.json'; - - $this->assertTrue($this->object->checkCreateFile($filePath)); - - $perm= $this->object->getDefaultPermissions(); - $this->assertTrue( in_array($this->object->getCurrentPermission($filePath), array($perm->file, $perm->dir)) ); - }*/ - - - function testGetDefaultPermissions() - { - $this->assertObjectHasAttribute('dir', $this->object->getDefaultPermissions()); - $this->assertObjectHasAttribute('file', $this->object->getDefaultPermissions()); - $this->assertObjectHasAttribute('user', $this->object->getDefaultPermissions()); - $this->assertObjectHasAttribute('group', $this->object->getDefaultPermissions()); - } - - - function testGetFileName() - { - $result= 'file1'; - $this->assertEquals($result, $this->object->getFileName('file1.json')); - - $result= 'file1'; - $this->assertEquals($result, $this->object->getFileName('file1.json', 'json')); - - $result= 'file1'; - $this->assertEquals($result, $this->object->getFileName('file1.json', '.json')); - } - - function testGetDirName() - { - $result= 'dirname'; - $this->assertEquals('dirname', $this->object->getDirName('test/dirname/Test.json')); - - $result= 'dirname'; - $this->assertEquals('dirname', $this->object->getDirName('test/dirname/')); - - $result= 'dirname'; - $this->assertEquals('dirname', $this->object->getDirName('test/dirname')); - } - - function testGetSingeFileList() - { - $input = array( - 'Dir1' => array( - 'file2.json', - 'Dir2' => array ( - 'file3.json' - ), - ), - 'file1.json', - 'file1.php', - ); - - $result = array( - 'Dir1/file2.json', - 'Dir1/Dir2/file3.json', - 'file1.json', - 'file1.php', - ); - - $this->assertEquals($result, $this->invokeMethod('getSingeFileList', array($input))); - } - - - -} - -?> diff --git a/tests/Espo/Core/Utils/JsonTest.php b/tests/Espo/Core/Utils/JsonTest.php new file mode 100644 index 0000000000..d15a9c6ed4 --- /dev/null +++ b/tests/Espo/Core/Utils/JsonTest.php @@ -0,0 +1,47 @@ +'Test'); + $this->assertEquals(json_encode($testVal), Json::encode($testVal)); + } + + function testDecode() + { + $testVal= array('testOption'=>'Test'); + $this->assertEquals($testVal, Json::decode(json_encode($testVal), true)); + + $test= '{"folder":"data\/logs"}'; + $this->assertEquals('data/logs', Json::decode($test)->folder); + } + + function testIsJSON() + { + $this->assertTrue(Json::isJSON('{"database":{"driver":"pdo_mysql","host":"localhost"},"devMode":true}')); + + $this->assertTrue(Json::isJSON('[]')); + + $this->assertTrue(Json::isJSON('{}')); + + $this->assertTrue(Json::isJSON('true')); + + $this->assertFalse(Json::isJSON('some string')); + + $this->assertTrue(Json::isJSON(true)); + $this->assertEquals('true', json_encode(true)); + + $this->assertFalse(Json::isJSON(false)); + $this->assertEquals('false', json_encode(false)); + } + + + +} + +?> \ No newline at end of file diff --git a/tests/Espo/Core/Utils/UtilTest.php b/tests/Espo/Core/Utils/UtilTest.php index 4cda010268..76b9861035 100644 --- a/tests/Espo/Core/Utils/UtilTest.php +++ b/tests/Espo/Core/Utils/UtilTest.php @@ -2,8 +2,6 @@ namespace tests\Espo\Core\Utils; -require_once('tests/testBootstrap.php'); - use Espo\Core\Utils\Util; diff --git a/tests/ReflectionHelper.php b/tests/ReflectionHelper.php new file mode 100644 index 0000000000..7c112742a3 --- /dev/null +++ b/tests/ReflectionHelper.php @@ -0,0 +1,43 @@ +object = $object; + + $this->reflection = new \ReflectionClass(get_class($this->object)); + } + + + public function invokeMethod($methodName, array $parameters = array()) + { + $method = $this->reflection->getMethod($methodName); + $method->setAccessible(true); + + return $method->invokeArgs($this->object, $parameters); + } + + public function getProperty($name) + { + $property = $this->reflection->getProperty($name); + $property->setAccessible(true); + return $property->getValue($this->object); + } + + public function setProperty($name, $value) + { + $property = $this->reflection->getProperty($name); + $property->setAccessible(true); + $property->setValue($this->object, $value); + } + + +} \ No newline at end of file diff --git a/tests/testBootstrap.php b/tests/testBootstrap.php deleted file mode 100644 index 526b9d54fc..0000000000 --- a/tests/testBootstrap.php +++ /dev/null @@ -1,15 +0,0 @@ -run(); - -$GLOBALS['app'] = $app; - - -?> \ No newline at end of file