snapshot of https://github.com/espocrm/espocrm.git (master, 2026-09-18; blobs > 131072B omitted)
Some checks failed
Test / Test on PHP 8.3 (push) Failing after 5s
Test / Test on PHP 8.4 (push) Failing after 4s
Test / Test on PHP 8.5 (push) Failing after 3s
TypeScript static analysis / TypeScript check (push) Failing after 14s
Test Integration on MySQL / Test on PHP 8.3 (push) Failing after 2s
Test Integration on MySQL / Test on PHP 8.5 (push) Failing after 0s
Test Integration on PostgreSQL / Test on PHP 8.4 fix (push) Failing after 1s
Test Integration on PostgreSQL / Test on PHP 8.4 master (push) Failing after 1s

This commit is contained in:
upstream-mirror
2026-09-18 04:19:15 +05:00
commit 7cc7e6b4f3
10077 changed files with 900756 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use Espo\Entities\Team;
use Espo\Modules\Crm\Entities\Account;
use tests\integration\Core\BaseTestCase;
class EntityManagerTest extends BaseTestCase
{
public function testRefreshEntity(): void
{
$em = $this->getEntityManager();
$team1 = $em->createEntity(Team::ENTITY_TYPE);
$team2 = $em->createEntity(Team::ENTITY_TYPE);
/** @var Account $account */
$account = $em->createEntity(Account::ENTITY_TYPE, [
'name' => 'Test',
'teamsIds' => [$team1->getId()],
]);
$account->set('name', 'Hello');
$em->getRelation($account, 'teams')->relateById($team2->getId());
$em->refreshEntity($account);
$this->assertEquals('Test', $account->get('name'));
$this->assertFalse($account->isAttributeChanged('name'));
$this->assertEqualsCanonicalizing([$team1->getId(), $team2->getId()], $account->getLinkMultipleIdList('teams'));
}
}

View File

@@ -0,0 +1,94 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use Espo\Core\Name\Field;
use Espo\Entities\Team;
use Espo\Modules\Crm\Entities\Account;
use tests\integration\Core\BaseTestCase;
class EntityTest extends BaseTestCase
{
public function testLinkMultiple(): void
{
$em = $this->getEntityManager();
$team1 = $em->createEntity(Team::ENTITY_TYPE);
$team2 = $em->createEntity(Team::ENTITY_TYPE);
$team3 = $em->createEntity(Team::ENTITY_TYPE);
$account = $em->createEntity(Account::ENTITY_TYPE, [
'teamsIds' => [
$team1->getId(),
$team2->getId(),
],
]);
$account = $em->getRDBRepositoryByClass(Account::class)->getById($account->getId());
$set = $account->getLinkMultipleIdList(Field::TEAMS);
$expected = [$team1->getId(), $team2->getId()];
$this->assertTrue(
array_diff($set, $expected) === array_diff($expected, $set)
);
//
$account = $em->getRDBRepositoryByClass(Account::class)->getById($account->getId());
$account->addLinkMultipleId(Field::TEAMS, $team3->getId());
$set = $account->getFetchedLinkMultipleIdList(Field::TEAMS);
$expected = [$team1->getId(), $team2->getId()];
$this->assertTrue(
array_diff($set, $expected) === array_diff($expected, $set)
);
$set = $account->getLinkMultipleIdList(Field::TEAMS);
$expected = [$team1->getId(), $team2->getId(), $team3->getId()];
$this->assertTrue(
array_diff($set, $expected) === array_diff($expected, $set)
);
//
$account = $em->getRDBRepositoryByClass(Account::class)->getById($account->getId());
$set = $account->getFetchedLinkMultipleIdList(Field::TEAMS);
$expected = [$team1->getId(), $team2->getId()];
$this->assertTrue(
array_diff($set, $expected) === array_diff($expected, $set)
);
}
}

View File

