Towards CRM-20155 remove duplicate calls to dedupe functions.
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTest.php
index 6bbcacc7e6adeb5947c40d9bf030ef75a31e489c..406d328fbb52831363de02ca5c7b680de865b4f0 100644 (file)
@@ -984,8 +984,6 @@ class api_v3_ContactTest extends CiviUnitTestCase {
    */
   public function testGetGroupIDFromContact() {
     $groupId = $this->groupCreate();
-    $description = "Get all from group and display contacts.";
-    $subFile = "GroupFilterUsingContactAPI";
     $params = array(
       'email' => 'man2@yahoo.com',
       'contact_type' => 'Individual',
@@ -999,7 +997,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
       'filter.group_id' => $groupId,
       'contact_type' => 'Individual',
     );
-    $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
+    $result = $this->callAPISuccess('contact', 'get', $params);
     $this->assertEquals(1, $result['count']);
     // group 26 doesn't exist, but we can still search contacts in it.
     $params = array(
@@ -1012,7 +1010,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
       'filter.group_id' => "$groupId, 26",
       'contact_type' => 'Individual',
     );
-    $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
+    $result = $this->callAPISuccess('contact', 'get', $params);
     $this->assertEquals(1, $result['count']);
     $params = array(
       'filter.group_id' => "26,27",
@@ -1025,7 +1023,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
       'filter.group_id' => array($groupId, 26),
       'contact_type' => 'Individual',
     );
-    $result = $this->callAPIAndDocument('contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subFile);
+    $result = $this->callAPISuccess('contact', 'get', $params);
     $this->assertEquals(1, $result['count']);
 
     //test in conjunction with other criteria
@@ -1648,6 +1646,40 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccess('contact', 'delete', $contact);
   }
 
+  /**
+   * Ensure consistent return format for option group fields.
+   */
+  public function testPseudoFields() {
+    $params = array(
+      'preferred_communication_method' => array('Phone', 'SMS'),
+      'preferred_language' => 'en_US',
+      'gender_id' => 'Female',
+      'prefix_id' => 'Mrs.',
+      'suffix_id' => 'II',
+      'communication_style_id' => 'Formal',
+    );
+
+    $contact = $this->callAPISuccess('contact', 'create', array_merge($this->_params, $params));
+
+    $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contact['id']));
+    $this->assertEquals('Both', $result['preferred_mail_format']);
+
+    $this->assertEquals('en_US', $result['preferred_language']);
+    $this->assertEquals(1, $result['communication_style_id']);
+    $this->assertEquals(1, $result['gender_id']);
+    $this->assertEquals('Female', $result['gender']);
+    $this->assertEquals('Mrs.', $result['individual_prefix']);
+    $this->assertEquals(1, $result['prefix_id']);
+    $this->assertEquals('II', $result['individual_suffix']);
+    $this->assertEquals(CRM_Core_PseudoConstant::getKey("CRM_Contact_BAO_Contact", 'suffix_id', 'II'), $result['suffix_id']);
+    $this->callAPISuccess('contact', 'delete', $contact);
+    $this->assertEquals(array(
+      CRM_Core_PseudoConstant::getKey("CRM_Contact_BAO_Contact", 'preferred_communication_method', 'Phone'),
+      CRM_Core_PseudoConstant::getKey("CRM_Contact_BAO_Contact", 'preferred_communication_method', 'SMS'),
+    ), $result['preferred_communication_method']);
+  }
+
+
   /**
    * Test birth date parameters.
    *
@@ -2959,19 +2991,15 @@ class api_v3_ContactTest extends CiviUnitTestCase {
    * Test the duplicate check function.
    */
   public function testDuplicateCheck() {
-    $this->callAPISuccess('Contact', 'create', array(
+    $harry = array(
       'first_name' => 'Harry',
       'last_name' => 'Potter',
       'email' => 'harry@hogwarts.edu',
       'contact_type' => 'Individual',
-    ));
+    );
+    $this->callAPISuccess('Contact', 'create', $harry);
     $result = $this->callAPISuccess('Contact', 'duplicatecheck', array(
-      'match' => array(
-        'first_name' => 'Harry',
-        'last_name' => 'Potter',
-        'email' => 'harry@hogwarts.edu',
-        'contact_type' => 'Individual',
-      ),
+      'match' => $harry,
     ));
 
     $this->assertEquals(1, $result['count']);
@@ -2984,6 +3012,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
       ),
     ));
     $this->assertEquals(0, $result['count']);
