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 d3479ea1d0f18a738cbda2903a602f3dd28996ac..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
@@ -247,6 +305,21 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
     CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
   }
 
+  /**
+   * Test searches are case insensitive.
+   */
+  public function testCaseInsensitive() {
+    $orgID = $this->organizationCreate(array('organization_name' => 'BOb'));
+    $this->callAPISuccess('Contact', 'create', array('display_name' => 'Minnie Mouse', 'employer_id' => $orgID, 'contact_type' => 'Individual'));
+    $searchParams = array(array('current_employer', '=', 'bob', 0, 1));
+    $query = new CRM_Contact_BAO_Query($searchParams);
+    $result = $query->apiQuery($searchParams);
+    $this->assertEquals(1, count($result[0]));
+    $contact = reset($result[0]);
+    $this->assertEquals('Minnie Mouse', $contact['display_name']);
+    $this->assertEquals('BOb', $contact['current_employer']);
+  }
+
   /**
    * Test smart groups with non-numeric don't fail on equal queries.
    *
@@ -315,4 +388,34 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * CRM-19562 ensure that only ids are used for contact_id searching.
+   */
+  public function testContactIDClause() {
+    $params = array(
+      array("mark_x_2", "=", 1, 0, 0),
+      array("mark_x_foo@example.com", "=", 1, 0, 0),
+    );
+    $returnProperties = array(
+      "sort_name" => 1,
+      "email" => 1,
+      "do_not_email" => 1,
+      "is_deceased" => 1,
+      "on_hold" => 1,
+      "display_name" => 1,
+      "preferred_mail_format" => 1,
+    );
+    $numberofContacts = 2;
+    $query = new CRM_Contact_BAO_Query($params, $returnProperties);
+    try {
+      $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
+    }
+    catch (Exception $e) {
+      $this->assertEquals("A fatal error was triggered: One of parameters  (value: foo@example.com) is not of the type Positive",
+        $e->getMessage());
+      return $this->assertTrue(TRUE);
+    }
+    return $this->fail('Test failed for some reason which is not good');
+  }
+
 }