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