Merge pull request #23283 from eileenmcnaughton/import_saved_map
[civicrm-core.git] / tests / phpunit / api / v3 / CountryTest.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_country* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
17 * @group headless
18 */
19 class api_v3_CountryTest extends CiviUnitTestCase {
20 protected $_params;
21
22 public function setUp(): void {
23 parent::setUp();
24 $this->useTransaction(TRUE);
25 $this->_params = [
26 'name' => 'Made Up Land',
27 'iso_code' => 'ZZ',
28 'region_id' => 1,
29 ];
30 }
31
32 /**
33 * @dataProvider versionThreeAndFour
34 */
35 public function testCreateCountry() {
36
37 $result = $this->callAPIAndDocument('country', 'create', $this->_params, __FUNCTION__, __FILE__);
38 $this->assertEquals(1, $result['count']);
39 $this->assertNotNull($result['values'][$result['id']]['id']);
40
41 $this->callAPISuccess('country', 'delete', ['id' => $result['id']]);
42 }
43
44 /**
45 * @dataProvider versionThreeAndFour
46 */
47 public function testDeleteCountry() {
48 //create one
49 $create = $this->callAPISuccess('country', 'create', $this->_params);
50
51 $result = $this->callAPIAndDocument('country', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__);
52 $this->assertEquals(1, $result['count']);
53 $get = $this->callAPISuccess('country', 'get', [
54 'id' => $create['id'],
55 ]);
56 $this->assertEquals(0, $get['count'], 'Country not successfully deleted');
57 }
58
59 /**
60 * Test civicrm_phone_get with empty params.
61 * @dataProvider versionThreeAndFour
62 */
63 public function testGetEmptyParams() {
64 $result = $this->callAPISuccess('Country', 'Get', []);
65 }
66
67 /**
68 * Test civicrm_phone_get with wrong params.
69 * @dataProvider versionThreeAndFour
70 */
71 public function testGetWrongParams() {
72 $this->callAPIFailure('Country', 'Get', ['id' => 'abc']);
73 }
74
75 /**
76 * Test civicrm_phone_get - success expected.
77 * @dataProvider versionThreeAndFour
78 */
79 public function testGet() {
80 $country = $this->callAPISuccess('Country', 'create', $this->_params);
81 $params = [
82 'iso_code' => $this->_params['iso_code'],
83 ];
84 $result = $this->callAPIAndDocument('Country', 'Get', $params, __FUNCTION__, __FILE__);
85 $this->assertEquals($country['values'][$country['id']]['name'], $result['values'][$country['id']]['name']);
86 $this->assertEquals($country['values'][$country['id']]['iso_code'], $result['values'][$country['id']]['iso_code']);
87 }
88
89 ///////////////// civicrm_country_create methods
90
91 /**
92 * If a new country is created and it is created again it should not create a second one.
93 * We check on the iso code (there should be only one iso code
94 * @dataProvider versionThreeAndFour
95 */
96 public function testCreateDuplicateFail() {
97 $params = $this->_params;
98 unset($params['id']);
99 $this->callAPISuccess('country', 'create', $params);
100 $this->callAPIFailure('country', 'create', $params);
101 $check = $this->callAPISuccess('country', 'getcount', [
102 'iso_code' => $params['iso_code'],
103 ]);
104 $this->assertEquals(1, $check);
105 }
106
107 /**
108 * Test that the list of states is in the correct format when chaining
109 * and using sequential.
110 */
111 public function testCountryStateChainSequential() {
112 // first without specifying
113 $result = $this->callAPISuccess('Country', 'getsingle', [
114 'iso_code' => 'US',
115 'api.Address.getoptions' => [
116 'field' => 'state_province_id',
117 'country_id' => '$value.id',
118 ],
119 ]);
120 $this->assertSame(['key' => 1000, 'value' => 'Alabama'], $result['api.Address.getoptions']['values'][0]);
121 $this->assertSame(['key' => 1001, 'value' => 'Alaska'], $result['api.Address.getoptions']['values'][1]);
122 $this->assertSame(['key' => 1049, 'value' => 'Wyoming'], $result['api.Address.getoptions']['values'][59]);
123
124 // now specifying sequential
125 $result = $this->callAPISuccess('Country', 'getsingle', [
126 'iso_code' => 'US',
127 'api.Address.getoptions' => [
128 'field' => 'state_province_id',
129 'country_id' => '$value.id',
130 'sequential' => 1,
131 ],
132 ]);
133 $this->assertSame(['key' => 1000, 'value' => 'Alabama'], $result['api.Address.getoptions']['values'][0]);
134 $this->assertSame(['key' => 1001, 'value' => 'Alaska'], $result['api.Address.getoptions']['values'][1]);
135 $this->assertSame(['key' => 1049, 'value' => 'Wyoming'], $result['api.Address.getoptions']['values'][59]);
136
137 // now specifying keyed
138 $result = $this->callAPISuccess('Country', 'getsingle', [
139 'iso_code' => 'US',
140 'api.Address.getoptions' => [
141 'field' => 'state_province_id',
142 'country_id' => '$value.id',
143 'sequential' => 0,
144 ],
145 ]);
146 $this->assertSame('Alabama', $result['api.Address.getoptions']['values'][1000]);
147 $this->assertSame('Alaska', $result['api.Address.getoptions']['values'][1001]);
148 $this->assertSame('Wyoming', $result['api.Address.getoptions']['values'][1049]);
149 }
150
151 }