From 4d4819b04f417a63bba415930bb3b74f55d9a68b Mon Sep 17 00:00:00 2001 From: JKingsnorth Date: Mon, 28 Nov 2016 11:04:07 +0000 Subject: [PATCH] CRM-19688 Create StateProvince API --- api/v3/StateProvince.php | 84 ++++++++++++++++ tests/phpunit/api/v3/StateProvinceTest.php | 111 +++++++++++++++++++++ 2 files changed, 195 insertions(+) create mode 100644 api/v3/StateProvince.php create mode 100644 tests/phpunit/api/v3/StateProvinceTest.php diff --git a/api/v3/StateProvince.php b/api/v3/StateProvince.php new file mode 100644 index 0000000000..d363f27ea3 --- /dev/null +++ b/api/v3/StateProvince.php @@ -0,0 +1,84 @@ +_apiversion = 3; + parent::setUp(); + $this->useTransaction(TRUE); + $this->_params = array( + 'name' => 'Wessex', + 'abbreviation' => 'WEX', + 'country_id' => 1226, + ); + } + + public function testCreateStateProvince() { + $result = $this->callAPIAndDocument('StateProvince', 'create', $this->_params, __FUNCTION__, __FILE__); + $this->assertEquals(1, $result['count']); + $this->assertNotNull($result['values'][$result['id']]['id']); + $this->callAPISuccess('StateProvince', 'delete', array('id' => $result['id'])); + } + + public function testDeleteStateProvince() { + // Create + $create = $this->callAPISuccess('StateProvince', 'create', $this->_params); + + // Delete + $result = $this->callAPIAndDocument('StateProvince', 'delete', array('id' => $create['id']), __FUNCTION__, __FILE__); + $this->assertEquals(1, $result['count']); + $get = $this->callAPISuccess('StateProvince', 'get', array( + 'id' => $create['id'], + )); + $this->assertEquals(0, $get['count'], 'State/province not successfully deleted'); + } + + /** + * Test with empty params + */ + public function testGetEmptyParams() { + $result = $this->callAPISuccess('StateProvince', 'Get', array()); + } + + /** + * Test with wrong params + */ + public function testGetWrongParams() { + $this->callAPIFailure('StateProvince', 'Get', array('id' => 'abc')); + } + + /** + * Test get + */ + public function testGet() { + $province = $this->callAPISuccess('StateProvince', 'create', $this->_params); + $params = array( + 'name' => $this->_params['name'], + ); + $result = $this->callAPIAndDocument('StateProvince', 'Get', $params, __FUNCTION__, __FILE__); + $this->assertEquals($province['values'][$province['id']]['name'], $result['values'][$province['id']]['name']); + $this->assertEquals($province['values'][$province['id']]['abbreviation'], $result['values'][$province['id']]['abbreviation']); + } + + /** + * There cannot be two state/provinces with the same name in the same country. + */ + public function testCreateDuplicateFail() { + $params = $this->_params; + unset($params['id']); + $this->callAPISuccess('StateProvince', 'create', $params); + $this->callAPIFailure('StateProvince', 'create', $params); + $check = $this->callAPISuccess('StateProvince', 'getcount', array( + 'name' => $params['name'], + )); + $this->assertEquals(1, $check); + } + +} -- 2.25.1