Mass update tests to use callAPIFailure
[civicrm-core.git] / tests / phpunit / api / v3 / OptionGroupTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 class api_v3_OptionGroupTest extends CiviUnitTestCase {
31 protected $_apiversion;
32 public $_eNoticeCompliant = TRUE;
33
34 function setUp() {
35 $this->_apiversion = 3;
36 parent::setUp();
37 }
38
39 function tearDown() {}
40 /*
41 * Good to test option group as a representative on the Camel Case
42 */
43
44 public function testGetOptionGroupGetFields() {
45 $result = civicrm_api('option_group', 'getfields', array('version' => 3));
46 $this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
47 }
48 public function testGetOptionGroupGetFieldsCreateAction() {
49 $result = civicrm_api('option_group', 'getfields', array('action' => 'create', 'version' => 3));
50 $this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
51 $this->assertEquals($result['values']['name']['api.unique'], 1);
52 }
53
54 public function testGetOptionGroupByID() {
55 $result = civicrm_api('option_group', 'get', array('id' => 1, 'version' => 3));
56 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
57 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
58 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
59 }
60
61 public function testGetOptionGroupByName() {
62 $params = array('name' => 'preferred_communication_method', 'version' => 3);
63 $result = civicrm_api('option_group', 'get', $params);
64 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
65 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
66 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
67 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
68 }
69
70 public function testGetOptionGroup() {
71 $result = civicrm_api('option_group', 'get', array('version' => 3));
72 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
73 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__);
74 }
75
76 public function testGetOptionDoesNotExist() {
77 $result = civicrm_api('option_group', 'get', array('name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB', 'version' => 3));
78 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
79 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
80 }
81 public function testGetOptionCreateSuccess() {
82 $params = array('version' => $this->_apiversion, '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));
83 $result = civicrm_api('OptionGroup', 'create', $params);
84 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
85 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__);
86 $this->assertEquals('civicrm_event.amount.560', $result['values'][0]['name'], 'In line ' . __LINE__);
87 $this->assertTrue(is_integer($result['values'][0]['api.OptionValue.create']));
88 $this->assertGreaterThan(0, $result['values'][0]['api.OptionValue.create']);
89 civicrm_api('OptionGroup', 'delete', array('version' => 3, 'id' => $result['id']));
90 }
91 /*
92 * Test the error message when a failure is due to a key duplication issue
93 */
94
95 public function testGetOptionCreateFailOnDuplicate() {
96 $params = array(
97 'version' => $this->_apiversion,
98 'sequential' => 1,
99 'name' => 'civicrm_dup entry',
100 'is_reserved' => 1,
101 'is_active' => 1,
102 );
103 $result1 = civicrm_api('OptionGroup', 'create', $params);
104 $this->assertAPISuccess($result1);
105 $result = civicrm_api('OptionGroup', 'create', $params);
106 civicrm_api('OptionGroup', 'delete', array('version' => 3, 'id' => $result1['id']));
107 $this->assertEquals("Field: `name` must be unique. An conflicting entity already exists - id: " . $result1['id'], $result['error_message']);
108 }
109
110 /*
111 * Test that transaction is completely rolled back on fail.
112 * Also check error returned
113 */
114
115 public function testGetOptionCreateFailRollback() {
116 $countFirst = civicrm_api('OptionGroup', 'getcount', array(
117 'version' => 3,
118 'options' => array('limit' => 5000),
119 )
120 );
121 $params = array(
122 'version' => $this->_apiversion,
123 'sequential' => 1,
124 'name' => 'civicrm_rolback_test',
125 'is_reserved' => 1,
126 'is_active' => 1,
127 'api.OptionValue.create' => array(
128 'label' => 'invalid entry',
129 'value' => 35,
130 'domain_id' => 999,
131 'is_active' => '0',
132 'debug' => 0,
133 ),
134 );
135 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
136 $countAfter = civicrm_api('OptionGroup', 'getcount', array(
137 'version' => 3,
138 'options' => array('limit' => 5000),
139 )
140 );
141 $this->assertEquals($countFirst, $countAfter,
142 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
143 );
144 }
145 }
146