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

[NFC][Test] Preliminary cleanup #17213

Merged
merged 1 commit into from
May 1, 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
65 changes: 40 additions & 25 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ public function testSearch($fv, $count, $ids) {
*/
public function testSearchProfileHomeCityCRM14263() {
$contactID = $this->individualCreate();
CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
Civi::settings()->set('defaultSearchProfileID', 1);
$this->callAPISuccess('address', 'create', [
'contact_id' => $contactID,
'city' => 'Cool City',
Expand Down Expand Up @@ -228,7 +228,7 @@ public function testSearchProfileHomeCityCRM14263() {
}
catch (PEAR_Exception $e) {
$err = $e->getCause();
$this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
$this->fail('invalid SQL created' . $e->getMessage() . ' ' . $err->userinfo);

}
}
Expand All @@ -241,7 +241,7 @@ public function testSearchProfileHomeCityCRM14263() {
*/
public function testSearchProfileHomeCityNoResultsCRM14263() {
$contactID = $this->individualCreate();
CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
Civi::settings()->set('defaultSearchProfileID', 1);
$this->callAPISuccess('address', 'create', [
'contact_id' => $contactID,
'city' => 'Cool City',
Expand Down Expand Up @@ -269,7 +269,7 @@ public function testSearchProfileHomeCityNoResultsCRM14263() {
}
catch (PEAR_Exception $e) {
$err = $e->getCause();
$this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
$this->fail('invalid SQL created' . $e->getMessage() . ' ' . $err->userinfo);

}
}
Expand Down Expand Up @@ -388,7 +388,7 @@ public function testSearchOtherLocationUpperLower() {
*/
public function testSearchProfilePrimaryCityCRM14263($params, $selectClause, $whereClause) {
$contactID = $this->individualCreate();
CRM_Core_Config::singleton()->defaultSearchProfileID = 1;
Civi::settings()->set('defaultSearchProfileID', 1);
$this->callAPISuccess('address', 'create', [
'contact_id' => $contactID,
'city' => 'Cool CITY',
Expand All @@ -400,7 +400,7 @@ public function testSearchProfilePrimaryCityCRM14263($params, $selectClause, $wh
'contact_sub_type' => 1,
'sort_name' => 1,
];
$expectedSQL = "SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, civicrm_address.id as address_id, " . $selectClause . " FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) WHERE ( ( " . $whereClause . " ) ) AND (contact_a.is_deleted = 0) ORDER BY `contact_a`.`sort_name` ASC, `contact_a`.`id` ";
$expectedSQL = 'SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, civicrm_address.id as address_id, ' . $selectClause . " FROM civicrm_contact contact_a LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 ) WHERE ( ( " . $whereClause . " ) ) AND (contact_a.is_deleted = 0) ORDER BY `contact_a`.`sort_name` ASC, `contact_a`.`id` ";
$queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
try {
$this->assertLike($expectedSQL, $queryObj->getSearchSQL());
Expand All @@ -411,7 +411,7 @@ public function testSearchProfilePrimaryCityCRM14263($params, $selectClause, $wh
}
catch (PEAR_Exception $e) {
$err = $e->getCause();
$this->fail('invalid SQL created' . $e->getMessage() . " " . $err->userinfo);
$this->fail('invalid SQL created' . $e->getMessage() . ' ' . $err->userinfo);

}
}
Expand All @@ -423,15 +423,15 @@ public function getSearchProfileData() {
return [
[
[['city', '=', 'Cool City', 1, 0]],
"civicrm_address.city as `city`",
'civicrm_address.city as `city`',
"civicrm_address.city = 'Cool City'",
],
[
// Note that in the query 'long street' is lower cased. We eventually want to change that & not mess with the vars - it turns out
// it doesn't work on some charsets. However, the the lcasing affects more vars & we are looking to stagger removal of lcasing 'in case'
// (although we have been removing without blowback since 2017)
[['street_address', '=', 'Long Street', 1, 0]],
"civicrm_address.street_address as `street_address`",
'civicrm_address.street_address as `street_address`',
"civicrm_address.street_address LIKE '%Long Street%'",
],
];
Expand All @@ -454,7 +454,7 @@ public function testGroupContactCacheAddSearch() {
TRUE, FALSE
);

list($select) = $query->query(FALSE);
list($select) = $query->query();
$this->assertEquals('SELECT contact_a.id as contact_id', $select);
}

Expand Down Expand Up @@ -486,7 +486,7 @@ public function testNumericPostal() {
TRUE, FALSE
);

$sql = $query->query(FALSE);
$sql = $query->query();
$result = CRM_Core_DAO::executeQuery(implode(' ', $sql));
$this->assertEquals(2, $result->N);

Expand Down Expand Up @@ -653,13 +653,15 @@ public function testNonReciprocalRelationshipTargetGroupIsCorrectResults() {

$query = new CRM_Contact_BAO_Query($params);
$dao = $query->searchQuery();
$this->assertEquals('1', $dao->N, "Search query returns exactly 1 result?");
$this->assertTrue($dao->fetch(), "Search query returns success?");
$this->assertEquals($contactID_b, $dao->contact_id, "Search query returns parent of contact A?");
$this->assertEquals('1', $dao->N, 'Search query returns exactly 1 result?');
$this->assertTrue($dao->fetch(), 'Search query returns success?');
$this->assertEquals($contactID_b, $dao->contact_id, 'Search query returns parent of contact A?');
}

/**
* Relationship search with custom fields.
*
* @throws \CRM_Core_Exception
*/
public function testReciprocalRelationshipWithCustomFields() {
$params = [
Expand Down Expand Up @@ -751,11 +753,16 @@ public function testReciprocalRelationshipTargetGroupIsCorrectResults() {

$query = new CRM_Contact_BAO_Query($params);
$dao = $query->searchQuery();
$this->assertEquals('1', $dao->N, "Search query returns exactly 1 result?");
$this->assertTrue($dao->fetch(), "Search query returns success?");
$this->assertEquals($contactID_b, $dao->contact_id, "Search query returns spouse of contact A?");
$this->assertEquals('1', $dao->N, 'Search query returns exactly 1 result?');
$this->assertTrue($dao->fetch(), 'Search query returns success?');
$this->assertEquals($contactID_b, $dao->contact_id, 'Search query returns spouse of contact A?');
}

/**
* Test correct temporary table in reciprocal relationship search.
*
* @throws \CRM_Core_Exception
*/
public function testReciprocalRelationshipTargetGroupUsesTempTable() {
$groupID = $this->groupCreate();
$params = [
Expand All @@ -781,7 +788,7 @@ public function testReciprocalRelationshipTargetGroupUsesTempTable() {
],
];
$sql = CRM_Contact_BAO_Query::getQuery($params);
$this->assertContains('INNER JOIN civicrm_tmp_e', $sql, "Query appears to use temporary table of compiled relationships?", TRUE);
$this->assertContains('INNER JOIN civicrm_tmp_e', $sql, 'Query appears to use temporary table of compiled relationships?', TRUE);
}

/**
Expand All @@ -802,8 +809,8 @@ public function testRelationshipPermissionClause() {
*/
public function testRelationshipClause() {
$today = date('Ymd');
$from1 = " FROM civicrm_contact contact_a LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id ) LEFT JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )";
$from2 = " FROM civicrm_contact contact_a LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id ) LEFT JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )";
$from1 = ' FROM civicrm_contact contact_a LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_a = contact_a.id ) LEFT JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_b = contact_b.id )';
$from2 = ' FROM civicrm_contact contact_a LEFT JOIN civicrm_relationship ON (civicrm_relationship.contact_id_b = contact_a.id ) LEFT JOIN civicrm_contact contact_b ON (civicrm_relationship.contact_id_a = contact_b.id )';
$where1 = "WHERE ( (
civicrm_relationship.is_active = 1 AND
( civicrm_relationship.end_date IS NULL OR civicrm_relationship.end_date >= {$today} ) AND
Expand All @@ -822,7 +829,7 @@ public function testRelationshipClause() {
TRUE,
TRUE, FALSE
);
$sql1 = $query1->query(FALSE);
$sql1 = $query1->query();
$this->assertLike($from1, $sql1[1]);
$this->assertLike($where1, $sql1[2]);
// Test single relationship type selected in multiple select.
Expand Down Expand Up @@ -855,7 +862,7 @@ public function testRelationshipClause() {
TRUE,
TRUE, FALSE
);
$sql4 = $query4->query(FALSE);
$sql4 = $query4->query();
$this->assertLike($from1, $sql4[1]);
$this->assertLike($where2, $sql4[2]);

Expand Down Expand Up @@ -960,8 +967,6 @@ public function testGroupClause() {

/**
* CRM-19562 ensure that only ids are used for contact_id searching.
*
* @throws \CRM_Core_Exception
*/
public function testContactIDClause() {
$params = [
Expand Down Expand Up @@ -1123,6 +1128,11 @@ public function testGetSummaryQueryWithFinancialACLEnabled() {
* Test relative date filters to ensure they generate correct SQL.
*
* @dataProvider relativeDateFilters
*
* @param string $filter
* @param string $expectedWhere
*
* @throws \CRM_Core_Exception
*/
public function testRelativeDateFilters($filter, $expectedWhere) {
$params = [['created_date_relative', '=', $filter, 0, 0]];
Expand All @@ -1141,6 +1151,11 @@ public function testRelativeDateFilters($filter, $expectedWhere) {
$this->assertEquals($expectedWhere, $where);
}

/**
* Data provider to relative date filter configurations.
*
* @return array
*/
public function relativeDateFilters() {
$dataProvider[] = ['this.year', "WHERE ( contact_a.created_date BETWEEN 'date0' AND 'date1' ) AND (contact_a.is_deleted = 0)"];
$dataProvider[] = ['greater.day', "WHERE ( contact_a.created_date >= 'date0' ) AND (contact_a.is_deleted = 0)"];
Expand Down Expand Up @@ -1217,7 +1232,7 @@ public function testQillOptions() {
*/
public function testGenericWhereHandling() {
$query = new CRM_Contact_BAO_Query([['suffix_id', '=', 2, 0]]);
$this->assertEquals("contact_a.suffix_id = 2", $query->_where[0][0]);
$this->assertEquals('contact_a.suffix_id = 2', $query->_where[0][0]);
$this->assertEquals('Individual Suffix = Sr.', $query->_qill[0][0]);
$this->assertNotTrue(isset($query->_tables['civicrm_activity']));

Expand Down
Loading