3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class api_v3_OptionGroupTest
33 class api_v3_OptionGroupTest
extends CiviUnitTestCase
{
35 protected $_apiversion = 3;
37 protected $_entity = 'OptionGroup';
39 public function setUp() {
41 $this->useTransaction(TRUE);
43 'name' => 'our test Option Group',
50 * Good to test option group as a representative on the Camel Case.
52 public function testGetOptionGroupGetFields() {
53 $result = $this->callAPISuccess('option_group', 'getfields', []);
54 $this->assertFalse(empty($result['values']));
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);
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']);
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']);
76 public function testGetOptionGroup() {
77 $result = $this->callAPISuccess('option_group', 'get', []);
78 $this->assertGreaterThan(1, $result['count']);
81 public function testGetOptionDoesNotExist() {
82 $result = $this->callAPISuccess('option_group', 'get', ['name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB']);
83 $this->assertEquals(0, $result['count']);
86 public function testGetOptionCreateSuccess() {
89 'name' => 'civicrm_event.amount.560',
92 'api.OptionValue.create' => [
93 'label' => 'workshop',
97 'format.only_id' => 1,
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']]);
108 * Test the error message when a failure is due to a key duplication issue.
110 public function testGetOptionCreateFailOnDuplicate() {
113 'name' => 'civicrm_dup entry',
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']]);
123 * Test that transaction is completely rolled back on fail.
125 * Check error returned.
127 public function testGetOptionCreateFailRollback() {
128 $countFirst = $this->callAPISuccess('OptionGroup', 'getcount', ['options' => ['limit' => 5000]]);
131 'name' => 'civicrm_rolback_test',
134 // executing within useTransactional() test case
135 'is_transactional' => 'nest',
136 'api.OptionValue.create' => [
137 'label' => 'invalid entry',
144 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
145 $countAfter = $this->callAPISuccess('OptionGroup', 'getcount', [
146 'options' => ['limit' => 5000],
148 $this->assertEquals($countFirst, $countAfter,
149 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
154 * Success test for updating an existing Option Group.
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']]);
164 * Success test for deleting an existing Option Group.
166 public function testDeleteOptionGroup() {
167 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
168 $this->callAPIAndDocument('OptionGroup', 'delete', ['id' => $result['id']], __FUNCTION__
, __FILE__
);
172 * Ensure only one option value exists after calling ensureOptionValueExists.
174 * @throws \CRM_Core_Exception
176 public function testEnsureOptionGroupExistsExistingValue() {
177 CRM_Core_BAO_OptionGroup
::ensureOptionGroupExists(['name' => 'participant_role']);
178 $this->callAPISuccessGetSingle('OptionGroup', ['name' => 'participant_role']);
182 * Ensure only one option value exists adds a new value.
184 public function testEnsureOptionGroupExistsNewValue() {
185 $optionGroupID = CRM_Core_BAO_OptionGroup
::ensureOptionGroupExists([
187 'title' => ts('Catastrophy'),
188 'description' => ts('blah blah'),
191 $optionGroup = $this->callAPISuccessGetSingle('OptionGroup', ['name' => 'Bombed']);
192 $this->assertEquals($optionGroupID, $optionGroup['id']);