Merge pull request #14974 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / api / v3 / OptionGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Class api_v3_OptionGroupTest
30 *
31 * @group headless
32 */
33 class api_v3_OptionGroupTest extends CiviUnitTestCase {
34
35 protected $_apiversion = 3;
36
37 protected $_entity = 'OptionGroup';
38
39 public function setUp() {
40 parent::setUp();
41 $this->useTransaction(TRUE);
42 $this->_params = [
43 'name' => 'our test Option Group',
44 'is_reserved' => 1,
45 'is_active' => 1,
46 ];
47 }
48
49 /**
50 * Good to test option group as a representative on the Camel Case.
51 */
52 public function testGetOptionGroupGetFields() {
53 $result = $this->callAPISuccess('option_group', 'getfields', []);
54 $this->assertFalse(empty($result['values']));
55 }
56
57 public function testGetOptionGroupGetFieldsCreateAction() {
58 $result = $this->callAPISuccess('option_group', 'getfields', ['action' => 'create']);
59 $this->assertFalse(empty($result['values']));
60 $this->assertEquals($result['values']['name']['api.unique'], 1);
61 }
62
63 public function testGetOptionGroupByID() {
64 $result = $this->callAPISuccess('option_group', 'get', ['id' => 1]);
65 $this->assertEquals(1, $result['count']);
66 $this->assertEquals(1, $result['id']);
67 }
68
69 public function testGetOptionGroupByName() {
70 $params = ['name' => 'preferred_communication_method'];
71 $result = $this->callAPIAndDocument('option_group', 'get', $params, __FUNCTION__, __FILE__);
72 $this->assertEquals(1, $result['count']);
73 $this->assertEquals(1, $result['id']);
74 }
75
76 public function testGetOptionGroup() {
77 $result = $this->callAPISuccess('option_group', 'get', []);
78 $this->assertGreaterThan(1, $result['count']);
79 }
80
81 public function testGetOptionDoesNotExist() {
82 $result = $this->callAPISuccess('option_group', 'get', ['name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB']);
83 $this->assertEquals(0, $result['count']);
84 }
85
86 public function testGetOptionCreateSuccess() {
87 $params = [
88 'sequential' => 1,
89 'name' => 'civicrm_event.amount.560',
90 'is_reserved' => 1,
91 'is_active' => 1,
92 'api.OptionValue.create' => [
93 'label' => 'workshop',
94 'value' => 35,
95 'is_default' => 1,
96 'is_active' => 1,
97 'format.only_id' => 1,
98 ],
99 ];
100 $result = $this->callAPIAndDocument('OptionGroup', 'create', $params, __FUNCTION__, __FILE__);
101 $this->assertEquals('civicrm_event.amount.560', $result['values'][0]['name']);
102 $this->assertTrue(is_int($result['values'][0]['api.OptionValue.create']));
103 $this->assertGreaterThan(0, $result['values'][0]['api.OptionValue.create']);
104 $this->callAPISuccess('OptionGroup', 'delete', ['id' => $result['id']]);
105 }
106
107 /**
108 * Test the error message when a failure is due to a key duplication issue.
109 */
110 public function testGetOptionCreateFailOnDuplicate() {
111 $params = [
112 'sequential' => 1,
113 'name' => 'civicrm_dup entry',
114 'is_reserved' => 1,
115 'is_active' => 1,
116 ];
117 $result1 = $this->callAPISuccess('OptionGroup', 'create', $params);
118 $result = $this->callAPIFailure('OptionGroup', 'create', $params, "Field: `name` must be unique. An conflicting entity already exists - id: " . $result1['id']);
119 $this->callAPISuccess('OptionGroup', 'delete', ['id' => $result1['id']]);
120 }
121
122 /**
123 * Test that transaction is completely rolled back on fail.
124 *
125 * Check error returned.
126 */
127 public function testGetOptionCreateFailRollback() {
128 $countFirst = $this->callAPISuccess('OptionGroup', 'getcount', ['options' => ['limit' => 5000]]);
129 $params = [
130 'sequential' => 1,
131 'name' => 'civicrm_rolback_test',
132 'is_reserved' => 1,
133 'is_active' => 1,
134 // executing within useTransactional() test case
135 'is_transactional' => 'nest',
136 'api.OptionValue.create' => [
137 'label' => 'invalid entry',
138 'value' => 35,
139 'domain_id' => 999,
140 'is_active' => '0',
141 'debug' => 0,
142 ],
143 ];
144 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
145 $countAfter = $this->callAPISuccess('OptionGroup', 'getcount', [
146 'options' => ['limit' => 5000],
147 ]);
148 $this->assertEquals($countFirst, $countAfter,
149 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
150 );
151 }
152
153 /**
154 * Success test for updating an existing Option Group.
155 */
156 public function testCreateUpdateOptionGroup() {
157 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
158 $params = array_merge($this->_params, ['id' => $result['id'], 'is_active' => 0]);
159 $this->callAPISuccess($this->_entity, 'create', $params);
160 $this->callAPISuccess('OptionGroup', 'delete', ['id' => $result['id']]);
161 }
162
163 /**
164 * Success test for deleting an existing Option Group.
165 */
166 public function testDeleteOptionGroup() {
167 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
168 $this->callAPIAndDocument('OptionGroup', 'delete', ['id' => $result['id']], __FUNCTION__, __FILE__);
169 }
170
171 /**
172 * Ensure only one option value exists after calling ensureOptionValueExists.
173 *
174 * @throws \CRM_Core_Exception
175 */
176 public function testEnsureOptionGroupExistsExistingValue() {
177 CRM_Core_BAO_OptionGroup::ensureOptionGroupExists(['name' => 'participant_role']);
178 $this->callAPISuccessGetSingle('OptionGroup', ['name' => 'participant_role']);
179 }
180
181 /**
182 * Ensure only one option value exists adds a new value.
183 */
184 public function testEnsureOptionGroupExistsNewValue() {
185 $optionGroupID = CRM_Core_BAO_OptionGroup::ensureOptionGroupExists([
186 'name' => 'Bombed',
187 'title' => ts('Catastrophy'),
188 'description' => ts('blah blah'),
189 'is_reserved' => 1,
190 ]);
191 $optionGroup = $this->callAPISuccessGetSingle('OptionGroup', ['name' => 'Bombed']);
192 $this->assertEquals($optionGroupID, $optionGroup['id']);
193 }
194
195 }