Skip to content

Commit

Permalink
Improve CustomGroupTest
Browse files Browse the repository at this point in the history
Use api, various cleanups
  • Loading branch information
eileenmcnaughton committed Jun 17, 2021
1 parent c0a7981 commit bb97035
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 74 deletions.
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@ public function testGetFieldsForImport() {
'label' => 'Enter text here',
'groupTitle' => 'Custom Group',
'default_value' => 'xyz',
'custom_group_id' => '1',
'custom_group_id' => $customGroupID,
'extends' => 'Contact',
'extends_entity_column_value' => NULL,
'extends_entity_column_id' => NULL,
Expand Down
71 changes: 43 additions & 28 deletions tests/phpunit/CRM/Core/BAO/CustomGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,26 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\CustomGroup;

/**
* Class CRM_Core_BAO_CustomGroupTest
* @group headless
*/
class CRM_Core_BAO_CustomGroupTest extends CiviUnitTestCase {

/**
* Clean up after test.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function tearDown(): void {
$this->quickCleanup(['civicrm_contact'], TRUE);
parent::tearDown();
}

/**
* Test getTree().
*/
Expand All @@ -23,7 +37,6 @@ public function testGetTree() {
$customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
$result = CRM_Core_BAO_CustomGroup::getTree('Individual', NULL, $customGroup['id']);
$this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
$this->customGroupDelete($customGroup['id']);
}

/**
Expand Down Expand Up @@ -137,13 +150,12 @@ public function testGetTreeActivitySubType() {
$customField = $this->customFieldCreate(['custom_group_id' => $customGroup['id']]);
$result = CRM_Core_BAO_CustomGroup::getTree('Activity', NULL, NULL, NULL, 1);
$this->assertEquals('Custom Field', $result[$customGroup['id']]['fields'][$customField['id']]['label']);
$this->customGroupDelete($customGroup['id']);
}

/**
* Test retrieve() with Empty Params.
*/
public function testRetrieveEmptyParams() {
public function testRetrieveEmptyParams(): void {
$params = [];
$customGroup = CRM_Core_BAO_CustomGroup::retrieve($params, $dafaults);
$this->assertNull($customGroup, 'Check that no custom Group is retreived');
Expand Down Expand Up @@ -342,8 +354,10 @@ public function testDeleteGroup() {

/**
* Test createTable()
*
* @throws \Exception
*/
public function testCreateTable() {
public function testCreateTable(): void {
$groupParams = [
'title' => 'My Custom Group',
'name' => 'my_custom_group',
Expand All @@ -363,7 +377,7 @@ public function testCreateTable() {
$customGroupId = $customGroup->id;

//check db for custom group.
$dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
$this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupId, 'title', 'id',
'Database check for custom group record.'
);
//check for custom group table name
Expand All @@ -376,6 +390,8 @@ public function testCreateTable() {

/**
* Test checkCustomField()
*
* @throws \CRM_Core_Exception
*/
public function testCheckCustomField() {
$groupParams = [
Expand Down Expand Up @@ -429,8 +445,10 @@ public function testCheckCustomField() {

/**
* Test getActiveGroups() with Invalid Params()
*
* @throws \CiviCRM_API3_Exception
*/
public function testGetActiveGroupsWithInvalidParams() {
public function testGetActiveGroupsWithInvalidParams(): void {
$contactId = $this->individualCreate();
$activeGroups = CRM_Core_BAO_CustomGroup::getActiveGroups('ABC', 'civicrm/contact/view/cd', $contactId);
$this->assertEquals(empty($activeGroups), TRUE, 'Check that Emprt params are retreived');
Expand All @@ -452,7 +470,7 @@ public function testGetActiveGroups() {
$activeGroup = CRM_Core_BAO_CustomGroup::getActiveGroups('Individual', 'civicrm/contact/view/cd', $contactId);
foreach ($activeGroup as $key => $value) {
if ($value['id'] == $customGroup['id']) {
$this->assertEquals($value['path'], 'civicrm/contact/view/cd');
$this->assertEquals('civicrm/contact/view/cd', $value['path']);
$this->assertEquals($value['title'], $customGroupTitle);
$query = 'reset=1&gid=' . $customGroup['id'] . '&cid=' . $contactId;
$this->assertEquals($value['query'], $query);
Expand All @@ -465,8 +483,10 @@ public function testGetActiveGroups() {

/**
* Test create()
*
* @throws \CRM_Core_Exception
*/
public function testCreate() {
public function testCreate(): void {
$params = [
'title' => 'Test_Group_1',
'name' => 'test_group_1',
Expand All @@ -479,26 +499,27 @@ public function testCreate() {
'is_active' => 1,
'version' => 3,
];
$customGroup = CRM_Core_BAO_CustomGroup::create($params);
$customGroupID = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];

$dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
$dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupID, 'title', 'id',
'Database check for custom group record.'
);
$this->assertEquals($params['title'], $dbCustomGroupTitle);

$dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
$dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroupID, 'table_name', 'id',
'Database check for custom group record.'
);
$this->assertEquals(strtolower("civicrm_value_{$params['name']}_{$customGroup->id}"), $dbCustomGroupTableName,
$this->assertEquals(strtolower("civicrm_value_{$params['name']}_$customGroupID"), $dbCustomGroupTableName,
"The table name should be suffixed with '_ID' unless specified.");

$this->customGroupDelete($customGroup->id);
}

/**
* Test create() given a table_name
*
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public function testCreateTableName() {
public function testCreateTableName(): void {
$params = [
'title' => 'Test_Group_2',
'name' => 'test_group_2',
Expand All @@ -511,19 +532,13 @@ public function testCreateTableName() {
'help_post' => 'This is Post Help For Test Group 1',
'is_active' => 1,
];
$customGroup = CRM_Core_BAO_CustomGroup::create($params);

$dbCustomGroupTitle = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'title', 'id',
'Database check for custom group record.'
);
$this->assertEquals($params['title'], $dbCustomGroupTitle);

$dbCustomGroupTableName = $this->assertDBNotNull('CRM_Core_DAO_CustomGroup', $customGroup->id, 'table_name', 'id',
'Database check for custom group record.'
);
$this->assertEquals($params['table_name'], $dbCustomGroupTableName);

$this->customGroupDelete($customGroup->id);
$customGroupID = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
$group = CustomGroup::get()
->addWhere('id', '=', $customGroupID)
->addSelect('title', 'table_name')
->execute()->first();
$this->assertEquals('Test_Group_2', $group['title']);
$this->assertEquals('test_otherTableName', $group['table_name']);
}

/**
Expand Down
6 changes: 1 addition & 5 deletions tests/phpunit/CRM/Dedupe/MergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,6 @@ public function testGetCidRefs(): void {
$unsetRefs = array_fill_keys(['civicrm_group_contact_cache', 'civicrm_acl_cache', 'civicrm_acl_contact_cache'], 1);
$this->assertEquals($sortRefs(array_diff_key($this->getStaticCIDRefs(), $unsetRefs)), $sortRefs(CRM_Dedupe_Merger::cidRefs()));
$this->assertEquals($sortRefs(array_diff_key($this->getCalculatedCIDRefs(), $unsetRefs)), $sortRefs(CRM_Dedupe_Merger::cidRefs()));

// These are deliberately unset.
// $unsetRefs = array_fill_keys(['civicrm_group_contact_cache', 'civicrm_acl_cache', 'civicrm_acl_contact_cache', 'civicrm_relationship_cache'], 1);
// $this->assertEquals(array_diff_key($this->getStaticCIDRefs(), $unsetRefs), CRM_Dedupe_Merger::cidRefs());
// $this->assertEquals(array_diff_key($this->getCalculatedCIDRefs(), $unsetRefs), CRM_Dedupe_Merger::cidRefs());
}

/**
Expand All @@ -314,6 +309,7 @@ public function testGetCidRefs(): void {
* focus is on ensuring they match.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testGetMatches(): void {
$this->setupMatchData();
Expand Down
40 changes: 14 additions & 26 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* <http://www.gnu.org/licenses/>.
*/

use Civi\Api4\CustomField;
use Civi\Api4\CustomGroup;
use Civi\Api4\OptionGroup;
use Civi\Api4\RelationshipType;
use Civi\Payment\System;
Expand Down Expand Up @@ -1844,22 +1846,24 @@ public function confirmEntitiesDeleted($entities) {
* @param bool $dropCustomValueTables
*
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE) {
if ($this->tx) {
throw new \CRM_Core_Exception("CiviUnitTestCase: quickCleanup() is not compatible with useTransaction()");
}
if ($dropCustomValueTables) {
$optionGroupResult = CRM_Core_DAO::executeQuery('SELECT option_group_id FROM civicrm_custom_field');
while ($optionGroupResult->fetch()) {
// We have a test that sets the option_group_id for a custom group to that of 'activity_type'.
// Then test tearDown deletes it. This is all mildly terrifying but for the context here we can be pretty
// sure the low-numbered (50 is arbitrary) option groups are not ones to 'just delete' in a
// generic cleanup routine.
if (!empty($optionGroupResult->option_group_id) && $optionGroupResult->option_group_id > 50) {
CRM_Core_DAO::executeQuery('DELETE FROM civicrm_option_group WHERE id = ' . $optionGroupResult->option_group_id);
}
}

CustomField::get(FALSE)->setSelect(['option_group_id', 'custom_group_id'])
->addChain('delete_options', OptionGroup::delete()
->addWhere('id', '=', '$option_group_id')
)
->addChain('delete_fields', CustomField::delete()
->addWhere('id', '=', '$id')
)->execute();

CustomGroup::delete(FALSE)->addWhere('id', '>', 0)->execute();
// Reset autoincrement too.
$tablesToTruncate[] = 'civicrm_custom_group';
$tablesToTruncate[] = 'civicrm_custom_field';
}
Expand All @@ -1872,22 +1876,6 @@ public function quickCleanup($tablesToTruncate, $dropCustomValueTables = FALSE)
CRM_Core_DAO::executeQuery($sql);
}
CRM_Core_DAO::executeQuery("SET FOREIGN_KEY_CHECKS = 1;");

if ($dropCustomValueTables) {
$dbName = self::getDBName();
$query = "
SELECT TABLE_NAME as tableName
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = '{$dbName}'
AND ( TABLE_NAME LIKE 'civicrm_value_%' )
";

$tableDAO = CRM_Core_DAO::executeQuery($query);
while ($tableDAO->fetch()) {
$sql = "DROP TABLE {$tableDAO->tableName}";
CRM_Core_DAO::executeQuery($sql);
}
}
}

/**
Expand Down
20 changes: 15 additions & 5 deletions tests/phpunit/api/v3/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\OptionGroup;

/**
* Test APIv3 civicrm_create_custom_group
*
Expand Down Expand Up @@ -573,17 +575,22 @@ public function testUpdateCustomField() {
$this->customGroupDelete($customGroup['id']);
}

public function testCustomFieldCreateWithOptionGroupName() {
/**
* @throws \API_Exception
* @throws \CRM_Core_Exception
*/
public function testCustomFieldCreateWithOptionGroupName(): void {
$customGroup = $this->customGroupCreate(['extends' => 'Individual', 'title' => 'test_custom_group']);
OptionGroup::create()->setValues(['name' => 'abc'])->execute();
$params = [
'custom_group_id' => $customGroup['id'],
'name' => 'Activity type',
'label' => 'Activity type',
'data_type' => 'String',
'html_type' => 'Select',
'option_group_id' => 'activity_type',
'option_group_id' => 'abc',
];
$result = $this->callAPISuccess('CustomField', 'create', $params);
$this->callAPISuccess('CustomField', 'create', $params);
}

/**
Expand All @@ -593,15 +600,18 @@ public function testCustomFieldCreateWithOptionGroupName() {
*/
public function getCustomFieldKeys($getFieldsResult) {
$isCustom = function ($key) {
return preg_match('/^custom_/', $key);
return 0 === strpos($key, 'custom_');
};
$r = array_values(array_filter(array_keys($getFieldsResult['values']), $isCustom));
sort($r);
return $r;
}

/**
* @throws \CRM_Core_Exception
*/
public function testMakeSearchableContactReferenceFieldUnsearchable() {
$customGroup = $this->customGroupCreate([
$this->customGroupCreate([
'name' => 'testCustomGroup',
'title' => 'testCustomGroup',
'extends' => 'Individual',
Expand Down
9 changes: 0 additions & 9 deletions tests/phpunit/api/v3/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,9 +1146,6 @@ public function testBatchMergeCustomDataViewOnlyField(): void {
$mouseParams['return'] = 'custom_' . $customField['id'];
$mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
$this->assertEquals('blah', $mouse['custom_' . $customField['id']]);

$this->customFieldDelete($customField['id']);
$this->customGroupDelete($customGroup['id']);
}

/**
Expand Down Expand Up @@ -1180,9 +1177,6 @@ public function testBatchMergeCustomDataZeroValueField(): void {
$mouseParams['return'] = 'custom_' . $customField['id'];
$mouse = $this->callAPISuccess('Contact', 'getsingle', $mouseParams);
$this->assertEquals(0, $mouse['custom_' . $customField['id']]);

$this->customFieldDelete($customField['id']);
$this->customGroupDelete($customGroup['id']);
}

/**
Expand All @@ -1207,9 +1201,6 @@ public function testBatchMergeCustomDataZeroValueFieldWithConflict(): void {
$this->individualCreate(array_merge($mouseParams, ['id' => $mouse2, 'custom_' . $customField['id'] => 0]));
$result = $this->callAPISuccess('Job', 'process_batch_merge', ['check_permissions' => 0, 'mode' => 'safe']);
$this->assertCount(0, $result['values']['merged']);

$this->customFieldDelete($customField['id']);
$this->customGroupDelete($customGroup['id']);
}

/**
Expand Down

0 comments on commit bb97035

Please sign in to comment.