added unit test
authordeb.monish <monish.deb@jmaconsulting.biz>
Thu, 29 Mar 2018 13:26:54 +0000 (18:56 +0530)
committereileen <emcnaughton@wikimedia.org>
Tue, 15 May 2018 07:55:42 +0000 (19:55 +1200)
CRM/Contact/Selector.php
tests/phpunit/CRM/Contact/SelectorTest.php

index a4145e062044d80c45fb6f21a4720d9497d23078..2efc534d0f7dbacd545647fe39d0c41596087796 100644 (file)
@@ -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
       ) &&
index 200f07b143027277d685aae1572fa69c072d2450..a949df54207f230e6b5b8959565c3a873c6e8d66 100644 (file)
@@ -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.
    */