From 9537dcb13708aa6fe97f13fb78fe88c8d11e1f10 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 12 Jan 2025 10:36:55 +1300 Subject: [PATCH] Minor cleanup in custom field test --- tests/phpunit/api/v3/CustomFieldTest.php | 118 +++++------------------ 1 file changed, 24 insertions(+), 94 deletions(-) diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index 37b5000224fd..6f9df434e0df 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -9,6 +9,7 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\CustomField; use Civi\Api4\OptionGroup; /** @@ -25,6 +26,14 @@ class api_v3_CustomFieldTest extends CiviUnitTestCase { * @throws \CRM_Core_Exception */ public function tearDown(): void { + // The parent tearDown will delete the linked option group (gender) + // if we don't do it more carefully here. + CRM_Core_DAO::executeQuery('UPDATE civicrm_custom_field f + INNER JOIN civicrm_option_group g ON g.id = option_group_id + SET option_group_id = NULL'); + CustomField::delete() + ->addWhere('id', '>', 0) + ->execute(); $this->quickCleanup([ 'civicrm_contact', 'civicrm_file', @@ -33,27 +42,6 @@ public function tearDown(): void { parent::tearDown(); } - /** - * Check with no label. - */ - public function testCustomFieldCreateWithoutLabel(): void { - $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'text_test_group']); - $params = [ - 'custom_group_id' => $customGroup['id'], - 'name' => 'test_textfield2', - 'html_type' => 'Text', - 'data_type' => 'String', - 'default_value' => 'abc', - 'weight' => 4, - 'is_required' => 1, - 'is_searchable' => 0, - 'is_active' => 1, - ]; - - $customField = $this->callAPIFailure('custom_field', 'create', $params); - $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: label'); - } - /** * Check with edit. */ @@ -79,27 +67,6 @@ public function testCustomFieldCreateWithEdit(): void { $this->assertNotNull($customFieldEdited['id']); } - /** - * Check without groupId. - */ - public function testCustomFieldCreateWithoutGroupID(): void { - $fieldParams = [ - 'name' => 'test_textfield1', - 'label' => 'Name', - 'html_type' => 'Text', - 'data_type' => 'String', - 'default_value' => 'abc', - 'weight' => 4, - 'is_required' => 1, - 'is_searchable' => 0, - 'is_active' => 1, - - ]; - - $customField = $this->callAPIFailure('custom_field', 'create', $fieldParams); - $this->assertEquals($customField['error_message'], 'Mandatory key(s) missing from params array: custom_group_id'); - } - /** * Check for Each data type: loop through available form input types */ @@ -141,7 +108,18 @@ public function _loopingCustomFieldCreateTest($params) { * @return array */ public function _buildParams($gid, $htype, $dtype) { - $params = $this->_buildBasicParams($gid, $htype, $dtype); + $params = [ + 'custom_group_id' => $gid, + 'label' => $dtype . $htype, + 'html_type' => $htype, + 'data_type' => $dtype, + 'weight' => 4, + 'is_required' => 0, + 'is_searchable' => 0, + 'is_active' => 1, + + ]; + /* //Not Working for any type. Maybe redundant with testCustomFieldCreateWithOptionValues() if ($htype == 'Multi-Select') $params = array_merge($params, array( @@ -155,27 +133,6 @@ public function _buildParams($gid, $htype, $dtype) { return $params; } - /** - * @param int $gid - * @param $htype - * @param $dtype - * - * @return array - */ - public function _buildBasicParams($gid, $htype, $dtype) { - return [ - 'custom_group_id' => $gid, - 'label' => $dtype . $htype, - 'html_type' => $htype, - 'data_type' => $dtype, - 'weight' => 4, - 'is_required' => 0, - 'is_searchable' => 0, - 'is_active' => 1, - - ]; - } - /** * Check with data type - Options with option_values */ @@ -258,7 +215,7 @@ public function testCustomFieldExistingOptionGroup(): void { 'return' => 'option_group_id', ]); - $this->assertEquals($optionGroupID, 3); + $this->assertEquals(3, $optionGroupID); } /** @@ -296,8 +253,8 @@ public function testCustomFieldGetReturnOptions(): void { 'id' => $customField['id'], 'return' => 'data_type', ]); - $this->assertTrue(array_key_exists('data_type', $result)); - $this->assertFalse(array_key_exists('custom_group_id', $result)); + $this->assertArrayHasKey('data_type', $result); + $this->assertArrayNotHasKey('custom_group_id', $result); } /** @@ -380,33 +337,6 @@ public function testCustomFieldCreateWithOptionValues(): void { $this->assertEquals('Label1', $result['values'][1]); } - ///////////////// civicrm_custom_field_delete methods - - /** - * Check without Field ID. - */ - public function testCustomFieldDeleteWithoutFieldID(): void { - $params = []; - $customField = $this->callAPIFailure('custom_field', 'delete', $params, - 'Mandatory key(s) missing from params array: id'); - } - - /** - * Check without valid array. - */ - public function testCustomFieldDelete(): void { - $customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_group']); - $customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]); - $this->assertNotNull($customField['id']); - - $params = [ - 'id' => $customField['id'], - ]; - $result = $this->callAPISuccess('custom_field', 'delete', $params); - - $this->assertAPISuccess($result); - } - /** * Check That any associated Mapping Field Entries are also removed. */