Merge pull request #16953 from jitendrapurohit/core-1685
[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 $_params;
21
22 public function setUp() {
23 parent::setUp();
24 $this->useTransaction(TRUE);
25 $this->_params = [
26 'name' => 'Wessex',
27 'abbreviation' => 'WEX',
28 'country_id' => 1226,
29 ];
30 }
31
32 /**
33 * @dataProvider versionThreeAndFour
34 */
35 public function testCreateStateProvince() {
36 $result = $this->callAPIAndDocument('StateProvince', 'create', $this->_params, __FUNCTION__, __FILE__);
37 $this->assertEquals(1, $result['count']);
38 $this->assertNotNull($result['values'][$result['id']]['id']);
39 $this->callAPISuccess('StateProvince', 'delete', ['id' => $result['id']]);
40 }
41
42 /**
43 * @dataProvider versionThreeAndFour
44 */
45 public function testDeleteStateProvince() {
46 // Create
47 $create = $this->callAPISuccess('StateProvince', 'create', $this->_params);
48
49 // Delete
50 $result = $this->callAPIAndDocument('StateProvince', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__);
51 $this->assertEquals(1, $result['count']);
52 $get = $this->callAPISuccess('StateProvince', 'get', [
53 'id' => $create['id'],
54 ]);
55 $this->assertEquals(0, $get['count'], 'State/province not successfully deleted');
56 }
57
58 /**
59 * Test with empty params
60 * @dataProvider versionThreeAndFour
61 */
62 public function testGetEmptyParams() {
63 $result = $this->callAPISuccess('StateProvince', 'Get', []);
64 }
65
66 /**
67 * Test with wrong params
68 * @dataProvider versionThreeAndFour
69 */
70 public function testGetWrongParams() {
71 $this->callAPIFailure('StateProvince', 'Get', ['id' => 'abc']);
72 }
73
74 /**
75 * Test get
76 * @dataProvider versionThreeAndFour
77 */
78 public function testGet() {
79 $province = $this->callAPISuccess('StateProvince', 'create', $this->_params);
80 $params = [
81 'name' => $this->_params['name'],
82 ];
83 $result = $this->callAPIAndDocument('StateProvince', 'Get', $params, __FUNCTION__, __FILE__);
84 $this->assertEquals($province['values'][$province['id']]['name'], $result['values'][$province['id']]['name']);
85 $this->assertEquals($province['values'][$province['id']]['abbreviation'], $result['values'][$province['id']]['abbreviation']);
86 }
87
88 /**
89 * There cannot be two state/provinces with the same name in the same country.
90 * @dataProvider versionThreeAndFour
91 */
92 public function testCreateDuplicateFail() {
93 $params = $this->_params;
94 unset($params['id']);
95 $this->callAPISuccess('StateProvince', 'create', $params);
96 $this->callAPIFailure('StateProvince', 'create', $params);
97 $check = $this->callAPISuccess('StateProvince', 'getcount', [
98 'name' => $params['name'],
99 ]);
100 $this->assertEquals(1, $check);
101 }
102
103 }