From 4df6b683d20f0d0584fd6b93c532f69fc21bc5a4 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 27 Feb 2023 14:00:30 -0500 Subject: [PATCH] APIv4 - Add 'Update' to conformanceTest --- CRM/Core/BAO/EntityTag.php | 12 ++-- CRM/Core/BAO/UFJoin.php | 4 +- Civi/Api4/Action/EntityTag/Create.php | 21 ------ .../Action/EntityTag/EntityTagSaveTrait.php | 34 ---------- Civi/Api4/Action/EntityTag/Save.php | 21 ------ Civi/Api4/Action/EntityTag/Update.php | 21 ------ Civi/Api4/EntityTag.php | 27 -------- .../phpunit/api/v4/Entity/ConformanceTest.php | 67 ++++++++++++------- 8 files changed, 51 insertions(+), 156 deletions(-) delete mode 100644 Civi/Api4/Action/EntityTag/Create.php delete mode 100644 Civi/Api4/Action/EntityTag/EntityTagSaveTrait.php delete mode 100644 Civi/Api4/Action/EntityTag/Save.php delete mode 100644 Civi/Api4/Action/EntityTag/Update.php diff --git a/CRM/Core/BAO/EntityTag.php b/CRM/Core/BAO/EntityTag.php index 606f5fad93..c48246775d 100644 --- a/CRM/Core/BAO/EntityTag.php +++ b/CRM/Core/BAO/EntityTag.php @@ -46,13 +46,9 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag { /** * Takes an associative array and creates a entityTag object. * - * the function extract all the params it needs to initialize the create a - * group object. the params array could contain additional unused name/value - * pairs - * * @param array $params - * (reference ) an assoc array of name/value pairs. * + * @deprecated * @return CRM_Core_BAO_EntityTag */ public static function add(&$params) { @@ -89,7 +85,7 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag { * @return bool */ public static function dataExists($params) { - return !($params['tag_id'] == 0); + return !empty($params['tag_id']); } /** @@ -252,8 +248,10 @@ class CRM_Core_BAO_EntityTag extends CRM_Core_DAO_EntityTag { /** * Takes an associative array and creates tag entity record for all tag entities. * + * Nonstandard function + * @deprecated + * * @param array $params - * (reference) an assoc array of name/value pairs. * @param string $entityTable * @param int $entityID */ diff --git a/CRM/Core/BAO/UFJoin.php b/CRM/Core/BAO/UFJoin.php index df3643984d..493594176d 100644 --- a/CRM/Core/BAO/UFJoin.php +++ b/CRM/Core/BAO/UFJoin.php @@ -21,11 +21,11 @@ class CRM_Core_BAO_UFJoin extends CRM_Core_DAO_UFJoin { /** - * Takes an associative array and creates a uf join object. + * This deprecated "create" function alarmingly will DELETE records if you don't pass them in just right! * * @param array $params - * (reference) an assoc array of name/value pairs. * + * @deprecated * @return CRM_Core_DAO_UFJoin */ public static function &create($params) { diff --git a/Civi/Api4/Action/EntityTag/Create.php b/Civi/Api4/Action/EntityTag/Create.php deleted file mode 100644 index afa0dba7fd..0000000000 --- a/Civi/Api4/Action/EntityTag/Create.php +++ /dev/null @@ -1,21 +0,0 @@ -setCheckPermissions($checkPermissions); - } - - /** - * @param bool $checkPermissions - * @return Action\EntityTag\Save - */ - public static function save($checkPermissions = TRUE) { - return (new Action\EntityTag\Save('EntityTag', __FUNCTION__)) - ->setCheckPermissions($checkPermissions); - } - - /** - * @param bool $checkPermissions - * @return Action\EntityTag\Update - */ - public static function update($checkPermissions = TRUE) { - return (new Action\EntityTag\Update('EntityTag', __FUNCTION__)) - ->setCheckPermissions($checkPermissions); - } - } diff --git a/tests/phpunit/api/v4/Entity/ConformanceTest.php b/tests/phpunit/api/v4/Entity/ConformanceTest.php index 9cdce6f5b1..68fd532428 100644 --- a/tests/phpunit/api/v4/Entity/ConformanceTest.php +++ b/tests/phpunit/api/v4/Entity/ConformanceTest.php @@ -130,36 +130,37 @@ class ConformanceTest extends Api4TestBase implements HookInterface { } /** - * @param string $entity + * @param string $entityName * Ex: 'Contact' * * @dataProvider getEntitiesLotech * * @throws \CRM_Core_Exception */ - public function testConformance(string $entity): void { - $entityClass = CoreUtil::getApiClass($entity); + public function testConformance(string $entityName): void { + $entityClass = CoreUtil::getApiClass($entityName); $this->checkEntityInfo($entityClass); $actions = $this->checkActions($entityClass); // Go no further if it's not a CRUD entity if (array_diff(['get', 'create', 'update', 'delete'], array_keys($actions))) { - $this->markTestSkipped("The API \"$entity\" does not implement CRUD actions"); + $this->markTestSkipped("The API \"$entityName\" does not implement CRUD actions"); } - $this->checkFields($entityClass, $entity); - $this->checkCreationDenied($entity, $entityClass); - $id = $this->checkCreation($entity, $entityClass); - $this->checkGet($entityClass, $id, $entity); - $this->checkGetAllowed($entityClass, $id, $entity); - $this->checkGetCount($entityClass, $id, $entity); + $this->checkFields($entityClass, $entityName); + $this->checkCreationDenied($entityName, $entityClass); + $id = $this->checkCreation($entityName, $entityClass); + $getResult = $this->checkGet($entityName, $id); + $this->checkGetAllowed($entityClass, $id, $entityName); + $this->checkGetCount($entityClass, $id, $entityName); $this->checkUpdateFailsFromCreate($entityClass, $id); + $this->checkUpdate($entityName, $getResult); $this->checkWrongParamType($entityClass); $this->checkDeleteWithNoId($entityClass); - $this->checkDeletionDenied($entityClass, $id, $entity); - $this->checkDeletionAllowed($entityClass, $id, $entity); - $this->checkPostDelete($entityClass, $id, $entity); + $this->checkDeletionDenied($entityClass, $id, $entityName); + $this->checkDeletionAllowed($entityClass, $id, $entityName); + $this->checkPostDelete($entityClass, $id, $entityName); } /** @@ -319,19 +320,39 @@ class ConformanceTest extends Api4TestBase implements HookInterface { } /** - * @param \Civi\Api4\Generic\AbstractEntity|string $entityClass + * @param string $entityName * @param int $id - * @param string $entity */ - protected function checkGet($entityClass, int $id, string $entity): void { - $getResult = $entityClass::get(FALSE) - ->addWhere('id', '=', $id) - ->execute(); - - $errMsg = sprintf('Failed to fetch a %s after creation', $entity); - $idField = CoreUtil::getIdFieldName($entity); + protected function checkGet(string $entityName, int $id): array { + $idField = CoreUtil::getIdFieldName($entityName); + $getResult = civicrm_api4($entityName, 'get', [ + 'checkPermissions' => FALSE, + 'where' => [[$idField, '=', $id]], + ]); + $errMsg = sprintf('Failed to fetch a %s after creation', $entityName); $this->assertEquals($id, $getResult->first()[$idField], $errMsg); - $this->assertEquals(1, $getResult->count(), $errMsg); + return $getResult->single(); + } + + /** + * Ensure updating an entity does not alter it + * + * @param string $entityName + * @param array $getResult + * @throws \CRM_Core_Exception + */ + protected function checkUpdate(string $entityName, array $getResult): void { + $idField = CoreUtil::getIdFieldName($entityName); + civicrm_api4($entityName, 'update', [ + 'checkPermissions' => FALSE, + 'where' => [[$idField, '=', $getResult[$idField]]], + 'values' => [$idField, $getResult[$idField]], + ]); + $getResult2 = civicrm_api4($entityName, 'get', [ + 'checkPermissions' => FALSE, + 'where' => [[$idField, '=', $getResult[$idField]]], + ]); + $this->assertEquals($getResult, $getResult2->single()); } /** -- 2.25.1