Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | |
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
232624b1 | 5 | | CiviCRM version 4.4 | |
6a488035 TO |
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 | ||
30 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
31 | class api_v3_GroupContactTest extends CiviUnitTestCase { | |
32 | ||
33 | protected $_contactId; | |
34 | protected $_contactId1; | |
7fbb4198 | 35 | protected $_apiversion = 3; |
6a488035 | 36 | protected $_groupId1; |
3d700d00 | 37 | protected $_groupContactId; |
6a488035 TO |
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); |
3d700d00 | 64 | $this->_groupContactId = $result['id']; |
6a488035 TO |
65 | |
66 | $group = array( | |
67 | 'name' => 'Test Group 2', | |
68 | 'domain_id' => 1, | |
69 | 'title' => 'New Test Group2 Created', | |
70 | 'description' => 'New Test Group2 Created', | |
71 | 'is_active' => 1, | |
72 | 'visibility' => 'User and User Admin Only', | |
6a488035 TO |
73 | ); |
74 | ||
75 | $this->_groupId2 = $this->groupCreate($group, 3); | |
6a488035 TO |
76 | |
77 | $this->_group = array( | |
78 | $this->_groupId1 => array('title' => 'New Test Group Created', | |
79 | 'visibility' => 'Public Pages', | |
80 | 'in_method' => 'API', | |
81 | ), | |
82 | $this->_groupId2 => array( | |
83 | 'title' => 'New Test Group2 Created', | |
84 | 'visibility' => 'User and User Admin Only', | |
85 | 'in_method' => 'API', | |
86 | ), | |
87 | ); | |
88 | } | |
89 | ||
90 | function tearDown() { | |
91 | $tablesToTruncate = array( | |
92 | 'civicrm_contact', | |
93 | 'civicrm_group', | |
94 | ); | |
95 | $this->quickCleanup($tablesToTruncate); | |
96 | } | |
97 | ||
98 | ///////////////// civicrm_group_contact_get methods | |
99 | function testGet() { | |
100 | $params = array( | |
101 | 'contact_id' => $this->_contactId, | |
6a488035 | 102 | ); |
7fbb4198 | 103 | $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
104 | foreach ($result['values'] as $v) { |
105 | $this->assertEquals($v['title'], $this->_group[$v['group_id']]['title']); | |
106 | $this->assertEquals($v['visibility'], $this->_group[$v['group_id']]['visibility']); | |
107 | $this->assertEquals($v['in_method'], $this->_group[$v['group_id']]['in_method']); | |
108 | } | |
109 | } | |
110 | ||
111 | function testGetGroupID() { | |
112 | $description = "Get all from group and display contacts"; | |
113 | $subfile = "GetWithGroupID"; | |
114 | $params = array( | |
115 | 'group_id' => $this->_groupId1, | |
6a488035 TO |
116 | 'api.group.get' => 1, |
117 | 'sequential' => 1, | |
118 | ); | |
7fbb4198 | 119 | $result = $this->callAPIAndDocument('group_contact', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile); |
6a488035 TO |
120 | foreach ($result['values'][0]['api.group.get']['values'] as $values) { |
121 | $key = $values['id']; | |
122 | $this->assertEquals($values['title'], $this->_group[$key]['title']); | |
123 | $this->assertEquals($values['visibility'], $this->_group[$key]['visibility']); | |
124 | } | |
125 | } | |
126 | ||
6a488035 TO |
127 | function testCreateWithEmptyParams() { |
128 | $params = array(); | |
d0e1eff2 | 129 | $groups = $this->callAPIFailure('group_contact', 'create', $params); |
6a488035 | 130 | $this->assertEquals($groups['error_message'], |
d0e1eff2 | 131 | 'Mandatory key(s) missing from params array: group_id, contact_id' |
6a488035 TO |
132 | ); |
133 | } | |
134 | ||
135 | function testCreateWithoutGroupIdParams() { | |
136 | $params = array( | |
7fbb4198 | 137 | 'contact_id' => $this->_contactId, ); |
6a488035 | 138 | |
d0e1eff2 | 139 | $groups = $this->callAPIFailure('group_contact', 'create', $params); |
6a488035 TO |
140 | $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id'); |
141 | } | |
142 | ||
143 | function testCreateWithoutContactIdParams() { | |
144 | $params = array( | |
7fbb4198 | 145 | 'group_id' => $this->_groupId1, ); |
d0e1eff2 | 146 | $groups = $this->callAPIFailure('group_contact', 'create', $params); |
6a488035 TO |
147 | $this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id'); |
148 | } | |
149 | ||
150 | function testCreate() { | |
151 | $cont = array( | |
152 | 'first_name' => 'Amiteshwar', | |
153 | 'middle_name' => 'L.', | |
154 | 'last_name' => 'Prasad', | |
155 | 'prefix_id' => 3, | |
156 | 'suffix_id' => 3, | |
157 | 'email' => 'amiteshwar.prasad@civicrm.org', | |
7fbb4198 | 158 | 'contact_type' => 'Individual', ); |
6a488035 TO |
159 | |
160 | $this->_contactId1 = $this->individualCreate($cont); | |
161 | $params = array( | |
162 | 'contact_id' => $this->_contactId, | |
163 | 'contact_id.2' => $this->_contactId1, | |
164 | 'group_id' => $this->_groupId1, | |
6a488035 TO |
165 | ); |
166 | ||
7fbb4198 | 167 | $result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
168 | $this->assertEquals($result['not_added'], 1, "in line " . __LINE__); |
169 | $this->assertEquals($result['added'], 1, "in line " . __LINE__); | |
170 | $this->assertEquals($result['total_count'], 2, "in line " . __LINE__); | |
171 | } | |
172 | ||
173 | ///////////////// civicrm_group_contact_remove methods | |
174 | function testDelete() { | |
175 | $params = array( | |
176 | 'contact_id' => $this->_contactId, | |
177 | 'group_id' => 1, | |
6a488035 TO |
178 | ); |
179 | ||
7fbb4198 | 180 | $result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
181 | $this->assertEquals($result['removed'], 1, "in line " . __LINE__); |
182 | $this->assertEquals($result['total_count'], 1, "in line " . __LINE__); | |
183 | } | |
3d700d00 CW |
184 | |
185 | function testDeletePermanent() { | |
186 | $params = array( | |
187 | 'id' => $this->_groupContactId, | |
188 | 'skip_undelete' => TRUE, | |
189 | ); | |
190 | $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__); | |
191 | $result = $this->callAPISuccess('group_contact', 'get', $params); | |
192 | $this->assertEquals(0, $result['count'], "in line " . __LINE__); | |
193 | } | |
6a488035 TO |
194 | } |
195 |