From 2eb3ff3ebf60986ce975c95784d408488f1fa669 Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Thu, 29 Mar 2018 18:56:54 +0530 Subject: [PATCH] added unit test --- CRM/Contact/Selector.php | 10 +++- tests/phpunit/CRM/Contact/SelectorTest.php | 62 ++++++++++++++++++++++ 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index a4145e0620..2efc534d0f 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -239,6 +239,15 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se $this->_options = &$this->_query->_options; } + /** + * This method set cache key, later used in test environment + * + * @param string $key + */ + public function setKey($key) { + $this->_key = $key; + } + /** * This method returns the links that are given for each search row. * currently the links added for each row are @@ -545,7 +554,6 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se * the total number of rows for this action */ public function &getRows($action, $offset, $rowCount, $sort, $output = NULL) { - if (($output == CRM_Core_Selector_Controller::EXPORT || $output == CRM_Core_Selector_Controller::SCREEN ) && diff --git a/tests/phpunit/CRM/Contact/SelectorTest.php b/tests/phpunit/CRM/Contact/SelectorTest.php index 200f07b143..a949df5420 100644 --- a/tests/phpunit/CRM/Contact/SelectorTest.php +++ b/tests/phpunit/CRM/Contact/SelectorTest.php @@ -82,6 +82,68 @@ class CRM_Contact_Form_SelectorTest extends CiviUnitTestCase { } } + /** + * Test the civicrm_prevnext_cache entry if it correctly stores the search query result + */ + public function testPrevNextCache() { + $contactID = $this->individualCreate(['email' => 'mickey@mouseville.com']); + $dataSet = array( + 'description' => 'Normal default behaviour', + 'class' => 'CRM_Contact_Selector', + 'settings' => array(), + 'form_values' => array('email' => 'mickey@mouseville.com'), + 'params' => array(), + 'return_properties' => NULL, + 'context' => 'advanced', + 'action' => CRM_Core_Action::ADVANCED, + 'includeContactIds' => NULL, + 'searchDescendentGroups' => FALSE, + 'expected_query' => array( + 0 => 'default', + 1 => 'default', + 2 => "WHERE ( civicrm_email.email LIKE '%mickey@mouseville.com%' ) AND (contact_a.is_deleted = 0)", + ), + ); + $params = CRM_Contact_BAO_Query::convertFormValues($dataSet['form_values'], 0, FALSE, NULL, array()); + + // create CRM_Contact_Selector instance and set desired query params + $selector = new CRM_Contact_Selector( + $dataSet['class'], + $dataSet['form_values'], + $params, + $dataSet['return_properties'], + $dataSet['action'], + $dataSet['includeContactIds'], + $dataSet['searchDescendentGroups'], + $dataSet['context'] + ); + // set cache key + $key = substr(sha1(rand()), 0, 7); + $selector->setKey($key); + + // fetch row and check the result + $rows = $selector->getRows(CRM_Core_Action::VIEW, 0, TRUE, 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) + ->execute() + ->fetchAll(); + $this->assertEquals(1, count($contacts)); + // check the prevNext record matches + $expectedEntry = [ + 'entity_table' => 'civicrm_contact', + 'entity_id1' => $contactID, + 'cacheKey' => $cacheKey, + ]; + $this->checkArrayEquals($contacts[0], $expectedEntry); + } + /** * Data sets for testing. */ -- 2.25.1