Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06a1bc01 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | ||
29 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
30 | require_once 'CiviTest/Contact.php'; | |
31 | ||
32 | /** | |
33 | * Test class for CRM_Contact_BAO_GroupContact BAO | |
34 | * | |
6c6e6187 | 35 | * @package CiviCRM |
6a488035 TO |
36 | */ |
37 | class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase { | |
38 | ||
39 | /** | |
40 | * Manually add and remove contacts from a smart group | |
41 | */ | |
00be9182 | 42 | public function testManualAddRemove() { |
6a488035 TO |
43 | // Create smart group $g |
44 | $params = array( | |
45 | 'name' => 'Deceased Contacts', | |
46 | 'title' => 'Deceased Contacts', | |
47 | 'is_active' => 1, | |
48 | 'formValues' => array('is_deceased' => 1), | |
49 | ); | |
50 | $group = CRM_Contact_BAO_Group::createSmartGroup($params); | |
51 | $this->registerTestObjects(array($group)); | |
52 | ||
53 | // Create contacs $y1, $y2, $y3 which do match $g; create $n1, $n2, $n3 which do not match $g | |
54 | $living = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 0), 3); | |
55 | $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3); | |
56 | $this->assertEquals(3, count($deceased)); | |
57 | $this->assertEquals(3, count($living)); | |
58 | ||
59 | // Assert: $g cache has exactly $y1, $y2, $y3 | |
60 | CRM_Contact_BAO_GroupContactCache::load($group, TRUE); | |
61 | $this->assertCacheMatches( | |
62 | array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id), | |
63 | $group->id | |
64 | ); | |
65 | ||
66 | // Add $n1 to $g | |
67 | $result = civicrm_api('group_contact', 'create', array( | |
68 | 'contact_id' => $living[0]->id, | |
69 | 'group_id' => $group->id, | |
70 | 'version' => '3', | |
71 | )); | |
72 | $this->assertAPISuccess($result); | |
73 | CRM_Contact_BAO_GroupContactCache::load($group, TRUE); | |
74 | $this->assertCacheMatches( | |
75 | array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id, $living[0]->id), | |
76 | $group->id | |
77 | ); | |
78 | ||
79 | // Remove $y1 from $g | |
80 | $result = civicrm_api('group_contact', 'create', array( | |
81 | 'contact_id' => $deceased[0]->id, | |
82 | 'group_id' => $group->id, | |
83 | 'status' => 'Removed', | |
84 | 'version' => '3', | |
85 | )); | |
86 | $this->assertAPISuccess($result); | |
87 | CRM_Contact_BAO_GroupContactCache::load($group, TRUE); | |
88 | $this->assertCacheMatches( | |
92915c55 TO |
89 | array(/* deceased[0], */ |
90 | $deceased[1]->id, | |
91 | $deceased[2]->id, | |
28a04ea9 | 92 | $living[0]->id, |
92915c55 | 93 | ), |
6a488035 TO |
94 | $group->id |
95 | ); | |
96 | } | |
b6708aeb | 97 | |
6a488035 TO |
98 | /** |
99 | * Allow removing contact from a parent group even if contact is in | |
100 | * a child group. (CRM-8858) | |
101 | */ | |
00be9182 | 102 | public function testRemoveFromParentSmartGroup() { |
6a488035 TO |
103 | // Create smart group $parent |
104 | $params = array( | |
105 | 'name' => 'Deceased Contacts', | |
106 | 'title' => 'Deceased Contacts', | |
107 | 'is_active' => 1, | |
108 | 'formValues' => array('is_deceased' => 1), | |
109 | ); | |
110 | $parent = CRM_Contact_BAO_Group::createSmartGroup($params); | |
111 | $this->registerTestObjects(array($parent)); | |
112 | ||
113 | // Create group $child in $parent | |
114 | $params = array( | |
115 | 'name' => 'Child Group', | |
116 | 'title' => 'Child Group', | |
117 | 'is_active' => 1, | |
118 | 'parents' => array($parent->id => 1), | |
119 | ); | |
120 | $child = CRM_Contact_BAO_Group::create($params); | |
121 | $this->registerTestObjects(array($child)); | |
122 | ||
123 | // Create $c1, $c2, $c3 | |
124 | $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3); | |
125 | ||
126 | // Add $c1, $c2, $c3 to $child | |
127 | foreach ($deceased as $contact) { | |
57c93d72 | 128 | $result = $this->callAPISuccess('group_contact', 'create', array( |
6a488035 TO |
129 | 'contact_id' => $contact->id, |
130 | 'group_id' => $child->id, | |
6a488035 | 131 | )); |
6a488035 TO |
132 | } |
133 | ||
134 | // GroupContactCache::load() | |
135 | CRM_Contact_BAO_GroupContactCache::load($parent, TRUE); | |
136 | $this->assertCacheMatches( | |
137 | array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id), | |
138 | $parent->id | |
139 | ); | |
b6708aeb | 140 | |
6a488035 TO |
141 | // Remove $c1 from $parent |
142 | $result = civicrm_api('group_contact', 'create', array( | |
143 | 'contact_id' => $deceased[0]->id, | |
144 | 'group_id' => $parent->id, | |
145 | 'status' => 'Removed', | |
146 | 'version' => '3', | |
147 | )); | |
148 | $this->assertAPISuccess($result); | |
b6708aeb | 149 | |
6a488035 TO |
150 | // Assert $c1 not in $parent |
151 | CRM_Contact_BAO_GroupContactCache::load($parent, TRUE); | |
152 | $this->assertCacheMatches( | |
92915c55 TO |
153 | array(/* deceased[0], */ |
154 | $deceased[1]->id, | |
28a04ea9 | 155 | $deceased[2]->id, |
92915c55 | 156 | ), |
6a488035 TO |
157 | $parent->id |
158 | ); | |
b6708aeb | 159 | |
6a488035 | 160 | // Assert $c1 still in $child |
b6708aeb | 161 | $this->assertDBQuery(1, |
6a488035 TO |
162 | 'select count(*) from civicrm_group_contact where group_id=%1 and contact_id=%2 and status=%3', |
163 | array( | |
164 | 1 => array($child->id, 'Integer'), | |
165 | 2 => array($deceased[0]->id, 'Integer'), | |
166 | 3 => array('Added', 'String'), | |
167 | ) | |
168 | ); | |
169 | } | |
170 | ||
171 | /** | |
172 | * Assert that the cache for a group contains exactly the listed contacts | |
173 | * | |
5a4f6742 | 174 | * @param array $expectedContactIds |
e16033b4 | 175 | * Array(int). |
5a4f6742 | 176 | * @param int $groupId |
6a488035 | 177 | */ |
00be9182 | 178 | public function assertCacheMatches($expectedContactIds, $groupId) { |
6a488035 TO |
179 | $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1'; |
180 | $params = array(1 => array($groupId, 'Integer')); | |
181 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
182 | $actualContactIds = array(); | |
183 | while ($dao->fetch()) { | |
184 | $actualContactIds[] = $dao->contact_id; | |
185 | } | |
186 | ||
187 | sort($expectedContactIds); | |
188 | sort($actualContactIds); | |
189 | $this->assertEquals($expectedContactIds, $actualContactIds); | |
190 | } | |
191 | ||
192 | // *** Everything below this should be moved to parent class **** | |
193 | ||
194 | /** | |
195 | * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown | |
196 | */ | |
197 | private $_testObjects; | |
198 | ||
199 | /** | |
200 | * Sets up the fixture, for example, opens a network connection. | |
201 | * This method is called before a test is executed. | |
6a488035 TO |
202 | */ |
203 | protected function setUp() { | |
204 | $this->_testObjects = array(); | |
205 | parent::setUp(); | |
206 | } | |
207 | ||
208 | /** | |
209 | * Tears down the fixture, for example, closes a network connection. | |
210 | * This method is called after a test is executed. | |
6a488035 TO |
211 | */ |
212 | protected function tearDown() { | |
213 | parent::tearDown(); | |
eb68c129 | 214 | $this->deleteTestObjects(); |
6a488035 TO |
215 | } |
216 | ||
217 | /** | |
218 | * This is a wrapper for CRM_Core_DAO::createTestObject which tracks | |
219 | * created entities and provides for brainless clenaup. | |
220 | * | |
221 | * @see CRM_Core_DAO::createTestObject | |
1e1fdcf6 EM |
222 | * @param $daoName |
223 | * @param array $params | |
224 | * @param int $numObjects | |
225 | * @param bool $createOnly | |
6a488035 | 226 | */ |
28a04ea9 | 227 | public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) { |
6a488035 TO |
228 | $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly); |
229 | if (is_array($objects)) { | |
230 | $this->registerTestObjects($objects); | |
0db6c3e1 TO |
231 | } |
232 | else { | |
6a488035 TO |
233 | $this->registerTestObjects(array($objects)); |
234 | } | |
235 | return $objects; | |
236 | } | |
237 | ||
238 | /** | |
5a4f6742 CW |
239 | * @param array $objects |
240 | * DAO or BAO objects. | |
6a488035 | 241 | */ |
00be9182 | 242 | public function registerTestObjects($objects) { |
6a488035 TO |
243 | //if (is_object($objects)) { |
244 | // $objects = array($objects); | |
245 | //} | |
246 | foreach ($objects as $object) { | |
247 | $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object)); | |
248 | $this->_testObjects[$daoName][] = $object->id; | |
249 | } | |
250 | } | |
251 | ||
00be9182 | 252 | public function deleteTestObjects() { |
6a488035 TO |
253 | // Note: You might argue that the FK relations between test |
254 | // objects could make this problematic; however, it should | |
255 | // behave intuitively as long as we mentally split our | |
256 | // test-objects between the "manual/primary records" | |
257 | // and the "automatic/secondary records" | |
258 | foreach ($this->_testObjects as $daoName => $daoIds) { | |
259 | foreach ($daoIds as $daoId) { | |
260 | CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId)); | |
261 | } | |
262 | } | |
263 | $this->_testObjects = array(); | |
264 | } | |
265 | ||
266 | } |