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() {
46 'civicrm_custom_group',
47 'civicrm_custom_field',
50 'civicrm_participant',
52 $this->dropByPrefix('civicrm_value_myfavorite');
53 $this->cleanup(['tablesToTruncate' => $tablesToTruncate]);
54 $this->setUpOptionCleanup();
55 $this->loadDataSet('ConformanceTest');
56 $this->creationParamProvider
= \Civi
::container()->get('test.param_provider');
61 * Get entities to test.
63 * This is the hi-tech list as generated via Civi's runtime services. It
64 * is canonical, but relies on services that may not be available during
65 * early parts of PHPUnit lifecycle.
69 * @throws \API_Exception
70 * @throws \Civi\API\Exception\UnauthorizedException
72 public function getEntitiesHitech() {
73 // Ensure all components are enabled so their entities show up
74 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviEvent');
75 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviGrant');
76 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
77 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviContribute');
78 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
79 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviPledge');
80 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviReport');
81 return $this->toDataProviderArray(Entity
::get(FALSE)->execute()->column('name'));
85 * Get entities to test.
87 * This is the low-tech list as generated by manual-overrides and direct inspection.
88 * It may be summoned at any time during PHPUnit lifecycle, but it may require
89 * occasional twiddling to give correct results.
93 public function getEntitiesLotech() {
95 $manual['remove'] = ['CustomValue'];
98 $srcDir = dirname(__DIR__
, 5);
99 foreach ((array) glob("$srcDir/Civi/Api4/*.php") as $name) {
100 $scanned[] = preg_replace('/\.php/', '', basename($name));
104 array_unique(array_merge($scanned, $manual['add'])),
108 return $this->toDataProviderArray($names);
112 * Ensure that "getEntitiesLotech()" (which is the 'dataProvider') is up to date
113 * with "getEntitiesHitech()" (which is a live feed available entities).
115 public function testEntitiesProvider() {
116 $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().");
120 * @param string $entity
123 * @dataProvider getEntitiesLotech
125 * @throws \API_Exception
127 public function testConformance($entity): void
{
128 $entityClass = CoreUtil
::getApiClass($entity);
130 $this->checkEntityInfo($entityClass);
131 $actions = $this->checkActions($entityClass);
133 // Go no further if it's not a CRUD entity
134 if (array_diff(['get', 'create', 'update', 'delete'], array_keys($actions))) {
135 $this->markTestSkipped("The API \"$entity\" does not implement CRUD actions");
138 $this->checkFields($entityClass, $entity);
139 $id = $this->checkCreation($entity, $entityClass);
140 $this->checkGet($entityClass, $id, $entity);
141 $this->checkGetCount($entityClass, $id, $entity);
142 $this->checkUpdateFailsFromCreate($entityClass, $id);
143 $this->checkWrongParamType($entityClass);
144 $this->checkDeleteWithNoId($entityClass);
145 $this->checkDeletion($entityClass, $id);
146 $this->checkPostDelete($entityClass, $id, $entity);
150 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
152 protected function checkEntityInfo($entityClass): void
{
153 $info = $entityClass::getInfo();
154 $this->assertNotEmpty($info['name']);
155 $this->assertNotEmpty($info['title']);
156 $this->assertNotEmpty($info['title_plural']);
157 $this->assertNotEmpty($info['type']);
158 $this->assertNotEmpty($info['description']);
162 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
163 * @param string $entity
165 * @throws \API_Exception
167 protected function checkFields($entityClass, $entity) {
168 $fields = $entityClass::getFields(FALSE)
169 ->setIncludeCustom(FALSE)
173 $errMsg = sprintf('%s is missing required ID field', $entity);
174 $subset = ['data_type' => 'Integer'];
176 $this->assertArraySubset($subset, $fields['id'], $errMsg);
180 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
184 * @throws \API_Exception
186 protected function checkActions($entityClass): array {
187 $actions = $entityClass::getActions(FALSE)
191 $this->assertNotEmpty($actions);
192 return (array) $actions;
196 * @param string $entity
197 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
201 protected function checkCreation($entity, $entityClass) {
202 $requiredParams = $this->creationParamProvider
->getRequired($entity);
203 $createResult = $entityClass::create()
204 ->setValues($requiredParams)
205 ->setCheckPermissions(FALSE)
209 $this->assertArrayHasKey('id', $createResult, "create missing ID");
210 $id = $createResult['id'];
212 $this->assertGreaterThanOrEqual(1, $id, "$entity ID not positive");
218 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
221 protected function checkUpdateFailsFromCreate($entityClass, $id): void
{
222 $exceptionThrown = '';
224 $entityClass::create(FALSE)
225 ->addValue('id', $id)
228 catch (\API_Exception
$e) {
229 $exceptionThrown = $e->getMessage();
231 $this->assertContains('id', $exceptionThrown);
235 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
237 * @param string $entity
239 protected function checkGet($entityClass, $id, $entity) {
240 $getResult = $entityClass::get(FALSE)
241 ->addWhere('id', '=', $id)
244 $errMsg = sprintf('Failed to fetch a %s after creation', $entity);
245 $this->assertEquals($id, $getResult->first()['id'], $errMsg);
246 $this->assertEquals(1, $getResult->count(), $errMsg);
250 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
252 * @param string $entity
254 protected function checkGetCount($entityClass, $id, $entity): void
{
255 $getResult = $entityClass::get(FALSE)
256 ->addWhere('id', '=', $id)
259 $errMsg = sprintf('%s getCount failed', $entity);
260 $this->assertEquals(1, $getResult->count(), $errMsg);
262 $getResult = $entityClass::get(FALSE)
265 $errMsg = sprintf('%s getCount failed', $entity);
266 $this->assertGreaterThanOrEqual(1, $getResult->count(), $errMsg);
270 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
272 protected function checkDeleteWithNoId($entityClass) {
273 $exceptionThrown = '';
275 $entityClass::delete()
278 catch (\API_Exception
$e) {
279 $exceptionThrown = $e->getMessage();
281 $this->assertContains('required', $exceptionThrown);
285 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
287 protected function checkWrongParamType($entityClass) {
288 $exceptionThrown = '';
291 ->setDebug('not a bool')
294 catch (\API_Exception
$e) {
295 $exceptionThrown = $e->getMessage();
297 $this->assertContains('debug', $exceptionThrown);
298 $this->assertContains('type', $exceptionThrown);
302 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
305 protected function checkDeletion($entityClass, $id) {
306 $deleteResult = $entityClass::delete(FALSE)
307 ->addWhere('id', '=', $id)
310 // should get back an array of deleted id
311 $this->assertEquals([['id' => $id]], (array) $deleteResult);
315 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
317 * @param string $entity
319 protected function checkPostDelete($entityClass, $id, $entity) {
320 $getDeletedResult = $entityClass::get(FALSE)
321 ->addWhere('id', '=', $id)
324 $errMsg = sprintf('Entity "%s" was not deleted', $entity);
325 $this->assertEquals(0, count($getDeletedResult), $errMsg);
329 * @param array $names
330 * List of entity names.
333 * List of data-provider arguments, one for each entity-name.
334 * Ex: ['Foo' => ['Foo'], 'Bar' => ['Bar']]
336 protected function toDataProviderArray($names) {
340 foreach ($names as $name) {
341 $result[$name] = [$name];