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
{
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 foreach (array_keys(\CRM_Core_Component
::getComponents()) as $component) {
75 \CRM_Core_BAO_ConfigSetting
::enableComponent($component);
77 return $this->toDataProviderArray(Entity
::get(FALSE)->execute()->column('name'));
81 * Get entities to test.
83 * This is the low-tech list as generated by manual-overrides and direct inspection.
84 * It may be summoned at any time during PHPUnit lifecycle, but it may require
85 * occasional twiddling to give correct results.
89 public function getEntitiesLotech() {
91 $manual['remove'] = ['CustomValue'];
92 $manual['transform'] = ['CiviCase' => 'Case'];
95 $srcDir = dirname(__DIR__
, 5);
96 foreach ((array) glob("$srcDir/Civi/Api4/*.php") as $name) {
97 $fileName = basename($name, '.php');
98 $scanned[] = $manual['transform'][$fileName] ??
$fileName;
102 array_unique(array_merge($scanned, $manual['add'])),
106 return $this->toDataProviderArray($names);
110 * Ensure that "getEntitiesLotech()" (which is the 'dataProvider') is up to date
111 * with "getEntitiesHitech()" (which is a live feed available entities).
113 public function testEntitiesProvider() {
114 $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().");
118 * @param string $entity
121 * @dataProvider getEntitiesLotech
123 * @throws \API_Exception
125 public function testConformance($entity): void
{
126 $entityClass = CoreUtil
::getApiClass($entity);
128 $this->checkEntityInfo($entityClass);
129 $actions = $this->checkActions($entityClass);
131 // Go no further if it's not a CRUD entity
132 if (array_diff(['get', 'create', 'update', 'delete'], array_keys($actions))) {
133 $this->markTestSkipped("The API \"$entity\" does not implement CRUD actions");
136 $this->checkFields($entityClass, $entity);
137 $id = $this->checkCreation($entity, $entityClass);
138 $this->checkGet($entityClass, $id, $entity);
139 $this->checkGetCount($entityClass, $id, $entity);
140 $this->checkUpdateFailsFromCreate($entityClass, $id);
141 $this->checkWrongParamType($entityClass);
142 $this->checkDeleteWithNoId($entityClass);
143 $this->checkDeletion($entityClass, $id);
144 $this->checkPostDelete($entityClass, $id, $entity);
148 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
150 protected function checkEntityInfo($entityClass): void
{
151 $info = $entityClass::getInfo();
152 $this->assertNotEmpty($info['name']);
153 $this->assertNotEmpty($info['title']);
154 $this->assertNotEmpty($info['title_plural']);
155 $this->assertNotEmpty($info['type']);
156 $this->assertNotEmpty($info['description']);
160 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
161 * @param string $entity
163 * @throws \API_Exception
165 protected function checkFields($entityClass, $entity) {
166 $fields = $entityClass::getFields(FALSE)
167 ->setIncludeCustom(FALSE)
171 $errMsg = sprintf('%s is missing required ID field', $entity);
172 $subset = ['data_type' => 'Integer'];
174 $this->assertArraySubset($subset, $fields['id'], $errMsg);
178 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
182 * @throws \API_Exception
184 protected function checkActions($entityClass): array {
185 $actions = $entityClass::getActions(FALSE)
189 $this->assertNotEmpty($actions);
190 return (array) $actions;
194 * @param string $entity
195 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
199 protected function checkCreation($entity, $entityClass) {
200 $requiredParams = $this->creationParamProvider
->getRequired($entity);
201 $createResult = $entityClass::create()
202 ->setValues($requiredParams)
203 ->setCheckPermissions(FALSE)
207 $this->assertArrayHasKey('id', $createResult, "create missing ID");
208 $id = $createResult['id'];
210 $this->assertGreaterThanOrEqual(1, $id, "$entity ID not positive");
216 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
219 protected function checkUpdateFailsFromCreate($entityClass, $id): void
{
220 $exceptionThrown = '';
222 $entityClass::create(FALSE)
223 ->addValue('id', $id)
226 catch (\API_Exception
$e) {
227 $exceptionThrown = $e->getMessage();
229 $this->assertContains('id', $exceptionThrown);
233 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
235 * @param string $entity
237 protected function checkGet($entityClass, $id, $entity) {
238 $getResult = $entityClass::get(FALSE)
239 ->addWhere('id', '=', $id)
242 $errMsg = sprintf('Failed to fetch a %s after creation', $entity);
243 $this->assertEquals($id, $getResult->first()['id'], $errMsg);
244 $this->assertEquals(1, $getResult->count(), $errMsg);
248 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
250 * @param string $entity
252 protected function checkGetCount($entityClass, $id, $entity): void
{
253 $getResult = $entityClass::get(FALSE)
254 ->addWhere('id', '=', $id)
257 $errMsg = sprintf('%s getCount failed', $entity);
258 $this->assertEquals(1, $getResult->count(), $errMsg);
260 $getResult = $entityClass::get(FALSE)
263 $errMsg = sprintf('%s getCount failed', $entity);
264 $this->assertGreaterThanOrEqual(1, $getResult->count(), $errMsg);
268 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
270 protected function checkDeleteWithNoId($entityClass) {
271 $exceptionThrown = '';
273 $entityClass::delete()
276 catch (\API_Exception
$e) {
277 $exceptionThrown = $e->getMessage();
279 $this->assertContains('required', $exceptionThrown);
283 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
285 protected function checkWrongParamType($entityClass) {
286 $exceptionThrown = '';
289 ->setDebug('not a bool')
292 catch (\API_Exception
$e) {
293 $exceptionThrown = $e->getMessage();
295 $this->assertContains('debug', $exceptionThrown);
296 $this->assertContains('type', $exceptionThrown);
300 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
303 protected function checkDeletion($entityClass, $id) {
304 $deleteResult = $entityClass::delete(FALSE)
305 ->addWhere('id', '=', $id)
308 // should get back an array of deleted id
309 $this->assertEquals([['id' => $id]], (array) $deleteResult);
313 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
315 * @param string $entity
317 protected function checkPostDelete($entityClass, $id, $entity) {
318 $getDeletedResult = $entityClass::get(FALSE)
319 ->addWhere('id', '=', $id)
322 $errMsg = sprintf('Entity "%s" was not deleted', $entity);
323 $this->assertEquals(0, count($getDeletedResult), $errMsg);
327 * @param array $names
328 * List of entity names.
331 * List of data-provider arguments, one for each entity-name.
332 * Ex: ['Foo' => ['Foo'], 'Bar' => ['Bar']]
334 protected function toDataProviderArray($names) {
338 foreach ($names as $name) {
339 $result[$name] = [$name];