commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-old / civicrm / tests / phpunit / api / v3 / GroupTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * Test class for Group API - civicrm_group_*
32 *
33 * @package CiviCRM_APIv3
34 */
35 class api_v3_GroupTest extends CiviUnitTestCase {
36 protected $_apiversion;
37 protected $_groupID;
38
39 public function setUp() {
40 $this->_apiversion = 3;
41
42 parent::setUp();
43 $this->_groupID = $this->groupCreate();
44 }
45
46 public function tearDown() {
47
48 $this->groupDelete($this->_groupID);
49 }
50
51 public function testgroupCreateNoTitle() {
52 $params = array(
53 'name' => 'Test Group No title ',
54 'domain_id' => 1,
55 'description' => 'New Test Group Created',
56 'is_active' => 1,
57 'visibility' => 'Public Pages',
58 'group_type' => array(
59 '1' => 1,
60 '2' => 1,
61 ),
62 );
63
64 $group = $this->callAPIFailure('group', 'create', $params, 'Mandatory key(s) missing from params array: title');
65 }
66
67
68 public function testGetGroupWithEmptyParams() {
69 $group = $this->callAPISuccess('group', 'get', $params = array());
70
71 $group = $group["values"];
72 $this->assertNotNull(count($group));
73 $this->assertEquals($group[$this->_groupID]['name'], "Test Group 1");
74 $this->assertEquals($group[$this->_groupID]['is_active'], 1);
75 $this->assertEquals($group[$this->_groupID]['visibility'], 'Public Pages');
76 }
77
78 public function testGetGroupParamsWithGroupId() {
79 $params = array('id' => $this->_groupID);
80 $group = $this->callAPISuccess('group', 'get', $params);
81
82 foreach ($group['values'] as $v) {
83 $this->assertEquals($v['name'], "Test Group 1");
84 $this->assertEquals($v['title'], 'New Test Group Created');
85 $this->assertEquals($v['description'], 'New Test Group Created');
86 $this->assertEquals($v['is_active'], 1);
87 $this->assertEquals($v['visibility'], 'Public Pages');
88 }
89 }
90
91 public function testGetGroupParamsWithGroupName() {
92 $params = array(
93 'name' => "Test Group 1",
94 );
95 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__);
96 $group = $group['values'];
97
98 foreach ($group as $v) {
99 $this->assertEquals($v['id'], $this->_groupID);
100 $this->assertEquals($v['title'], 'New Test Group Created');
101 $this->assertEquals($v['description'], 'New Test Group Created');
102 $this->assertEquals($v['is_active'], 1);
103 $this->assertEquals($v['visibility'], 'Public Pages');
104 }
105 }
106
107 public function testGetGroupParamsWithReturnName() {
108 $params = array();
109 $params['id'] = $this->_groupID;
110 $params['return.name'] = 1;
111 $group = $this->callAPISuccess('group', 'get', $params);
112 $this->assertEquals($group['values'][$this->_groupID]['name'],
113 "Test Group 1"
114 );
115 }
116
117 public function testGetGroupParamsWithGroupTitle() {
118 $params = array();
119 $params['title'] = 'New Test Group Created';
120 $group = $this->callAPISuccess('group', 'get', $params);
121
122 foreach ($group['values'] as $v) {
123 $this->assertEquals($v['id'], $this->_groupID);
124 $this->assertEquals($v['name'], "Test Group 1");
125 $this->assertEquals($v['description'], 'New Test Group Created');
126 $this->assertEquals($v['is_active'], 1);
127 $this->assertEquals($v['visibility'], 'Public Pages');
128 }
129 }
130
131 /**
132 * Test Group create with Group Type and Parent
133 */
134 public function testGroupCreateWithTypeAndParent() {
135 $params = array(
136 'name' => 'Test Group type',
137 'title' => 'Test Group Type',
138 'description' => 'Test Group with Group Type',
139 'is_active' => 1,
140 //check for empty parent
141 'parents' => "",
142 'visibility' => 'Public Pages',
143 'group_type' => array(1, 2),
144 );
145 $result = $this->callAPISuccess('Group', 'create', $params);
146 $group = $result['values'][$result['id']];
147 $this->assertEquals($group['name'], "Test Group type");
148 $this->assertEquals($group['is_active'], 1);
149 $this->assertEquals($group['parents'], "");
150 $this->assertEquals($group['group_type'], $params['group_type']);
151 //assert single value for group_type and parent
152 $params = array_merge($params, array(
153 'name' => 'Test Group 2',
154 'title' => 'Test Group 2',
155 'group_type' => 2,
156 'parents' => $result['id'],
157 )
158 );
159 $result = $this->callAPISuccess('Group', 'create', $params);
160 $group = $result["values"][$result['id']];
161 $this->assertEquals($group['group_type'], array($params['group_type']));
162 $this->assertEquals($group['parents'], $params['parents']);
163 }
164
165 public function testGetNonExistingGroup() {
166 $params = array();
167 $params['title'] = 'No such group Exist';
168 $group = $this->callAPISuccess('group', 'get', $params);
169 $this->assertEquals(0, $group['count']);
170 }
171
172 public function testgroupdeleteParamsnoId() {
173 $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id');
174 }
175
176 public function testgetfields() {
177 $description = "Demonstrate use of getfields to interrogate api.";
178 $params = array('action' => 'create');
179 $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description);
180 $this->assertEquals(1, $result['values']['is_active']['api.default']);
181 }
182
183 public function testIllegalParentsParams() {
184 $params = array(
185 'title' => 'Test illegal Group',
186 'domain_id' => 1,
187 'description' => 'Testing illegal Parents params',
188 'is_active' => 1,
189 'parents' => "(SELECT api_key FROM civicrm_contact where id = 1)",
190 );
191 $this->callAPIFailure('group', 'create', $params);
192 unset($params['parents']);
193 $this->callAPISuccess('group', 'create', $params);
194 $group1 = $this->callAPISuccess('group', 'get', array(
195 'title' => 'Test illegal Group',
196 'parents' => array('IS NOT NULL' => 1),
197 ));
198 $this->assertEquals(0, $group1['count']);
199 $params['title'] = 'Test illegal Group 2';
200 $params['parents'] = array();
201 $params['parents'][$this->_groupID] = 'test Group';
202 $params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"] = "Test";
203 $group2 = $this->callAPIFailure('group', 'create', $params);
204 unset($params['parents']["(SELECT api_key FROM civicrm_contact where id = 1)"]);
205 $group2 = $this->callAPISuccess('group', 'create', $params);
206 $this->assertEquals(count($group2['values'][$group2['id']]['parents']), 1);
207 }
208
209 }