Merge pull request #15794 from KarinG/master
[civicrm-core.git] / tests / phpunit / api / v3 / StateProvinceTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_state_province* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
17 * @group headless
18 */
19 class api_v3_StateProvinceTest extends CiviUnitTestCase {
20 protected $_apiversion;
21 protected $_params;
22
23 public function setUp() {
24 $this->_apiversion = 3;
25 parent::setUp();
26 $this->useTransaction(TRUE);
27 $this->_params = [
28 'name' => 'Wessex',
29 'abbreviation' => 'WEX',
30 'country_id' => 1226,
31 ];
32 }
33
34 public function testCreateStateProvince() {
35 $result = $this->callAPIAndDocument('StateProvince', 'create', $this->_params, __FUNCTION__, __FILE__);
36 $this->assertEquals(1, $result['count']);
37 $this->assertNotNull($result['values'][$result['id']]['id']);
38 $this->callAPISuccess('StateProvince', 'delete', ['id' => $result['id']]);
39 }
40
41 public function testDeleteStateProvince() {
42 // Create
43 $create = $this->callAPISuccess('StateProvince', 'create', $this->_params);
44
45 // Delete
46 $result = $this->callAPIAndDocument('StateProvince', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__);
47 $this->assertEquals(1, $result['count']);
48 $get = $this->callAPISuccess('StateProvince', 'get', [
49 'id' => $create['id'],
50 ]);
51 $this->assertEquals(0, $get['count'], 'State/province not successfully deleted');
52 }
53
54 /**
55 * Test with empty params
56 */
57 public function testGetEmptyParams() {
58 $result = $this->callAPISuccess('StateProvince', 'Get', []);
59 }
60
61 /**
62 * Test with wrong params
63 */
64 public function testGetWrongParams() {
65 $this->callAPIFailure('StateProvince', 'Get', ['id' => 'abc']);
66 }
67
68 /**
69 * Test get
70 */
71 public function testGet() {
72 $province = $this->callAPISuccess('StateProvince', 'create', $this->_params);
73 $params = [
74 'name' => $this->_params['name'],
75 ];
76 $result = $this->callAPIAndDocument('StateProvince', 'Get', $params, __FUNCTION__, __FILE__);
77 $this->assertEquals($province['values'][$province['id']]['name'], $result['values'][$province['id']]['name']);
78 $this->assertEquals($province['values'][$province['id']]['abbreviation'], $result['values'][$province['id']]['abbreviation']);
79 }
80
81 /**
82 * There cannot be two state/provinces with the same name in the same country.
83 */
84 public function testCreateDuplicateFail() {
85 $params = $this->_params;
86 unset($params['id']);
87 $this->callAPISuccess('StateProvince', 'create', $params);
88 $this->callAPIFailure('StateProvince', 'create', $params);
89 $check = $this->callAPISuccess('StateProvince', 'getcount', [
90 'name' => $params['name'],
91 ]);
92 $this->assertEquals(1, $check);
93 }
94
95 }