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