CRM-4287: Contact search for email address shows only primary email matches as results
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / QueryTest.php
index 8bffbf8c80d4c562ed7bde7373699ae1490176b5..965a3a8f8bef082bf1da1b14647a959c78b59b8b 100644 (file)
@@ -31,8 +31,10 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
   }
 
   /**
-   *  Test CRM_Contact_BAO_Query::searchQuery()
+   *  Test CRM_Contact_BAO_Query::searchQuery().
+   *
    * @dataProvider dataProvider
+   *
    * @param $fv
    * @param $count
    * @param $ids
@@ -149,6 +151,62 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * Test searchByPrimaryEmailOnly setting.
+   */
+  public function testSearchByPrimaryEmailOnly() {
+    $contactID = $this->individualCreate();
+    $params = array(
+      'contact_id' => $contactID,
+      'email' => 'primary@example.com',
+      'is_primary' => 1,
+    );
+    $this->callAPISuccess('email', 'create', $params);
+
+    unset($params['is_primary']);
+    $params['email'] = 'secondary@team.com';
+    $this->callAPISuccess('email', 'create', $params);
+
+    foreach (array(0, 1) as $searchPrimary) {
+      Civi::settings()->set('searchPrimaryEmailOnly', $searchPrimary);
+
+      $params = array(
+        0 => array(
+          0 => 'email',
+          1 => 'LIKE',
+          2 => 'secondary@example.com',
+          3 => 0,
+          4 => 1,
+        ),
+      );
+      $returnProperties = array(
+        'contact_type' => 1,
+        'contact_sub_type' => 1,
+        'sort_name' => 1,
+      );
+
+      $queryObj = new CRM_Contact_BAO_Query($params, $returnProperties);
+      $resultDAO = $queryObj->searchQuery(0, 0, NULL,
+        FALSE, FALSE,
+        FALSE, FALSE,
+        FALSE);
+
+      if ($searchPrimary) {
+        $this->assertEquals($resultDAO->N, 0);
+      }
+      else {
+        //Assert secondary email gets included in search results.
+        while ($resultDAO->fetch()) {
+          $this->assertEquals('secondary@example.com', $resultDAO->email);
+        }
+      }
+
+      // API should always return primary email.
+      $result = $this->callAPISuccess('Contact', 'get', array('contact_id' => $contactID));
+      $this->assertEquals('primary@example.com', $result['values'][$contactID]['email']);
+    }
+  }
+
   /**
    * CRM-14263 search builder failure with search profile & address in criteria
    * We are retrieving primary here - checking the actual sql seems super prescriptive - but since the massive query object has
@@ -331,7 +389,7 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
   }
 
   /**
-   * CRM-19562 ensure that only ids are used for contactid searching.
+   * CRM-19562 ensure that only ids are used for contact_id searching.
    */
   public function testContactIDClause() {
     $params = array(