Set a default location in Email.create API call. Add test for this.
authorAidan Saunders <aidan.saunders@squiffle.uk>
Mon, 10 Jun 2019 12:38:53 +0000 (13:38 +0100)
committerAidan Saunders <aidan.saunders@squiffle.uk>
Mon, 10 Jun 2019 20:15:42 +0000 (21:15 +0100)
Default location used to create the email is the default LocationType

api/v3/Email.php
tests/phpunit/api/v3/EmailTest.php

index 095e082e7365e83de225ebbf719c9add9bfaaf7a..d1dd4ab669733a5a702393e10697b3ed2c02f9dc 100644 (file)
@@ -57,6 +57,10 @@ function _civicrm_api3_email_create_spec(&$params) {
   $params['is_primary']['api.default'] = 0;
   $params['email']['api.required'] = 1;
   $params['contact_id']['api.required'] = 1;
+  $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
+  if ($defaultLocation) {
+    $params['location_type_id']['api.default'] = $defaultLocation->id;
+  }
 }
 
 /**
index 2a6910635052b9261670b190e5b56cc492d6f38c..4299e87d33258fc3f0baecab224618b025d017c7 100644 (file)
@@ -78,6 +78,20 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
   }
 
+  /**
+   * If no location is specified when creating a new email, it should default to
+   * the LocationType default
+   *
+   * Only API v3
+   */
+  public function testCreateEmailDefaultLocation() {
+    $params = $this->_params;
+    unset($params['location_type_id']);
+    $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
+    $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
+    $delresult = $this->callAPISuccess('email', 'delete', array('id' => $result['id']));
+  }
+
   /**
    * If a new email is set to is_primary the prev should no longer be.
    *