CRM-15168 fix test to cope with problems with getactions
[civicrm-core.git] / tests / phpunit / api / v3 / OptionGroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 function setUp() {
39 parent::setUp();
40 $this->_params = array(
41 'name' => 'our test Option Group',
42 'is_reserved' => 1,
43 'is_active' => 1,
44 );
45 }
46
47 function tearDown() {}
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 public function testGetOptionGroupGetFieldsCreateAction() {
57 $result = $this->callAPISuccess('option_group', 'getfields', array('action' => 'create'));
58 $this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
59 $this->assertEquals($result['values']['name']['api.unique'], 1);
60 }
61
62 public function testGetOptionGroupByID() {
63 $result = $this->callAPISuccess('option_group', 'get', array('id' => 1));
64 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
65 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
66 }
67
68 public function testGetOptionGroupByName() {
69 $params = array('name' => 'preferred_communication_method');
70 $result = $this->callAPIAndDocument('option_group', 'get', $params, __FUNCTION__, __FILE__);
71 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
72 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
73 }
74
75 public function testGetOptionGroup() {
76 $result = $this->callAPISuccess('option_group', 'get', array());
77 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__);
78 }
79
80 public function testGetOptionDoesNotExist() {
81 $result = $this->callAPISuccess('option_group', 'get', array('name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
82 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
83 }
84 public function testGetOptionCreateSuccess() {
85 $params = array('sequential' => 1, 'name' => 'civicrm_event.amount.560', 'is_reserved' => 1, 'is_active' => 1, 'api.OptionValue.create' => array('label' => 'workshop', 'value' => 35, 'is_default' => 1, 'is_active' => 1, 'format.only_id' => 1));
86 $result = $this->callAPIAndDocument('OptionGroup', 'create', $params, __FUNCTION__, __FILE__);
87 $this->assertEquals('civicrm_event.amount.560', $result['values'][0]['name'], 'In line ' . __LINE__);
88 $this->assertTrue(is_integer($result['values'][0]['api.OptionValue.create']));
89 $this->assertGreaterThan(0, $result['values'][0]['api.OptionValue.create']);
90 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result['id']));
91 }
92 /*
93 * Test the error message when a failure is due to a key duplication issue
94 */
95
96 public function testGetOptionCreateFailOnDuplicate() {
97 $params = array( 'sequential' => 1,
98 'name' => 'civicrm_dup entry',
99 'is_reserved' => 1,
100 'is_active' => 1,
101 );
102 $result1 = $this->callAPISuccess('OptionGroup', 'create', $params);
103 $result = $this->callAPIFailure('OptionGroup', 'create', $params, "Field: `name` must be unique. An conflicting entity already exists - id: " . $result1['id']);
104 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result1['id']));
105 }
106
107 /*
108 * Test that transaction is completely rolled back on fail.
109 * Also check error returned
110 */
111
112 public function testGetOptionCreateFailRollback() {
113 $countFirst = $this->callAPISuccess('OptionGroup', 'getcount', array(
114 'options' => array('limit' => 5000),
115 )
116 );
117 $params = array( 'sequential' => 1,
118 'name' => 'civicrm_rolback_test',
119 'is_reserved' => 1,
120 'is_active' => 1,
121 'api.OptionValue.create' => array(
122 'label' => 'invalid entry',
123 'value' => 35,
124 'domain_id' => 999,
125 'is_active' => '0',
126 'debug' => 0,
127 ),
128 );
129 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
130 $countAfter = $this->callAPISuccess('OptionGroup', 'getcount', array(
131 'options' => array('limit' => 5000),
132 )
133 );
134 $this->assertEquals($countFirst, $countAfter,
135 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
136 );
137 }
138
139 /**
140 * success test for updating an existing Option Group
141 */
142 public function testCreateUpdateOptionGroup() {
143 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
144 $params = array_merge($this->_params, array('id' => $result['id'], 'is_active' => 0));
145 $this->callAPISuccess($this->_entity, 'create', $params);
146 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result['id']));
147 }
148
149 /**
150 * success test for deleting an existing Option Group
151 */
152 public function testDeleteOptionGroup() {
153 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
154 $this->callAPIAndDocument('OptionGroup', 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
155 }
156 }
157