From bb97035ffeb12229fc1406073d6d7bbfdb8ce3d2 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Wed, 16 Jun 2021 15:00:06 +1200 Subject: [PATCH] Improve CustomGroupTest Use api, various cleanups --- .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 2 +- .../phpunit/CRM/Core/BAO/CustomGroupTest.php | 71 +++++++++++-------- tests/phpunit/CRM/Dedupe/MergerTest.php | 6 +- tests/phpunit/CiviTest/CiviUnitTestCase.php | 40 ++++------- tests/phpunit/api/v3/CustomFieldTest.php | 20 ++++-- tests/phpunit/api/v3/JobTest.php | 9 --- 6 files changed, 74 insertions(+), 74 deletions(-) diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 721f168edcda..f554aa905895 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -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, diff --git a/tests/phpunit/CRM/Core/BAO/CustomGroupTest.php b/tests/phpunit/CRM/Core/BAO/CustomGroupTest.php index 6fe01558763d..568f8e3ea5d3 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomGroupTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomGroupTest.php @@ -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(). */ @@ -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']); } /** @@ -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'); @@ -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', @@ -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 @@ -376,6 +390,8 @@ public function testCreateTable() { /** * Test checkCustomField() + * + * @throws \CRM_Core_Exception */ public function testCheckCustomField() { $groupParams = [ @@ -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'); @@ -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); @@ -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', @@ -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', @@ -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']); } /** diff --git a/tests/phpunit/CRM/Dedupe/MergerTest.php b/tests/phpunit/CRM/Dedupe/MergerTest.php index 80168f2ae2fd..b7415be6cf5f 100644 --- a/tests/phpunit/CRM/Dedupe/MergerTest.php +++ b/tests/phpunit/CRM/Dedupe/MergerTest.php @@ -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()); } /** @@ -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(); diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 50c94f7a1f3c..9a31a35ea739 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -26,6 +26,8 @@ * . */ +use Civi\Api4\CustomField; +use Civi\Api4\CustomGroup; use Civi\Api4\OptionGroup; use Civi\Api4\RelationshipType; use Civi\Payment\System; @@ -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'; } @@ -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); - } - } } /** diff --git a/tests/phpunit/api/v3/CustomFieldTest.php b/tests/phpunit/api/v3/CustomFieldTest.php index 303a91d7c8d1..87acb78cba0b 100644 --- a/tests/phpunit/api/v3/CustomFieldTest.php +++ b/tests/phpunit/api/v3/CustomFieldTest.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\OptionGroup; + /** * Test APIv3 civicrm_create_custom_group * @@ -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); } /** @@ -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', diff --git a/tests/phpunit/api/v3/JobTest.php b/tests/phpunit/api/v3/JobTest.php index ef77a10a569f..f4802f46f533 100644 --- a/tests/phpunit/api/v3/JobTest.php +++ b/tests/phpunit/api/v3/JobTest.php @@ -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']); } /** @@ -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']); } /** @@ -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']); } /**