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

dev/core#1723 - Adv Search - Reciprocal relationship search with cust… #17142

Merged
merged 1 commit into from
Apr 22, 2020
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
6 changes: 3 additions & 3 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -4190,7 +4190,7 @@ public function relationship(&$values) {
if (self::$_relType == 'reciprocal') {
$where = [];
self::$_relationshipTempTable = $relationshipTempTable = CRM_Utils_SQL_TempTable::build()
->createWithColumns("`contact_id` int(10) unsigned NOT NULL DEFAULT '0', `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0', relationship_id int unsigned, KEY `contact_id` (`contact_id`), KEY `contact_id_alt` (`contact_id_alt`)")
->createWithColumns("`contact_id` int(10) unsigned NOT NULL DEFAULT '0', `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0', id int unsigned, KEY `contact_id` (`contact_id`), KEY `contact_id_alt` (`contact_id_alt`)")
->getName();
if ($nameClause) {
$where[$grouping][] = " sort_name $nameClause ";
Expand All @@ -4206,7 +4206,7 @@ public function relationship(&$values) {
$groupJoinTable = "contact_b";
$groupJoinColumn = "id";
}
$allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE);
$allRelationshipType = CRM_Contact_BAO_Relationship::getContactRelationshipType(NULL, 'null', NULL, NULL, TRUE, 'label', FALSE);
if ($nameClause || !$targetGroup) {
if (!empty($relationType)) {
$relQill = '';
Expand Down Expand Up @@ -4314,7 +4314,7 @@ public function relationship(&$values) {
$whereClause = str_replace('contact_b', 'c', $whereClause);
}
$sql = "
INSERT INTO {$relationshipTempTable} (contact_id, contact_id_alt, relationship_id)
INSERT INTO {$relationshipTempTable} (contact_id, contact_id_alt, id)
(SELECT contact_id_b as contact_id, contact_id_a as contact_id_alt, civicrm_relationship.id
FROM civicrm_relationship
INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id
Expand Down
48 changes: 48 additions & 0 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,54 @@ public function testNonReciprocalRelationshipTargetGroupIsCorrectResults() {
$this->assertEquals($contactID_b, $dao->contact_id, "Search query returns parent of contact A?");
}

/**
* Relationship search with custom fields.
*/
public function testReciprocalRelationshipWithCustomFields() {
$params = [
'extends' => 'Relationship',
];
$customGroup = $this->customGroupCreate($params);
$customFieldId = $this->customFieldCreate(['custom_group_id' => $customGroup['id']])['id'];
$contactID_a = $this->individualCreate();
$contactID_b = $this->individualCreate();
$relationship = $this->callAPISuccess('Relationship', 'create', [
'contact_id_a' => $contactID_a,
'contact_id_b' => $contactID_b,
'relationship_type_id' => 2,
'is_active' => 1,
"custom_{$customFieldId}" => 'testvalue',
]);
$params = [
[
0 => 'relation_type_id',
1 => 'IN',
2 =>
[
0 => '2_a_b',
],
3 => 0,
4 => 0,
],
[
0 => "custom_{$customFieldId}",
1 => '=',
2 => 'testvalue',
3 => 0,
4 => 0,
],
];

$query = new CRM_Contact_BAO_Query($params);
$dao = $query->searchQuery();
$this->assertEquals('2', $dao->N);
$this->callAPISuccess('Relationship', 'delete', ['id' => $relationship['id']]);
$this->callAPISuccess('Contact', 'delete', ['id' => $contactID_a, 'skip_undelete' => 1]);
$this->callAPISuccess('Contact', 'delete', ['id' => $contactID_b, 'skip_undelete' => 1]);
$this->callAPISuccess('CustomField', 'delete', ['id' => $customFieldId, 'skip_undelete' => 1]);
$this->callAPISuccess('CustomGroup', 'delete', ['id' => $customGroup]);
}

/**
* @throws \CRM_Core_Exception
*/
Expand Down