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