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