CRM-19068 Fix SQLI in parents in group.create api call
[civicrm-core.git] / tests / phpunit / api / v3 / GroupTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Test class for Group API - civicrm_group_*
30 *
6c6e6187 31 * @package CiviCRM_APIv3
acb109b7 32 * @group headless
6a488035 33 */
6a488035
TO
34class api_v3_GroupTest extends CiviUnitTestCase {
35 protected $_apiversion;
36 protected $_groupID;
6a488035 37
00be9182 38 public function setUp() {
6a488035
TO
39 $this->_apiversion = 3;
40
41 parent::setUp();
fadb804f 42 $this->_groupID = $this->groupCreate();
6a488035
TO
43 }
44
00be9182 45 public function tearDown() {
6a488035
TO
46
47 $this->groupDelete($this->_groupID);
48 }
49
00be9182 50 public function testgroupCreateNoTitle() {
6a488035
TO
51 $params = array(
52 'name' => 'Test Group No title ',
53 'domain_id' => 1,
54 'description' => 'New Test Group Created',
55 'is_active' => 1,
56 'visibility' => 'Public Pages',
57 'group_type' => array(
58 '1' => 1,
59 '2' => 1,
60 ),
61 );
62
b26e776b 63 $group = $this->callAPIFailure('group', 'create', $params, 'Mandatory key(s) missing from params array: title');
6a488035
TO
64 }
65
6a488035 66
00be9182 67 public function testGetGroupWithEmptyParams() {
b26e776b 68 $group = $this->callAPISuccess('group', 'get', $params = array());
6a488035
TO
69
70 $group = $group["values"];
71 $this->assertNotNull(count($group));
5667b84b 72 $this->assertEquals($group[$this->_groupID]['name'], "Test Group 1");
6a488035
TO
73 $this->assertEquals($group[$this->_groupID]['is_active'], 1);
74 $this->assertEquals($group[$this->_groupID]['visibility'], 'Public Pages');
75 }
76
00be9182 77 public function testGetGroupParamsWithGroupId() {
b26e776b 78 $params = array('id' => $this->_groupID);
79 $group = $this->callAPISuccess('group', 'get', $params);
6a488035
TO
80
81 foreach ($group['values'] as $v) {
5667b84b 82 $this->assertEquals($v['name'], "Test Group 1");
6a488035
TO
83 $this->assertEquals($v['title'], 'New Test Group Created');
84 $this->assertEquals($v['description'], 'New Test Group Created');
85 $this->assertEquals($v['is_active'], 1);
86 $this->assertEquals($v['visibility'], 'Public Pages');
87 }
88 }
89
00be9182 90 public function testGetGroupParamsWithGroupName() {
b26e776b 91 $params = array(
5667b84b 92 'name' => "Test Group 1",
b26e776b 93 );
94 $group = $this->callAPIAndDocument('group', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
95 $group = $group['values'];
96
97 foreach ($group as $v) {
98 $this->assertEquals($v['id'], $this->_groupID);
99 $this->assertEquals($v['title'], 'New Test Group Created');
100 $this->assertEquals($v['description'], 'New Test Group Created');
101 $this->assertEquals($v['is_active'], 1);
102 $this->assertEquals($v['visibility'], 'Public Pages');
103 }
104 }
105
00be9182 106 public function testGetGroupParamsWithReturnName() {
b26e776b 107 $params = array();
6a488035
TO
108 $params['id'] = $this->_groupID;
109 $params['return.name'] = 1;
b26e776b 110 $group = $this->callAPISuccess('group', 'get', $params);
6a488035 111 $this->assertEquals($group['values'][$this->_groupID]['name'],
5667b84b 112 "Test Group 1"
6a488035
TO
113 );
114 }
115
00be9182 116 public function testGetGroupParamsWithGroupTitle() {
5667b84b 117 $params = array();
6a488035 118 $params['title'] = 'New Test Group Created';
5667b84b 119 $group = $this->callAPISuccess('group', 'get', $params);
6a488035
TO
120
121 foreach ($group['values'] as $v) {
122 $this->assertEquals($v['id'], $this->_groupID);
5667b84b 123 $this->assertEquals($v['name'], "Test Group 1");
6a488035
TO
124 $this->assertEquals($v['description'], 'New Test Group Created');
125 $this->assertEquals($v['is_active'], 1);
126 $this->assertEquals($v['visibility'], 'Public Pages');
127 }
128 }
129
140609dc 130 /**
131 * Test Group create with Group Type
132 */
133 public function testgroupCreateWithGroupType() {
134 $params = array(
135 'name' => 'Test Group type',
136 'title' => 'Test Group Type',
137 'description' => 'Test Group with Group Type',
138 'is_active' => 1,
139 'visibility' => 'Public Pages',
140 'group_type' => array(1, 2),
141 );
142
143 $result = $this->callAPISuccess('Group', 'create', $params);
144 $group = $result['values'][$result['id']];
145 $this->assertEquals($group['name'], "Test Group type");
146 $this->assertEquals($group['is_active'], 1);
147 $this->assertEquals($group['group_type'], $params['group_type']);
148 $this->groupDelete($result['id']);
149
150 //assert single value for group_type
151 $params['group_type'] = 2;
152 $result = $this->callAPISuccess('Group', 'create', $params);
153 $group = $result["values"][$result['id']];
154 $this->assertEquals($group['group_type'], array($params['group_type']));
155 }
156
00be9182 157 public function testGetNonExistingGroup() {
5667b84b 158 $params = array();
6a488035 159 $params['title'] = 'No such group Exist';
5667b84b 160 $group = $this->callAPISuccess('group', 'get', $params);
d0e1eff2 161 $this->assertEquals(0, $group['count']);
6a488035
TO
162 }
163
00be9182 164 public function testgroupdeleteParamsnoId() {
b26e776b 165 $group = $this->callAPIFailure('group', 'delete', array(), 'Mandatory key(s) missing from params array: id');
6a488035
TO
166 }
167
00be9182 168 public function testgetfields() {
5c49fee0 169 $description = "Demonstrate use of getfields to interrogate api.";
5667b84b 170 $params = array('action' => 'create');
a828d7b8 171 $result = $this->callAPIAndDocument('group', 'getfields', $params, __FUNCTION__, __FILE__, $description);
6a488035
TO
172 $this->assertEquals(1, $result['values']['is_active']['api.default']);
173 }
96025800 174
6a488035 175}