Merge pull request #9295 from JMAConsulting/CRM-19540
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTest.php
index 8ea30e2eb1947eedbd6f30057bbbc00c8cc85a77..480cc9355a6b9862794df9da93f612c87693629b 100644 (file)
@@ -115,6 +115,46 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     );
   }
 
+  /**
+   * Test for international string acceptance (CRM-10210).
+   *
+   * @dataProvider getInternationalStrings
+   *
+   * @param string $string
+   *   String to be tested.
+   *
+   * @throws \Exception
+   */
+  public function testInternationalStrings($string) {
+    $this->callAPISuccess('Contact', 'create', array_merge(
+      $this->_params,
+      array('first_name' => $string)
+    ));
+    $result = $this->callAPISuccessGetSingle('Contact', array('first_name' => $string));
+    $this->assertEquals($string, $result['first_name']);
+
+    $organizationParams = array(
+      'organization_name' => $string,
+      'contact_type' => 'Organization',
+    );
+
+    $this->callAPISuccess('Contact', 'create', $organizationParams);
+    $result = $this->callAPISuccessGetSingle('Contact', $organizationParams);
+    $this->assertEquals($string, $result['organization_name']);
+  }
+
+  /**
+   * Get international string data for testing against api calls.
+   */
+  public function getInternationalStrings() {
+    $invocations = array();
+    $invocations[] = array('Scarabée');
+    $invocations[] = array('Iñtërnâtiônàlizætiøn');
+    $invocations[] = array('これは日本語のテキストです。読めますか');
+    $invocations[] = array('देखें हिन्दी कैसी नजर आती है। अरे वाह ये तो नजर आती है।');
+    return $invocations;
+  }
+
   /**
    * Test civicrm_contact_create.
    *
@@ -159,6 +199,45 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->assertEquals(array('Student', 'Staff'), $contact['values'][$cid]['contact_sub_type']);
   }
 
+  /**
+   * Verify that we can retreive contacts of different sub types
+   */
+  public function testGetMultipleContactSubTypes() {
+
+    // This test presumes that there are no parents or students in the dataset
+
+    // create a student
+    $student = $this->callAPISuccess('contact', 'create', array(
+      'email' => 'student@example.com',
+      'contact_type' => 'Individual',
+      'contact_sub_type' => 'Student',
+    ));
+
+    // create a parent
+    $parent = $this->callAPISuccess('contact', 'create', array(
+      'email' => 'parent@example.com',
+      'contact_type' => 'Individual',
+      'contact_sub_type' => 'Parent',
+    ));
+
+    // create a parent
+    $contact = $this->callAPISuccess('contact', 'create', array(
+      'email' => 'parent@example.com',
+      'contact_type' => 'Individual',
+    ));
+
+    // get all students and parents
+    $getParams = array('contact_sub_type' => array('IN' => array('Parent', 'Student')));
+    $result = civicrm_api3('contact', 'get', $getParams);
+
+    // check that we retrieved the student and the parent
+    $this->assertArrayHasKey($student['id'], $result['values']);
+    $this->assertArrayHasKey($parent['id'], $result['values']);
+    $this->assertEquals(2, $result['count']);
+
+  }
+
+
   /**
    * Verify that attempt to create contact with empty params fails.
    */
