From 0e70279a1456605ab3ac362f4150fb5ddb6e6b39 Mon Sep 17 00:00:00 2001 From: Aidan Saunders Date: Fri, 26 Jul 2019 11:14:03 +0100 Subject: [PATCH] Add default location for API v3 creates of Address, IM, OpenID and Phone. 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 | 4 ++++ api/v3/Im.php | 4 ++++ api/v3/OpenID.php | 4 ++++ api/v3/Phone.php | 4 ++++ tests/phpunit/api/v3/AddressTest.php | 16 ++++++++++++++++ tests/phpunit/api/v3/EmailTest.php | 4 ++-- tests/phpunit/api/v3/ImTest.php | 16 ++++++++++++++++ tests/phpunit/api/v3/OpenIDTest.php | 16 ++++++++++++++++ tests/phpunit/api/v3/PhoneTest.php | 16 ++++++++++++++++ 9 files changed, 82 insertions(+), 2 deletions(-) diff --git a/api/v3/Address.php b/api/v3/Address.php index 05586e2c37..6c7c9ca743 100644 --- a/api/v3/Address.php +++ b/api/v3/Address.php @@ -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; + } } /** diff --git a/api/v3/Im.php b/api/v3/Im.php index aaabb5d545..711db2e57e 100644 --- a/api/v3/Im.php +++ b/api/v3/Im.php @@ -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; + } } /** diff --git a/api/v3/OpenID.php b/api/v3/OpenID.php index de226c09b3..6066355e14 100644 --- a/api/v3/OpenID.php +++ b/api/v3/OpenID.php @@ -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; + } } /** diff --git a/api/v3/Phone.php b/api/v3/Phone.php index dea8cc9cf6..e5a4513d4b 100644 --- a/api/v3/Phone.php +++ b/api/v3/Phone.php @@ -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; + } } /** diff --git a/tests/phpunit/api/v3/AddressTest.php b/tests/phpunit/api/v3/AddressTest.php index ee7bc0ab4a..f272499364 100644 --- a/tests/phpunit/api/v3/AddressTest.php +++ b/tests/phpunit/api/v3/AddressTest.php @@ -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 */ diff --git a/tests/phpunit/api/v3/EmailTest.php b/tests/phpunit/api/v3/EmailTest.php index dde6232582..f1eaca5dbe 100644 --- a/tests/phpunit/api/v3/EmailTest.php +++ b/tests/phpunit/api/v3/EmailTest.php @@ -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']]); } /** diff --git a/tests/phpunit/api/v3/ImTest.php b/tests/phpunit/api/v3/ImTest.php index 0f8a4ba145..f918b333a8 100644 --- a/tests/phpunit/api/v3/ImTest.php +++ b/tests/phpunit/api/v3/ImTest.php @@ -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 * diff --git a/tests/phpunit/api/v3/OpenIDTest.php b/tests/phpunit/api/v3/OpenIDTest.php index d3a065aac8..062ee4a626 100644 --- a/tests/phpunit/api/v3/OpenIDTest.php +++ b/tests/phpunit/api/v3/OpenIDTest.php @@ -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 diff --git a/tests/phpunit/api/v3/PhoneTest.php b/tests/phpunit/api/v3/PhoneTest.php index 2cc6fc630b..89eff191ea 100644 --- a/tests/phpunit/api/v3/PhoneTest.php +++ b/tests/phpunit/api/v3/PhoneTest.php @@ -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 -- 2.25.1