Action schedule API modifications
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTest.php
index 37779fba56beb75d0afdb91d66056a3fac2dfe64..570696bd88059767dfee961d2296bfb93f6d484e 100644 (file)
@@ -87,6 +87,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
       'civicrm_website',
       'civicrm_relationship',
       'civicrm_uf_match',
+      'civicrm_phone',
     );
 
     $this->quickCleanup($tablesToTruncate, TRUE);
@@ -252,14 +253,20 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     );
 
     $contact = $this->callAPISuccess('contact', 'create', $params);
-    $this->assertEquals(1, $contact['id'], "In line " . __LINE__);
+    $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contact['id']));
+
+    $this->assertArrayKeyExists('prefix_id', $result);
+    $this->assertArrayKeyExists('suffix_id', $result);
+    $this->assertArrayKeyExists('gender_id', $result);
+    $this->assertEquals(4, $result['prefix_id']);
+    $this->assertEquals(1, $result['suffix_id']);
   }
 
   /**
    *  Verify that attempt to create individual contact with
    *  first and last names and old key values works
    */
-  function testCreateNameIndividualOldKeys2() {
+  function testCreateNameIndividualrecommendedKeys2() {
     $params = array(
       'prefix_id' => 'Dr.',
       'first_name' => 'abc1',
@@ -270,7 +277,13 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     );
 
     $contact = $this->callAPISuccess('contact', 'create', $params);
-    $this->assertEquals(1, $contact['id'], "In line " . __LINE__);
+    $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contact['id']));
+
+    $this->assertArrayKeyExists('prefix_id', $result);
+    $this->assertArrayKeyExists('suffix_id', $result);
+    $this->assertArrayKeyExists('gender_id', $result);
+    $this->assertEquals(4, $result['prefix_id']);
+    $this->assertEquals(1, $result['suffix_id']);
   }
 
   /**
@@ -321,7 +334,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $params['custom_' . $ids['custom_field_id']] = "custom string";
     $description = "/*this demonstrates setting a custom field through the API ";
     $subfile = "CustomFieldCreate";
-    $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description);
 
     $check = $this->callAPISuccess($this->_entity, 'get', array('return.custom_' . $ids['custom_field_id'] => 1, 'id' => $result['id']));
     $this->assertEquals("custom string", $check['values'][$check['id']]['custom_' . $ids['custom_field_id']], ' in line ' . __LINE__);
@@ -357,6 +370,10 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
       'current_employer' => 'new employer org',)
     ));
+    // do it again as an update to check it doesn't cause an error
+    $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
+        'current_employer' => 'new employer org', 'id' => $employerResult['id'])
+    ));
     $expectedCount = 1;
     $count = $this->callAPISuccess('contact', 'getcount', array(
       'organization_name' => 'new employer org',
@@ -371,6 +388,47 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->assertEquals('new employer org', $result['current_employer']);
 
   }
+  /*
+   * Test creating a current employer through API
+   * - check it will re-activate a de-activated employer
+  */
+  function testContactCreateDuplicateCurrentEmployerEnables(){
+    //set up  - create employer relationship
+    $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
+        'current_employer' => 'new employer org',)
+    ));
+    $relationship = $this->callAPISuccess('relationship','get', array(
+      'contact_id_a' => $employerResult['id'],
+   ));
+
+    //disable & check it is disabled
+    $this->callAPISuccess('relationship', 'create', array('id' => $relationship['id'], 'is_active' => 0));
+    $relationship = $this->callAPISuccess('relationship','getvalue', array(
+      'id' => $relationship['id'],
+      'return' => 'is_active'
+    ), 0);
+
+    //re-set the current employer - thus enabling the relationshp
+    $employerResult = $this->callAPISuccess('contact', 'create', array_merge($this->_params, array(
+        'current_employer' => 'new employer org', 'id' => $employerResult['id'])
+    ));
+    //check is_active is now 1
+   $relationship = $this->callAPISuccess('relationship','getsingle', array(
+     'return' => 'is_active',));
+   $this->assertEquals(1, $relationship['is_active']);
+  }
+
+  /**
+   * Check deceased contacts are not retrieved
+   * Note at time of writing the default is to return default. This should possibly be changed & test added
+   *
+   */
+  function testGetDeceasedRetrieved() {
+    $c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+    $c2 = $this->callAPISuccess($this->_entity, 'create', array('first_name' => 'bb', 'last_name' => 'ccc', 'contact_type' => 'Individual', 'is_deceased' => 1));
+    $result = $this->callAPISuccess($this->_entity, 'get', array('is_deceased' => 0));
+    $this->assertFalse(array_key_exists($c2['id'], $result['values']));
+  }
 
   /*
      * Test that sort works - old syntax
@@ -397,6 +455,21 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccess($this->_entity, 'delete', array('id' => $c1['id']));
     $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
   }
+  /*
+   * Test that we can retrieve contacts using
+   * 'id' => array('IN' => array('3,4')) syntax
+  */
+  function testGetINIDArray() {
+    $c1 = $this->callAPISuccess($this->_entity, 'create', $this->_params);
+    $c2 = $this->callAPISuccess($this->_entity, 'create', array('first_name' => 'bb', 'last_name' => 'ccc', 'contact_type' => 'Individual'));
+    $c3 = $this->callAPISuccess($this->_entity, 'create', array('first_name' => 'hh', 'last_name' => 'll', 'contact_type' => 'Individual'));
+    $result = $this->callAPISuccess($this->_entity, 'get', array('id' => array('IN' => array($c1['id'], $c3['id']))));
+    $this->assertEquals(2, $result['count']);
+    $this->assertEquals(array($c1['id'], $c3['id']), array_keys($result['values']));
+    $this->callAPISuccess($this->_entity, 'delete', array('id' => $c1['id']));
+    $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
+    $this->callAPISuccess($this->_entity, 'delete', array('id' => $c3['id']));
+  }
   /*
    * Test variants on deleted behaviour
    */
