CRM-13312 - CRM_Utils_API_MatchOptionTest - http://forum.civicrm.org/index.php/topic...
authorTim Otten <totten@civicrm.org>
Tue, 1 Oct 2013 16:17:32 +0000 (18:17 +0200)
committerTim Otten <totten@civicrm.org>
Tue, 1 Oct 2013 16:17:32 +0000 (18:17 +0200)
----------------------------------------
* CRM-13312: Implement API support for options.match
  http://issues.civicrm.org/jira/browse/CRM-13312

tests/phpunit/CRM/Utils/API/MatchOptionTest.php

index ea087029e17e84c90f5621ff613e21bc62c72a70..1f0ef717fcb2cf6ea588fb048e846eedc45d423d 100644 (file)
@@ -137,7 +137,7 @@ class CRM_Utils_API_MatchOptionTest extends CiviUnitTestCase {
    * When replacing one set with another set, match items within
    * the set using a key.
    */
-  function testReplaceMatch() {
+  function testReplaceMatch_Email() {
     // Create contact with two emails (j1,j2)
     $createResult = $this->callAPISuccess('contact', 'create', array(
       'contact_type' => 'Individual',
@@ -210,4 +210,81 @@ class CRM_Utils_API_MatchOptionTest extends CiviUnitTestCase {
     $this->assertTrue(empty($getValues[1]['signature_text']));
   }
 
+  /**
+   * When replacing one set with another set, match items within
+   * the set using a key.
+   */
+  function testReplaceMatch_Address() {
+    // Create contact with two addresses (j1,j2)
+    $createResult = $this->callAPISuccess('contact', 'create', array(
+      'contact_type' => 'Individual',
+      'first_name' => 'Jeffrey',
+      'last_name' => 'Lebowski',
+      'api.Address.replace' => array(
+        'options' => array('match' => 'location_type_id'),
+        'values' => array(
+          array('location_type_id' => 1, 'street_address' => 'j1-a Example Ave', 'supplemental_address_1' => 'The Dude abides.'),
+          array('location_type_id' => 2, 'street_address' => 'j2 Example Ave', 'supplemental_address_1' => 'You know, a lotta ins, a lotta outs, a lotta what-have-yous.'),
+        ),
+      ),
+    ));
+    $this->assertEquals(1, $createResult['count']);
+    foreach ($createResult['values'] as $value) {
+      $this->assertAPISuccess($value['api.Address.replace']);
+      $this->assertEquals(2, $value['api.Address.replace']['count']);
+      foreach ($value['api.Address.replace']['values'] as $v2) {
+        $this->assertEquals($createResult['id'], $v2['contact_id']);
+      }
+      $createAddressValues = array_values($value['api.Address.replace']['values']);
+    }
+
+    // Update contact's addresses -- specifically, modify j1, delete j2, add j3
+    $updateResult = $this->callAPISuccess('contact', 'create', array(
+      'id' => $createResult['id'],
+      'nick_name' => 'The Dude',
+      'api.Address.replace' => array(
+        'options' => array('match' => 'location_type_id'),
+        'values' => array(
+          array('location_type_id' => 1, 'street_address' => 'j1-b Example Ave'),
+          array('location_type_id' => 3, 'street_address' => 'j3 Example Ave'),
+        ),
+      ),
+    ));
+    $this->assertEquals(1, $updateResult['count']);
+    foreach ($updateResult['values'] as $value) {
+      $this->assertAPISuccess($value['api.Address.replace']);
+      $this->assertEquals(2, $value['api.Address.replace']['count']);
+      foreach ($value['api.Address.replace']['values'] as $v2) {
+        $this->assertEquals($createResult['id'], $v2['contact_id']);
+      }
+      $updateAddressValues = array_values($value['api.Address.replace']['values']);
+    }
+
+    // Re-read from DB
+    $getResult = $this->callAPISuccess('Address', 'get', array(
+      'contact_id' => $createResult['id'],
+    ));
+    $this->assertEquals(2, $getResult['count']);
+    $getValues = array_values($getResult['values']);
+
+    // The first street_address (j1 Example Ave) is updated (same ID#) because it matched on contact_id+location_type_id.
+    $this->assertTrue(is_numeric($createAddressValues[0]['id']));
+    $this->assertTrue(is_numeric($updateAddressValues[0]['id']));
+    $this->assertTrue(is_numeric($getValues[0]['id']));
+    $this->assertEquals($createAddressValues[0]['id'], $updateAddressValues[0]['id']);
+    $this->assertEquals($createAddressValues[0]['id'], $getValues[0]['id']);
+    $this->assertEquals('j1-b Example Ave', $getValues[0]['street_address']);
+    $this->assertEquals('The Dude abides.', $getValues[0]['supplemental_address_1']); // preserved from original creation; proves that we updated existing record
+
+    // The second street_address (j2 Example Ave) is deleted because contact_id+location_type_id doesn't appear in new list.
+    // The third street_address (j3 Example Ave) is inserted (new ID#) because it doesn't match an existing contact_id+location_type_id.
+    $this->assertTrue(is_numeric($createAddressValues[1]['id']));
+    $this->assertTrue(is_numeric($updateAddressValues[1]['id']));
+    $this->assertTrue(is_numeric($getValues[1]['id']));
+    $this->assertNotEquals($createAddressValues[1]['id'], $updateAddressValues[1]['id']);
+    $this->assertEquals($updateAddressValues[1]['id'], $getValues[1]['id']);
+    $this->assertEquals('j3 Example Ave', $getValues[1]['street_address']);
+    $this->assertTrue(empty($getValues[1]['supplemental_address_1']));
+  }
+
 }