EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Bool filters: use relation conditions in middle table join (#3790)
Leo Camus26838016b388
diff --git a/application/Espo/Core/Select/Bool/Filters/OnlyMy.php b/application/Espo/Core/Select/Bool/Filters/OnlyMy.phpindex 2591951238..017ef2727e 100644--- a/application/Espo/Core/Select/Bool/Filters/OnlyMy.php+++ b/application/Espo/Core/Select/Bool/Filters/OnlyMy.php@@ -75,13 +75,19 @@ class OnlyMy implements Filter$key1 = $relationDefs->getMidKey();$key2 = $relationDefs->getForeignMidKey();+ $midConditions = [+ "assignedUsersMiddle.$key1:" => Attribute::ID,+ 'assignedUsersMiddle.deleted' => false,+ ];++ foreach ($relationDefs->getConditions() as $key => $value) {+ $midConditions["assignedUsersMiddle.$key"] = $value;+ }+$subQuery = SelectBuilder::create()->select(Attribute::ID)->from($this->entityType)- ->leftJoin($middleEntityType, 'assignedUsersMiddle', [- "assignedUsersMiddle.$key1:" => Attribute::ID,- 'assignedUsersMiddle.deleted' => false,- ])+ ->leftJoin($middleEntityType, 'assignedUsersMiddle', $midConditions)->where(["assignedUsersMiddle.$key2" => $this->user->getId()])->build();diff --git a/application/Espo/Core/Select/Bool/Filters/Shared.php b/application/Espo/Core/Select/Bool/Filters/Shared.phpindex ed9e3556ee..3bdf5f3e9e 100644--- a/application/Espo/Core/Select/Bool/Filters/Shared.php+++ b/application/Espo/Core/Select/Bool/Filters/Shared.php@@ -67,13 +67,19 @@ class Shared implements Filter$key1 = $relationDefs->getMidKey();$key2 = $relationDefs->getForeignMidKey();+ $midConditions = [+ "collaboratorsMiddle.$key1:" => Attribute::ID,+ 'collaboratorsMiddle.deleted' => false,+ ];++ foreach ($relationDefs->getConditions() as $key => $value) {+ $midConditions["collaboratorsMiddle.$key"] = $value;+ }+$subQuery = SelectBuilder::create()->select(Attribute::ID)->from($this->entityType)- ->leftJoin($middleEntityType, 'collaboratorsMiddle', [- "collaboratorsMiddle.$key1:" => Attribute::ID,- 'collaboratorsMiddle.deleted' => false,- ])+ ->leftJoin($middleEntityType, 'collaboratorsMiddle', $midConditions)->where(["collaboratorsMiddle.$key2" => $this->user->getId()])->build();diff --git a/tests/unit/Espo/Core/Select/Bool/FiltersTest.php b/tests/unit/Espo/Core/Select/Bool/FiltersTest.phpnew file mode 100644index 0000000000..605f0dbf95--- /dev/null+++ b/tests/unit/Espo/Core/Select/Bool/FiltersTest.php@@ -0,0 +1,118 @@+<?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\unit\Espo\Core\Select\Bool;++use Espo\Core\Select\Bool\Filters\OnlyMy;+use Espo\Core\Select\Bool\Filters\Shared;+use Espo\Core\Select\Helpers\FieldHelper;+use Espo\Entities\User;+use Espo\ORM\Defs;+use Espo\ORM\Defs\EntityDefs;+use Espo\ORM\Defs\RelationDefs;+use Espo\ORM\Query\Part\Where\OrGroupBuilder;+use Espo\ORM\Query\Select;+use Espo\ORM\Query\SelectBuilder;+use Espo\ORM\Type\RelationType;+use PHPUnit\Framework\TestCase;++class FiltersTest extends TestCase+{+ public function testOnlyMyUsesMiddleConditions(): void+ {+ $user = $this->createMock(User::class);+ $user->method('getId')->willReturn('user-id');+ $user->method('isPortal')->willReturn(false);++ $fieldHelper = $this->createMock(FieldHelper::class);+ $fieldHelper->method('hasAssignedUsersField')->willReturn(true);++ $filter = new OnlyMy('Test', $user, $fieldHelper, $this->createDefs('assignedUsers', 'entityUser'));++ $joinConditions = $this->applyAndGetJoinConditions($filter);++ $this->assertEquals('Test', $joinConditions['assignedUsersMiddle.entityType'] ?? null);+ }++ public function testSharedUsesMiddleConditions(): void+ {+ $user = $this->createMock(User::class);+ $user->method('getId')->willReturn('user-id');++ $fieldHelper = $this->createMock(FieldHelper::class);+ $fieldHelper->method('hasCollaboratorsField')->willReturn(true);++ $filter = new Shared('Test', $user, $fieldHelper, $this->createDefs('collaborators', 'entityCollaborator'));++ $joinConditions = $this->applyAndGetJoinConditions($filter);++ $this->assertEquals('Test', $joinConditions['collaboratorsMiddle.entityType'] ?? null);+ }++ private function createDefs(string $link, string $relationName): Defs+ {+ $relationDefs = RelationDefs::fromRaw([+ 'type' => RelationType::MANY_MANY,+ 'entity' => User::ENTITY_TYPE,+ 'relationName' => $relationName,+ 'midKeys' => ['entityId', 'userId'],+ 'conditions' => ['entityType' => 'Test'],+ ], $link);++ $entityDefs = $this->createMock(EntityDefs::class);+ $entityDefs->method('getRelation')->with($link)->willReturn($relationDefs);++ $defs = $this->createMock(Defs::class);+ $defs->method('getEntity')->with('Test')->willReturn($entityDefs);++ return $defs;+ }++ /**+ * @return array<string, mixed>+ */+ private function applyAndGetJoinConditions(OnlyMy|Shared $filter): array+ {+ $orGroupBuilder = new OrGroupBuilder();++ $filter->apply(SelectBuilder::create()->from('Test'), $orGroupBuilder);++ $rawValue = $orGroupBuilder->build()->getRawValue();++ $subQuery = $rawValue['id=s'] ?? null;++ $this->assertInstanceOf(Select::class, $subQuery);++ $joins = $subQuery->getRaw()['joins'] ?? [];++ $this->assertCount(1, $joins);++ return $joins[0][2];+ }+}