Merge pull request #16469 from civicrm/5.22
[civicrm-core.git] / tests / phpunit / api / v3 / StateProvinceTest.php
CommitLineData
4d4819b0
J
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
4d4819b0 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 |
4d4819b0
J
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 * Test APIv3 civicrm_state_province* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
17 * @group headless
18 */
19class api_v3_StateProvinceTest extends CiviUnitTestCase {
4d4819b0
J
20 protected $_params;
21
22 public function setUp() {
4d4819b0
J
23 parent::setUp();
24 $this->useTransaction(TRUE);
9099cab3 25 $this->_params = [
4d4819b0
J
26 'name' => 'Wessex',
27 'abbreviation' => 'WEX',
28 'country_id' => 1226,
9099cab3 29 ];
4d4819b0
J
30 }
31
1e810dc9
SL
32 /**
33 * @dataProvider versionThreeAndFour
34 */
4d4819b0
J
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']);
9099cab3 39 $this->callAPISuccess('StateProvince', 'delete', ['id' => $result['id']]);
4d4819b0
J
40 }
41
1e810dc9
SL
42 /**
43 * @dataProvider versionThreeAndFour
44 */
4d4819b0
J
45 public function testDeleteStateProvince() {
46 // Create
47 $create = $this->callAPISuccess('StateProvince', 'create', $this->_params);
48
49 // Delete
9099cab3 50 $result = $this->callAPIAndDocument('StateProvince', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__);
4d4819b0 51 $this->assertEquals(1, $result['count']);
9099cab3 52 $get = $this->callAPISuccess('StateProvince', 'get', [
4d4819b0 53 'id' => $create['id'],
9099cab3 54 ]);
4d4819b0
J
55 $this->assertEquals(0, $get['count'], 'State/province not successfully deleted');
56 }
57
58 /**
59 * Test with empty params
1e810dc9 60 * @dataProvider versionThreeAndFour
4d4819b0
J
61 */
62 public function testGetEmptyParams() {
9099cab3 63 $result = $this->callAPISuccess('StateProvince', 'Get', []);
4d4819b0
J
64 }
65
66 /**
67 * Test with wrong params
1e810dc9 68 * @dataProvider versionThreeAndFour
4d4819b0
J
69 */
70 public function testGetWrongParams() {
9099cab3 71 $this->callAPIFailure('StateProvince', 'Get', ['id' => 'abc']);
4d4819b0
J
72 }
73
74 /**
75 * Test get
1e810dc9 76 * @dataProvider versionThreeAndFour
4d4819b0
J
77 */
78 public function testGet() {
79 $province = $this->callAPISuccess('StateProvince', 'create', $this->_params);
9099cab3 80 $params = [
4d4819b0 81 'name' => $this->_params['name'],
9099cab3 82 ];
4d4819b0
J
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.
1e810dc9 90 * @dataProvider versionThreeAndFour
4d4819b0
J
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);
9099cab3 97 $check = $this->callAPISuccess('StateProvince', 'getcount', [
4d4819b0 98 'name' => $params['name'],
9099cab3 99 ]);
4d4819b0
J
100 $this->assertEquals(1, $check);
101 }
102
103}