Merge pull request #5536 from totten/4.5-httpclient
[civicrm-core.git] / tests / phpunit / api / v3 / ContactTest.php
index a8a7f03eb9b13040496ce3394837e6fe6a270ff7..be633e4ec9184ff6e098efde4be0590594df3d8e 100644 (file)
@@ -357,6 +357,76 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->customGroupDelete($ids['custom_group_id']);
   }
 
+  /**
+   * CRM-15792 - create/update datetime field for contact.
+   */
+  public function testCreateContactCustomFldDateTime() {
+    $customGroup = $this->customGroupCreate(array('extends' => 'Individual', 'title' => 'datetime_test_group'));
+    $dateTime = CRM_Utils_Date::currentDBDate();
+    //check date custom field is saved along with time when time_format is set
+    $params = array(
+      'first_name' => 'abc3',
+      'last_name' => 'xyz3',
+      'contact_type' => 'Individual',
+      'email' => 'man3@yahoo.com',
+      'api.CustomField.create' => array(
+        'custom_group_id' => $customGroup['id'],
+        'name' => 'test_datetime',
+        'label' => 'Demo Date',
+        'html_type' => 'Select Date',
+        'data_type' => 'Date',
+        'time_format' => 2,
+        'weight' => 4,
+        'is_required' => 1,
+        'is_searchable' => 0,
+        'is_active' => 1,
+      ),
+    );
+
+    $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__);
+    $customFldId = $result['values'][$result['id']]['api.CustomField.create']['id'];
+    $this->assertNotNull($result['id'], 'in line ' . __LINE__);
+    $this->assertNotNull($customFldId, 'in line ' . __LINE__);
+
+    $params = array(
+      'id' => $result['id'],
+      "custom_{$customFldId}" => $dateTime,
+      'api.CustomValue.get' => 1,
+    );
+
+    $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__);
+    $this->assertNotNull($result['id'], 'in line ' . __LINE__);
+    $customFldDate = date("YmdHis", strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
+    $this->assertNotNull($customFldDate, 'in line ' . __LINE__);
+    $this->assertEquals($dateTime, $customFldDate);
+    $customValueId = $result['values'][$result['id']]['api.CustomValue.get']['values'][0]['id'];
+    $dateTime = date('Ymd');
+    //date custom field should not contain time part when time_format is null
+    $params = array(
+      'id' => $result['id'],
+      'api.CustomField.create' => array(
+        'id' => $customFldId,
+        'html_type' => 'Select Date',
+        'data_type' => 'Date',
+        'time_format' => '',
+      ),
+      'api.CustomValue.create' => array(
+        'id' => $customValueId,
+        'entity_id' => $result['id'],
+        "custom_{$customFldId}" => $dateTime,
+      ),
+      'api.CustomValue.get' => 1,
+    );
+    $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__);
+    $this->assertNotNull($result['id'], 'in line ' . __LINE__);
+    $customFldDate = date("Ymd", strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
+    $customFldTime = date("His", strtotime($result['values'][$result['id']]['api.CustomValue.get']['values'][0]['latest']));
+    $this->assertNotNull($customFldDate, 'in line ' . __LINE__);
+    $this->assertEquals($dateTime, $customFldDate);
+    $this->assertEquals(000000, $customFldTime);
+    $result = $this->callAPIAndDocument('Contact', 'create', $params, __FUNCTION__, __FILE__);
+  }
+
 
   /**
    * Test creating a current employer through API.
@@ -561,6 +631,70 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccess($this->_entity, 'delete', array('id' => $c2['id']));
   }
 
+  /**
+   * Test sort and limit for chained relationship get.
+   *
+   * https://issues.civicrm.org/jira/browse/CRM-15983
+   */
+  public function testSortLimitChainedRelationshipGetCRM15983() {
+    // Some contact
+    $create_result_1 = $this->callAPISuccess('contact', 'create', array(
+      'first_name' => 'Jules',
+      'last_name' => 'Smos',
+      'contact_type' => 'Individual',
+    ));
+
+    // Create another contact with two relationships.
+    $create_params = array(
+      'first_name' => 'Jos',
+      'last_name' => 'Smos',
+      'contact_type' => 'Individual',
+      'api.relationship.create' => array(
+        array(
+          'contact_id_a' => '$value.id',
+          'contact_id_b' => $create_result_1['id'],
+          // spouse of:
+          'relationship_type_id' => 2,
+          'start_date' => '2005-01-12',
+          'end_date' => '2006-01-11',
+          'description' => 'old',
+        ),
+        array(
+          'contact_id_a' => '$value.id',
+          'contact_id_b' => $create_result_1['id'],
+          // spouse of (was married twice :))
+          'relationship_type_id' => 2,
+          'start_date' => '2006-07-01',
+          'end_date' => '2010-07-01',
+          'description' => 'new',
+        ),
+      ),
+    );
+    $create_result = $this->callAPISuccess('contact', 'create', $create_params);
+
+    // Try to retrieve the contact and the most recent relationship.
+    $get_params = array(
+      'sequential' => 1,
+      'id' => $create_result['id'],
+      'api.relationship.get' => array(
+        'contact_id_a' => '$value.id',
+        'options' => array(
+          'limit' => '1',
+          'sort' => 'start_date DESC',
+        )),
+    );
+    $get_result = $this->callAPISuccess('contact', 'getsingle', $get_params);
+
+    // Clean up.
+    $this->callAPISuccess('contact', 'delete', array(
+      'id' => $create_result['id'],
+    ));
+
+    // Assert.
+    $this->assertEquals(1, $get_result['api.relationship.get']['count']);
+    $this->assertEquals('new', $get_result['api.relationship.get']['values'][0]['description']);
+  }
+
   /**
    * Test apostrophe works in get & create.
    */
