Merge in 5.20
[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 $_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' => 'Made Up Land',
29 'iso_code' => 'ZZ',
30 'region_id' => 1,
31 ];
32 }
33
34 public function testCreateCountry() {
35
36 $result = $this->callAPIAndDocument('country', 'create', $this->_params, __FUNCTION__, __FILE__);
37 $this->assertEquals(1, $result['count']);
38 $this->assertNotNull($result['values'][$result['id']]['id']);
39
40 $this->callAPISuccess('country', 'delete', ['id' => $result['id']]);
41 }
42
43 public function testDeleteCountry() {
44 //create one
45 $create = $this->callAPISuccess('country', 'create', $this->_params);
46
47 $result = $this->callAPIAndDocument('country', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__);
48 $this->assertEquals(1, $result['count']);
49 $get = $this->callAPISuccess('country', 'get', [
50 'id' => $create['id'],
51 ]);
52 $this->assertEquals(0, $get['count'], 'Country not successfully deleted');
53 }
54
55 /**
56 * Test civicrm_phone_get with empty params.
57 */
58 public function testGetEmptyParams() {
59 $result = $this->callAPISuccess('Country', 'Get', []);
60 }
61
62 /**
63 * Test civicrm_phone_get with wrong params.
64 */
65 public function testGetWrongParams() {
66 $this->callAPIFailure('Country', 'Get', ['id' => 'abc']);
67 }
68
69 /**
70 * Test civicrm_phone_get - success expected.
71 */
72 public function testGet() {
73 $country = $this->callAPISuccess('Country', 'create', $this->_params);
74 $params = [
75 'iso_code' => $this->_params['iso_code'],
76 ];
77 $result = $this->callAPIAndDocument('Country', 'Get', $params, __FUNCTION__, __FILE__);
78 $this->assertEquals($country['values'][$country['id']]['name'], $result['values'][$country['id']]['name']);
79 $this->assertEquals($country['values'][$country['id']]['iso_code'], $result['values'][$country['id']]['iso_code']);
80 }
81
82 ///////////////// civicrm_country_create methods
83
84 /**
85 * If a new country is created and it is created again it should not create a second one.
86 * We check on the iso code (there should be only one iso code
87 */
88 public function testCreateDuplicateFail() {
89 $params = $this->_params;
90 unset($params['id']);
91 $this->callAPISuccess('country', 'create', $params);
92 $this->callAPIFailure('country', 'create', $params);
93 $check = $this->callAPISuccess('country', 'getcount', [
94 'iso_code' => $params['iso_code'],
95 ]);
96 $this->assertEquals(1, $check);
97 }
98
99 }