Skip to content

Commit

Permalink
Add test to lock in obscure custom join handling
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Nov 4, 2019
1 parent c796d7a commit 53266d2
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 19 deletions.
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
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();
$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]);
}

}
82 changes: 68 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

0 comments on commit 53266d2

Please sign in to comment.