Merge pull request #11965 from colemanw/CRM-21855
[civicrm-core.git] / tests / phpunit / api / v3 / StateProvinceTest.php
CommitLineData
4d4819b0
J
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
8c9251b3 6 | Copyright CiviCRM LLC (c) 2004-2018 |
4d4819b0
J
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28/**
29 * Test APIv3 civicrm_state_province* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
33 * @group headless
34 */
35class api_v3_StateProvinceTest extends CiviUnitTestCase {
36 protected $_apiversion;
37 protected $_params;
38
39 public function setUp() {
40 $this->_apiversion = 3;
41 parent::setUp();
42 $this->useTransaction(TRUE);
43 $this->_params = array(
44 'name' => 'Wessex',
45 'abbreviation' => 'WEX',
46 'country_id' => 1226,
47 );
48 }
49
50 public function testCreateStateProvince() {
51 $result = $this->callAPIAndDocument('StateProvince', 'create', $this->_params, __FUNCTION__, __FILE__);
52 $this->assertEquals(1, $result['count']);
53 $this->assertNotNull($result['values'][$result['id']]['id']);
54 $this->callAPISuccess('StateProvince', 'delete', array('id' => $result['id']));
55 }
56
57 public function testDeleteStateProvince() {
58 // Create
59 $create = $this->callAPISuccess('StateProvince', 'create', $this->_params);
60
61 // Delete
62 $result = $this->callAPIAndDocument('StateProvince', 'delete', array('id' => $create['id']), __FUNCTION__, __FILE__);
63 $this->assertEquals(1, $result['count']);
64 $get = $this->callAPISuccess('StateProvince', 'get', array(
65 'id' => $create['id'],
66 ));
67 $this->assertEquals(0, $get['count'], 'State/province not successfully deleted');
68 }
69
70 /**
71 * Test with empty params
72 */
73 public function testGetEmptyParams() {
74 $result = $this->callAPISuccess('StateProvince', 'Get', array());
75 }
76
77 /**
78 * Test with wrong params
79 */
80 public function testGetWrongParams() {
81 $this->callAPIFailure('StateProvince', 'Get', array('id' => 'abc'));
82 }
83
84 /**
85 * Test get
86 */
87 public function testGet() {
88 $province = $this->callAPISuccess('StateProvince', 'create', $this->_params);
89 $params = array(
90 'name' => $this->_params['name'],
91 );
92 $result = $this->callAPIAndDocument('StateProvince', 'Get', $params, __FUNCTION__, __FILE__);
93 $this->assertEquals($province['values'][$province['id']]['name'], $result['values'][$province['id']]['name']);
94 $this->assertEquals($province['values'][$province['id']]['abbreviation'], $result['values'][$province['id']]['abbreviation']);
95 }
96
97 /**
98 * There cannot be two state/provinces with the same name in the same country.
99 */
100 public function testCreateDuplicateFail() {
101 $params = $this->_params;
102 unset($params['id']);
103 $this->callAPISuccess('StateProvince', 'create', $params);
104 $this->callAPIFailure('StateProvince', 'create', $params);
105 $check = $this->callAPISuccess('StateProvince', 'getcount', array(
106 'name' => $params['name'],
107 ));
108 $this->assertEquals(1, $check);
109 }
110
111}