@@ -0,0 +1,51 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use Espo\ORM\Query\Part\Selection;
use tests\integration\Core\BaseTestCase;
class FunctionConverterTest extends BaseTestCase
{
public function testAbs(): void
{
$entityManager = $this->getEntityManager();
$query = $entityManager->getQueryBuilder()
->select(Selection::fromString('ABS:(-1)')->withAlias('value'))
->build();
$sth = $entityManager->getQueryExecutor()->execute($query);
$value = $sth->fetch()['value'];
$this->assertEquals('1', $value);
}
}

View File

@@ -0,0 +1,341 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use Espo\ORM\EntityManager;
use tests\integration\Core\BaseTestCase;
class MapperTest extends BaseTestCase
{
public function testRelate1()
{
$entityManager = $this->getContainer()->getByClass(EntityManager::class);
$account = $entityManager->getNewEntity('Account');
$account->set('name', 'Test');
$entityManager->saveEntity($account);
$contact = $entityManager->getNewEntity('Contact');
$contact->set('lastName', 'Test');
$entityManager->saveEntity($contact);
$entityManager->getRelation($account, 'contacts')->relate($contact);
$isRelated = $entityManager->getRelation($account, 'contacts')->isRelated($contact);
$this->assertTrue($isRelated);
$entityManager->getRelation($account, 'contacts')->unrelate($contact);
$isRelated = $entityManager->getRelation($account, 'contacts')->isRelated($contact);
$this->assertFalse($isRelated);
}
public function testRelate2()
{
$entityManager = $this->getEntityManager();
$account = $entityManager->getNewEntity('Account');
$account->set('name', 'Test');
$entityManager->saveEntity($account);
$contact = $entityManager->getNewEntity('Contact');
$contact->set('lastName', 'Test');
$entityManager->saveEntity($contact);
$entityManager->getRelation($contact, 'account')->relate($account);
$isRelated = $entityManager->getRelation($contact, 'account')->isRelated($account);
$this->assertTrue($isRelated);
$contact = $entityManager->getEntityById('Contact', $contact->getId());
$entityManager->getRelation($contact, 'account')->unrelate($account);
$isRelated = $entityManager->getRelation($contact, 'account')->isRelated($account);
$this->assertFalse($isRelated);
}
public function testRelate3WithEntityReFetching()
{
$entityManager = $this->getEntityManager();
$account = $entityManager->getNewEntity('Account');
$account->set('name', 'Test');
$entityManager->saveEntity($account);
$contact = $entityManager->getNewEntity('Contact');
$contact->set('lastName', 'Test');
$entityManager->saveEntity($contact);
$entityManager->getRDBRepository('Contact')
->getRelation($contact, 'account')
->relate($account);
$contact = $entityManager->getEntityById('Contact', $contact->get('id'));
$isRelated = $entityManager->getRDBRepository('Contact')
->getRelation($contact, 'account')
->isRelated($account);
$this->assertTrue($isRelated);
$entityManager = $this->getEntityManager();
$entityManager->getRDBRepository('Contact')
->getRelation($contact, 'account')
->unrelate($account);
$contact = $entityManager->getEntityById('Contact', $contact->get('id'));
$isRelated = $entityManager
->getRDBRepository('Contact')
->getRelation($contact, 'account')
->isRelated($account);
$this->assertFalse($isRelated);
}
public function testRelate4()
{
$entityManager = $this->getEntityManager();
$account = $entityManager->getNewEntity('Account');
$account->set('name', 'Test');
$entityManager->saveEntity($account);
$task = $entityManager->getNewEntity('Task');
$task->set('name', 'Test');
$entityManager->saveEntity($task);
$entityManager->getRelation($task, 'parent')->relate($account);
$isRelated = $entityManager->getRelation($task, 'parent')->isRelated($account);
$this->assertTrue($isRelated);
$task = $entityManager->getEntityById('Task', $task->getId());
$entityManager->getRelation($task, 'parent')->unrelate($account);
$isRelated = $entityManager->getRelation($task, 'parent')->isRelated($account);
$this->assertFalse($isRelated);
}
public function testRelateOneToOne1()
{
$em = $this->getEntityManager();
$a1 = $em->createEntity('Account', [
'name' => '1',
]);
$a2 = $em->createEntity('Account', [
'name' => '2',
]);
$l1 = $em->createEntity('Lead', [
'lastName' => '1',
]);
$em->createEntity('Lead', [
'lastName' => '2',
]);
$em->getRelation($l1, 'createdAccount')->relate($a1);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a1);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($a1, 'originalLead')->isRelated($l1);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a2);
$this->assertFalse($isRelated);
$isRelated = $em->getRelation($a2, 'originalLead')->isRelated($l1);
$this->assertFalse($isRelated);
$em->getRelation($l1, 'createdAccount')->relate($a2);
$isRelated = $em->getRelation($a2, 'originalLead')->isRelated($l1);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a2);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a1);
$this->assertFalse($isRelated);
$isRelated = $em->getRelation($a1, 'originalLead')->isRelated($l1);
$this->assertFalse($isRelated);
$c = $em->getRDBRepository('Lead')->where(['createdAccountId' => $a1->getId()])->count();
$this->assertEquals(0, $c);
$c = $em->getRDBRepository('Lead')->where(['createdAccountId' => $a2->getId()])->count();
$this->assertEquals(1, $c);
}
public function testRelateOneToOne2()
{
$em = $this->getEntityManager();
$a1 = $em->createEntity('Account', [
'name' => '1',
]);
$a2 = $em->createEntity('Account', [
'name' => '2',
]);
$l1 = $em->createEntity('Lead', [
'lastName' => '1',
]);
$em->getRelation($a1, 'originalLead')->relate($l1);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a1);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($a1, 'originalLead')->isRelated($l1);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a2);
$this->assertFalse($isRelated);
$isRelated = $em->getRelation($a2, 'originalLead')->isRelated($l1);
$this->assertFalse($isRelated);
$em->getRelation($a2, 'originalLead')->relate($l1);
$isRelated = $em->getRelation($a2, 'originalLead')->isRelated($l1);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a2);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a1);
$this->assertFalse($isRelated);
$isRelated = $em->getRelation($a1, 'originalLead')->isRelated($l1);
$this->assertFalse($isRelated);
$c = $em->getRDBRepository('Lead')
->where(['createdAccountId' => $a1->getId()])
->count();
$this->assertEquals(0, $c);
$c = $em->getRDBRepository('Lead')
->where(['createdAccountId' => $a2->getId()])
->count();
$this->assertEquals(1, $c);
}
public function testRelateOneToOne3()
{
$em = $this->getEntityManager();
$a1 = $em->createEntity('Account', [
'name' => '1',
]);
$em->createEntity('Account', [
'name' => '2',
]);
$l1 = $em->createEntity('Lead', [
'lastName' => '1',
]);
$l2 = $em->createEntity('Lead', [
'lastName' => '2',
]);
$em->getRelation($l1, 'createdAccount')->relate($a1);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a1);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($a1, 'originalLead')->isRelated($l1);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($l2, 'createdAccount')->isRelated($a1);
$this->assertFalse($isRelated);
$isRelated = $em->getRelation($a1, 'originalLead')->isRelated($l2);
$this->assertFalse($isRelated);
$em->getRelation($l2, 'createdAccount')->relate($a1);
$isRelated = $em->getRelation($a1, 'originalLead')->isRelated($l2);
$this->assertTrue($isRelated);
$isRelated = $em->getRelation($l2, 'createdAccount')->isRelated($a1);
$this->assertTrue($isRelated);
$l1 = $em->getEntityById('Lead', $l1->getId());
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a1);
$this->assertFalse($isRelated);
$isRelated = $em->getRelation($a1, 'originalLead')->isRelated($l1);
$this->assertFalse($isRelated);
$c = $em->getRDBRepository('Lead')
->where(['createdAccountId' => $a1->getId()])
->count();
$this->assertEquals(1, $c);
}
public function testUnrelateOneToOne1()
{
$em = $this->getEntityManager();
$a1 = $em->createEntity('Account', [
'name' => '1',
]);
$em->createEntity('Account', [
'name' => '2',
]);
$l1 = $em->createEntity('Lead', [
'lastName' => '1',
]);
$em->createEntity('Lead', [
'lastName' => '2',
]);
$em->getRelation($l1, 'createdAccount')->relate($a1);
$em->getRelation($l1, 'createdAccount')->unrelate($a1);
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a1);
$this->assertFalse($isRelated);
$em->getRelation($l1, 'createdAccount')->relate($a1);
$em->getRelation($a1, 'originalLead')->unrelate($l1);
$l1 = $em->getEntityById('Lead', $l1->getId());
$isRelated = $em->getRelation($l1, 'createdAccount')->isRelated($a1);
$this->assertFalse($isRelated);
}
}

