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
32 class api_v3_OptionGroupTest
extends CiviUnitTestCase
{
33 protected $_apiversion = 3;
35 protected $_entity = 'OptionGroup';
37 public function setUp() {
39 $this->useTransaction(TRUE);
40 $this->_params
= array(
41 'name' => 'our test Option Group',
48 * Good to test option group as a representative on the Camel Case.
50 public function testGetOptionGroupGetFields() {
51 $result = $this->callAPISuccess('option_group', 'getfields', array());
52 $this->assertFalse(empty($result['values']));
55 public function testGetOptionGroupGetFieldsCreateAction() {
56 $result = $this->callAPISuccess('option_group', 'getfields', array('action' => 'create'));
57 $this->assertFalse(empty($result['values']));
58 $this->assertEquals($result['values']['name']['api.unique'], 1);
61 public function testGetOptionGroupByID() {
62 $result = $this->callAPISuccess('option_group', 'get', array('id' => 1));
63 $this->assertEquals(1, $result['count']);
64 $this->assertEquals(1, $result['id']);
67 public function testGetOptionGroupByName() {
68 $params = array('name' => 'preferred_communication_method');
69 $result = $this->callAPIAndDocument('option_group', 'get', $params, __FUNCTION__
, __FILE__
);
70 $this->assertEquals(1, $result['count']);
71 $this->assertEquals(1, $result['id']);
74 public function testGetOptionGroup() {
75 $result = $this->callAPISuccess('option_group', 'get', array());
76 $this->assertGreaterThan(1, $result['count']);
79 public function testGetOptionDoesNotExist() {
80 $result = $this->callAPISuccess('option_group', 'get', array('name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
81 $this->assertEquals(0, $result['count']);
84 public function testGetOptionCreateSuccess() {
87 'name' => 'civicrm_event.amount.560',
90 'api.OptionValue.create' => array(
91 'label' => 'workshop',
95 'format.only_id' => 1,
98 $result = $this->callAPIAndDocument('OptionGroup', 'create', $params, __FUNCTION__
, __FILE__
);
99 $this->assertEquals('civicrm_event.amount.560', $result['values'][0]['name']);
100 $this->assertTrue(is_int($result['values'][0]['api.OptionValue.create']));
101 $this->assertGreaterThan(0, $result['values'][0]['api.OptionValue.create']);
102 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result['id']));
106 * Test the error message when a failure is due to a key duplication issue.
108 public function testGetOptionCreateFailOnDuplicate() {
111 'name' => 'civicrm_dup entry',
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']));
121 * Test that transaction is completely rolled back on fail.
123 * Check error returned.
125 public function testGetOptionCreateFailRollback() {
126 $countFirst = $this->callAPISuccess('OptionGroup', 'getcount', array(
127 'options' => array('limit' => 5000),
132 'name' => 'civicrm_rolback_test',
135 'is_transactional' => 'nest', // executing within useTransactional() test case
136 'api.OptionValue.create' => array(
137 'label' => 'invalid entry',
144 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
145 $countAfter = $this->callAPISuccess('OptionGroup', 'getcount', array(
146 'options' => array('limit' => 5000),
149 $this->assertEquals($countFirst, $countAfter,
150 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
155 * Success test for updating an existing Option Group.
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']));
165 * Success test for deleting an existing Option Group.
167 public function testDeleteOptionGroup() {
168 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
169 $this->callAPIAndDocument('OptionGroup', 'delete', array('id' => $result['id']), __FUNCTION__
, __FILE__
);
173 * Ensure only one option value exists after calling ensureOptionValueExists.
175 public function testEnsureOptionGroupExistsExistingValue() {
176 CRM_Core_BAO_OptionGroup
::ensureOptionGroupExists(array('name' => 'participant_role'));
177 $this->callAPISuccessGetSingle('OptionGroup', array('name' => 'participant_role'));
181 * Ensure only one option value exists adds a new value.
183 public function testEnsureOptionGroupExistsNewValue() {
184 $optionGroupID = CRM_Core_BAO_OptionGroup
::ensureOptionGroupExists(array(
186 'title' => ts('Catastrophy'),
187 'description' => ts('blah blah'),
190 $optionGroup = $this->callAPISuccessGetSingle('OptionGroup', array('name' => 'Bombed'));
191 $this->assertEquals($optionGroupID, $optionGroup['id']);