Add default location for API v3 creates of Address, IM, OpenID and Phone.
authorAidan Saunders <aidan.saunders@squiffle.uk>
Fri, 26 Jul 2019 10:14:03 +0000 (11:14 +0100)
committerAidan Saunders <aidan.saunders@squiffle.uk>
Fri, 26 Jul 2019 22:38:25 +0000 (23:38 +0100)
Has tests.

Default location was recently added for APIv3 Email.create
(https://github.com/civicrm/civicrm-core/pull/14489)
and for Address, Email, IM, OpenID and Phone in APIv4
(https://github.com/civicrm/org.civicrm.api4/pull/162)
so this brings consistency of behaviour for these entities between each other
and between APIv3 and APIv4

api/v3/Address.php
api/v3/Im.php
api/v3/OpenID.php
api/v3/Phone.php
tests/phpunit/api/v3/AddressTest.php
tests/phpunit/api/v3/EmailTest.php
tests/phpunit/api/v3/ImTest.php
tests/phpunit/api/v3/OpenIDTest.php
tests/phpunit/api/v3/PhoneTest.php

index 05586e2c37ddd429f2ec1216bc8725c75dc4feb7..6c7c9ca743ca7c3f0503d7e216016e5d45da2c79 100644 (file)
@@ -125,6 +125,10 @@ function _civicrm_api3_address_create_spec(&$params) {
     'name' => 'world_region',
     'type' => CRM_Utils_Type::T_TEXT,
   ];
+  $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
+  if ($defaultLocation) {
+    $params['location_type_id']['api.default'] = $defaultLocation->id;
+  }
 }
 
 /**
index aaabb5d5450d8b109904356787325d27442f11f6..711db2e57e76a645e0ff502f9cb3763df377f260 100644 (file)
@@ -52,6 +52,10 @@ function civicrm_api3_im_create($params) {
  */
 function _civicrm_api3_im_create_spec(&$params) {
   $params['contact_id']['api.required'] = 1;
+  $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
+  if ($defaultLocation) {
+    $params['location_type_id']['api.default'] = $defaultLocation->id;
+  }
 }
 
 /**
index de226c09b3f30bf053ae93d5f4bdeb8a92c6e1aa..6066355e14ed487bab6f14ea0f13df7d64fbbfae 100644 (file)
@@ -53,6 +53,10 @@ function civicrm_api3_open_i_d_create($params) {
  */
 function _civicrm_api3_open_i_d_create_spec(&$params) {
   $params['contact_id']['api.required'] = 1;
+  $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
+  if ($defaultLocation) {
+    $params['location_type_id']['api.default'] = $defaultLocation->id;
+  }
 }
 
 /**
index dea8cc9cf6df823f36669dd0b9cc756f549ca6ec..e5a4513d4b58bed1f50d96324265a606b854173c 100644 (file)
@@ -56,6 +56,10 @@ function _civicrm_api3_phone_create_spec(&$params) {
   $params['phone']['api.required'] = 1;
   // hopefully change to use handleprimary
   $params['is_primary']['api.default'] = 0;
+  $defaultLocation = CRM_Core_BAO_LocationType::getDefault();
+  if ($defaultLocation) {
+    $params['location_type_id']['api.default'] = $defaultLocation->id;
+  }
 }
 
 /**
index ee7bc0ab4a22e22c28e2b62662cd91e65659c2bd..f2724993644a92ce08f92c4af24fdf78144aaa27 100644 (file)
@@ -121,6 +121,22 @@ class api_v3_AddressTest extends CiviUnitTestCase {
     $this->getAndCheck($this->_params, $result['id'], 'address');
   }
 
+  /**
+   * If no location is specified when creating a new address, it should default to
+   * the LocationType default
+   *
+   * @param int $version
+   * @dataProvider versionThreeAndFour
+   */
+  public function testCreateAddressDefaultLocation($version) {
+    $this->_apiversion = $version;
+    $params = $this->_params;
+    unset($params['location_type_id']);
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
+    $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
+    $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
+  }
+
   /**
    * FIXME: Api4
    */
index dde623258220a9c636cca089eebbaed41e2cd3a0..f1eaca5dbe64aa1adb961bba3bf6fa89a25df092 100644 (file)
@@ -89,9 +89,9 @@ class api_v3_EmailTest extends CiviUnitTestCase {
     $this->_apiversion = $version;
     $params = $this->_params;
     unset($params['location_type_id']);
-    $result = $this->callAPIAndDocument('email', 'create', $params, __FUNCTION__, __FILE__);
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
     $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
-    $delresult = $this->callAPISuccess('email', 'delete', ['id' => $result['id']]);
+    $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
   }
 
   /**
index 0f8a4ba145876be56b4faa466fef7f9fa3420d87..f918b333a8a3f224a9aa70bc203161c4cc8cc23f 100644 (file)
@@ -71,6 +71,22 @@ class api_v3_ImTest extends CiviUnitTestCase {
     $this->assertNotNull($result['values'][$result['id']]['id']);
   }
 
+  /**
+   * If no location is specified when creating a new IM, it should default to
+   * the LocationType default
+   *
+   * @param int $version
+   * @dataProvider versionThreeAndFour
+   */
+  public function testCreateImDefaultLocation($version) {
+    $this->_apiversion = $version;
+    $params = $this->_params;
+    unset($params['location_type_id']);
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
+    $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
+    $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
+  }
+
   /**
    * @param int $version
    *
index d3a065aac8f545d36faeb4de10494b3475f2bf0c..062ee4a626352143fea35525e51d28121802b9b2 100644 (file)
@@ -66,6 +66,22 @@ class api_v3_OpenIDTest extends CiviUnitTestCase {
     $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__);
   }
 
+  /**
+   * If no location is specified when creating a new openid, it should default to
+   * the LocationType default
+   *
+   * @param int $version
+   * @dataProvider versionThreeAndFour
+   */
+  public function testCreateOpenIDDefaultLocation($version) {
+    $this->_apiversion = $version;
+    $params = $this->_params;
+    unset($params['location_type_id']);
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
+    $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
+    $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
+  }
+
   /**
    * @param int $version
    * @dataProvider versionThreeAndFour
index 2cc6fc630b32b04fe3c63733408e10396bbac827..89eff191ea76f63e1524b4c8cc4a16267d8acb8d 100644 (file)
@@ -68,6 +68,22 @@ class api_v3_PhoneTest extends CiviUnitTestCase {
     $this->callAPISuccess('phone', 'delete', ['id' => $result['id']]);
   }
 
+  /**
+   * If no location is specified when creating a new phone, it should default to
+   * the LocationType default
+   *
+   * @param int $version
+   * @dataProvider versionThreeAndFour
+   */
+  public function testCreatePhoneDefaultLocation($version) {
+    $this->_apiversion = $version;
+    $params = $this->_params;
+    unset($params['location_type_id']);
+    $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
+    $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
+    $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
+  }
+
   /**
    * @param int $version
    * @dataProvider versionThreeAndFour