Merge pull request #2530 from monishdeb/CRM-14195
[civicrm-core.git] / tests / phpunit / api / v3 / OptionGroupTest.php
CommitLineData
6a488035 1<?php
b6708aeb 2/*
3 +--------------------------------------------------------------------+
232624b1 4| CiviCRM version 4.4 |
b6708aeb 5+--------------------------------------------------------------------+
6| Copyright CiviCRM LLC (c) 2004-2013 |
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*/
6a488035
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
29class api_v3_OptionGroupTest extends CiviUnitTestCase {
0505d9d2 30 protected $_apiversion = 3;
b7c9bc4c 31
4033343a 32 protected $_entity = 'OptionGroup';
6a488035
TO
33
34 function setUp() {
6a488035 35 parent::setUp();
4033343a 36 $this->_params = array(
37 'name' => 'our test Option Group',
38 'is_reserved' => 1,
39 'is_active' => 1,
40 );
6a488035
TO
41 }
42
43 function tearDown() {}
44 /*
45 * Good to test option group as a representative on the Camel Case
46 */
47
48 public function testGetOptionGroupGetFields() {
4033343a 49 $result = $this->callAPISuccess('option_group', 'getfields', array());
6a488035
TO
50 $this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
51 }
52 public function testGetOptionGroupGetFieldsCreateAction() {
0505d9d2 53 $result = $this->callAPISuccess('option_group', 'getfields', array('action' => 'create'));
6a488035
TO
54 $this->assertFalse(empty($result['values']), 'In line ' . __LINE__);
55 $this->assertEquals($result['values']['name']['api.unique'], 1);
56 }
57
58 public function testGetOptionGroupByID() {
0505d9d2 59 $result = $this->callAPISuccess('option_group', 'get', array('id' => 1));
6a488035
TO
60 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
61 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
62 }
63
64 public function testGetOptionGroupByName() {
0505d9d2 65 $params = array('name' => 'preferred_communication_method');
4033343a 66 $result = $this->callAPIAndDocument('option_group', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
67 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__);
68 $this->assertEquals(1, $result['id'], 'In line ' . __LINE__);
69 }
70
71 public function testGetOptionGroup() {
0505d9d2 72 $result = $this->callAPISuccess('option_group', 'get', array());
6a488035
TO
73 $this->assertGreaterThan(1, $result['count'], 'In line ' . __LINE__);
74 }
75
76 public function testGetOptionDoesNotExist() {
0505d9d2 77 $result = $this->callAPISuccess('option_group', 'get', array('name' => 'FSIGUBSFGOMUUBSFGMOOUUBSFGMOOBUFSGMOOIIB'));
6a488035
TO
78 $this->assertEquals(0, $result['count'], 'In line ' . __LINE__);
79 }
80 public function testGetOptionCreateSuccess() {
0505d9d2 81 $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));
4033343a 82 $result = $this->callAPIAndDocument('OptionGroup', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
83 $this->assertEquals('civicrm_event.amount.560', $result['values'][0]['name'], 'In line ' . __LINE__);
84 $this->assertTrue(is_integer($result['values'][0]['api.OptionValue.create']));
85 $this->assertGreaterThan(0, $result['values'][0]['api.OptionValue.create']);
0505d9d2 86 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result['id']));
6a488035
TO
87 }
88 /*
89 * Test the error message when a failure is due to a key duplication issue
90 */
91
92 public function testGetOptionCreateFailOnDuplicate() {
0505d9d2 93 $params = array( 'sequential' => 1,
6a488035
TO
94 'name' => 'civicrm_dup entry',
95 'is_reserved' => 1,
96 'is_active' => 1,
97 );
4033343a 98 $result1 = $this->callAPISuccess('OptionGroup', 'create', $params);
99 $result = $this->callAPIFailure('OptionGroup', 'create', $params, "Field: `name` must be unique. An conflicting entity already exists - id: " . $result1['id']);
100 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result1['id']));
6a488035
TO
101 }
102
103 /*
104 * Test that transaction is completely rolled back on fail.
105 * Also check error returned
106 */
107
108 public function testGetOptionCreateFailRollback() {
0505d9d2 109 $countFirst = $this->callAPISuccess('OptionGroup', 'getcount', array(
110 'options' => array('limit' => 5000),
6a488035
TO
111 )
112 );
0505d9d2 113 $params = array( 'sequential' => 1,
6a488035
TO
114 'name' => 'civicrm_rolback_test',
115 'is_reserved' => 1,
116 'is_active' => 1,
117 'api.OptionValue.create' => array(
118 'label' => 'invalid entry',
119 'value' => 35,
120 'domain_id' => 999,
121 'is_active' => '0',
122 'debug' => 0,
123 ),
124 );
d0e1eff2 125 $result = $this->callAPIFailure('OptionGroup', 'create', $params);
0505d9d2 126 $countAfter = $this->callAPISuccess('OptionGroup', 'getcount', array(
127 'options' => array('limit' => 5000),
6a488035
TO
128 )
129 );
130 $this->assertEquals($countFirst, $countAfter,
131 'Count of option groups should not have changed due to rollback triggered by option value In line ' . __LINE__
132 );
133 }
4033343a 134
135 /**
136 * success test for updating an existing Option Group
137 */
138 public function testCreateUpdateOptionGroup() {
139 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
140 $params = array_merge($this->_params, array('id' => $result['id'], 'is_active' => 0));
141 $this->callAPISuccess($this->_entity, 'create', $params);
142 $this->callAPISuccess('OptionGroup', 'delete', array('id' => $result['id']));
143 }
144
145 /**
146 * success test for deleting an existing Option Group
147 */
148 public function testDeleteOptionGroup() {
149 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
150 $this->callAPIAndDocument('OptionGroup', 'delete', array('id' => $result['id']), __FUNCTION__, __FILE__);
151 }
6a488035
TO
152}
153