From: Seamus Lee Date: Tue, 8 Jan 2019 01:18:10 +0000 (+1100) Subject: (dev/core#647) Fix Up Contact Selector Test by changing class name to get test to... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1d17e4fc0b43398b4b5f3f0722ba4d9380073f83;p=civicrm-core.git (dev/core#647) Fix Up Contact Selector Test by changing class name to get test to run and make changes following code changes that affect clauses and calling get rows CRM_Contact_SelectorTest - Fix escaping (dev/core#217) CRM_Contact_SelectorTest - Remove stale/unnecessary assertion The table `civicrm_prevnext_cache` includes three columns (`entity_id1`, `entity_id2`, `entity_table`). These columns have some use-cases (e.g. for deduping) and some historical value, but the only one needed for caching search-results is `entity_id1`. dev/core#217 introduced an interface `CRM_Core_PrevNextCache_Interface` for caching search-results. This interface only passes through a list of CID's (effectively `entity_id1`); because we don't need `entity_id2` and `entity_table`, it makes no provisions for passing them through, and they're not saved. This simplifies the interface and makes it more realistic to implement alternative prevnext storage mechanisms. --- diff --git a/tests/phpunit/CRM/Contact/SelectorTest.php b/tests/phpunit/CRM/Contact/SelectorTest.php index ebee22500c..2e3bc97fc8 100644 --- a/tests/phpunit/CRM/Contact/SelectorTest.php +++ b/tests/phpunit/CRM/Contact/SelectorTest.php @@ -35,7 +35,7 @@ * @package CiviCRM * @group headless */ -class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { +class CRM_Contact_SelectorTest extends CiviUnitTestCase { public function tearDown() { @@ -140,22 +140,21 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { $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, ]; @@ -201,7 +200,7 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { '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)", ), ), ), @@ -220,7 +219,7 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { '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)", ), ), ), @@ -240,7 +239,7 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { '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)", ), ), ), @@ -355,13 +354,13 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { $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']); @@ -383,7 +382,7 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { $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 ]; @@ -408,7 +407,7 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { $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)); } @@ -465,6 +464,8 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { $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']]); } /** @@ -477,7 +478,7 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { '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', [ @@ -517,7 +518,7 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { // 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)); }