Merge branch 'master' of ssh://172.20.0.1/var/git/espo/backend
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014 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/.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Controllers;
|
||||
|
||||
use \Espo\Core\Exceptions\Error,
|
||||
\Espo\Core\Exceptions\Forbidden,
|
||||
\Espo\Core\Exceptions\NotFound;
|
||||
|
||||
class FieldManager extends \Espo\Core\Controllers\Base
|
||||
{
|
||||
protected function checkGlobalAccess()
|
||||
{
|
||||
if (!$this->getUser()->isAdmin()) {
|
||||
throw new Forbidden();
|
||||
}
|
||||
}
|
||||
|
||||
public function actionRead($params, $data)
|
||||
{
|
||||
$data = $this->getContainer()->get('fieldManager')->read($params['name'], $params['scope']);
|
||||
|
||||
if (!isset($data)) {
|
||||
throw new NotFound();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function actionCreate($params, $data)
|
||||
{
|
||||
if (empty($data['name'])) {
|
||||
throw new Error("Field 'name' cannnot be empty");
|
||||
}
|
||||
|
||||
$name = $data['name'];
|
||||
unset($data['name']);
|
||||
|
||||
$this->getContainer()->get('fieldManager')->create($name, $data, $params['scope']);
|
||||
|
||||
return $this->getContainer()->get('fieldManager')->read($name, $params['scope']);
|
||||
}
|
||||
|
||||
public function actionUpdate($params, $data)
|
||||
{
|
||||
$this->getContainer()->get('fieldManager')->update($params['name'], $data, $params['scope']);
|
||||
|
||||
return $this->getContainer()->get('fieldManager')->read($params['name'], $params['scope']);
|
||||
}
|
||||
|
||||
public function actionDelete($params, $data)
|
||||
{
|
||||
return $this->getContainer()->get('fieldManager')->delete($params['name'], $params['scope']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,8 @@ class FieldManager
|
||||
|
||||
protected $metadataType = 'entityDefs';
|
||||
|
||||
protected $customOptionName = 'isCustom';
|
||||
|
||||
|
||||
public function __construct(Metadata $metadata)
|
||||
{
|
||||
@@ -52,6 +54,11 @@ class FieldManager
|
||||
|
||||
public function update($name, $fieldDef, $scope)
|
||||
{
|
||||
/*Add option to metadata that identify the custom field*/
|
||||
if (!isset($fieldDef[$this->customOptionName]) || !$fieldDef[$this->customOptionName]) {
|
||||
$fieldDef[$this->customOptionName] = true;
|
||||
}
|
||||
|
||||
$defs = $this->normalizeDefs($name, $fieldDef);
|
||||
|
||||
return $this->setEntityDefs($defs, $scope);
|
||||
|
||||
@@ -1,276 +1,318 @@
|
||||
[
|
||||
{
|
||||
"route":"/",
|
||||
"method":"get",
|
||||
"params":"<h1>EspoCRM REST API<\/h1>"
|
||||
"route":"/",
|
||||
"method":"get",
|
||||
"params":"<h1>EspoCRM REST API<\/h1>"
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/App/user",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"App",
|
||||
"action":"user"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Metadata",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Metadata"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"I18n",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"I18n"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Settings",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Settings"
|
||||
},
|
||||
"conditions":{
|
||||
"auth":false
|
||||
"route":"/App/user",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"App",
|
||||
"action":"user"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Settings",
|
||||
"method":"patch",
|
||||
"params":{
|
||||
"controller":"Settings"
|
||||
}
|
||||
"route":"/Metadata",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Metadata"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Admin/jobs",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Admin",
|
||||
"action":"jobs"
|
||||
}
|
||||
"route":"I18n",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"I18n"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Stream",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Stream",
|
||||
"route":"/Settings",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Settings"
|
||||
},
|
||||
"conditions":{
|
||||
"auth":false
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Settings",
|
||||
"method":"patch",
|
||||
"params":{
|
||||
"controller":"Settings"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Stream",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Stream",
|
||||
"action":"list",
|
||||
"scope": "User"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/GlobalSearch/:query",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"GlobalSearch",
|
||||
"route":"/GlobalSearch/:query",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"GlobalSearch",
|
||||
"action":"search",
|
||||
"query": ":query"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/action/:action",
|
||||
"method":"post",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":":action"
|
||||
}
|
||||
"route":"/:controller/action/:action",
|
||||
"method":"post",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":":action"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/action/:action",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":":action"
|
||||
}
|
||||
"route":"/:controller/action/:action",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":":action"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/action/:action",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":":action"
|
||||
}
|
||||
"route":"/:controller/action/:action",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":":action"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/layout/:name",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Layout",
|
||||
"route":"/:controller/layout/:name",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Layout",
|
||||
"scope":":controller"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/layout/:name",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":"Layout",
|
||||
"route":"/:controller/layout/:name",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":"Layout",
|
||||
"scope":":controller"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/layout/:name",
|
||||
"method":"patch",
|
||||
"params":{
|
||||
"controller":"Layout",
|
||||
"route":"/:controller/layout/:name",
|
||||
"method":"patch",
|
||||
"params":{
|
||||
"controller":"Layout",
|
||||
"scope":":controller"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Admin/rebuild",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Admin",
|
||||
"action":"rebuild"
|
||||
}
|
||||
"route":"/Admin/rebuild",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Admin",
|
||||
"action":"rebuild"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Admin/clearCache",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Admin",
|
||||
"action":"clearCache"
|
||||
}
|
||||
"route":"/Admin/clearCache",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Admin",
|
||||
"action":"clearCache"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/Admin/jobs",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Admin",
|
||||
"action":"jobs"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Admin/fieldManager/:scope/:name",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"FieldManager",
|
||||
"action":"read",
|
||||
"scope":":scope",
|
||||
"name":":name"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Admin/fieldManager/:scope",
|
||||
"method":"post",
|
||||
"params":{
|
||||
"controller":"FieldManager",
|
||||
"action":"create",
|
||||
"scope":":scope"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Admin/fieldManager/:scope/:name",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":"FieldManager",
|
||||
"action":"update",
|
||||
"scope":":scope",
|
||||
"name":":name"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/Admin/fieldManager/:scope/:name",
|
||||
"method":"delete",
|
||||
"params":{
|
||||
"controller":"FieldManager",
|
||||
"action":"delete",
|
||||
"scope":":scope",
|
||||
"name":":name"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"read",
|
||||
"id":":id"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/:controller",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"index"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller",
|
||||
"method":"post",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/:controller",
|
||||
"method":"post",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"create"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/:controller/:id",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"update",
|
||||
"id":":id"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id",
|
||||
"method":"patch",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/:controller/:id",
|
||||
"method":"patch",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"patch",
|
||||
"id":":id"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id",
|
||||
"method":"delete",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/:controller/:id",
|
||||
"method":"delete",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"delete",
|
||||
"id":":id"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id/stream",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Stream",
|
||||
"route":"/:controller/:id/stream",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":"Stream",
|
||||
"action":"list",
|
||||
"id":":id",
|
||||
"scope":":controller"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id/subscription",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"id":":id",
|
||||
"action":"follow"
|
||||
}
|
||||
"route":"/:controller/:id/subscription",
|
||||
"method":"put",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"id":":id",
|
||||
"action":"follow"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id/subscription",
|
||||
"method":"delete",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"id":":id",
|
||||
"action":"unfollow"
|
||||
}
|
||||
"route":"/:controller/:id/subscription",
|
||||
"method":"delete",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"id":":id",
|
||||
"action":"unfollow"
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id/:link",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/:controller/:id/:link",
|
||||
"method":"get",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"listLinked",
|
||||
"id":":id",
|
||||
"link":":link"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id/:link",
|
||||
"method":"post",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/:controller/:id/:link",
|
||||
"method":"post",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"createLink",
|
||||
"id":":id",
|
||||
"link":":link"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"route":"/:controller/:id/:link",
|
||||
"method":"delete",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"route":"/:controller/:id/:link",
|
||||
"method":"delete",
|
||||
"params":{
|
||||
"controller":":controller",
|
||||
"action":"removeLink",
|
||||
"id":":id",
|
||||
"link":":link"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
]
|
||||
|
||||
@@ -1,16 +0,0 @@
|
||||
{
|
||||
"fields": {
|
||||
"someName": {
|
||||
"type": "varchar",
|
||||
"maxLength": 40
|
||||
},
|
||||
"someName2": {
|
||||
"type": "varchar",
|
||||
"maxLength": 40
|
||||
},
|
||||
"some": {
|
||||
"type": "varchar",
|
||||
"maxLength": 40
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?php
|
||||
|
||||
namespace tests\Api;
|
||||
|
||||
class FieldManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $fixture;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$config = include('tests/Api/config.php');
|
||||
if (!$config['apiTestsEnabled']) {
|
||||
$this->markTestSkipped('API tests are not enabled.');
|
||||
}
|
||||
|
||||
require_once('tests/Api/RestTesterClass.php');
|
||||
$this->fixture = new RestTesterClass();
|
||||
|
||||
/****************************************/
|
||||
$this->fixture->setUrl('/Admin/fieldManager');
|
||||
/****************************************/
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->fixture = NULL;
|
||||
}
|
||||
|
||||
|
||||
public function testCreate()
|
||||
{
|
||||
$this->fixture->setType('POST');
|
||||
|
||||
$this->fixture->setUrl('/Admin/fieldManager/CustomEntity');
|
||||
|
||||
$input = '{"name":"customField","type":"varchar","maxLength":50}';
|
||||
$result = '{"type":"varchar","maxLength":50,"isCustom":true}';
|
||||
|
||||
$this->assertEquals($result, $this->fixture->getResponse($input)['response']);
|
||||
}
|
||||
|
||||
|
||||
public function testReadAfterCreate()
|
||||
{
|
||||
$this->fixture->setType('GET');
|
||||
|
||||
$this->fixture->setUrl('/Admin/fieldManager/CustomEntity/customField');
|
||||
$data = '{"type":"varchar","maxLength":50,"isCustom":true}';
|
||||
$this->assertEquals($data, $this->fixture->getResponse()['response']);
|
||||
}
|
||||
|
||||
public function testUpdate()
|
||||
{
|
||||
$this->fixture->setType('PUT');
|
||||
|
||||
$this->fixture->setUrl('/Admin/fieldManager/CustomEntity/customField');
|
||||
|
||||
$input = '{"type":"varchar","maxLength":50,"default":"this is a test"}';
|
||||
$result = '{"type":"varchar","maxLength":50,"isCustom":true,"default":"this is a test"}';
|
||||
|
||||
$this->assertEquals($result, $this->fixture->getResponse($input)['response']);
|
||||
}
|
||||
|
||||
public function testReadAfterUpdate()
|
||||
{
|
||||
$this->fixture->setType('GET');
|
||||
|
||||
$this->fixture->setUrl('/Admin/fieldManager/CustomEntity/customField');
|
||||
$data = '{"type":"varchar","maxLength":50,"isCustom":true,"default":"this is a test"}';
|
||||
$this->assertEquals($data, $this->fixture->getResponse()['response']);
|
||||
}
|
||||
|
||||
public function testDelete()
|
||||
{
|
||||
$this->fixture->setType('DELETE');
|
||||
|
||||
$this->fixture->setUrl('/Admin/fieldManager/CustomEntity/customField');
|
||||
$this->assertEquals('true', $this->fixture->getResponse()['response']);
|
||||
}
|
||||
|
||||
public function testReadAfterDetele()
|
||||
{
|
||||
$this->fixture->setType('GET');
|
||||
|
||||
$this->fixture->setUrl('/Admin/fieldManager/CustomEntity/customField');
|
||||
$response= $this->fixture->getResponse();
|
||||
$this->assertEquals(404, $response['code']);
|
||||
}
|
||||
|
||||
public function testDeleteTestFile()
|
||||
{
|
||||
$file = 'custom/Espo/Custom/Resources/metadata/entityDefs/CustomEntity.json';
|
||||
if (file_exists($file)) {
|
||||
@unlink($file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
return array(
|
||||
'apiTestsEnabled' => false,
|
||||
'apiUrl' => 'http://172.20.0.1/espocrm/api',
|
||||
'apiUrl' => 'http://172.20.0.1/taras/espo/api/v1',
|
||||
'username' => 'admin',
|
||||
'password' => '1',
|
||||
);
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace tests\Espo\Core\Utils;
|
||||
|
||||
use tests\ReflectionHelper;
|
||||
|
||||
|
||||
class FieldManagerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $object;
|
||||
|
||||
protected $objects;
|
||||
|
||||
protected $reflection;
|
||||
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->objects['metadata'] = $this->getMockBuilder('\Espo\Core\Utils\Metadata')->disableOriginalConstructor()->getMock();
|
||||
|
||||
$this->object = new \Espo\Core\Utils\FieldManager($this->objects['metadata']);
|
||||
|
||||
$this->reflection = new ReflectionHelper($this->object);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
$this->object = NULL;
|
||||
}
|
||||
|
||||
|
||||
public function testRead()
|
||||
{
|
||||
$data = array(
|
||||
"type" => "varchar",
|
||||
"maxLength" => "50",
|
||||
"isCustom" => true,
|
||||
);
|
||||
|
||||
$this->objects['metadata']
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->will($this->returnValue($data));
|
||||
|
||||
$this->assertEquals($data, $this->object->read('varName', 'Account'));
|
||||
}
|
||||
|
||||
public function testNormalizeDefs()
|
||||
{
|
||||
$input1 = 'fielName';
|
||||
$input2 = array(
|
||||
"type" => "varchar",
|
||||
"maxLength" => "50",
|
||||
);
|
||||
$result = array(
|
||||
'fields' => array(
|
||||
'fielName' => array(
|
||||
"type" => "varchar",
|
||||
"maxLength" => "50",
|
||||
),
|
||||
),
|
||||
);
|
||||
$this->assertEquals($result, $this->reflection->invokeMethod('normalizeDefs', array($input1, $input2)));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -15,48 +15,48 @@ require_once 'tests/testData/DB/MockDBResult.php';
|
||||
class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected $db;
|
||||
protected $pdo;
|
||||
protected $pdo;
|
||||
protected $post;
|
||||
protected $note;
|
||||
protected $comment;
|
||||
protected $entityFactory;
|
||||
protected $comment;
|
||||
protected $entityFactory;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->pdo = $this->getMock('MockPDO');
|
||||
$this->pdo = $this->getMock('MockPDO');
|
||||
$this->pdo
|
||||
->expects($this->any())
|
||||
->expects($this->any())
|
||||
->method('quote')
|
||||
->will($this->returnCallback(function() {
|
||||
$args = func_get_args();
|
||||
return "'" . $args[0] . "'";
|
||||
}));
|
||||
|
||||
|
||||
$this->entityFactory = $this->getMockBuilder('\\Espo\\ORM\\EntityFactory')->disableOriginalConstructor()->getMock();
|
||||
$this->entityFactory->expects($this->any())
|
||||
->method('create')
|
||||
->will($this->returnCallback(function() {
|
||||
$args = func_get_args();
|
||||
$args = func_get_args();
|
||||
$className = "\\Espo\\Entities\\" . $args[0];
|
||||
return new $className();
|
||||
}));
|
||||
|
||||
$this->db = new MysqlMapper($this->pdo, $this->entityFactory);
|
||||
|
||||
$this->db = new MysqlMapper($this->pdo, $this->entityFactory);
|
||||
$this->post = new \Espo\Entities\Post();
|
||||
$this->comment = new \Espo\Entities\Comment();
|
||||
$this->tag = new \Espo\Entities\Tag();
|
||||
$this->note = new \Espo\Entities\Note();
|
||||
|
||||
$this->contact = new \Espo\Entities\Contact();
|
||||
$this->account = new \Espo\Entities\Account();
|
||||
|
||||
$this->contact = new \Espo\Entities\Contact();
|
||||
$this->account = new \Espo\Entities\Account();
|
||||
|
||||
}
|
||||
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
unset($this->pdo, $this->db, $this->post, $this->comment);
|
||||
}
|
||||
|
||||
|
||||
protected function mockQuery($query, $return, $any = false)
|
||||
{
|
||||
if ($any) {
|
||||
@@ -64,16 +64,16 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
} else {
|
||||
$expects = $this->once();
|
||||
}
|
||||
|
||||
$this->pdo->expects($expects)
|
||||
|
||||
$this->pdo->expects($expects)
|
||||
->method('query')
|
||||
->with($query)
|
||||
->will($this->returnValue($return));
|
||||
}
|
||||
|
||||
|
||||
public function testSelectById()
|
||||
{
|
||||
$query =
|
||||
$query =
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(user_f.salutation_name, user_f.first_name, ' ', user_f.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"FROM `post` ".
|
||||
"LEFT JOIN `user` AS `user_f` ON post.created_by_id = user_f.id " .
|
||||
@@ -86,14 +86,14 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
));
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->db->selectById($this->post, '1');
|
||||
$this->assertEquals($this->post->id, '1');
|
||||
$this->assertEquals($this->post->id, '1');
|
||||
}
|
||||
|
||||
|
||||
public function testSelect()
|
||||
{
|
||||
$query =
|
||||
$query =
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(user_f.salutation_name, user_f.first_name, ' ', user_f.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"FROM `post` ".
|
||||
"LEFT JOIN `user` AS `user_f` ON post.created_by_id = user_f.id " .
|
||||
@@ -116,13 +116,13 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
));
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$selectParams = array(
|
||||
'whereClause' => array(
|
||||
'name' => 'test_1',
|
||||
'OR' => array(
|
||||
'id' => '100',
|
||||
'name*' => 'test_%',
|
||||
'name*' => 'test_%',
|
||||
),
|
||||
'Tag.name' => 'yoTag',
|
||||
),
|
||||
@@ -135,22 +135,22 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
);
|
||||
$list = $this->db->select($this->post, $selectParams);
|
||||
|
||||
|
||||
|
||||
|
||||
$this->assertTrue($list[0] instanceof Post);
|
||||
$this->assertTrue(isset($list[0]->id));
|
||||
$this->assertEquals($list[0]->id, '2');
|
||||
$this->assertTrue(isset($list[0]->id));
|
||||
$this->assertEquals($list[0]->id, '2');
|
||||
}
|
||||
|
||||
|
||||
public function testSelectWithSpecifiedParams()
|
||||
{
|
||||
$query =
|
||||
$query =
|
||||
"SELECT contact.id AS `id`, TRIM(CONCAT(contact.first_name, ' ', contact.last_name)) AS `name`, contact.first_name AS `firstName`, contact.last_name AS `lastName`, contact.deleted AS `deleted` ".
|
||||
"FROM `contact` ".
|
||||
"WHERE (contact.first_name LIKE 'test%' OR contact.last_name LIKE 'test%' OR CONCAT(contact.first_name, ' ', contact.last_name) LIKE 'test%') AND contact.deleted = '0' ".
|
||||
"ORDER BY contact.first_name DESC, contact.last_name DESC ".
|
||||
"LIMIT 0, 10";
|
||||
|
||||
|
||||
$return = new MockDBResult(array(
|
||||
array(
|
||||
'id' => '1',
|
||||
@@ -159,7 +159,7 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
));
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$selectParams = array(
|
||||
'whereClause' => array(
|
||||
'name*' => 'test%',
|
||||
@@ -170,10 +170,10 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
);
|
||||
$list = $this->db->select($this->contact, $selectParams);
|
||||
}
|
||||
|
||||
|
||||
public function testJoin()
|
||||
{
|
||||
$query =
|
||||
$query =
|
||||
"SELECT comment.id AS `id`, comment.post_id AS `postId`, post_f.name AS `postName`, comment.name AS `name`, comment.deleted AS `deleted` ".
|
||||
"FROM `comment` ".
|
||||
"LEFT JOIN `post` AS `post_f` ON comment.post_id = post_f.id ".
|
||||
@@ -188,17 +188,17 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
));
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$list = $this->db->select($this->comment);
|
||||
|
||||
$this->assertTrue($list[0] instanceof Comment);
|
||||
$this->assertTrue($list[0]->has('postName'));
|
||||
$this->assertEquals($list[0]->get('postName'), 'test');
|
||||
}
|
||||
|
||||
|
||||
public function testSelectRelatedManyMany()
|
||||
{
|
||||
$query =
|
||||
$query =
|
||||
"SELECT tag.id AS `id`, tag.name AS `name`, tag.deleted AS `deleted` ".
|
||||
"FROM `tag` ".
|
||||
"JOIN `post_tag` ON tag.id = post_tag.tag_id AND post_tag.post_id = '1' AND post_tag.deleted = '0' ".
|
||||
@@ -213,15 +213,15 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
$this->mockQuery($query, $return);
|
||||
$this->post->id = '1';
|
||||
$list = $this->db->selectRelated($this->post, 'tags');
|
||||
|
||||
|
||||
$this->assertTrue($list[0] instanceof Tag);
|
||||
$this->assertTrue($list[0]->has('name'));
|
||||
$this->assertEquals($list[0]->get('name'), 'test');
|
||||
}
|
||||
|
||||
|
||||
public function testSelectRelatedHasChildren()
|
||||
{
|
||||
$query =
|
||||
$query =
|
||||
"SELECT note.id AS `id`, note.name AS `name`, note.parent_id AS `parentId`, note.parent_type AS `parentType`, note.deleted AS `deleted` ".
|
||||
"FROM `note` ".
|
||||
"WHERE note.deleted = '0' AND note.parent_id = '1' AND note.parent_type = 'Post'";
|
||||
@@ -235,15 +235,15 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
$this->mockQuery($query, $return);
|
||||
$this->post->id = '1';
|
||||
$list = $this->db->selectRelated($this->post, 'notes');
|
||||
|
||||
|
||||
$this->assertTrue($list[0] instanceof Note);
|
||||
$this->assertTrue($list[0]->has('name'));
|
||||
$this->assertEquals($list[0]->get('name'), 'test');
|
||||
}
|
||||
|
||||
|
||||
public function testSelectRelatedBelongsTo()
|
||||
{
|
||||
$query =
|
||||
$query =
|
||||
"SELECT post.id AS `id`, post.name AS `name`, TRIM(CONCAT(user_f.salutation_name, user_f.first_name, ' ', user_f.last_name)) AS `createdByName`, post.created_by_id AS `createdById`, post.deleted AS `deleted` ".
|
||||
"FROM `post` ".
|
||||
"LEFT JOIN `user` AS `user_f` ON post.created_by_id = user_f.id " .
|
||||
@@ -257,20 +257,20 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
));
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->comment->id = '11';
|
||||
$this->comment->set('postId', '1');
|
||||
$post = $this->db->selectRelated($this->comment, 'post');
|
||||
|
||||
$post = $this->db->selectRelated($this->comment, 'post');
|
||||
|
||||
$this->assertTrue($post instanceof Post);
|
||||
$this->assertTrue(($post->has('name')));
|
||||
$this->assertEquals($post->get('name'), 'test');
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function testCountRelated()
|
||||
{
|
||||
$query =
|
||||
$query =
|
||||
"SELECT COUNT(tag.id) AS AggregateValue ".
|
||||
"FROM `tag` ".
|
||||
"JOIN `post_tag` ON tag.id = post_tag.tag_id AND post_tag.post_id = '1' AND post_tag.deleted = '0' ".
|
||||
@@ -281,117 +281,117 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
),
|
||||
));
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->post->id = '1';
|
||||
$count = $this->db->countRelated($this->post, 'tags');
|
||||
|
||||
|
||||
$this->assertEquals($count, 1);
|
||||
}
|
||||
|
||||
|
||||
public function testInsert()
|
||||
{
|
||||
$query = "INSERT INTO `post` (id, name) VALUES ('1', 'test')";
|
||||
{
|
||||
$query = "INSERT INTO `post` (`id`, `name`) VALUES ('1', 'test')";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
$this->post->reset();
|
||||
|
||||
$this->post->reset();
|
||||
$this->post->id = '1';
|
||||
$this->post->set('name', 'test');
|
||||
$this->post->set('privateField', 'dontStoreThis');
|
||||
|
||||
$this->db->insert($this->post);
|
||||
|
||||
$this->db->insert($this->post);
|
||||
}
|
||||
|
||||
|
||||
public function testUpdate()
|
||||
{
|
||||
{
|
||||
$query = "UPDATE `post` SET name = 'test' WHERE post.id = '1' AND post.deleted = '0'";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
$this->post->reset();
|
||||
|
||||
$this->post->reset();
|
||||
$this->post->id = '1';
|
||||
$this->post->set('name', 'test');
|
||||
|
||||
$this->db->update($this->post);
|
||||
|
||||
$this->db->update($this->post);
|
||||
}
|
||||
|
||||
|
||||
public function testRemoveRelationHasMany()
|
||||
{
|
||||
$query = "UPDATE `comment` SET post_id = NULL WHERE comment.deleted = '0' AND comment.id = '100'";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->post->id = '1';
|
||||
$this->db->removeRelation($this->post, 'comments', '100');
|
||||
}
|
||||
|
||||
|
||||
public function testRemoveAllHasMany()
|
||||
{
|
||||
$query = "UPDATE `comment` SET post_id = NULL WHERE comment.deleted = '0' AND comment.post_id = '1'";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->post->id = '1';
|
||||
$this->db->removeAllRelations($this->post, 'comments');
|
||||
}
|
||||
|
||||
|
||||
public function testRemoveRelationManyMany()
|
||||
{
|
||||
$query = "UPDATE `post_tag` SET deleted = 1 WHERE post_id = '1' AND tag_id = '100'";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->post->id = '1';
|
||||
$this->db->removeRelation($this->post, 'tags', '100');
|
||||
$this->db->removeRelation($this->post, 'tags', '100');
|
||||
}
|
||||
|
||||
|
||||
public function testRemoveAllManyMany()
|
||||
{
|
||||
$query = "UPDATE `post_tag` SET deleted = 1 WHERE post_id = '1'";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->post->id = '1';
|
||||
$this->db->removeAllRelations($this->post, 'tags');
|
||||
$this->db->removeAllRelations($this->post, 'tags');
|
||||
}
|
||||
|
||||
|
||||
public function testRemoveRelationManyManyWithCondition()
|
||||
{
|
||||
$query = "UPDATE `entity_team` SET deleted = 1 WHERE entity_id = '1' AND team_id = '100' AND entity_type = 'Account'";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->account->id = '1';
|
||||
$this->db->removeRelation($this->account, 'teams', '100');
|
||||
$this->db->removeRelation($this->account, 'teams', '100');
|
||||
}
|
||||
|
||||
|
||||
public function testRemoveAllManyManyWithCondition()
|
||||
{
|
||||
$query = "UPDATE `entity_team` SET deleted = 1 WHERE entity_id = '1' AND entity_type = 'Account'";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->account->id = '1';
|
||||
$this->db->removeAllRelations($this->account, 'teams');
|
||||
$this->db->removeAllRelations($this->account, 'teams');
|
||||
}
|
||||
|
||||
|
||||
public function testUnrelate()
|
||||
{
|
||||
$query = "UPDATE `post_tag` SET deleted = 1 WHERE post_id = '1' AND tag_id = '100'";
|
||||
$return = true;
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$this->post->id = '1';
|
||||
$this->tag->id = '100';
|
||||
$this->db->unrelate($this->post, 'tags', $this->tag);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public function testAddRelation()
|
||||
{
|
||||
// @todo
|
||||
// @todo
|
||||
}
|
||||
|
||||
|
||||
public function testMax()
|
||||
{
|
||||
$query = "SELECT MAX(post.id) AS AggregateValue FROM `post` LEFT JOIN `user` AS `user_f` ON post.created_by_id = user_f.id";
|
||||
@@ -401,9 +401,9 @@ class DBMapperTest extends PHPUnit_Framework_TestCase
|
||||
)
|
||||
));
|
||||
$this->mockQuery($query, $return);
|
||||
|
||||
|
||||
$value = $this->db->max($this->post, array(), 'id', true);
|
||||
|
||||
|
||||
$this->assertEquals($value, 10);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user