4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Entity
;
23 use api\v
4\UnitTestCase
;
24 use Civi\Api4\Utils\CoreUtil
;
29 class ConformanceTest
extends UnitTestCase
{
31 use \api\v
4\Traits\TableDropperTrait
;
32 use \api\v
4\Traits\OptionCleanupTrait
{
33 setUp
as setUpOptionCleanup
;
37 * @var \api\v4\Service\TestCreationParameterProvider
39 protected $creationParamProvider;
42 * Set up baseline for testing
44 public function setUp(): void
{
47 'civicrm_custom_group',
48 'civicrm_custom_field',
51 'civicrm_participant',
53 $this->dropByPrefix('civicrm_value_myfavorite');
54 $this->cleanup(['tablesToTruncate' => $tablesToTruncate]);
55 $this->setUpOptionCleanup();
56 $this->loadDataSet('ConformanceTest');
57 $this->creationParamProvider
= \Civi
::container()->get('test.param_provider');
62 * Get entities to test.
64 * This is the hi-tech list as generated via Civi's runtime services. It
65 * is canonical, but relies on services that may not be available during
66 * early parts of PHPUnit lifecycle.
70 * @throws \API_Exception
71 * @throws \Civi\API\Exception\UnauthorizedException
73 public function getEntitiesHitech() {
74 // Ensure all components are enabled so their entities show up
75 foreach (array_keys(\CRM_Core_Component
::getComponents()) as $component) {
76 \CRM_Core_BAO_ConfigSetting
::enableComponent($component);
78 return $this->toDataProviderArray(Entity
::get(FALSE)->execute()->column('name'));
82 * Get entities to test.
84 * This is the low-tech list as generated by manual-overrides and direct inspection.
85 * It may be summoned at any time during PHPUnit lifecycle, but it may require
86 * occasional twiddling to give correct results.
90 public function getEntitiesLotech() {
92 $manual['remove'] = ['CustomValue'];
93 $manual['transform'] = ['CiviCase' => 'Case'];
96 $srcDir = dirname(__DIR__
, 5);
97 foreach ((array) glob("$srcDir/Civi/Api4/*.php") as $name) {
98 $fileName = basename($name, '.php');
99 $scanned[] = $manual['transform'][$fileName] ??
$fileName;
103 array_unique(array_merge($scanned, $manual['add'])),
107 return $this->toDataProviderArray($names);
111 * Ensure that "getEntitiesLotech()" (which is the 'dataProvider') is up to date
112 * with "getEntitiesHitech()" (which is a live feed available entities).
114 public function testEntitiesProvider() {
115 $this->assertEquals($this->getEntitiesHitech(), $this->getEntitiesLotech(), "The lo-tech list of entities does not match the hi-tech list. You probably need to update getEntitiesLotech().");
119 * @param string $entity
122 * @dataProvider getEntitiesLotech
124 * @throws \API_Exception
126 public function testConformance($entity): void
{
127 $entityClass = CoreUtil
::getApiClass($entity);
129 $this->checkEntityInfo($entityClass);
130 $actions = $this->checkActions($entityClass);
132 // Go no further if it's not a CRUD entity
133 if (array_diff(['get', 'create', 'update', 'delete'], array_keys($actions))) {
134 $this->markTestSkipped("The API \"$entity\" does not implement CRUD actions");
137 $this->checkFields($entityClass, $entity);
138 $id = $this->checkCreation($entity, $entityClass);
139 $this->checkGet($entityClass, $id, $entity);
140 $this->checkGetCount($entityClass, $id, $entity);
141 $this->checkUpdateFailsFromCreate($entityClass, $id);
142 $this->checkWrongParamType($entityClass);
143 $this->checkDeleteWithNoId($entityClass);
144 $this->checkDeletion($entityClass, $id);
145 $this->checkPostDelete($entityClass, $id, $entity);
149 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
151 protected function checkEntityInfo($entityClass): void
{
152 $info = $entityClass::getInfo();
153 $this->assertNotEmpty($info['name']);
154 $this->assertNotEmpty($info['title']);
155 $this->assertNotEmpty($info['title_plural']);
156 $this->assertNotEmpty($info['type']);
157 $this->assertNotEmpty($info['description']);
161 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
162 * @param string $entity
164 * @throws \API_Exception
166 protected function checkFields($entityClass, $entity) {
167 $fields = $entityClass::getFields(FALSE)
168 ->setIncludeCustom(FALSE)
172 $errMsg = sprintf('%s is missing required ID field', $entity);
173 $subset = ['data_type' => 'Integer'];
175 $this->assertArraySubset($subset, $fields['id'], $errMsg);
179 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
183 * @throws \API_Exception
185 protected function checkActions($entityClass): array {
186 $actions = $entityClass::getActions(FALSE)
190 $this->assertNotEmpty($actions);
191 return (array) $actions;
195 * @param string $entity
196 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
200 protected function checkCreation($entity, $entityClass) {
201 $requiredParams = $this->creationParamProvider
->getRequired($entity);
202 $createResult = $entityClass::create()
203 ->setValues($requiredParams)
204 ->setCheckPermissions(FALSE)
208 $this->assertArrayHasKey('id', $createResult, "create missing ID");
209 $id = $createResult['id'];
211 $this->assertGreaterThanOrEqual(1, $id, "$entity ID not positive");
217 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
220 protected function checkUpdateFailsFromCreate($entityClass, $id): void
{
221 $exceptionThrown = '';
223 $entityClass::create(FALSE)
224 ->addValue('id', $id)
227 catch (\API_Exception
$e) {
228 $exceptionThrown = $e->getMessage();
230 $this->assertContains('id', $exceptionThrown);
234 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
236 * @param string $entity
238 protected function checkGet($entityClass, $id, $entity) {
239 $getResult = $entityClass::get(FALSE)
240 ->addWhere('id', '=', $id)
243 $errMsg = sprintf('Failed to fetch a %s after creation', $entity);
244 $this->assertEquals($id, $getResult->first()['id'], $errMsg);
245 $this->assertEquals(1, $getResult->count(), $errMsg);
249 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
251 * @param string $entity
253 protected function checkGetCount($entityClass, $id, $entity): void
{
254 $getResult = $entityClass::get(FALSE)
255 ->addWhere('id', '=', $id)
258 $errMsg = sprintf('%s getCount failed', $entity);
259 $this->assertEquals(1, $getResult->count(), $errMsg);
261 $getResult = $entityClass::get(FALSE)
264 $errMsg = sprintf('%s getCount failed', $entity);
265 $this->assertGreaterThanOrEqual(1, $getResult->count(), $errMsg);
269 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
271 protected function checkDeleteWithNoId($entityClass) {
272 $exceptionThrown = '';
274 $entityClass::delete()
277 catch (\API_Exception
$e) {
278 $exceptionThrown = $e->getMessage();
280 $this->assertContains('required', $exceptionThrown);
284 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
286 protected function checkWrongParamType($entityClass) {
287 $exceptionThrown = '';
290 ->setDebug('not a bool')
293 catch (\API_Exception
$e) {
294 $exceptionThrown = $e->getMessage();
296 $this->assertContains('debug', $exceptionThrown);
297 $this->assertContains('type', $exceptionThrown);
301 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
304 protected function checkDeletion($entityClass, $id) {
305 $deleteResult = $entityClass::delete(FALSE)
306 ->addWhere('id', '=', $id)
309 // should get back an array of deleted id
310 $this->assertEquals([['id' => $id]], (array) $deleteResult);
314 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
316 * @param string $entity
318 protected function checkPostDelete($entityClass, $id, $entity) {
319 $getDeletedResult = $entityClass::get(FALSE)
320 ->addWhere('id', '=', $id)
323 $errMsg = sprintf('Entity "%s" was not deleted', $entity);
324 $this->assertEquals(0, count($getDeletedResult), $errMsg);
328 * @param array $names
329 * List of entity names.
332 * List of data-provider arguments, one for each entity-name.
333 * Ex: ['Foo' => ['Foo'], 'Bar' => ['Bar']]
335 protected function toDataProviderArray($names) {
339 foreach ($names as $name) {
340 $result[$name] = [$name];