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
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:
144
tests/unit/Espo/Core/Select/AccessControl/FilterFactoryTest.php
Normal file
144
tests/unit/Espo/Core/Select/AccessControl/FilterFactoryTest.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?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\AccessControl;
|
||||
|
||||
use Espo\Core\Acl;
|
||||
use Espo\Core\AclManager;
|
||||
use Espo\Core\Binding\Binder;
|
||||
use Espo\Core\Binding\BindingContainer;
|
||||
use Espo\Core\Binding\BindingData;
|
||||
use Espo\Core\InjectableFactory;
|
||||
use Espo\Core\Select\AccessControl\FilterFactory;
|
||||
use Espo\Core\Select\AccessControl\Filters\OnlyOwn;
|
||||
use Espo\Core\Select\Helpers\FieldHelper;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Entities\User;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FilterFactoryTest extends TestCase
|
||||
{
|
||||
private $aclManager;
|
||||
private $acl;
|
||||
private $metadata;
|
||||
private $injectableFactory;
|
||||
private $factory;
|
||||
private $user;
|
||||
|
||||
protected function setUp() : void
|
||||
{
|
||||
$this->injectableFactory = $this->createMock(InjectableFactory::class);
|
||||
$this->metadata = $this->createMock(Metadata::class);
|
||||
$this->user = $this->createMock(User::class);
|
||||
$this->aclManager = $this->createMock(AclManager::class);
|
||||
$this->acl = $this->createMock(Acl::class);
|
||||
|
||||
$this->factory = new FilterFactory(
|
||||
$this->injectableFactory,
|
||||
$this->metadata,
|
||||
$this->aclManager,
|
||||
$this->acl,
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreate1()
|
||||
{
|
||||
$this->prepareFactoryTest(null, OnlyOwn::class, 'onlyOwn');
|
||||
}
|
||||
|
||||
public function testCreate2()
|
||||
{
|
||||
$this->prepareFactoryTest('SomeClass', OnlyOwn::class, 'onlyOwn');
|
||||
}
|
||||
|
||||
protected function prepareFactoryTest(?string $className, string $defaultClassName, string $name)
|
||||
{
|
||||
$entityType = 'Test';
|
||||
|
||||
$this->metadata
|
||||
->expects($this->any())
|
||||
->method('get')
|
||||
->willReturnMap([
|
||||
[['selectDefs', $entityType, 'accessControlFilterClassNameMap', $name], null, $className],
|
||||
]);
|
||||
|
||||
$className = $className ?? $defaultClassName;
|
||||
|
||||
$object = $this->createMock($defaultClassName);
|
||||
|
||||
$bindingData = new BindingData();
|
||||
|
||||
$binder = new Binder($bindingData);
|
||||
|
||||
$binder
|
||||
->bindInstance(User::class, $this->user)
|
||||
->bindInstance(AclManager::class, $this->aclManager)
|
||||
->bindInstance(Acl::class, $this->acl);
|
||||
|
||||
$binder
|
||||
->for($className)
|
||||
->bindValue('$entityType', $entityType);
|
||||
|
||||
$binder
|
||||
->for(FieldHelper::class)
|
||||
->bindValue('$entityType', $entityType);
|
||||
|
||||
$bindingContainer = new BindingContainer($bindingData);
|
||||
|
||||
$this->injectableFactory
|
||||
->expects($this->exactly(1))
|
||||
->method('createWithBinding')
|
||||
->with($className, $bindingContainer)
|
||||
->willReturn($object);
|
||||
|
||||
$resultObject = $this->factory->create(
|
||||
$entityType,
|
||||
$this->user,
|
||||
$name
|
||||
);
|
||||
|
||||
$this->assertEquals($object, $resultObject);
|
||||
|
||||
$this->assertTrue(
|
||||
$this->factory->has($entityType, $name)
|
||||
);
|
||||
|
||||
$this->metadata
|
||||
->expects($this->any())
|
||||
->method('get')
|
||||
->willReturnMap([
|
||||
[['selectDefs', $entityType, 'accessControlFilterClassNameMap', 'badName'], null, null],
|
||||
]);
|
||||
|
||||
$this->assertFalse(
|
||||
$this->factory->has($entityType, 'badName')
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?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\AccessControl;
|
||||
|
||||
use Espo\Core\Utils\Acl\UserAclManagerProvider;
|
||||
|
||||
use Espo\Core\Acl;
|
||||
use Espo\Core\AclManager;
|
||||
use Espo\Core\Binding\Binder;
|
||||
use Espo\Core\Binding\BindingContainer;
|
||||
use Espo\Core\Binding\BindingData;
|
||||
use Espo\Core\InjectableFactory;
|
||||
use Espo\Core\Portal\Acl as PortalAcl;
|
||||
use Espo\Core\Portal\AclManager as PortalAclManager;
|
||||
use Espo\Core\Select\AccessControl\DefaultFilterResolver;
|
||||
use Espo\Core\Select\AccessControl\DefaultPortalFilterResolver;
|
||||
use Espo\Core\Select\AccessControl\FilterResolverFactory;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Entities\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FilterResolverFactoryTest extends TestCase
|
||||
{
|
||||
private $injectableFactory;
|
||||
private $metadata;
|
||||
private $user;
|
||||
private $aclManager;
|
||||
private $acl;
|
||||
private $factory;
|
||||
|
||||
protected function setUp() : void
|
||||
{
|
||||
$this->injectableFactory = $this->createMock(InjectableFactory::class);
|
||||
$this->metadata = $this->createMock(Metadata::class);
|
||||
$this->user = $this->createMock(User::class);
|
||||
$this->aclManager = $this->createMock(AclManager::class);
|
||||
$this->acl = $this->createMock(Acl::class);
|
||||
|
||||
$this->factory = new FilterResolverFactory(
|
||||
$this->injectableFactory,
|
||||
$this->metadata,
|
||||
$this->aclManager,
|
||||
$this->acl,
|
||||
);
|
||||
}
|
||||
|
||||
public function testCreate1()
|
||||
{
|
||||
$this->prepareFactoryTest(null);
|
||||
}
|
||||
|
||||
public function testCreate2()
|
||||
{
|
||||
$this->prepareFactoryTest('SomeClass');
|
||||
}
|
||||
|
||||
public function testCreatePortal()
|
||||
{
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('isPortal')
|
||||
->willReturn(true);
|
||||
|
||||
$this->prepareFactoryTest(null);
|
||||
}
|
||||
|
||||
protected function prepareFactoryTest(?string $className)
|
||||
{
|
||||
$entityType = 'Test';
|
||||
|
||||
if (!$this->user->isPortal()) {
|
||||
$defaultClassName = DefaultFilterResolver::class;
|
||||
} else {
|
||||
$defaultClassName = DefaultPortalFilterResolver::class;
|
||||
}
|
||||
|
||||
$this->metadata
|
||||
->expects($this->once())
|
||||
->method('get')
|
||||
->with([
|
||||
'selectDefs', $entityType,
|
||||
!$this->user->isPortal() ?
|
||||
'accessControlFilterResolverClassName':
|
||||
'portalAccessControlFilterResolverClassName'
|
||||
])
|
||||
->willReturn($className);
|
||||
|
||||
$className = $className ?? $defaultClassName;
|
||||
|
||||
$bindingData = new BindingData();
|
||||
|
||||
$binder = new Binder($bindingData);
|
||||
|
||||
$binder
|
||||
->bindInstance(User::class, $this->user)
|
||||
->bindInstance(AclManager::class, $this->aclManager)
|
||||
->bindInstance(Acl::class, $this->acl);
|
||||
|
||||
|
||||
if ($this->user->isPortal()) {
|
||||
$binder->bindInstance(PortalAcl::class, $this->acl);
|
||||
$binder->bindInstance(PortalAclManager::class, $this->aclManager);
|
||||
}
|
||||
|
||||
$binder
|
||||
->for($className)
|
||||
->bindValue('$entityType', $entityType);
|
||||
|
||||
$bindingContainer = new BindingContainer($bindingData);
|
||||
|
||||
$object = $this->createMock($defaultClassName);
|
||||
|
||||
$this->injectableFactory
|
||||
->expects($this->once())
|
||||
->method('createWithBinding')
|
||||
->with($className, $bindingContainer)
|
||||
->willReturn($object);
|
||||
|
||||
$resultObject = $this->factory->create($entityType, $this->user);
|
||||
|
||||
$this->assertEquals($object, $resultObject);
|
||||
}
|
||||
}
|
||||
158
tests/unit/Espo/Core/Select/AccessControl/FilterResolverTest.php
Normal file
158
tests/unit/Espo/Core/Select/AccessControl/FilterResolverTest.php
Normal file
@@ -0,0 +1,158 @@
|
||||
<?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\AccessControl;
|
||||
|
||||
use Espo\Core\Acl;
|
||||
use Espo\Core\Portal\Acl as AclPortal;
|
||||
use Espo\Core\Select\AccessControl\DefaultFilterResolver;
|
||||
use Espo\Core\Select\AccessControl\DefaultPortalFilterResolver;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FilterResolverTest extends TestCase
|
||||
{
|
||||
private $acl;
|
||||
private $aclPortal;
|
||||
private $entityType;
|
||||
private $resolver;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->acl = $this->createMock(Acl::class);
|
||||
$this->aclPortal = $this->createMock(AclPortal::class);
|
||||
|
||||
$this->entityType = 'Test';
|
||||
}
|
||||
|
||||
public function testResolveRegularOnlyOwn()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'onlyOwn',
|
||||
$this->initResolveTest(false, 'checkReadOnlyOwn')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveRegularOnlyTeam()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'onlyTeam',
|
||||
$this->initResolveTest(false, 'checkReadOnlyTeam')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveRegularNo()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'no',
|
||||
$this->initResolveTest(false, 'checkReadNo')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolvePortalOnlyOwn()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'portalOnlyOwn',
|
||||
$this->initResolveTest(true, 'checkReadOnlyOwn')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolvePortalOnlyAccount()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'portalOnlyAccount',
|
||||
$this->initResolveTest(true, 'checkReadOnlyAccount')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolvePortalOnlyContact()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'portalOnlyContact',
|
||||
$this->initResolveTest(true, 'checkReadOnlyContact')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolvePortalNo()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'no',
|
||||
$this->initResolveTest(true, 'checkReadNo')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolveAll()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'all',
|
||||
$this->initResolveTest(false, 'checkReadAll')
|
||||
);
|
||||
}
|
||||
|
||||
public function testResolvePortalAll()
|
||||
{
|
||||
$this->assertEquals(
|
||||
'portalAll',
|
||||
$this->initResolveTest(true, 'checkReadAll')
|
||||
);
|
||||
}
|
||||
|
||||
protected function initResolveTest(bool $isPortal = false, ?string $method = null): ?string
|
||||
{
|
||||
$acl = $this->acl;
|
||||
|
||||
if ($isPortal) {
|
||||
$acl = $this->aclPortal;
|
||||
}
|
||||
|
||||
if (!$isPortal) {
|
||||
$this->resolver = new DefaultFilterResolver(
|
||||
$this->entityType,
|
||||
$acl
|
||||
);
|
||||
}
|
||||
|
||||
if ($isPortal) {
|
||||
$this->resolver = new DefaultPortalFilterResolver(
|
||||
$this->entityType,
|
||||
$acl
|
||||
);
|
||||
}
|
||||
|
||||
if ($method) {
|
||||
$acl
|
||||
->expects($this->any())
|
||||
->method($method)
|
||||
->with($this->entityType)
|
||||
->willReturn(true);
|
||||
}
|
||||
|
||||
return $this->resolver->resolve();
|
||||
}
|
||||
}
|
||||
348
tests/unit/Espo/Core/Select/AccessControl/FiltersTest.php
Normal file
348
tests/unit/Espo/Core/Select/AccessControl/FiltersTest.php
Normal file
@@ -0,0 +1,348 @@
|
||||
<?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\AccessControl;
|
||||
|
||||
use Espo\Core\Field\LinkMultiple;
|
||||
use Espo\Core\Field\LinkMultipleItem;
|
||||
use Espo\Core\Portal\Acl\OwnershipChecker\MetadataProvider;
|
||||
use Espo\Core\Select\AccessControl\Filter as AccessControlFilter;
|
||||
use Espo\Core\Select\AccessControl\Filters\No;
|
||||
use Espo\Core\Select\AccessControl\Filters\PortalOnlyAccount;
|
||||
use Espo\Core\Select\AccessControl\Filters\PortalOnlyContact;
|
||||
use Espo\Core\Select\AccessControl\Filters\PortalOnlyOwn;
|
||||
use Espo\Core\Select\Helpers\FieldHelper;
|
||||
use Espo\Core\Select\Helpers\RelationQueryHelper;
|
||||
use Espo\Entities\User;
|
||||
use Espo\Modules\Crm\Entities\Account;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\ORM\Defs;
|
||||
use Espo\ORM\Defs\RelationDefs;
|
||||
use Espo\ORM\Query\Part\Where\OrGroup;
|
||||
use Espo\ORM\Query\SelectBuilder as QueryBuilder;
|
||||
use Espo\ORM\Type\RelationType;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FiltersTest extends TestCase
|
||||
{
|
||||
private $queryBuilder;
|
||||
private $fieldHelper;
|
||||
private $user;
|
||||
private $entityType;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->queryBuilder = $this->createMock(QueryBuilder::class);
|
||||
$this->fieldHelper = $this->createMock(FieldHelper::class);
|
||||
$this->user = $this->createMock(User::class);
|
||||
|
||||
$this->user->set('id', 'user-id');
|
||||
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('getId')
|
||||
->willReturn('user-id');
|
||||
|
||||
$this->entityType = 'Test';
|
||||
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('getTeamIdList')
|
||||
->willReturn(['team-id']);
|
||||
}
|
||||
|
||||
public function testNo(): void
|
||||
{
|
||||
$filter = $this->createFilter(No::class);
|
||||
|
||||
$this->queryBuilder
|
||||
->expects($this->once())
|
||||
->method('where')
|
||||
->with([
|
||||
'id' => null,
|
||||
]);
|
||||
|
||||
$filter->apply($this->queryBuilder);
|
||||
}
|
||||
|
||||
public function testPortalOnlyAccount1(): void
|
||||
{
|
||||
$defs = $this->createMock(Defs::class);
|
||||
$metadataProvider = $this->createMock(MetadataProvider::class);
|
||||
|
||||
$queryHelper = new RelationQueryHelper($defs);
|
||||
|
||||
$filter = new PortalOnlyAccount(
|
||||
'Test',
|
||||
$this->user,
|
||||
$this->fieldHelper,
|
||||
$metadataProvider,
|
||||
$queryHelper
|
||||
);
|
||||
|
||||
$metadataProvider
|
||||
->expects($this->any())
|
||||
->method('getContactLink')
|
||||
->willReturn(
|
||||
RelationDefs::fromRaw([
|
||||
'type' => RelationType::BELONGS_TO_PARENT,
|
||||
'entityList' => [Contact::ENTITY_TYPE],
|
||||
], 'contact')
|
||||
);
|
||||
|
||||
$metadataProvider
|
||||
->expects($this->any())
|
||||
->method('getAccountLink')
|
||||
->willReturn(
|
||||
RelationDefs::fromRaw([
|
||||
'type' => RelationType::MANY_MANY,
|
||||
'entity' => [Account::ENTITY_TYPE],
|
||||
'midKeys' => ['nId', 'fId'],
|
||||
'relationName' => 'TestAccount'
|
||||
], 'account')
|
||||
);
|
||||
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('getAccounts')
|
||||
->willReturn(LinkMultiple::create([
|
||||
LinkMultipleItem::create('account-id')
|
||||
]));
|
||||
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('getContactId')
|
||||
->willReturn('contact-id');
|
||||
|
||||
$this->initHelperMethods([
|
||||
['hasCreatedByField', true],
|
||||
]);
|
||||
|
||||
$this->queryBuilder
|
||||
->expects($this->once())
|
||||
->method('where')
|
||||
->with(
|
||||
$this->callback(function ($where) {
|
||||
if (!$where instanceof OrGroup) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!isset($where->getRawValue()[0]['id=s'])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $where->getItemCount() === 3;
|
||||
})
|
||||
)
|
||||
->willReturn($this->queryBuilder);
|
||||
|
||||
$filter->apply($this->queryBuilder);
|
||||
}
|
||||
|
||||
public function testPortalOnlyAccount2(): void
|
||||
{
|
||||
$defs = $this->createMock(Defs::class);
|
||||
$metadataProvider = $this->createMock(MetadataProvider::class);
|
||||
|
||||
$queryHelper = new RelationQueryHelper($defs);
|
||||
|
||||
$filter = new PortalOnlyAccount(
|
||||
'Test',
|
||||
$this->user,
|
||||
$this->fieldHelper,
|
||||
$metadataProvider,
|
||||
$queryHelper
|
||||
);
|
||||
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('getLinkMultipleIdList')
|
||||
->with('accounts')
|
||||
->willReturn([]);
|
||||
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('get')
|
||||
->with('contactId')
|
||||
->willReturn(null);
|
||||
|
||||
$this->initHelperMethods([
|
||||
['hasCreatedByField', false],
|
||||
]);
|
||||
|
||||
$this->queryBuilder
|
||||
->expects($this->once())
|
||||
->method('where')
|
||||
->with([
|
||||
'id' => null,
|
||||
]);
|
||||
|
||||
$filter->apply($this->queryBuilder);
|
||||
}
|
||||
|
||||
public function testPortalOnlyContact1(): void
|
||||
{
|
||||
$defs = $this->createMock(Defs::class);
|
||||
$metadataProvider = $this->createMock(MetadataProvider::class);
|
||||
|
||||
$queryHelper = new RelationQueryHelper($defs);
|
||||
|
||||
$filter = new PortalOnlyContact(
|
||||
'Test',
|
||||
$this->user,
|
||||
$this->fieldHelper,
|
||||
$metadataProvider,
|
||||
$queryHelper
|
||||
);
|
||||
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('getContactId')
|
||||
->willReturn('contact-id');
|
||||
|
||||
$this->initHelperMethods([
|
||||
['hasCreatedByField', true],
|
||||
]);
|
||||
|
||||
$metadataProvider
|
||||
->expects($this->once())
|
||||
->method('getContactLink')
|
||||
->willReturn(
|
||||
RelationDefs::fromRaw([
|
||||
'type' => RelationType::BELONGS_TO,
|
||||
'entity' => Contact::ENTITY_TYPE,
|
||||
], 'contact')
|
||||
);
|
||||
|
||||
$this->queryBuilder
|
||||
->expects($this->once())
|
||||
->method('where')
|
||||
->with(
|
||||
$this->callback(function ($where) {
|
||||
return $where instanceof OrGroup &&
|
||||
$where->getItemCount() === 2;
|
||||
})
|
||||
)
|
||||
->willReturn($this->queryBuilder);
|
||||
|
||||
$filter->apply($this->queryBuilder);
|
||||
}
|
||||
|
||||
public function testPortalOnlyContact2()
|
||||
{
|
||||
$defs = $this->createMock(Defs::class);
|
||||
$metadataProvider = $this->createMock(MetadataProvider::class);
|
||||
|
||||
$queryHelper = new RelationQueryHelper($defs);
|
||||
|
||||
$filter = new PortalOnlyContact(
|
||||
'Test',
|
||||
$this->user,
|
||||
$this->fieldHelper,
|
||||
$metadataProvider,
|
||||
$queryHelper
|
||||
);
|
||||
|
||||
$this->user
|
||||
->expects($this->any())
|
||||
->method('get')
|
||||
->with('contactId')
|
||||
->willReturn(null);
|
||||
|
||||
$this->initHelperMethods([
|
||||
['hasCreatedByField', false],
|
||||
]);
|
||||
|
||||
$this->queryBuilder
|
||||
->expects($this->once())
|
||||
->method('where')
|
||||
->with([
|
||||
'id' => null,
|
||||
])
|
||||
->willReturn($this->queryBuilder);
|
||||
|
||||
$filter->apply($this->queryBuilder);
|
||||
}
|
||||
|
||||
public function testPortalOnlyOwn1()
|
||||
{
|
||||
$filter = $this->createFilter(PortalOnlyOwn::class);
|
||||
|
||||
$this->initHelperMethods([
|
||||
['hasCreatedByField', true],
|
||||
]);
|
||||
|
||||
$this->queryBuilder
|
||||
->expects($this->once())
|
||||
->method('where')
|
||||
->with([
|
||||
'createdById' => $this->user->getId(),
|
||||
])
|
||||
->willReturn($this->queryBuilder);
|
||||
|
||||
$filter->apply($this->queryBuilder);
|
||||
}
|
||||
|
||||
public function testPortalOnlyOwn2()
|
||||
{
|
||||
$filter = $this->createFilter(PortalOnlyOwn::class);
|
||||
|
||||
$this->initHelperMethods([
|
||||
['hasCreatedByField', false],
|
||||
]);
|
||||
|
||||
$this->queryBuilder
|
||||
->expects($this->once())
|
||||
->method('where')
|
||||
->with([
|
||||
'id' => null,
|
||||
])
|
||||
->willReturn($this->queryBuilder);
|
||||
|
||||
$filter->apply($this->queryBuilder);
|
||||
}
|
||||
|
||||
protected function initHelperMethods(array $map)
|
||||
{
|
||||
foreach ($map as $i => $item) {
|
||||
$this->fieldHelper
|
||||
->expects($this->once())
|
||||
->method($item[0])
|
||||
->willReturn($item[1]);
|
||||
}
|
||||
}
|
||||
|
||||
protected function createFilter(string $className): AccessControlFilter
|
||||
{
|
||||
return new $className(
|
||||
$this->user,
|
||||
$this->fieldHelper
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user