Skip to content

Commit

Permalink
Merge pull request #13416 from seamuslee001/647_selector_test
Browse files Browse the repository at this point in the history
(dev/core#647) Fix Up Contact Selector Test by changing class name to…
  • Loading branch information
eileenmcnaughton authored Jan 9, 2019
2 parents 6ddd029 + 1d17e4f commit 44e2ee4
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions tests/phpunit/CRM/Contact/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @package CiviCRM
* @group headless
*/
class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase {
class CRM_Contact_SelectorTest extends CiviUnitTestCase {

public function tearDown() {

Expand Down Expand Up @@ -140,22 +140,21 @@ public function testPrevNextCache() {
$selector->setKey($key);

// fetch row and check the result
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, NULL);
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
$this->assertEquals(1, count($rows));
$this->assertEquals($contactID, key($rows));

// build cache key and use to it to fetch prev-next cache record
$cacheKey = 'civicrm search ' . $key;
$contacts = CRM_Utils_SQL_Select::from('civicrm_prevnext_cache')
->select(['entity_table', 'entity_id1', 'cacheKey'])
->where("cacheKey = '!key'")
->param('!key', $cacheKey)
->select(['entity_id1', 'cacheKey'])
->where("cacheKey = @key")
->param('key', $cacheKey)
->execute()
->fetchAll();
$this->assertEquals(1, count($contacts));
// check the prevNext record matches
$expectedEntry = [
'entity_table' => 'civicrm_contact',
'entity_id1' => $contactID,
'cacheKey' => $cacheKey,
];
Expand Down Expand Up @@ -201,7 +200,7 @@ public function querySets() {
'expected_query' => array(
0 => 'default',
1 => 'default',
2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE '%mouse%' ) OR ( civicrm_email.email LIKE '%mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE '%Mouse%' ) OR ( civicrm_email.email LIKE '%Mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
),
),
),
Expand All @@ -220,7 +219,7 @@ public function querySets() {
'expected_query' => array(
0 => 'default',
1 => 'default',
2 => "WHERE ( civicrm_email.email LIKE 'mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE 'mouse%' ) OR ( civicrm_email.email LIKE 'mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
2 => "WHERE ( civicrm_email.email LIKE 'mickey@mouseville.com%' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
),
),
),
Expand All @@ -240,7 +239,7 @@ public function querySets() {
'expected_query' => array(
0 => 'default',
1 => 'default',
2 => "WHERE ( civicrm_email.email = 'mickey@mouseville.com' AND ( ( ( contact_a.sort_name LIKE 'mouse%' ) OR ( civicrm_email.email LIKE 'mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
2 => "WHERE ( civicrm_email.email = 'mickey@mouseville.com' AND ( ( ( contact_a.sort_name LIKE 'Mouse%' ) OR ( civicrm_email.email LIKE 'Mouse%' ) ) ) ) AND (contact_a.is_deleted = 0)",
),
),
),
Expand Down Expand Up @@ -355,13 +354,13 @@ public function testSelectorQueryOnNonASCIIlocationType() {
$expectedQuery = [
0 => "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`, `Non_ASCII_Location_Type-location_type`.id as `Non_ASCII_Location_Type-location_type_id`, `Non_ASCII_Location_Type-location_type`.name as `Non_ASCII_Location_Type-location_type`, `Non_ASCII_Location_Type-email`.id as `Non_ASCII_Location_Type-email_id`, `Non_ASCII_Location_Type-email`.email as `Non_ASCII_Location_Type-email`",
// @TODO these FROM clause doesn't matches due to extra spaces or special character
2 => "WHERE ( ( LOWER(`Non_ASCII_Location_Type-email`.email) IS NOT NULL ) ) AND (contact_a.is_deleted = 0)",
2 => "WHERE ( ( `Non_ASCII_Location_Type-email`.email IS NOT NULL ) ) AND (contact_a.is_deleted = 0)",
];
foreach ($expectedQuery as $index => $queryString) {
$this->assertEquals($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
}

$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, NULL);
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
$this->assertEquals(1, count($rows));
$this->assertEquals($contactID, key($rows));
$this->assertEquals('test@test.com', $rows[$contactID]['Non_ASCII_Location_Type-email']);
Expand All @@ -383,7 +382,7 @@ public function testWhereClauseByOperator() {
$filtersByWhereClause = [
'IS NOT NULL' => '( contact_a.first_name IS NOT NULL )', // doesn't matter
'=' => "( contact_a.first_name = 'Adam' )", // case sensitive check
'LIKE' => "( contact_a.first_name LIKE '%ad%' )", // case insensitive check
'LIKE' => "( contact_a.first_name LIKE '%Ad%' )", // case insensitive check
'RLIKE' => "( contact_a.first_name RLIKE BINARY '^A[a-z]{3}$' )", // case sensitive check
'IN' => '( contact_a.first_name IN ("Adam") )', // case sensitive check
];
Expand All @@ -408,7 +407,7 @@ public function testWhereClauseByOperator() {
$sql = $selector->getQueryObject()->query();
$this->assertEquals(TRUE, strpos($sql[2], $filtersByWhereClause[$op]));

$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, NULL);
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
$this->assertEquals(1, count($rows));
$this->assertEquals($contactID, key($rows));
}
Expand Down Expand Up @@ -465,6 +464,8 @@ public function testSelectorQueryOrderByCustomField() {
$this->assertTrue(in_array($cgTableName, array_keys($query->_tables)));
//Assert if from clause joins the custom table.
$this->assertTrue(strpos($query->_fromClause, $cgTableName) !== FALSE);
$this->callAPISuccess('CustomField', 'delete', ['id' => $customField['id']]);
$this->callAPISuccess('CustomGroup', 'delete', ['id' => $customGroup['id']]);
}

/**
Expand All @@ -477,7 +478,7 @@ public function testCustomDateField() {
'title' => "test custom group",
'extends' => "Individual",
));
$customTableName = $this->callAPISuccess('CustomGroup', 'getValue', ['id' => $customGroup, 'return' => 'table_name']);
$customTableName = $this->callAPISuccess('CustomGroup', 'getValue', ['id' => $customGroup['id'], 'return' => 'table_name']);
$customGroupTableName = $customGroup['values'][$customGroup['id']]['table_name'];

$createdField = $this->callAPISuccess('customField', 'create', [
Expand Down Expand Up @@ -517,7 +518,7 @@ public function testCustomDateField() {
// test the presence of expected date clause
$this->assertEquals(TRUE, strpos($whereClause, $expectedClause));

$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, NULL);
$rows = $selector->getRows(CRM_Core_Action::VIEW, 0, 1, NULL);
$this->assertEquals(1, count($rows));
}

Expand Down

0 comments on commit 44e2ee4

Please sign in to comment.