@@ -795,6 +929,78 @@ class api_v3_ContactTest extends CiviUnitTestCase {
     $this->callAPISuccess('contact', 'delete', $result);
   }
 
+  /**
+   * Test for direction when chaining relationships.
+   *
+   * https://issues.civicrm.org/jira/browse/CRM-16084
+   */
+  public function testDirectionChainingRelationshipsCRM16084() {
+    // Some contact, called Jules.
+    $create_result_1 = $this->callAPISuccess('contact', 'create', array(
+      'first_name' => 'Jules',
+      'last_name' => 'Smos',
+      'contact_type' => 'Individual',
+    ));
+
+    // Another contact: Jos, child of Jules.
+    $create_params = array(
+      'first_name' => 'Jos',
+      'last_name' => 'Smos',
+      'contact_type' => 'Individual',
+      'api.relationship.create' => array(
+        array(
+          'contact_id_a' => '$value.id',
+          'contact_id_b' => $create_result_1['id'],
+          // child of
+          'relationship_type_id' => 1,
+        ),
+      ),
+    );
+    $create_result_2 = $this->callAPISuccess('contact', 'create', $create_params);
+
+    // Mia is the child of Jos.
+    $create_params = array(
+      'first_name' => 'Mia',
+      'last_name' => 'Smos',
+      'contact_type' => 'Individual',
+      'api.relationship.create' => array(
+        array(
+          'contact_id_a' => '$value.id',
+          'contact_id_b' => $create_result_2['id'],
+          // child of
+          'relationship_type_id' => 1,
+        ),
+      ),
+    );
+    $create_result_3 = $this->callAPISuccess('contact', 'create', $create_params);
+
+    // Get Jos and his children.
+    $get_params = array(
+      'sequential' => 1,
+      'id' => $create_result_2['id'],
+      'api.relationship.get' => array(
+        'contact_id_b' => '$value.id',
+        'relationship_type_id' => 1,
+      ),
+    );
+    $get_result = $this->callAPISuccess('contact', 'getsingle', $get_params);
+
+    // Clean up first.
+    $this->callAPISuccess('contact', 'delete', array(
+      'id' => $create_result_1['id'],
+      ));
+    $this->callAPISuccess('contact', 'delete', array(
+      'id' => $create_result_2['id'],
+      ));
+    $this->callAPISuccess('contact', 'delete', array(
+      'id' => $create_result_2['id'],
+    ));
+
+    // Assert.
+    $this->assertEquals(1, $get_result['api.relationship.get']['count']);
+    $this->assertEquals($create_result_3['id'], $get_result['api.relationship.get']['values'][0]['contact_id_a']);
+  }
+
   /**
    * Verify that attempt to create individual contact with first, and last names and email succeeds.
    */
@@ -1728,7 +1934,7 @@ class api_v3_ContactTest extends CiviUnitTestCase {
       It will be ignored if there is not exactly 1 result";
     $subFile = "FormatSingleValue";
     $params = array('id' => 17, 'return' => 'display_name');
-    $result = $this->callAPIAndDocument('Contact', 'getvalue', $params, __FUNCTION__, __FILE__, $description, $subFile, 'getvalue');
+    $result = $this->callAPIAndDocument('Contact', 'getvalue', $params, __FUNCTION__, __FILE__, $description, $subFile);
     $this->assertEquals('Test Contact', $result);
     $this->callAPISuccess('Contact', 'Delete', $params);
   }