3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
31 * Class api_v3_OptionGroupTest
33 class api_v3_OptionGroupTest
extends CiviUnitTestCase
{
34 protected $_apiversion = 3;
36 protected $_entity = 'OptionGroup';
38 public function setUp() {
40 $this->useTransaction(TRUE);
41 $this->_params
= array(
42 'name' => 'our test Option Group',
49 * Good to test option group as a representative on the Camel Case.
51 public function testGetOptionGroupGetFields() {
52 $result = $this->callAPISuccess('option_group', 'getfields', array());
53 $this->assertFalse(empty($result['values']));
56 public function testGetOptionGroupGetFieldsCreateAction() {
57 $result = $this->callAPISuccess('option_group', 'getfields', array('action' => 'create'));
58 $this->assertFalse(empty($result['values']));
59 $this->assertEquals($result['values']['name']['api.unique'], 1);
62 public function testGetOptionGroupByID() {
63 $result = $this->callAPISuccess('option_group', 'get', array('id' => 1));
64 $this->assertEquals(1, $result['count']);
65 $this->assertEquals(1, $result['id']);
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']);
72 $this->assertEquals(1, $result['id']);
75 public function testGetOptionGroup() {
76 $result = $this->callAPISuccess('option_group', 'get', array());
77 $this->assertGreaterThan(1, $result['count']);
80 public function testGetOptionDoesNotExist() {
81 $result = $this->callAPISuccess('option_group', 'get', array('name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
82 $this->assertEquals(0, $result['count']);
85 public function testGetOptionCreateSuccess() {
88 'name' => 'civicrm_event.amount.560',
91 'api.OptionValue.create' => array(
92 'label' => 'workshop',
96 'format.only_id' => 1,
99 $result = $this->callAPIAndDocument('OptionGroup', 'create', $params, __FUNCTION__
, __FILE__
);
100 $this->assertEquals('civicrm_event.amount.560', $result['values'][0]['name']);
101 $this->assertTrue(is_int($result['values'][0]['api.OptionValue.create']));
102 $this->assertGreaterThan(0, $result['values'][0]['api.OptionValue.create']);
103 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result['id']));
107 * Test the error message when a failure is due to a key duplication issue.
109 public function testGetOptionCreateFailOnDuplicate() {
112 'name' => 'civicrm_dup entry',
116 $result1 = $this->callAPISuccess('OptionGroup', 'create', $params);
117 $result = $this->callAPIFailure('OptionGroup', 'create', $params, "Field: `name` must be unique. An conflicting entity already exists - id: " . $result1['id']);
118 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result1['id']));
122 * Test that transaction is completely rolled back on fail.
124 * Check error returned.
126 public function testGetOptionCreateFailRollback() {
127 $countFirst = $this->callAPISuccess('OptionGroup', 'getcount', array(
128 'options' => array('limit' => 5000),
133 'name' => 'civicrm_rolback_test',
136 'is_transactional' => 'nest', // executing within useTransactional() test case
137 'api.OptionValue.create' => array(
138 'label' => 'invalid entry',
145 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
146 $countAfter = $this->callAPISuccess('OptionGroup', 'getcount', array(
147 'options' => array('limit' => 5000),
150 $this->assertEquals($countFirst, $countAfter,
151 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
156 * Success test for updating an existing Option Group.
158 public function testCreateUpdateOptionGroup() {
159 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
160 $params = array_merge($this->_params
, array('id' => $result['id'], 'is_active' => 0));
161 $this->callAPISuccess($this->_entity
, 'create', $params);
162 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result['id']));
166 * Success test for deleting an existing Option Group.
168 public function testDeleteOptionGroup() {
169 $result = $this->callAPISuccess($this->_entity
, 'create', $this->_params
);
170 $this->callAPIAndDocument('OptionGroup', 'delete', array('id' => $result['id']), __FUNCTION__
, __FILE__
);