+--------------------------------------------------------------------+
*/
+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().
*/
$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']);
}
/**
$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');
/**
* Test createTable()
+ *
+ * @throws \Exception
*/
- public function testCreateTable() {
+ public function testCreateTable(): void {
$groupParams = [
'title' => 'My Custom Group',
'name' => 'my_custom_group',
$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
/**
* Test checkCustomField()
+ *
+ * @throws \CRM_Core_Exception
*/
public function testCheckCustomField() {
$groupParams = [
/**
* 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');
$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);
/**
* Test create()
+ *
+ * @throws \CRM_Core_Exception
*/
- public function testCreate() {
+ public function testCreate(): void {
$params = [
'title' => 'Test_Group_1',
'name' => 'test_group_1',
'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',
'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']);
}
/**
* <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;
* @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';
}
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);
- }
- }
}
/**
+--------------------------------------------------------------------+
*/
+use Civi\Api4\OptionGroup;
+
/**
* Test APIv3 civicrm_create_custom_group
*
$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);
}
/**
*/
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',