View File

@@ -0,0 +1,62 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use PDO;
use tests\integration\Core\BaseTestCase;
class QueryTest extends BaseTestCase
{
public function testQuery1()
{
$entityManager = $this->getEntityManager();
$account = $entityManager->getNewEntity('Account');
$account->set('name', 'Test');
$entityManager->saveEntity($account);
$query = $entityManager->getQueryBuilder()
->select()
->from('Account')
->select('id')
->select("CONCAT:(',test',\"+\",'\"', \"',ROUND(1)\")", 'value')
->build();
$rowList = [];
$sth = $entityManager->getQueryExecutor()->execute($query);
while ($row = $sth->fetch(PDO::FETCH_ASSOC)) {
$rowList[] = $row;
}
$this->assertEquals(",test+\"',ROUND(1)", $rowList[0]['value']);
}
}

View File

@@ -0,0 +1,311 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use Espo\Modules\Crm\Entities\Contact;
use Espo\Modules\Crm\Entities\Lead;
use Espo\Modules\Crm\Entities\Task;
use Espo\ORM\Defs\Params\RelationParam;
use Espo\ORM\EntityCollection;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Opportunity;
use Espo\ORM\Repository\Option\SaveOption;
use tests\integration\Core\BaseTestCase;
use tests\integration\testClasses\Entities\Account as AccountExtended;
class RelationsTest extends BaseTestCase
{
public function testGetOne(): void
{
$metadata = $this->getMetadata();
$metadata->set('entityDefs', Opportunity::ENTITY_TYPE, [
'links' => [
'account' => [
'deferredLoad' => true,
],
],
]);
$metadata->save();
$this->authenticate();
$em = $this->getEntityManager();
$opp = $em->getNewEntity(Opportunity::ENTITY_TYPE);
$this->assertInstanceOf(Opportunity::class, $opp);
$this->assertNull($opp->getAccount());
/** @var Account $account */
$account = $em->createEntity(Account::ENTITY_TYPE, ['name' => 'Account 1']);
$opp = $em->createEntity(Opportunity::ENTITY_TYPE, [
'accountId' => $account->getId(),
]);
$this->assertInstanceOf(Opportunity::class, $opp);
$account1 = $opp->getAccount();
$this->assertInstanceOf(Account::class, $account1);
$this->assertEquals($account->getId(), $account1->getId());
$em->refreshEntity($opp);
$account2 = $opp->getAccount();
$this->assertInstanceOf(Account::class, $account2);
$this->assertNotSame($account1, $account2);
$this->assertEquals($account->getId(), $account2->getId());
$this->assertEquals($account->getName(), $account2->getName());
$em->saveEntity($opp);
$account3 = $opp->getAccount();
$this->assertInstanceOf(Account::class, $account3);
$this->assertNotSame($account1, $account3);
$this->assertEquals($account->getId(), $account3->getId());
// Soft-deleted.
$em->removeEntity($account3);
$em->refreshEntity($opp);
$this->assertNull($opp->getAccount());
}
public function testGetMany(): void
{
$metadata = $this->getMetadata();
$metadata->set('entityDefs', Account::ENTITY_TYPE, [
'entityClassName' => AccountExtended::class,
]);
$metadata->save();
$this->authenticate();
$em = $this->getEntityManager();
$account = $em->getNewEntity(Account::ENTITY_TYPE);
$this->assertInstanceOf(AccountExtended::class, $account);
$this->assertInstanceOf(EntityCollection::class, $account->getRelatedOpportunities());
$account = $em->createEntity(Account::ENTITY_TYPE);
$this->assertInstanceOf(AccountExtended::class, $account);
$em->createEntity(Opportunity::ENTITY_TYPE, ['accountId' => $account->getId()]);
$em->createEntity(Opportunity::ENTITY_TYPE, ['accountId' => $account->getId()]);
$em->createEntity(Opportunity::ENTITY_TYPE);
$collection = $account->getRelatedOpportunities();
$this->assertInstanceOf(EntityCollection::class, $collection);
$items = iterator_to_array($collection);
$this->assertCount(2, $items);
}
public function testSet(): void
{
$metadata = $this->getMetadata();
$metadata->set('entityDefs', Account::ENTITY_TYPE, [
'entityClassName' => AccountExtended::class,
]);
$metadata->save();
$this->authenticate(null);
//$this->reCreateApplication();
$em = $this->getEntityManager();
/** @var Account $account */
$account = $em->createEntity(Account::ENTITY_TYPE, ['name' => 'Account 1']);
// belongsTo
$opp = $em->getRDBRepositoryByClass(Opportunity::class)->getNew();
$opp->setAccount($account);
$em->saveEntity($opp, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($opp);
$this->assertEquals($account->getId(), $opp->getAccount()->getId());
$this->assertEquals($account->getName(), $opp->getAccount()->getName());
$opp->setAccount(null);
$em->saveEntity($opp, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($opp);
$this->assertNull($opp->getAccount());
// belongsToParent
$task = $em->getRDBRepositoryByClass(Task::class)->getNew();
$task->setParent($account);
$em->saveEntity($task, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($task);
$this->assertEquals($account->getId(), $task->get('parentId'));
$this->assertEquals($account->getEntityType(), $task->get('parentType'));
$this->assertEquals($account->getId(), $task->getParent()->getId());
$this->assertEquals($account->getName(), $task->getParent()->get('name'));
$task = $em->getRDBRepositoryByClass(Task::class)->getNew();
$task->setParent(null);
$em->saveEntity($task, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($task);
$this->assertNull($task->get('parentId'));
$this->assertNull($task->get('parentType'));
// belongsTo hasOne
$lead1 = $em->getRDBRepositoryByClass(Lead::class)->getNew();
$lead1->setCreatedAccount($account);
$em->saveEntity($lead1, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($lead1);
$this->assertEquals($account->getId(), $lead1->get('createdAccountId'));
$lead2 = $em->getRDBRepositoryByClass(Lead::class)->getNew();
$lead2->setCreatedAccount($account);
$em->saveEntity($lead2, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($lead2);
$this->assertEquals($account->getId(), $lead2->get('createdAccountId'));
$em->refreshEntity($lead1);
$this->assertEquals(null, $lead1->get('createdAccountId'));
$lead2->setCreatedAccount(null);
$em->saveEntity($lead2);
// hasOne
$em->refreshEntity($account);
$this->assertInstanceOf(AccountExtended::class, $account);
$account->setRelatedOriginalLead($lead1);
$em->saveEntity($account, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($lead1);
$this->assertEquals($account->getId(), $lead1->get('createdAccountId'));
$account->setRelatedOriginalLead($lead2);
$em->saveEntity($account, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($lead2);
$this->assertEquals($account->getId(), $lead2->get('createdAccountId'));
$em->refreshEntity($lead1);
$this->assertEquals(null, $lead1->get('createdAccountId'));
$account->setRelatedOriginalLead(null);
$em->saveEntity($account, [SaveOption::SKIP_ALL => true]);
$em->refreshEntity($lead2);
$this->assertEquals(null, $lead2->get('createdAccountId'));
}
public function testOrder(): void
{
$em = $this->getEntityManager();
$account1 = $em->createEntity(Account::ENTITY_TYPE, ['name' => 'a1']);
$account2 = $em->createEntity(Account::ENTITY_TYPE, ['name' => 'a2']);
$contact = $em->createEntity(Contact::ENTITY_TYPE);
$this->assertInstanceOf(Contact::class, $contact);
$em->getRelation($contact, 'accounts')->relate($account1);
$em->getRelation($contact, 'accounts')->relate($account2);
//
$metadata = $this->getMetadata();
$metadata->set('entityDefs', Contact::ENTITY_TYPE, [
'links' => [
'accounts' => [
RelationParam::ORDER_BY => 'name',
RelationParam::ORDER => 'desc',
]
]
]);
$metadata->save();
$this->getDataManager()->rebuildMetadata();
$this->reCreateApplication(reuse: true);
$em = $this->getEntityManager();
$contact = $em->getRDBRepositoryByClass(Contact::class)->getById($contact->getId());
$this->assertNotNull($contact);
$accounts = $contact->getAccounts();
$this->assertCount(2, $accounts);
$this->assertEquals('a2', $accounts[0]->getName());
$this->assertEquals('a1', $accounts[1]->getName());
//
$metadata = $this->getMetadata();
$metadata->set('entityDefs', Contact::ENTITY_TYPE, [
'links' => [
'accounts' => [
RelationParam::ORDER_BY => 'name',
RelationParam::ORDER => 'asc',
]
]
]);
$metadata->save();
$this->getDataManager()->clearCache();
$this->getDataManager()->rebuildMetadata();
$this->reCreateApplication(reuse: true);
$em = $this->getEntityManager();
$contact = $em->getRDBRepositoryByClass(Contact::class)->getById($contact->getId());
$this->assertNotNull($contact);
$accounts = $contact->getAccounts();
$this->assertCount(2, $accounts);
$this->assertEquals('a1', $accounts[0]->getName());
$this->assertEquals('a2', $accounts[1]->getName());
}
}

View File

@@ -0,0 +1,123 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use Espo\Modules\Crm\Entities\Account;
use Espo\Modules\Crm\Entities\Contact;
use Espo\Modules\Crm\Entities\Opportunity;
use tests\integration\Core\BaseTestCase;
class RepositoryTest extends BaseTestCase
{
public function testModifiedBy(): void
{
$user1 = $this->createUser('test-1');
$user2 = $this->createUser('test-2');
$this->authenticate('test-1');
$em = $this->getEntityManager();
$account = $em->createEntity(Account::ENTITY_TYPE, ['name' => '1']);
$this->assertEquals($user1->getId(), $account->get('createdById'));
$this->authenticate('test-2');
$em = $this->getEntityManager();
$account = $em->getEntityById(Account::ENTITY_TYPE, $account->getId());
$account->set('name', '2');
$em->saveEntity($account);
$this->assertEquals($user2->getId(), $account->get('modifiedById'));
$this->authenticate('test-1');
$em = $this->getEntityManager();
$account = $em->getEntityById(Account::ENTITY_TYPE, $account->getId());
$account->set('name', '2');
$em->saveEntity($account);
$this->assertEquals($user2->getId(), $account->get('modifiedById'));
}
public function testIsRelated(): void
{
$em = $this->getEntityManager();
$acc1 = $em->createEntity(Account::ENTITY_TYPE, []);
$acc2 = $em->createEntity(Account::ENTITY_TYPE, []);
$opp1 = $em->createEntity(Opportunity::ENTITY_TYPE, []);
$contact1 = $em->createEntity(Contact::ENTITY_TYPE, []);
$contact2 = $em->createEntity(Contact::ENTITY_TYPE, []);
$oppRepo = $em->getRDBRepositoryByClass(Opportunity::class);
$oppRepo->getRelation($opp1, 'account')->relate($acc1);
$oppRepo->getRelation($opp1, 'contacts')->relate($contact1);
$this->assertTrue(
$oppRepo->getRelation($opp1, 'account')->isRelatedById($acc1->getId())
);
$this->assertTrue(
$oppRepo->getRelation($opp1, 'account')->isRelated($acc1)
);
$this->assertFalse(
$oppRepo->getRelation($opp1, 'account')->isRelatedById($acc2->getId())
);
$this->assertFalse(
$oppRepo->getRelation($opp1, 'account')->isRelated($acc2)
);
$this->assertTrue(
$oppRepo->getRelation($opp1, 'contacts')->isRelatedById($contact1->getId())
);
$this->assertTrue(
$oppRepo->getRelation($opp1, 'contacts')->isRelated($contact1)
);
$this->assertFalse(
$oppRepo->getRelation($opp1, 'contacts')->isRelatedById($contact2->getId())
);
$this->assertFalse(
$oppRepo->getRelation($opp1, 'contacts')->isRelated($contact2)
);
}
}

View File

@@ -0,0 +1,245 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use Espo\Modules\Crm\Entities\Account;
use Espo\ORM\SthCollection;
use tests\integration\Core\BaseTestCase;
class SthCollectionTest extends BaseTestCase
{
public function test1()
{
$em = $this->getEntityManager();
$em->createEntity('Account', [
'name' => 'test-1',
]);
$em->createEntity('Account', [
'name' => 'test-2',
]);
$em->createEntity('Account', [
'name' => 'test-3',
]);
$query = $em->getQueryBuilder()
->select()
->from('Account')
->limit(0, 2)
->order('name')
->build();
$collection = $em->getCollectionFactory()->createFromQuery($query);
$count = 0;
$list = [];
foreach ($collection as $e) {
$count++;
$list[] = $e;
}
$this->assertEquals(2, $count);
$this->assertEquals('test-1', $list[0]->get('name'));
$array = $collection->getValueMapList();
$this->assertEquals('test-2', $array[1]->name);
}
public function test2(): void
{
$em = $this->getEntityManager();
$em->createEntity('Account', [
'name' => 'test-2',
]);
$em->createEntity('Account', [
'name' => 'test-3',
]);
$query = $em->getQueryBuilder()
->select()
->from('Account')
->limit(0, 2)
->order('name')
->build();
$collection = $em->getCollectionFactory()->createFromQuery($query);
$count = 0;
foreach ($collection as $ignored) {
$count++;
}
$this->assertEquals(2, $count);
}
public function testFind1()
{
$em = $this->getEntityManager();
$em->createEntity('Account', [
'name' => 'test-1',
]);
$em->createEntity('Account', [
'name' => 'test-2',
]);
$em->createEntity('Account', [
'name' => 'test-3',
]);
$query = $em->getQueryBuilder()
->select()
->from('Account')
->where(['name' => 'test-1'])
->build();
$collection = $em->getRDBRepository('Account')
->clone($query)
->sth()
->find();
$this->assertEquals(SthCollection::class, get_class($collection));
$count = 0;
foreach ($collection as $e) {
$count++;
}
$this->assertEquals(1, $count);
}
public function testFindRelatedOneToMany()
{
$em = $this->getEntityManager();
$account = $em->createEntity('Account', [
'name' => 'test-1',
]);
$em->createEntity('Opportunity', [
'name' => 'o-1',
'accountId' => $account->getId(),
]);
$em->createEntity('Opportunity', [
'name' => 'o-2',
'accountId' => $account->getId(),
]);
$query = $em->getQueryBuilder()
->select()
->from('Opportunity')
->order('name')
->build();
$collection = $em
->getRelation($account, 'opportunities')
->clone($query)
->sth()
->find();
$this->assertEquals(SthCollection::class, get_class($collection));
$count = 0;
foreach ($collection as $e) {
$count++;
}
$this->assertEquals(2, $count);
$array = $collection->getValueMapList();
$this->assertEquals('o-1', $array[0]->name);
}
public function testFindRelatedManyToMany()
{
$em = $this->getEntityManager();
$contact = $em->createEntity('Contact', [
'lastName' => 'test-1',
]);
$em->createEntity('Opportunity', [
'name' => 'o-1',
'contactsIds' => [$contact->getId()],
]);
$em->createEntity('Opportunity', [
'name' => 'o-2',
'contactsIds' => [$contact->getId()],
]);
$query = $em->getQueryBuilder()
->select()
->from('Opportunity')
->build();
$collection = $em->getRepository('Contact')->getRelation($contact, 'opportunities')
->clone($query)
->sth()
->find();
$this->assertEquals(SthCollection::class, get_class($collection));
$count = 0;
foreach ($collection as $e) {
$count++;
}
$this->assertEquals(2, $count);
}
public function testMethods(): void
{
$e1 = $this->getEntityManager()->createEntity(Account::ENTITY_TYPE, ['name' => '1']);
$e2 = $this->getEntityManager()->createEntity(Account::ENTITY_TYPE, ['name' => '2']);
$e3 = $this->getEntityManager()->createEntity(Account::ENTITY_TYPE, ['name' => '3']);
$e4 = $this->getEntityManager()->createEntity(Account::ENTITY_TYPE, ['name' => '4']);
$collection = $this->getEntityManager()
->getRDBRepositoryByClass(Account::class)
->sth()
->order('name')
->find();
$filtered = $collection->filter(function ($e) use ($e2, $e3) {
return $e->getId() !== $e2->getId() && $e->getId() !== $e3->getId();
});
$this->assertEquals([$e1->getId(), $e4->getId()], array_map(fn ($it) => $it->getId(), [...$filtered]));
$this->assertEquals($collection->getEntityType(), $filtered->getEntityType());
}
}

View File

@@ -0,0 +1,170 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM – Open Source CRM application.
* Copyright (C) 2014-2026 EspoCRM, Inc.
* Website: https://www.espocrm.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace tests\integration\Espo\ORM;
use tests\integration\Core\BaseTestCase;
class TransactionManagerTest extends BaseTestCase
{
public function testOne()
{
$em = $this->getEntityManager();
$tm = $em->getTransactionManager();
$tm->start();
$account = $em->createEntity('Account', [
'name' => 'test',
]);
$this->assertNotNull($account);
$id = $account->getId();
$tm->commit();
$account = $em->getEntityById('Account', $id);
$this->assertNotNull($account);
}
public function testRollbackOne()
{
$em = $this->getEntityManager();
$tm = $em->getTransactionManager();
$tm->start();
$account = $em->createEntity('Account', [
'name' => 'test',
]);
$this->assertNotNull($account);
$id = $account->getId();
$tm->rollback();
$account = $em->getEntityById('Account', $id);
$this->assertNull($account);
}
public function testRollbackNested()
{
$em = $this->getEntityManager();
$tm = $em->getTransactionManager();
$tm->start();
$account1 = $em->createEntity('Account', [
'name' => 'test1',
]);
$id1 = $account1->getId();
$tm->start();
$account2 = $em->createEntity('Account', [
'name' => 'test2',
]);
$id2 = $account2->getId();
$tm->rollback();
$tm->commit();
$account1 = $em->getEntityById('Account', $id1);
$account2 = $em->getEntityById('Account', $id2);
$this->assertNotNull($account1);
$this->assertNull($account2);
}
public function testRunCommit()
{
$em = $this->getEntityManager();
$tm = $em->getTransactionManager();
$account = $em->createEntity('Account', [
'name' => 'test',
]);
$id = $account->getId();
$tm->run(
function () use ($em, $id){
$account = $em->getEntityById('Account', $id);
$account->set('name', 'test-1');
$em->saveEntity($account);
}
);
$account = $em->getEntityById('Account', $id);
$this->assertNotNull($account);
$this->assertEquals('test-1', $account->get('name'));
}
public function testRunRollback()
{
$em = $this->getEntityManager();
$tm = $em->getTransactionManager();
$account = $em->createEntity('Account', [
'name' => 'test',
]);
$id = $account->getId();
try {
$tm->run(
function () use ($em, $id){
$account = $em->getEntity('Account', $id);
$account->set('name', 'test-1');
$em->saveEntity($account);
throw new \Exception();
}
);
} catch (\Exception) {}
$account = $em->getEntityById('Account', $id);
$this->assertNotNull($account);
$this->assertEquals('test', $account->get('name'));
}
}