Fix undefined var
[civicrm-core.git] / tests / phpunit / api / v3 / GroupContactTest.php
CommitLineData
6a488035 1<?php
6a488035
TO
2
3/*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
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. |
14 | |
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. |
19 | |
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 +--------------------------------------------------------------------+
27*/
28
29
30require_once 'CiviTest/CiviUnitTestCase.php';
31class api_v3_GroupContactTest extends CiviUnitTestCase {
32
33 protected $_contactId;
34 protected $_contactId1;
7fbb4198 35 protected $_apiversion = 3;
6a488035
TO
36 protected $_groupId1;
37 public $_eNoticeCompliant = True;
38
39 function get_info() {
40 return array(
41 'name' => 'Group Contact Create',
42 'description' => 'Test all Group Contact Create API methods.',
43 'group' => 'CiviCRM API Tests',
44 );
45 }
46
47 /*
48 * Set up for group contact tests
49 *
50 * @todo set up calls function that doesn't work @ the moment
51 */
52 function setUp() {
6a488035
TO
53 parent::setUp();
54
e4d5f1e2 55 $this->_contactId = $this->individualCreate();
6a488035
TO
56
57 $this->_groupId1 = $this->groupCreate(NULL);
58 $params = array(
59 'contact_id' => $this->_contactId,
60 'group_id' => $this->_groupId1,
6a488035
TO
61 );
62
7fbb4198 63 $result = $this->callAPISuccess('group_contact', 'create', $params);
6a488035
TO
64
65 $group = array(
66 'name' => 'Test Group 2',
67 'domain_id' => 1,
68 'title' => 'New Test Group2 Created',
69 'description' => 'New Test Group2 Created',
70 'is_active' => 1,
71 'visibility' => 'User and User Admin Only',
6a488035
TO
72 );
73
74 $this->_groupId2 = $this->groupCreate($group, 3);
75 $params = array(
76 'contact_id.1' => $this->_contactId,
77 'group_id' => $this->_groupId2,
6a488035 78 );
6a488035
TO
79
80 $this->_group = array(
81 $this->_groupId1 => array('title' => 'New Test Group Created',
82 'visibility' => 'Public Pages',
83 'in_method' => 'API',
84 ),
85 $this->_groupId2 => array(
86 'title' => 'New Test Group2 Created',
87 'visibility' => 'User and User Admin Only',
88 'in_method' => 'API',
89 ),
90 );
91 }
92
93 function tearDown() {
94 $tablesToTruncate = array(
95 'civicrm_contact',
96 'civicrm_group',
97 );
98 $this->quickCleanup($tablesToTruncate);
99 }
100
101 ///////////////// civicrm_group_contact_get methods
102 function testGet() {
103 $params = array(
104 'contact_id' => $this->_contactId,
6a488035 105 );
7fbb4198 106 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__);
6a488035
TO
107 foreach ($result['values'] as $v) {
108 $this->assertEquals($v['title'], $this->_group[$v['group_id']]['title']);
109 $this->assertEquals($v['visibility'], $this->_group[$v['group_id']]['visibility']);
110 $this->assertEquals($v['in_method'], $this->_group[$v['group_id']]['in_method']);
111 }
112 }
113
114 function testGetGroupID() {
115 $description = "Get all from group and display contacts";
116 $subfile = "GetWithGroupID";
117 $params = array(
118 'group_id' => $this->_groupId1,
6a488035
TO
119 'api.group.get' => 1,
120 'sequential' => 1,
121 );
7fbb4198 122 $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
6a488035
TO
123 foreach ($result['values'][0]['api.group.get']['values'] as $values) {
124 $key = $values['id'];
125 $this->assertEquals($values['title'], $this->_group[$key]['title']);
126 $this->assertEquals($values['visibility'], $this->_group[$key]['visibility']);
127 }
128 }
129
6a488035
TO
130 function testCreateWithEmptyParams() {
131 $params = array();
d0e1eff2 132 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035 133 $this->assertEquals($groups['error_message'],
d0e1eff2 134 'Mandatory key(s) missing from params array: group_id, contact_id'
6a488035
TO
135 );
136 }
137
138 function testCreateWithoutGroupIdParams() {
139 $params = array(
7fbb4198 140 'contact_id' => $this->_contactId, );
6a488035 141
d0e1eff2 142 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
143 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
144 }
145
146 function testCreateWithoutContactIdParams() {
147 $params = array(
7fbb4198 148 'group_id' => $this->_groupId1, );
d0e1eff2 149 $groups = $this->callAPIFailure('group_contact', 'create', $params);
6a488035
TO
150 $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
151 }
152
153 function testCreate() {
154 $cont = array(
155 'first_name' => 'Amiteshwar',
156 'middle_name' => 'L.',
157 'last_name' => 'Prasad',
158 'prefix_id' => 3,
159 'suffix_id' => 3,
160 'email' => 'amiteshwar.prasad@civicrm.org',
7fbb4198 161 'contact_type' => 'Individual', );
6a488035
TO
162
163 $this->_contactId1 = $this->individualCreate($cont);
164 $params = array(
165 'contact_id' => $this->_contactId,
166 'contact_id.2' => $this->_contactId1,
167 'group_id' => $this->_groupId1,
6a488035
TO
168 );
169
7fbb4198 170 $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__);
6a488035
TO
171 $this->assertEquals($result['not_added'], 1, "in line " . __LINE__);
172 $this->assertEquals($result['added'], 1, "in line " . __LINE__);
173 $this->assertEquals($result['total_count'], 2, "in line " . __LINE__);
174 }
175
176 ///////////////// civicrm_group_contact_remove methods
177 function testDelete() {
178 $params = array(
179 'contact_id' => $this->_contactId,
180 'group_id' => 1,
6a488035
TO
181 );
182
7fbb4198 183 $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
6a488035
TO
184 $this->assertEquals($result['removed'], 1, "in line " . __LINE__);
185 $this->assertEquals($result['total_count'], 1, "in line " . __LINE__);
186 }
187}
188