+    $this->callAPIFailure('Contact', 'create', array_merge($harry, array('dupe_check' => 1)));
   }
 
   public function testGetByContactType() {
@@ -3166,4 +3195,184 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * CRM-20144 Verify that passing title of group works as well as id
+   * Tests the following formats
+   * contact.get group='title1'
+   * contact.get group=id1
+   */
+  public function testContactGetWithGroupTitle() {
+    // Set up a contact, asser that they were created.
+    $contact_params = array(
+      'contact_type' => 'Individual',
+      'first_name' => 'Test2',
+      'last_name' => 'Groupmember',
+      'email' => 'test@example.org',
+    );
+    $create_contact = $this->callApiSuccess('Contact', 'create', $contact_params);
+    $created_contact_id = $create_contact['id'];
+    // Set up multiple groups, add the contact to the groups.
+    $test_groups = array('Test group C', 'Test group D');
+    foreach ($test_groups as $title) {
+      $group_params = array(
+        'title' => $title,
+        'created_id' => $created_contact_id,
+      );
+      $create_group = $this->callApiSuccess('Group', 'create', $group_params);
+      $created_group_id = $create_group['id'];
+
+      // Add contact to the new group.
+      $group_contact_params = array(
+        'contact_id' => $created_contact_id,
+        'group_id' => $create_group['id'],
+      );
+      $create_group_contact = $this->callApiSuccess('GroupContact', 'create', $group_contact_params);
+      $contact_get = $this->callAPISuccess('contact', 'get', array('group' => $title, 'return' => 'group'));
+      $this->assertEquals(1, $contact_get['count']);
+      $this->assertEquals($created_contact_id, $contact_get['id']);
+      $contact_groups = explode(',', $contact_get['values'][$created_contact_id]['groups']);
+      $this->assertContains((string) $create_group['id'], $contact_groups);
+      $contact_get2 = $this->callAPISuccess('contact', 'get', array('group' => $created_group_id, 'return' => 'group'));
+      $this->assertEquals($created_contact_id, $contact_get2['id']);
+      $contact_groups2 = explode(',', $contact_get2['values'][$created_contact_id]['groups']);
+      $this->assertContains((string) $create_group['id'], $contact_groups2);
+      $this->callAPISuccess('group', 'delete', array('id' => $created_group_id));
+    }
+    $this->callAPISuccess('contact', 'delete', array('id' => $created_contact_id, 'skip_undelete' => TRUE));
+  }
+
+  /**
+   * CRM-20144 Verify that passing title of group works as well as id
+   * Tests the following formats
+   * contact.get group=array('title1', title1)
+   * contact.get group=array('IN' => array('title1', 'title2)
+   */
+  public function testContactGetWithGroupTitleMultipleGroups() {
+    $description = "Get all from group and display contacts.";
+    $subFile = "GroupFilterUsingContactAPI";
+    // Set up a contact, asser that they were created.
+    $contact_params = array(
+      'contact_type' => 'Individual',
+      'first_name' => 'Test2',
+      'last_name' => 'Groupmember',
+      'email' => 'test@example.org',
+    );
+    $create_contact = $this->callApiSuccess('Contact', 'create', $contact_params);
+    $created_contact_id = $create_contact['id'];
+    $createdGroupsTitles = $createdGroupsIds = array();
+    // Set up multiple groups, add the contact to the groups.
+    $test_groups = array('Test group C', 'Test group D');
+    foreach ($test_groups as $title) {
+      $group_params = array(
+        'title' => $title,
+        'created_id' => $created_contact_id,
+      );
+      $create_group = $this->callApiSuccess('Group', 'create', $group_params);
+      $created_group_id = $create_group['id'];
+      $createdGroupsIds[] = $create_group['id'];
+      $createdGroupTitles[] = $title;
+      // Add contact to the new group.
+      $group_contact_params = array(
+        'contact_id' => $created_contact_id,
+        'group_id' => $create_group['id'],
+      );
+      $create_group_contact = $this->callApiSuccess('GroupContact', 'create', $group_contact_params);
+    }
+    $contact_get = $this->callAPISuccess('contact', 'get', array('group' => $createdGroupTitles, 'return' => 'group'));
+    $this->assertEquals(1, $contact_get['count']);
+    $this->assertEquals($created_contact_id, $contact_get['id']);
+    $contact_groups = explode(',', $contact_get['values'][$created_contact_id]['groups']);
+    foreach ($createdGroupsIds as $id) {
+      $this->assertContains((string) $id, $contact_groups);
+    }
+    $contact_get2 = $this->callAPIAndDocument('contact', 'get', array('group' => array('IN' => $createdGroupTitles)), __FUNCTION__, __FILE__, $description, $subFile);
+    $contact_get2 = $this->callAPISuccess('contact', 'get', array('group' => array('IN' => $createdGroupTitles), 'return' => 'group'));
+    $this->assertEquals($created_contact_id, $contact_get2['id']);
+    $contact_groups2 = explode(',', $contact_get2['values'][$created_contact_id]['groups']);
+    foreach ($createdGroupsIds as $id) {
+      $this->assertContains((string) $id, $contact_groups2);
+    }
+    foreach ($createdGroupsIds as $id) {
+      $this->callAPISuccess('group', 'delete', array('id' => $id));
+    }
+    $this->callAPISuccess('contact', 'delete', array('id' => $created_contact_id, 'skip_undelete' => TRUE));
+  }
+
+  /**
+   * CRM-20144 Verify that passing title of group works as well as id
+   * Tests the following formats
+   * contact.get group=array('title1' => 1)
+   * contact.get group=array('titke1' => 1, 'title2' => 1)
+   * contact.get group=array('id1' => 1)
+   * contact.get group=array('id1' => 1, id2 => 1)
+   */
+  public function testContactGetWithGroupTitleMultipleGroupsLegacyFormat() {
+    // Set up a contact, asser that they were created.
+    $contact_params = array(
+      'contact_type' => 'Individual',
+      'first_name' => 'Test2',
+      'last_name' => 'Groupmember',
+      'email' => 'test@example.org',
+    );
+    $create_contact = $this->callApiSuccess('Contact', 'create', $contact_params);
+    $created_contact_id = $create_contact['id'];
+    $createdGroupsTitles = $createdGroupsIds = array();
+    // Set up multiple groups, add the contact to the groups.
+    $test_groups = array('Test group C', 'Test group D');
+    foreach ($test_groups as $title) {
+      $group_params = array(
+        'title' => $title,
+        'created_id' => $created_contact_id,
+      );
+      $create_group = $this->callApiSuccess('Group', 'create', $group_params);
+      $created_group_id = $create_group['id'];
+      $createdGroupsIds[] = $create_group['id'];
+      $createdGroupTitles[] = $title;
+      // Add contact to the new group.
+      $group_contact_params = array(
+        'contact_id' => $created_contact_id,
+        'group_id' => $create_group['id'],
+      );
+      $create_group_contact = $this->callApiSuccess('GroupContact', 'create', $group_contact_params);
+    }
+    $contact_get = $this->callAPISuccess('contact', 'get', array('group' => array($createdGroupTitles[0] => 1), 'return' => 'group'));
+    $this->assertEquals(1, $contact_get['count']);
+    $this->assertEquals($created_contact_id, $contact_get['id']);
+    $contact_groups = explode(',', $contact_get['values'][$created_contact_id]['groups']);
+    foreach ($createdGroupsIds as $id) {
+      $this->assertContains((string) $id, $contact_groups);
+    }
+    $contact_get2 = $this->callAPISuccess('contact', 'get', array('group' => array($createdGroupTitles[0] => 1, $createdGroupTitles[1] => 1), 'return' => 'group'));
+    $this->assertEquals(1, $contact_get2['count']);
+    $this->assertEquals($created_contact_id, $contact_get2['id']);
+    $contact_groups2 = explode(',', $contact_get2['values'][$created_contact_id]['groups']);
+    foreach ($createdGroupsIds as $id) {
+      $this->assertContains((string) $id, $contact_groups2);
+    }
+    $contact_get3 = $this->callAPISuccess('contact', 'get', array('group' => array($createdGroupsIds[0] => 1), 'return' => 'group'));
+    $this->assertEquals($created_contact_id, $contact_get3['id']);
+    $contact_groups3 = explode(',', $contact_get3['values'][$created_contact_id]['groups']);
+    foreach ($createdGroupsIds as $id) {
+      $this->assertContains((string) $id, $contact_groups3);
+    }
+    $contact_get4 = $this->callAPISuccess('contact', 'get', array('group' => array($createdGroupsIds[0] => 1, $createdGroupsIds[1] => 1), 'return' => 'group'));
+    $this->assertEquals($created_contact_id, $contact_get4['id']);
+    $contact_groups4 = explode(',', $contact_get4['values'][$created_contact_id]['groups']);
+    foreach ($createdGroupsIds as $id) {
+      $this->assertContains((string) $id, $contact_groups4);
+    }
+    foreach ($createdGroupsIds as $id) {
+      $this->callAPISuccess('group', 'delete', array('id' => $id));
+    }
+    $this->callAPISuccess('contact', 'delete', array('id' => $created_contact_id, 'skip_undelete' => TRUE));
+  }
+
+  public function testLoggedInUserAPISupportToken() {
+    $description = "Get contact id of the current logged in user";
+    $subFile = "ContactIDOfLoggedInUserContactAPI";
+    $cid = $this->createLoggedInUser();
+    $contact = $this->callAPIAndDocument('contact', 'get', array('id' => 'user_contact_id'), __FUNCTION__, __FILE__, $description, $subFile);
+    $this->assertEquals($cid, $contact['id']);
+  }
+
 }