Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test to lock in obscure custom join handling #15715

Merged
merged 1 commit into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,8 @@ public function testRelationshipPermissionClause() {

/**
* Test Relationship Clause
*
* @throws \CRM_Core_Exception
*/
public function testRelationshipClause() {
$today = date('Ymd');
Expand Down
34 changes: 33 additions & 1 deletion tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ public function testGetFieldsForImport() {
'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'),
Expand Down Expand Up @@ -639,6 +639,38 @@ public function testGetFieldsForImport() {
'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());
}
Expand Down
49 changes: 44 additions & 5 deletions tests/phpunit/CRM/Core/BAO/CustomQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @group headless
*/
class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
use CRMTraits_Custom_CustomDataTrait;

/**
* Restore database to empty state.
Expand All @@ -23,6 +24,8 @@ public function tearDown() {

/**
* Test filtering by relative custom data dates.
*
* @throws \CRM_Core_Exception
*/
public function testSearchCustomDataDateRelative() {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest');
Expand Down Expand Up @@ -87,6 +90,8 @@ public function testSearchCustomDataDateRelative() {

/**
* Test filtering by relative custom data dates.
*
* @throws \CRM_Core_Exception
*/
public function testSearchCustomDataDateFromTo() {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest');
Expand All @@ -108,7 +113,7 @@ public function testSearchCustomDataDateFromTo() {

$params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues);
$queryObj = new CRM_Core_BAO_CustomQuery($params);
$queryObj->Query();
$queryObj->query();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

correction of case to match fn

$this->assertEquals(
'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "20140606000000" AND "20150606235959"',
$queryObj->_where[0][0]
Expand Down Expand Up @@ -153,7 +158,7 @@ public function testSearchCustomDataFromTo() {

$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]
Expand Down Expand Up @@ -200,7 +205,7 @@ public function testSearchCustomDataFromAndTo() {

$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(
Expand All @@ -218,7 +223,7 @@ public function testSearchCustomDataFromAndTo() {

$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;
Expand All @@ -235,6 +240,8 @@ public function testSearchCustomDataFromAndTo() {

/**
* Test filtering by relative custom data dates.
*
* @throws \CRM_Core_Exception
*/
public function testSearchCustomDataDateEquals() {
$ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest');
Expand All @@ -253,7 +260,7 @@ public function testSearchCustomDataDateEquals() {
$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'",
Expand All @@ -262,4 +269,36 @@ public function testSearchCustomDataDateEquals() {
$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]);
}

}
84 changes: 70 additions & 14 deletions tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,37 @@ public function createCustomGroupWithFieldsOfAllTypes($groupParams = []) {
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']);
}

/**
Expand All @@ -71,24 +95,29 @@ public function createCustomGroup($params = []) {
*
* @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'];
Expand All @@ -107,8 +136,11 @@ public function createCustomFieldsOfAllTypes() {
$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',
Expand Down Expand Up @@ -157,8 +189,7 @@ public function createCustomFieldsOfAllTypes() {
* @return string
*/
protected function getCustomFieldName($key) {
$linkField = 'custom_' . $this->getCustomFieldID($key);
return $linkField;
return 'custom_' . $this->getCustomFieldID($key);
}

/**
Expand All @@ -173,8 +204,31 @@ protected function getCustomFieldName($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];
}

/**
Expand Down Expand Up @@ -261,6 +315,8 @@ protected function createDateCustomField($params): array {
'data_type' => 'Date',
'default_value' => '20090711',
'weight' => 3,
'is_searchable' => 1,
'is_search_range' => 1,
'time_format' => 1,
], $params);

Expand Down