Merge pull request #16469 from civicrm/5.22
[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() {
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 }