A unit test for CRM-15983.
authorJohan Vervloet <johanv@johanv.org>
Tue, 10 Mar 2015 13:42:23 +0000 (14:42 +0100)
committerJohan Vervloet <johanv@johanv.org>
Tue, 10 Mar 2015 13:42:23 +0000 (14:42 +0100)
This hack seems to fix CRM-15983 as well. (At least in the case that
contact_id_a or contact_id_b is given.)

I copied the unit test for CRM-15983.

----------------------------------------
* CRM-15983: limit and sort options don't work inside chained call to relationship API
  https://issues.civicrm.org/jira/browse/CRM-15983

tests/phpunit/api/v3/ContactTest.php

index 8247cbaca85556a901d71a67e99984242de91195..be633e4ec9184ff6e098efde4be0590594df3d8e 100644 (file)
@@ -631,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.
    */