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