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