/**
* Test Relationship Clause
+ *
+ * @throws \CRM_Core_Exception
*/
public function testRelationshipClause() {
$today = date('Ymd');
'text_length' => NULL,
'data_type' => 'Date',
'html_type' => 'Select Date',
- 'is_search_range' => '0',
+ 'is_search_range' => '1',
'date_format' => 'mm/dd/yy',
'time_format' => '1',
'id' => $this->getCustomFieldID('select_date'),
'extends_table' => 'civicrm_contact',
'search_table' => 'contact_a',
],
+ $this->getCustomFieldName('int') => [
+ 'name' => $this->getCustomFieldName('int'),
+ 'type' => CRM_Utils_Type::T_INT,
+ 'title' => 'Enter integer here',
+ 'headerPattern' => '//',
+ 'import' => 1,
+ 'custom_field_id' => $this->getCustomFieldID('int'),
+ 'options_per_line' => NULL,
+ 'text_length' => NULL,
+ 'data_type' => 'Int',
+ 'html_type' => 'Text',
+ 'is_search_range' => '1',
+ 'id' => $this->getCustomFieldID('int'),
+ 'label' => 'Enter integer here',
+ 'groupTitle' => 'Custom Group',
+ 'default_value' => '4',
+ 'custom_group_id' => $customGroupID,
+ 'extends' => 'Contact',
+ 'extends_entity_column_value' => NULL,
+ 'extends_entity_column_id' => NULL,
+ 'is_view' => '0',
+ 'is_multiple' => '0',
+ 'option_group_id' => NULL,
+ 'date_format' => NULL,
+ 'time_format' => NULL,
+ 'is_required' => '1',
+ 'table_name' => 'civicrm_value_custom_group_' . $customGroupID,
+ 'column_name' => $this->getCustomFieldColumnName('int'),
+ 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('int'),
+ 'extends_table' => 'civicrm_contact',
+ 'search_table' => 'contact_a',
+ ],
];
$this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport());
}
* @group headless
*/
class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
+ use CRMTraits_Custom_CustomDataTrait;
/**
* Restore database to empty state.
/**
* Test filtering by relative custom data dates.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testSearchCustomDataDateRelative() {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest');
/**
* Test filtering by relative custom data dates.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testSearchCustomDataDateFromTo() {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest');
$params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues);
$queryObj = new CRM_Core_BAO_CustomQuery($params);
- $queryObj->Query();
+ $queryObj->query();
$this->assertEquals(
'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "20140606000000" AND "20150606235959"',
$queryObj->_where[0][0]
$params = [$customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)];
$queryObj = new CRM_Core_BAO_CustomQuery($params);
- $queryObj->Query();
+ $queryObj->query();
$this->assertEquals(
"civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} BETWEEN \"$from\" AND \"$to\"",
$queryObj->_where[0][0]
$params = [$customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)];
$queryObj = new CRM_Core_BAO_CustomQuery($params);
- $queryObj->Query();
+ $queryObj->query();
$wierdStringThatMeansGreaterEquals = chr(226) . chr(137) . chr(164);
$this->assertEquals(
$params = [$customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)];
$queryObj = new CRM_Core_BAO_CustomQuery($params);
- $queryObj->Query();
+ $queryObj->query();
$wierdStringThatMeansLessThanEquals = chr(226) . chr(137) . chr(165);
$expectedValue = ($isDate) ? '"20150606000000"' : $expectedValue;
/**
* Test filtering by relative custom data dates.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testSearchCustomDataDateEquals() {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest');
$formValues = [$dateCustomFieldName => '2015-06-06'];
$params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues);
$queryObj = new CRM_Core_BAO_CustomQuery($params);
- $queryObj->Query();
+ $queryObj->query();
$this->assertEquals(
"civicrm_value_testsearchcus_1.date_field_2 = '2015-06-06'",
$this->assertEquals($queryObj->_qill[0][0], "date field = 'June 6th, 2015'");
}
+ /**
+ * Test search builder style query including custom address fields.
+ *
+ * @throws \CRM_Core_Exception
+ */
+ public function testAddressCustomFields() {
+ $this->createCustomGroupWithFieldOfType(['extends' => 'Address'], 'int');
+ $individualID = $this->individualCreate();
+ $this->callAPISuccess('Address', 'create', [
+ 'contact_id' => $individualID,
+ 'street_address' => '10 Downing Street',
+ 'location_type_id' => 'Home',
+ $this->getCustomFieldName('int') => 5,
+ ]);
+
+ $queryObject = new CRM_Contact_BAO_Query(
+ [[$this->getCustomFieldName('int') . '-1', '=', 5, 1, 0]],
+ ['contact_type' => 1, 'location' => ['Home' => ['location_type' => 1, $this->getCustomFieldName('int') => 1]]]
+ );
+ $queryObject->query();
+ $tableName = $this->getCustomGroupTable();
+ $fieldName = $this->getCustomFieldColumnName('int');
+
+ $this->assertEquals([], $queryObject->_where[0]);
+ $this->assertEquals($tableName . '.' . $fieldName . ' = 5', implode(', ', $queryObject->_where[1]));
+ $this->assertEquals(1, $queryObject->_whereTables['civicrm_contact']);
+ $this->assertEquals('LEFT JOIN civicrm_address `Home-address` ON (`Home-address`.contact_id = contact_a.id AND `Home-address`.location_type_id = 1)', trim($queryObject->_whereTables['Home-address']));
+ $this->assertEquals("LEFT JOIN {$tableName} ON {$tableName}.entity_id = `Home-address`.id", trim($queryObject->_whereTables[$tableName]));
+ $this->assertEquals([], $queryObject->_qill[0]);
+ $this->assertEquals(['Enter integer here = 5'], $queryObject->_qill[1]);
+ }
+
}
public function createCustomGroup($params = []) {
$params = array_merge([
'title' => 'Custom Group',
- 'extends' => [$this->entity],
+ 'extends' => [$this->entity ?? 'Contact'],
'weight' => 5,
'style' => 'Inline',
'max_multiple' => 0,
], $params);
- $this->ids['CustomGroup'][$params['title']] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
- return $this->ids['CustomGroup'][$params['title']];
+ $identifier = $params['name'] ?? $params['title'];
+ $this->ids['CustomGroup'][$identifier] = $this->callAPISuccess('CustomGroup', 'create', $params)['id'];
+ return $this->ids['CustomGroup'][$identifier];
+ }
+
+ /**
+ * Get the table_name for the specified custom group.
+ *
+ * @param string $identifier
+ *
+ * @return string
+ */
+ public function getCustomGroupTable($identifier = 'Custom Group') {
+ return $this->callAPISuccessGetValue('CustomGroup', ['id' => $this->ids['CustomGroup'][$identifier], 'return' => 'table_name']);
+ }
+
+ /**
+ * Get the the column name for the identified custom field.
+ *
+ * @param string $key
+ * Identifier - generally keys map to data type - eg. 'text', 'int' etc.
+ *
+ * @return string
+ */
+ protected function getCustomFieldColumnName($key) {
+ return $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID($key), 'return' => 'column_name']);
}
/**
*
* @throws \CRM_Core_Exception
*/
- public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = '') {
- $supported = ['text', 'select', 'date'];
- if (!in_array($customFieldType, $supported)) {
+ public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = NULL) {
+ $supported = ['text', 'select', 'date', 'int'];
+ if (!in_array($customFieldType, $supported, TRUE)) {
throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do');
}
$groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title'];
+ $groupParams['name'] = $identifier ?? 'Custom Group';
$this->createCustomGroup($groupParams);
switch ($customFieldType) {
case 'text':
- $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+ $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
break;
case 'select':
- $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+ $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
+ break;
+
+ case 'int':
+ $customField = $this->createIntCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
break;
case 'date':
- $customField = $this->createDateCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]);
+ $customField = $this->createDateCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['name']]]);
break;
}
$this->ids['CustomField'][$identifier . $customFieldType] = $customField['id'];
$ids['select_string'] = $customField['id'];
$customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]);
-
$ids['select_date'] = $customField['id'];
+
+ $customField = $this->createIntCustomField(['custom_group_id' => $customGroupID]);
+ $ids['int'] = $customField['id'];
+
$params = [
'custom_group_id' => $customGroupID,
'name' => 'test_link',
* @return string
*/
protected function getCustomFieldName($key) {
- $linkField = 'custom_' . $this->getCustomFieldID($key);
- return $linkField;
+ return 'custom_' . $this->getCustomFieldID($key);
}
/**
* @return string
*/
protected function getCustomFieldID($key) {
- $linkField = $this->ids['CustomField'][$key];
- return $linkField;
+ return $this->ids['CustomField'][$key];
+ }
+
+ /**
+ * Create a custom text fields.
+ *
+ * @param array $params
+ * Parameter overrides, must include custom_group_id.
+ *
+ * @return array
+ */
+ protected function createIntCustomField($params = []) {
+ $params = array_merge([
+ 'label' => 'Enter integer here',
+ 'html_type' => 'Text',
+ 'data_type' => 'Int',
+ 'default_value' => '4',
+ 'weight' => 1,
+ 'is_required' => 1,
+ 'sequential' => 1,
+ 'is_searchable' => 1,
+ 'is_search_range' => 1,
+ ], $params);
+
+ return $this->callAPISuccess('CustomField', 'create', $params)['values'][0];
}
/**
'data_type' => 'Date',
'default_value' => '20090711',
'weight' => 3,
+ 'is_searchable' => 1,
+ 'is_search_range' => 1,
'time_format' => 1,
], $params);