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