Skip to content

Commit

Permalink
Minor cleanup in custom field test
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Jan 11, 2025
1 parent 8e2baa6 commit 9537dcb
Showing 1 changed file with 24 additions and 94 deletions.
118 changes: 24 additions & 94 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\CustomField;
use Civi\Api4\OptionGroup;

/**
Expand All @@ -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',
Expand All @@ -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.
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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(
Expand All @@ -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
*/
Expand Down Expand Up @@ -258,7 +215,7 @@ public function testCustomFieldExistingOptionGroup(): void {
'return' => 'option_group_id',
]);

$this->assertEquals($optionGroupID, 3);
$this->assertEquals(3, $optionGroupID);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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.
*/
Expand Down

0 comments on commit 9537dcb

Please sign in to comment.