3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Class api_v3_OptionGroupTest
17 class api_v3_OptionGroupTest
extends CiviUnitTestCase
{
19 protected $_apiversion = 3;
21 protected $_entity = 'OptionGroup';
23 public function setUp() {
25 $this->useTransaction(TRUE);
27 'name' => 'our test Option Group',
34 * Good to test option group as a representative on the Camel Case.
36 public function testGetOptionGroupGetFields() {
37 $result = $this->callAPISuccess('option_group', 'getfields', []);
38 $this->assertFalse(empty($result['values']));
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);
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']);
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']);
60 public function testGetOptionGroup() {
61 $result = $this->callAPISuccess('option_group', 'get', []);
62 $this->assertGreaterThan(1, $result['count']);
65 public function testGetOptionDoesNotExist() {
66 $result = $this->callAPISuccess('option_group', 'get', ['name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB']);
67 $this->assertEquals(0, $result['count']);
70 public function testGetOptionCreateSuccess() {
73 'name' => 'civicrm_event.amount.560',
76 'api.OptionValue.create' => [
77 'label' => 'workshop',
81 'format.only_id' => 1,
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']]);
92 * Test the error message when a failure is due to a key duplication issue.
94 public function testGetOptionCreateFailOnDuplicate() {
97 'name' => 'civicrm_dup_entry',
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']]);
107 * Test that transaction is completely rolled back on fail.
109 * Check error returned.
111 public function testGetOptionCreateFailRollback() {
112 $countFirst = $this->callAPISuccess('OptionGroup', 'getcount', ['options' => ['limit' => 5000]]);
115 'name' => 'civicrm_rolback_test',
118 // executing within useTransactional() test case
119 'is_transactional' => 'nest',
120 'api.OptionValue.create' => [
121 'label' => 'invalid entry',
128 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
129 $countAfter = $this->callAPISuccess('OptionGroup', 'getcount', [
130 'options' => ['limit' => 5000],
132 $this->assertEquals($countFirst, $countAfter,
133 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
138 * Success test for updating an existing Option Group.
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']]);
148 * Success test for deleting an existing Option Group.
150 public function testDeleteOptionGroup() {
151 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
152 $this->callAPIAndDocument('OptionGroup', 'delete', ['id' => $result['id']], __FUNCTION__
, __FILE__
);
156 * Ensure only one option value exists after calling ensureOptionValueExists.
158 * @throws \CRM_Core_Exception
160 public function testEnsureOptionGroupExistsExistingValue() {
161 CRM_Core_BAO_OptionGroup
::ensureOptionGroupExists(['name' => 'participant_role']);
162 $this->callAPISuccessGetSingle('OptionGroup', ['name' => 'participant_role']);
166 * Ensure only one option value exists adds a new value.
168 public function testEnsureOptionGroupExistsNewValue() {
169 $optionGroupID = CRM_Core_BAO_OptionGroup
::ensureOptionGroupExists([
171 'title' => ts('Catastrophy'),
172 'description' => ts('blah blah'),
175 $optionGroup = $this->callAPISuccessGetSingle('OptionGroup', ['name' => 'Bombed']);
176 $this->assertEquals($optionGroupID, $optionGroup['id']);