From: Tim Otten Date: Mon, 7 Jun 2021 11:40:44 +0000 (-0700) Subject: ConformanceTest - Add support for read-only entities X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=cb72c80e967c878519a03626f639840adecaa8c7;p=civicrm-core.git ConformanceTest - Add support for read-only entities --- diff --git a/tests/phpunit/api/v4/Entity/ConformanceTest.php b/tests/phpunit/api/v4/Entity/ConformanceTest.php index ab112e9b1d..43d8e78da1 100644 --- a/tests/phpunit/api/v4/Entity/ConformanceTest.php +++ b/tests/phpunit/api/v4/Entity/ConformanceTest.php @@ -31,6 +31,8 @@ use Civi\Test\HookInterface; */ class ConformanceTest extends UnitTestCase implements HookInterface { + const READ_ONLY_ENTITIES = '/^(FinancialItem)$/'; + use \api\v4\Traits\CheckAccessTrait; use \api\v4\Traits\TableDropperTrait; use \api\v4\Traits\OptionCleanupTrait { @@ -142,7 +144,7 @@ class ConformanceTest extends UnitTestCase implements HookInterface { $this->checkFields($entityClass, $entity); $this->checkCreationDenied($entity, $entityClass); - $id = $this->checkCreationAllowed($entity, $entityClass); + $id = $this->checkCreation($entity, $entityClass); $this->checkGet($entityClass, $id, $entity); $this->checkGetAllowed($entityClass, $id, $entity); $this->checkGetCount($entityClass, $id, $entity); @@ -212,7 +214,9 @@ class ConformanceTest extends UnitTestCase implements HookInterface { * * @return mixed */ - protected function checkCreationAllowed($entity, $entityClass) { + protected function checkCreation($entity, $entityClass) { + $isReadOnly = preg_match(static::READ_ONLY_ENTITIES, $entity); + $hookLog = []; $onValidate = function(ValidateValuesEvent $e) use (&$hookLog) { $hookLog[$e->getEntityName()][$e->getActionName()] = 1 + ($hookLog[$e->getEntityName()][$e->getActionName()] ?? 0); @@ -226,14 +230,16 @@ class ConformanceTest extends UnitTestCase implements HookInterface { $requiredParams = $this->creationParamProvider->getRequired($entity); $createResult = $entityClass::create() ->setValues($requiredParams) - ->setCheckPermissions(TRUE) + ->setCheckPermissions(!$isReadOnly) ->execute() ->first(); $this->assertArrayHasKey('id', $createResult, "create missing ID"); $id = $createResult['id']; $this->assertGreaterThanOrEqual(1, $id, "$entity ID not positive"); - $this->assertEquals(1, $this->checkAccessCounts["{$entity}::create"]); + if (!$isReadOnly) { + $this->assertEquals(1, $this->checkAccessCounts["{$entity}::create"]); + } $this->resetCheckAccess(); $this->assertEquals(2, $hookLog[$entity]['create']); @@ -266,7 +272,9 @@ class ConformanceTest extends UnitTestCase implements HookInterface { catch (UnauthorizedException $e) { // OK, expected exception } - $this->assertEquals(1, $this->checkAccessCounts["{$entity}::create"]); + if (!preg_match(static::READ_ONLY_ENTITIES, $entity)) { + $this->assertEquals(1, $this->checkAccessCounts["{$entity}::create"]); + } $this->resetCheckAccess(); }