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