672def2d32007cfe5f71d55c9d7993e7b2ad452f
[civicrm-core.git] / tests / phpunit / api / v3 / OptionValueTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30 class api_v3_OptionValueTest extends CiviUnitTestCase {
31 protected $_apiversion = 3;
32 public $_eNoticeCompliant = TRUE;
33 function setUp() {
34 parent::setUp();
35 }
36
37 function tearDown() {}
38
39
40 public function testGetCount() {
41 $result = $this->callAPISuccess('option_value', 'getcount', array());
42 $this->assertGreaterThan(100, $result);
43 }
44
45 public function testGetOptionValueByID() {
46 $result = $this->callAPISuccess('option_value', 'get', array('id' => 1));
47 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
48 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
49 }
50
51 public function testGetOptionValueByValue() {
52 $result = $this->callAPISuccess('option_value', 'get', array('option_group_id' => 1, 'value' => '1'));
53 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
54 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
55 }
56
57 /**
58 * Test limit param
59 */
60 function testGetOptionValueLimit() {
61 $params = array();
62 $result = $this->callAPISuccess('option_value', 'get', $params);
63 $this->assertGreaterThan(1, $result['count'], "Check more than one exists In line " . __LINE__);
64 $params['options']['limit'] = 1;
65 $result = $this->callAPISuccess('option_value', 'get', $params);
66 $this->assertEquals(1, $result['count'], "Check only 1 retrieved " . __LINE__);
67 }
68
69 /**
70 * Test offset param
71 */
72 function testGetOptionValueOffSet() {
73
74 $result = $this->callAPISuccess('option_value', 'get', array(
75 'option_group_id' => 1,
76 'value' => '1', ));
77 $result2 = $this->callAPISuccess('option_value', 'get', array(
78 'option_group_id' => 1,
79 'value' => '1',
80 'options' => array('offset' => 1),
81 ));
82 $this->assertGreaterThan($result2['count'], $result['count']);
83 }
84
85 /**
86 * Test offset param
87 */
88 function testGetSingleValueOptionValueSort() {
89 $description = "demonstrates use of Sort param (available in many api functions). Also, getsingle";
90 $subfile = 'SortOption';
91 $result = $this->callAPISuccess('option_value', 'getsingle', array(
92 'option_group_id' => 1, 'options' => array(
93 'sort' => 'label ASC',
94 'limit' => 1,
95 ),
96 ));
97 $params = array(
98 'option_group_id' => 1, 'options' => array(
99 'sort' => 'label DESC',
100 'limit' => 1,
101 ),
102 );
103 $result2 = $this->callAPIAndDocument('option_value', 'getsingle', $params, __FUNCTION__, __FILE__, $description, $subfile);
104 $this->assertGreaterThan($result['label'], $result2['label']);
105 }
106
107 /**
108 * Try to emulate a pagination: fetch the first page of 10 options, then fetch the second page with an offset of 9 (instead of 10) and check the start of the second page is the end of the 1st one.
109 */
110 function testGetValueOptionPagination() {
111 $pageSize = 10;
112 $page1 = $this->callAPISuccess('option_value', 'get', array('options' => array('limit' => $pageSize), ));
113 $page2 = $this->callAPISuccess('option_value', 'get', array(
114 'options' => array('limit' => $pageSize,
115 // if you use it for pagination, option.offset=pageSize*pageNumber
116 'offset' => $pageSize - 1,
117 ), ));
118 $this->assertEquals($pageSize, $page1['count'], "Check only 10 retrieved in the 1st page " . __LINE__);
119 $this->assertEquals($pageSize, $page2['count'], "Check only 10 retrieved in the 2nd page " . __LINE__);
120
121 $last = array_pop($page1['values']);
122 $first = array_shift($page2['values']);
123
124 $this->assertEquals($first, $last, "the first item of the second page should be the last of the 1st page" . __LINE__);
125 }
126
127 public function testGetOptionGroup() {
128 $params = array('option_group_id' => 1);
129 $result = $this->callAPIAndDocument('option_value', 'get', $params, __FUNCTION__, __FILE__);
130 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__);
131 }
132 /*
133 * test that using option_group_name returns more than 1 & less than all
134 */
135
136
137
138 public function testGetOptionGroupByName() {
139 $activityTypesParams = array('option_group_name' => 'activity_type', 'option.limit' => 100);
140 $params = array('option.limit' => 100);
141 $activityTypes = $this->callAPISuccess('option_value', 'get', $activityTypesParams);
142 $result = $this->callAPISuccess('option_value', 'get', $params);
143 $this->assertGreaterThan(1, $activityTypes['count'], 'In line ' . __LINE__);
144 $this->assertGreaterThan($activityTypes['count'], $result['count'], 'In line ' . __LINE__);
145 }
146 public function testGetOptionDoesNotExist() {
147 $result = $this->callAPISuccess('option_value', 'get', array('label' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
148 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
149 }
150 /*
151 * Check that domain_id is honoured
152 */
153 public function testCreateOptionSpecifyDomain() {
154 $result = $this->callAPISuccess('option_group', 'get', array(
155 'name' => 'from_email_address',
156 'sequential' => 1,
157 'api.option_value.create' => array('domain_id' => 2, 'name' => 'my@y.com'),
158 ));
159
160 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
161 $domain_id = $this->callAPISuccess('option_value', 'getvalue', array(
162 'id' => $optionValueId,
163 'return' => 'domain_id',
164 ));
165 $this->assertEquals(2, $domain_id);
166 }
167 /*
168 * Check that component_id is honoured
169 */
170 public function testCreateOptionSpecifyComponentID() {
171 $result = $this->callAPISuccess('option_group', 'get', array(
172 'name' => 'from_email_address',
173 'sequential' => 1, 'api.option_value.create' => array('component_id' => 2, 'name' => 'my@y.com'),
174 ));
175 $this->assertAPISuccess($result);
176 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
177 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
178 'id' => $optionValueId, 'return' => 'component_id',
179 ));
180 $this->assertEquals(2, $component_id);
181 }
182 /*
183 * Check that component continues to be honoured
184 */
185 public function testCreateOptionSpecifyComponent() {
186 $result = $this->callAPISuccess('option_group', 'get', array(
187 'name' => 'from_email_address',
188 'sequential' => 1, 'api.option_value.create' => array(
189 'component_id' => 'CiviContribute',
190 'name' => 'my@y.com'
191 ),
192
193 ));
194 $this->assertAPISuccess($result);
195 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
196 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
197 'id' => $optionValueId, 'return' => 'component_id',
198 ));
199 $this->assertEquals(2, $component_id);
200 }
201 /*
202 * Check that component string is honoured
203 */
204 public function testCreateOptionSpecifyComponentString() {
205 $result = $this->callAPISuccess('option_group', 'get', array(
206 'name' => 'from_email_address',
207 'sequential' => 1, 'api.option_value.create' => array(
208 'component_id' => 'CiviContribute',
209 'name' => 'my@y.com'),
210
211 ));
212 $this->assertAPISuccess($result);
213 $optionValueId = $result['values'][0]['api.option_value.create']['id'];
214 $component_id = $this->callAPISuccess('option_value', 'getvalue', array(
215 'id' => $optionValueId, 'return' => 'component_id',
216 ));
217 $this->assertEquals(2, $component_id);
218 }
219 /*
220 * Check that domain_id is honoured
221 */
222 public function testCRM12133CreateOptionWeightNoValue() {
223 $optionGroup = $this->callAPISuccess(
224 'option_group', 'get', array(
225 'name' => 'gender',
226 'sequential' => 1, ));
227 $this->assertAPISuccess($optionGroup);
228 $params = array(
229 'option_group_id' => $optionGroup['id'],
230 'label' => 'my@y.com', 'weight' => 3,
231 );
232 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
233 $this->assertAPISuccess($optionValue);
234 $params['weight'] = 4;
235 $optionValue2 = $this->callAPISuccess('option_value', 'create', $params );
236 $this->assertAPISuccess($optionValue2);
237 $options = $this->callAPISuccess('option_value', 'get', array('option_group_id' => $optionGroup['id']));
238 $this->assertNotEquals($options['values'][$optionValue['id']]['value'], $options['values'][$optionValue2['id']]['value']);
239
240 //cleanup
241 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue['id']));
242 $this->callAPISuccess('option_value', 'delete', array('id' => $optionValue2['id']));
243 }
244
245 /*
246 * Check that domain_id is honoured
247 */
248 public function testCreateOptionNoName() {
249 $optionGroup = $this->callAPISuccess('option_group', 'get', array(
250 'name' => 'gender',
251 'sequential' => 1, ));
252
253 $params = array('option_group_id' => $optionGroup['id'], 'label' => 'my@y.com');
254 $optionValue = $this->callAPISuccess('option_value', 'create', $params);
255 $this->assertAPISuccess($optionValue);
256 $this->getAndCheck($params, $optionValue['id'], 'option_value');
257 }
258
259 /**
260 * Check that pseudoconstant reflects new value added
261 * and deleted
262 */
263 public function testCRM11876CreateOptionPseudoConstantUpdated() {
264 $optionGroupID = $this->callAPISuccess('option_group', 'getvalue', array( 'name' => 'payment_instrument',
265 'return' => 'id',
266 ));
267 $newOption = (string) time();
268 $apiResult = $this->callAPISuccess('option_value', 'create', array(
269 'option_group_id' => $optionGroupID,
270 'label' => $newOption,
271 ));
272
273 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
274 $this->assertTrue(in_array($newOption, $fields['values']));
275
276 $this->callAPISuccess('option_value', 'delete', array('id' => $apiResult['id']));
277
278 $fields = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
279 $this->assertFalse(in_array($newOption, $fields['values']));
280 }
281 }
282