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\Traits\TableDropperTrait
;
24 use api\v
4\UnitTestCase
;
29 class ConformanceTest
extends UnitTestCase
{
31 use 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 return $this->toDataProviderArray(Entity
::get(FALSE)->execute()->column('name'));
77 * Get entities to test.
79 * This is the low-tech list as generated by manual-overrides and direct inspection.
80 * It may be summoned at any time during PHPUnit lifecycle, but it may require
81 * occasional twiddling to give correct results.
85 public function getEntitiesLotech() {
87 $manual['remove'] = ['CustomValue'];
90 $srcDir = dirname(dirname(dirname(dirname(dirname(__DIR__
)))));
91 foreach ((array) glob("$srcDir/Civi/Api4/*.php") as $name) {
92 $scanned[] = preg_replace('/\.php/', '', basename($name));
96 array_unique(array_merge($scanned, $manual['add'])),
100 return $this->toDataProviderArray($names);
104 * Ensure that "getEntitiesLotech()" (which is the 'dataProvider') is up to date
105 * with "getEntitiesHitech()" (which is a live feed available entities).
107 public function testEntitiesProvider() {
108 $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().");
112 * @param string $entity
114 * @dataProvider getEntitiesLotech
116 public function testConformance($entity) {
117 $entityClass = 'Civi\Api4\\' . $entity;
119 $this->checkEntityInfo($entityClass);
120 $actions = $this->checkActions($entityClass);
122 // Go no further if it's not a CRUD entity
123 if (array_diff(['get', 'create', 'update', 'delete'], array_keys($actions))) {
124 $this->markTestSkipped("The entity \"$entity\" does not ");
128 $this->checkFields($entityClass, $entity);
129 $id = $this->checkCreation($entity, $entityClass);
130 $this->checkGet($entityClass, $id, $entity);
131 $this->checkGetCount($entityClass, $id, $entity);
132 $this->checkUpdateFailsFromCreate($entityClass, $id);
133 $this->checkWrongParamType($entityClass);
134 $this->checkDeleteWithNoId($entityClass);
135 $this->checkDeletion($entityClass, $id);
136 $this->checkPostDelete($entityClass, $id, $entity);
140 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
142 protected function checkEntityInfo($entityClass) {
143 $info = $entityClass::getInfo();
144 $this->assertNotEmpty($info['name']);
145 $this->assertNotEmpty($info['title']);
146 $this->assertNotEmpty($info['title_plural']);
147 $this->assertNotEmpty($info['type']);
148 $this->assertNotEmpty($info['description']);
152 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
153 * @param string $entity
155 protected function checkFields($entityClass, $entity) {
156 $fields = $entityClass::getFields(FALSE)
157 ->setIncludeCustom(FALSE)
161 $errMsg = sprintf('%s is missing required ID field', $entity);
162 $subset = ['data_type' => 'Integer'];
164 $this->assertArraySubset($subset, $fields['id'], $errMsg);
168 * @param string $entityClass
172 protected function checkActions($entityClass) {
173 $actions = $entityClass::getActions(FALSE)
177 $this->assertNotEmpty($actions);
178 return (array) $actions;
182 * @param string $entity
183 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
187 protected function checkCreation($entity, $entityClass) {
188 $requiredParams = $this->creationParamProvider
->getRequired($entity);
189 $createResult = $entityClass::create()
190 ->setValues($requiredParams)
191 ->setCheckPermissions(FALSE)
195 $this->assertArrayHasKey('id', $createResult, "create missing ID");
196 $id = $createResult['id'];
198 $this->assertGreaterThanOrEqual(1, $id, "$entity ID not positive");
204 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
207 protected function checkUpdateFailsFromCreate($entityClass, $id) {
208 $exceptionThrown = '';
210 $entityClass::create(FALSE)
211 ->addValue('id', $id)
214 catch (\API_Exception
$e) {
215 $exceptionThrown = $e->getMessage();
217 $this->assertContains('id', $exceptionThrown);
221 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
223 * @param string $entity
225 protected function checkGet($entityClass, $id, $entity) {
226 $getResult = $entityClass::get(FALSE)
227 ->addWhere('id', '=', $id)
230 $errMsg = sprintf('Failed to fetch a %s after creation', $entity);
231 $this->assertEquals($id, $getResult->first()['id'], $errMsg);
232 $this->assertEquals(1, $getResult->count(), $errMsg);
236 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
238 * @param string $entity
240 protected function checkGetCount($entityClass, $id, $entity) {
241 $getResult = $entityClass::get(FALSE)
242 ->addWhere('id', '=', $id)
245 $errMsg = sprintf('%s getCount failed', $entity);
246 $this->assertEquals(1, $getResult->count(), $errMsg);
248 $getResult = $entityClass::get(FALSE)
251 $errMsg = sprintf('%s getCount failed', $entity);
252 $this->assertGreaterThanOrEqual(1, $getResult->count(), $errMsg);
256 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
258 protected function checkDeleteWithNoId($entityClass) {
259 $exceptionThrown = '';
261 $entityClass::delete()
264 catch (\API_Exception
$e) {
265 $exceptionThrown = $e->getMessage();
267 $this->assertContains('required', $exceptionThrown);
271 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
273 protected function checkWrongParamType($entityClass) {
274 $exceptionThrown = '';
277 ->setDebug('not a bool')
280 catch (\API_Exception
$e) {
281 $exceptionThrown = $e->getMessage();
283 $this->assertContains('debug', $exceptionThrown);
284 $this->assertContains('type', $exceptionThrown);
288 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
291 protected function checkDeletion($entityClass, $id) {
292 $deleteResult = $entityClass::delete(FALSE)
293 ->addWhere('id', '=', $id)
296 // should get back an array of deleted id
297 $this->assertEquals([['id' => $id]], (array) $deleteResult);
301 * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass
303 * @param string $entity
305 protected function checkPostDelete($entityClass, $id, $entity) {
306 $getDeletedResult = $entityClass::get(FALSE)
307 ->addWhere('id', '=', $id)
310 $errMsg = sprintf('Entity "%s" was not deleted', $entity);
311 $this->assertEquals(0, count($getDeletedResult), $errMsg);
315 * @param array $names
316 * List of entity names.
319 * List of data-provider arguments, one for each entity-name.
320 * Ex: ['Foo' => ['Foo'], 'Bar' => ['Bar']]
322 protected function toDataProviderArray($names) {
326 foreach ($names as $name) {
327 $result[$name] = [$name];