@@ -1165,6 +1238,49 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->assertEquals('testGetByUsername', $result['values'][$cid]['first_name']);
   }
 
+  /**
+   * Test to check return works OK
+   */
+  function testContactGetReturnValues() {
+    $extraParams = array('nick_name' => 'Bob', 'phone' => '456', 'email' => 'e@mail.com');
+    $contactID = $this->individualCreate($extraParams);
+    //actually it turns out the above doesn't create a phone
+    $phones = $this->callAPISuccess('phone', 'create', array('contact_id' => $contactID, 'phone' => '456',));
+    $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contactID));
+    foreach ($extraParams as $key => $value) {
+      $this->assertEquals($result[$key], $value);
+    }
+    //now we check they are still returned with 'return' key
+    $result = $this->callAPISuccess('contact', 'getsingle', array('id' => $contactID, 'return' => array_keys($extraParams)));
+    foreach ($extraParams as $key => $value) {
+      $this->assertEquals($result[$key], $value);
+    }
+  }
+
+  function testCRM13252MultipleChainedPhones() {
+    $contactID = $this->householdCreate();
+    $this->callAPISuccessGetCount('phone', array('contact_id' => $contactID), 0);
+    $params = array(
+     'contact_id' => $contactID,
+     'household_name' => 'Household 1',
+     'contact_type' => 'Household',
+     'api.phone.create' => array(
+        0 => array(
+          'phone' => '111-111-1111',
+          'location_type_id' => 1,
+          'phone_type_id' => 1,
+        ),
+        1 => array(
+          'phone' => '222-222-2222',
+          'location_type_id' => 1,
+          'phone_type_id' => 2,
+        )
+      )
+    );
+    $result = $this->callAPISuccess('contact', 'create', $params);
+    $this->callAPISuccessGetCount('phone', array('contact_id' => $contactID), 2);
+
+  }
   /**
    * Test for Contact.get id=@user:username (with an invalid username)
    */
@@ -1318,7 +1434,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__);
     $params['custom_' . $ids['custom_field_id']] = "custom string";
     $moreids = $this->CustomGroupMultipleCreateWithFields();
-    $andmoreids = $this->CustomGroupMultipleCreateWithFields(array('title' => "another group"));
+    $andmoreids = $this->CustomGroupMultipleCreateWithFields(array('title' => "another group", 'name' => 'another name'));
     $description = "/*this demonstrates the usage of chained api functions. A variety of techniques are used";
     $subfile = "APIChainedArrayMultipleCustom";
     $params = array(
@@ -1500,7 +1616,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $config = CRM_Core_Config::singleton();
     $config->userPermissionClass->permissions = array('access CiviCRM');
     $result = $this->callAPIFailure('contact', 'create', $params);
-    $this->assertEquals('API permission check failed for contact/create call; missing permission: add contacts.', $result['error_message'], 'lacking permissions should not be enough to create a contact');
+    $this->assertEquals('API permission check failed for contact/create call; insufficient permission: require access CiviCRM and add contacts', $result['error_message'], 'lacking permissions should not be enough to create a contact');
 
     $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'import contacts');
     $result = $this->callAPISuccess('contact', 'create', $params, NULL, 'overfluous permissions should be enough to create a contact');
@@ -1514,7 +1630,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
 
     $config->userPermissionClass->permissions = array('access CiviCRM');
     $result = $this->callAPIFailure('contact', 'update', $params);
-    $this->assertEquals('API permission check failed for contact/update call; missing permission: edit all contacts.', $result['error_message'], 'lacking permissions should not be enough to update a contact');
+    $this->assertEquals('API permission check failed for contact/update call; insufficient permission: require access CiviCRM and edit all contacts', $result['error_message'], 'lacking permissions should not be enough to update a contact');
 
     $config->userPermissionClass->permissions = array('access CiviCRM', 'add contacts', 'view all contacts', 'edit all contacts', 'import contacts');
     $result = $this->callAPISuccess('contact', 'update', $params, NULL, 'overfluous permissions should be enough to update a contact');
@@ -1562,4 +1678,17 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $result = $this->callAPISuccess('contact', 'proximity', $proxParams);
     $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
   }
+
+  /**
+   * Test that Ajax API permission is suffient to access quicksearch api
+   * (note that quicksearch api is required for autocomplete & has ACL permissions applied)
+   */
+  function testQuickSearchPermission_CRM_13744() {
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviEvent');
+    $result = $this->callAPIFailure('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access CiviCRM');
+    $result = $this->callAPISuccess('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = array('access AJAX API');
+    $result = $this->callAPISuccess('contact', 'getquick', array('name' => 'b', 'check_permissions' => TRUE));
+  }
 }