Merge branch 'master' of ssh://172.20.0.1/var/git/espo/backend

This commit is contained in:
Yuri Kuznetsov
2014-01-20 16:29:22 +02:00
26 changed files with 228 additions and 1164 deletions
+7 -4
View File
@@ -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;
}
+94 -90
View File
@@ -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;
}
}
+1 -1
View File
@@ -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'));
}
@@ -1,33 +0,0 @@
<?php
return array (
'Espo\\Entities\\User' =>
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)',
),
),
),
);
?>
@@ -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' =>
+34 -9
View File
@@ -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']);
}
-59
View File
@@ -1,59 +0,0 @@
<?php
namespace tests\Api;
require_once('tests/testBootstrap.php');
class MetadataTest extends \PHPUnit_Framework_TestCase
{
protected $fixture;
protected function setUp()
{
require_once('tests/Api/RestTesterClass.php');
$this->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'));
}*/
}
?>
+1 -1
View File
@@ -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'];
-49
View File
@@ -1,49 +0,0 @@
<?php
namespace tests\Api;
require_once('tests/testBootstrap.php');
class SettingsTest extends \PHPUnit_Framework_TestCase
{
protected $fixture;
protected function setUp()
{
require_once('tests/Api/RestTesterClass.php');
$this->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);
}
}
?>
+1 -1
View File
@@ -1,7 +1,7 @@
<?php
return array(
'apiUrl' => 'http://172.20.0.1/espocrm-new/api',
'apiUrl' => 'http://172.20.0.1/espocrm/api',
'username' => 'admin',
'password' => '1',
);
@@ -1,535 +0,0 @@
<?php
namespace tests\Espo\Core\Utils\Database;
require_once('tests/testBootstrap.php');
use Espo\Utils as Utils;
class RelationsTest extends \PHPUnit_Framework_TestCase
{
protected $object;
private $app;
public function __construct()
{
$this->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'])) );
}
}
@@ -1,83 +0,0 @@
<?php
namespace tests\Espo\Core\Utils\Database\Schema;
class ConverterTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected $reflection;
protected function setUp()
{
/*$fileManager = $this->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);
}
}
-269
View File
@@ -1,269 +0,0 @@
<?php
namespace tests\Espo\Core\Utils\File;
class ManagerTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected $filesPath= 'tests/testData/FileManager';
protected function setUp()
{
$this->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)));
}
}
?>
+47
View File
@@ -0,0 +1,47 @@
<?php
namespace tests\Espo\Core\Utils;
use Espo\Core\Utils\Json;
class JsonTest extends \PHPUnit_Framework_TestCase
{
function testEncode()
{
$testVal= array('testOption'=>'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));
}
}
?>
-2
View File
@@ -2,8 +2,6 @@
namespace tests\Espo\Core\Utils;
require_once('tests/testBootstrap.php');
use Espo\Core\Utils\Util;
+43
View File
@@ -0,0 +1,43 @@
<?php
namespace tests;
class ReflectionHelper
{
private $object;
private $reflection;
public function __construct($object)
{
$this->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);
}
}
-15
View File
@@ -1,15 +0,0 @@
<?php
error_reporting(E_ALL ^ E_NOTICE);
require_once('bootstrap.php');
$app = new \Espo\Core\Application();
//$app->run();
$GLOBALS['app'] = $app;
?>