Commit | Line | Data |
---|---|---|
fe14170b JJ |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
fe14170b | 5 | | | |
7d61e75f TO |
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 | | |
fe14170b JJ |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
fe14170b JJ |
12 | /** |
13 | * Test APIv3 civicrm_country* functions | |
14 | * | |
6c6e6187 TO |
15 | * @package CiviCRM_APIv3 |
16 | * @subpackage API_Contact | |
acb109b7 | 17 | * @group headless |
fe14170b JJ |
18 | */ |
19 | class api_v3_CountryTest extends CiviUnitTestCase { | |
fe14170b JJ |
20 | protected $_params; |
21 | ||
7ef12efc | 22 | public function setUp(): void { |
fe14170b | 23 | parent::setUp(); |
a6e1ef97 | 24 | $this->useTransaction(TRUE); |
9099cab3 | 25 | $this->_params = [ |
78abe068 | 26 | 'name' => 'Made Up Land', |
a6e1ef97 | 27 | 'iso_code' => 'ZZ', |
78abe068 | 28 | 'region_id' => 1, |
9099cab3 | 29 | ]; |
fe14170b JJ |
30 | } |
31 | ||
1e810dc9 SL |
32 | /** |
33 | * @dataProvider versionThreeAndFour | |
34 | */ | |
fe14170b JJ |
35 | public function testCreateCountry() { |
36 | ||
37 | $result = $this->callAPIAndDocument('country', 'create', $this->_params, __FUNCTION__, __FILE__); | |
78abe068 C |
38 | $this->assertEquals(1, $result['count']); |
39 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
fe14170b | 40 | |
9099cab3 | 41 | $this->callAPISuccess('country', 'delete', ['id' => $result['id']]); |
fe14170b JJ |
42 | } |
43 | ||
1e810dc9 SL |
44 | /** |
45 | * @dataProvider versionThreeAndFour | |
46 | */ | |
fe14170b JJ |
47 | public function testDeleteCountry() { |
48 | //create one | |
49 | $create = $this->callAPISuccess('country', 'create', $this->_params); | |
50 | ||
9099cab3 | 51 | $result = $this->callAPIAndDocument('country', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__); |
78abe068 | 52 | $this->assertEquals(1, $result['count']); |
9099cab3 | 53 | $get = $this->callAPISuccess('country', 'get', [ |
fe14170b | 54 | 'id' => $create['id'], |
9099cab3 | 55 | ]); |
78abe068 | 56 | $this->assertEquals(0, $get['count'], 'Country not successfully deleted'); |
fe14170b JJ |
57 | } |
58 | ||
59 | /** | |
60 | * Test civicrm_phone_get with empty params. | |
1e810dc9 | 61 | * @dataProvider versionThreeAndFour |
fe14170b JJ |
62 | */ |
63 | public function testGetEmptyParams() { | |
9099cab3 | 64 | $result = $this->callAPISuccess('Country', 'Get', []); |
fe14170b JJ |
65 | } |
66 | ||
67 | /** | |
68 | * Test civicrm_phone_get with wrong params. | |
1e810dc9 | 69 | * @dataProvider versionThreeAndFour |
fe14170b JJ |
70 | */ |
71 | public function testGetWrongParams() { | |
9099cab3 | 72 | $this->callAPIFailure('Country', 'Get', ['id' => 'abc']); |
fe14170b JJ |
73 | } |
74 | ||
75 | /** | |
76 | * Test civicrm_phone_get - success expected. | |
1e810dc9 | 77 | * @dataProvider versionThreeAndFour |
fe14170b JJ |
78 | */ |
79 | public function testGet() { | |
80 | $country = $this->callAPISuccess('Country', 'create', $this->_params); | |
9099cab3 | 81 | $params = [ |
6c6e6187 | 82 | 'iso_code' => $this->_params['iso_code'], |
9099cab3 | 83 | ]; |
fe14170b | 84 | $result = $this->callAPIAndDocument('Country', 'Get', $params, __FUNCTION__, __FILE__); |
78abe068 C |
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']); | |
fe14170b JJ |
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 | |
1e810dc9 | 94 | * @dataProvider versionThreeAndFour |
fe14170b | 95 | */ |
78abe068 | 96 | public function testCreateDuplicateFail() { |
fe14170b JJ |
97 | $params = $this->_params; |
98 | unset($params['id']); | |
78abe068 C |
99 | $this->callAPISuccess('country', 'create', $params); |
100 | $this->callAPIFailure('country', 'create', $params); | |
9099cab3 | 101 | $check = $this->callAPISuccess('country', 'getcount', [ |
fe14170b | 102 | 'iso_code' => $params['iso_code'], |
9099cab3 | 103 | ]); |
fe14170b JJ |
104 | $this->assertEquals(1, $check); |
105 | } | |
96025800 | 106 | |
ea5a2b75 | 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 | ||
fe14170b | 151 | } |