@@ -217,21 +296,39 @@ class api_v3_ContactTest extends CiviUnitTestCase {
    * Verify that attempt to create individual contact with only an email succeeds.
    */
   public function testCreateEmailIndividual() {
-
+    $primaryEmail = 'man3@yahoo.com';
+    $notPrimaryEmail = 'man4@yahoo.com';
     $params = array(
-      'email' => 'man3@yahoo.com',
+      'email' => $primaryEmail,
       'contact_type' => 'Individual',
       'location_type_id' => 1,
     );
 
-    $contact = $this->callAPISuccess('contact', 'create', $params);
+    $contact1 = $this->callAPISuccess('contact', 'create', $params);
 
-    $this->assertEquals(3, $contact['id']);
-    $email = $this->callAPISuccess('email', 'get', array('contact_id' => $contact['id']));
-    $this->assertEquals(1, $email['count']);
-    $this->assertEquals('man3@yahoo.com', $email['values'][$email['id']]['email']);
+    $this->assertEquals(3, $contact1['id']);
+    $email1 = $this->callAPISuccess('email', 'get', array('contact_id' => $contact1['id']));
+    $this->assertEquals(1, $email1['count']);
+    $this->assertEquals($primaryEmail, $email1['values'][$email1['id']]['email']);
 
-    $this->callAPISuccess('contact', 'delete', $contact);
+    $email2 = $this->callAPISuccess('email', 'create', array('contact_id' => $contact1['id'], 'is_primary' => 0, 'email' => $notPrimaryEmail));
+
+    // Case 1: Check with criteria primary 'email' => array('IS NOT NULL' => 1)
+    $result = $this->callAPISuccess('contact', 'get', array('email' => array('IS NOT NULL' => 1)));
+    $primaryEmailContactIds = array_keys($result['values']);
+    $this->assertEquals($primaryEmail, $email1['values'][$email1['id']]['email']);
+
+    // Case 2: Check with criteria primary 'email' => array('<>' => '')
+    $result = $this->callAPISuccess('contact', 'get', array('email' => array('<>' => '')));
+    $primaryEmailContactIds = array_keys($result['values']);
+    $this->assertEquals($primaryEmail, $email1['values'][$email1['id']]['email']);
+
+    // Case 3: Check with email_id='primary email id'
+    $result = $this->callAPISuccess('contact', 'get', array('email_id' => $email1['id']));
+    $this->assertEquals(1, $result['count']);
+    $this->assertEquals($contact1['id'], $result['id']);
+
+    $this->callAPISuccess('contact', 'delete', $contact1);
   }
 
   /**
@@ -2191,6 +2288,24 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccess('contact', 'create', $params);
   }
 
+  /**
+   * Test that delete with skip undelete respects permissions.
+   */
+  public function testContactDeletePermissions() {
+    $contactID = $this->individualCreate();
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
+    $this->callAPIFailure('Contact', 'delete', array(
+      'id' => $contactID,
+      'check_permissions' => 1,
+      'skip_undelete' => 1,
+    ));
+    $this->callAPISuccess('Contact', 'delete', array(
+      'id' => $contactID,
+      'check_permissions' => 0,
+      'skip_undelete' => 1,
+    ));
+  }
+
   /**
    * Test update with check permissions set.
    */
@@ -2909,4 +3024,63 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccess('Setting', 'create', array('contact_undelete' => TRUE));
   }
 
+  /**
+   * Ensure format with return=group shows comma-separated group IDs.
+   *
+   * CRM-19426
+   */
+  public function testContactGetReturnGroup() {
+    // Set up a contact, asser that they were created.
+    $contact_params = array(
+      'contact_type' => 'Individual',
+      'first_name' => 'Test',
+      'last_name' => 'Groupmember',
+      'email' => 'test@example.org',
+    );
+    $create_contact = $this->callApiSuccess('Contact', 'create', $contact_params);
+    $this->assertEquals(0, $create_contact['is_error']);
+    $this->assertInternalType('int', $create_contact['id']);
+
+    $created_contact_id = $create_contact['id'];
+
+    // Set up multiple groups, add the contact to the groups.
+    $test_groups = array('Test group A', 'Test group B');
+    foreach ($test_groups as $title) {
+      // Use this contact as group owner, since we know they exist.
+      $group_params = array(
+        'title' => $title,
+        'created_id' => $created_contact_id,
+      );
+      $create_group = $this->callApiSuccess('Group', 'create', $group_params);
+      $this->assertEquals(0, $create_group['is_error']);
+      $this->assertInternalType('int', $create_group['id']);
+
+      $created_group_ids[] = $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);
+      $this->assertEquals(0, $create_group_contact['is_error']);
+      $this->assertInternalType('int', $create_group_contact['added']);
+    }
+
+    // Use the Contact,get API to retrieve the contact
+    $contact_get_params = array(
+      'id' => $created_contact_id,
+      'return' => 'group',
+    );
+    $contact_get = $this->callApiSuccess('Contact', 'get', $contact_get_params);
+    $this->assertInternalType('array', $contact_get['values'][$created_contact_id]);
+    $this->assertInternalType('string', $contact_get['values'][$created_contact_id]['groups']);
+
+    // Ensure they are shown as being in each created group.
+    $contact_group_ids = explode(',', $contact_get['values'][$created_contact_id]['groups']);
+    foreach ($created_group_ids as $created_group_id) {
+      $this->assertContains($created_group_id, $contact_group_ids);
+    }
+  }
+
 }