4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 class api_v3_OptionGroupTest
extends CiviUnitTestCase
{
31 protected $_apiversion;
32 public $_eNoticeCompliant = TRUE;
35 $this->_apiversion
= 3;
39 function tearDown() {}
41 * Good to test option group as a representative on the Camel Case
44 public function testGetOptionGroupGetFields() {
45 $result = civicrm_api('option_group', 'getfields', array('version' => 3));
46 $this->assertFalse(empty($result['values']), 'In line ' . __LINE__
);
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);
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__
);
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__
);
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__
);
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__
);
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']));
92 * Test the error message when a failure is due to a key duplication issue
95 public function testGetOptionCreateFailOnDuplicate() {
97 'version' => $this->_apiversion
,
99 'name' => 'civicrm_dup entry',
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']);
111 * Test that transaction is completely rolled back on fail.
112 * Also check error returned
115 public function testGetOptionCreateFailRollback() {
116 $countFirst = civicrm_api('OptionGroup', 'getcount', array(
118 'options' => array('limit' => 5000),
122 'version' => $this->_apiversion
,
124 'name' => 'civicrm_rolback_test',
127 'api.OptionValue.create' => array(
128 'label' => 'invalid entry',
135 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
136 $countAfter = civicrm_api('OptionGroup', 'getcount', array(
138 'options' => array('limit' => 5000),
141 $this->assertEquals($countFirst, $countAfter,
142 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__