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( | |
89 | array(/* deceased[0], */ $deceased[1]->id, $deceased[2]->id, $living[0]->id), | |
90 | $group->id | |
91 | ); | |
92 | } | |
b6708aeb | 93 | |
6a488035 TO |
94 | /** |
95 | * Allow removing contact from a parent group even if contact is in | |
96 | * a child group. (CRM-8858) | |
97 | */ | |
00be9182 | 98 | public function testRemoveFromParentSmartGroup() { |
6a488035 TO |
99 | // Create smart group $parent |
100 | $params = array( | |
101 | 'name' => 'Deceased Contacts', | |
102 | 'title' => 'Deceased Contacts', | |
103 | 'is_active' => 1, | |
104 | 'formValues' => array('is_deceased' => 1), | |
105 | ); | |
106 | $parent = CRM_Contact_BAO_Group::createSmartGroup($params); | |
107 | $this->registerTestObjects(array($parent)); | |
108 | ||
109 | // Create group $child in $parent | |
110 | $params = array( | |
111 | 'name' => 'Child Group', | |
112 | 'title' => 'Child Group', | |
113 | 'is_active' => 1, | |
114 | 'parents' => array($parent->id => 1), | |
115 | ); | |
116 | $child = CRM_Contact_BAO_Group::create($params); | |
117 | $this->registerTestObjects(array($child)); | |
118 | ||
119 | // Create $c1, $c2, $c3 | |
120 | $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3); | |
121 | ||
122 | // Add $c1, $c2, $c3 to $child | |
123 | foreach ($deceased as $contact) { | |
57c93d72 | 124 | $result = $this->callAPISuccess('group_contact', 'create', array( |
6a488035 TO |
125 | 'contact_id' => $contact->id, |
126 | 'group_id' => $child->id, | |
6a488035 | 127 | )); |
6a488035 TO |
128 | } |
129 | ||
130 | // GroupContactCache::load() | |
131 | CRM_Contact_BAO_GroupContactCache::load($parent, TRUE); | |
132 | $this->assertCacheMatches( | |
133 | array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id), | |
134 | $parent->id | |
135 | ); | |
b6708aeb | 136 | |
6a488035 TO |
137 | // Remove $c1 from $parent |
138 | $result = civicrm_api('group_contact', 'create', array( | |
139 | 'contact_id' => $deceased[0]->id, | |
140 | 'group_id' => $parent->id, | |
141 | 'status' => 'Removed', | |
142 | 'version' => '3', | |
143 | )); | |
144 | $this->assertAPISuccess($result); | |
b6708aeb | 145 | |
6a488035 TO |
146 | // Assert $c1 not in $parent |
147 | CRM_Contact_BAO_GroupContactCache::load($parent, TRUE); | |
148 | $this->assertCacheMatches( | |
481a74f4 | 149 | array(/* deceased[0], */ $deceased[1]->id, $deceased[2]->id), |
6a488035 TO |
150 | $parent->id |
151 | ); | |
b6708aeb | 152 | |
6a488035 | 153 | // Assert $c1 still in $child |
b6708aeb | 154 | $this->assertDBQuery(1, |
6a488035 TO |
155 | 'select count(*) from civicrm_group_contact where group_id=%1 and contact_id=%2 and status=%3', |
156 | array( | |
157 | 1 => array($child->id, 'Integer'), | |
158 | 2 => array($deceased[0]->id, 'Integer'), | |
159 | 3 => array('Added', 'String'), | |
160 | ) | |
161 | ); | |
162 | } | |
163 | ||
164 | /** | |
165 | * Assert that the cache for a group contains exactly the listed contacts | |
166 | * | |
5a4f6742 | 167 | * @param array $expectedContactIds |
e16033b4 | 168 | * Array(int). |
5a4f6742 | 169 | * @param int $groupId |
6a488035 | 170 | */ |
00be9182 | 171 | public function assertCacheMatches($expectedContactIds, $groupId) { |
6a488035 TO |
172 | $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1'; |
173 | $params = array(1 => array($groupId, 'Integer')); | |
174 | $dao = CRM_Core_DAO::executeQuery($sql, $params); | |
175 | $actualContactIds = array(); | |
176 | while ($dao->fetch()) { | |
177 | $actualContactIds[] = $dao->contact_id; | |
178 | } | |
179 | ||
180 | sort($expectedContactIds); | |
181 | sort($actualContactIds); | |
182 | $this->assertEquals($expectedContactIds, $actualContactIds); | |
183 | } | |
184 | ||
185 | // *** Everything below this should be moved to parent class **** | |
186 | ||
187 | /** | |
188 | * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown | |
189 | */ | |
190 | private $_testObjects; | |
191 | ||
192 | /** | |
193 | * Sets up the fixture, for example, opens a network connection. | |
194 | * This method is called before a test is executed. | |
6a488035 TO |
195 | */ |
196 | protected function setUp() { | |
197 | $this->_testObjects = array(); | |
198 | parent::setUp(); | |
199 | } | |
200 | ||
201 | /** | |
202 | * Tears down the fixture, for example, closes a network connection. | |
203 | * This method is called after a test is executed. | |
6a488035 TO |
204 | */ |
205 | protected function tearDown() { | |
206 | parent::tearDown(); | |
eb68c129 | 207 | $this->deleteTestObjects(); |
6a488035 TO |
208 | } |
209 | ||
210 | /** | |
211 | * This is a wrapper for CRM_Core_DAO::createTestObject which tracks | |
212 | * created entities and provides for brainless clenaup. | |
213 | * | |
214 | * @see CRM_Core_DAO::createTestObject | |
215 | */ | |
6c6e6187 | 216 | function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) { |
6a488035 TO |
217 | $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly); |
218 | if (is_array($objects)) { | |
219 | $this->registerTestObjects($objects); | |
0db6c3e1 TO |
220 | } |
221 | else { | |
6a488035 TO |
222 | $this->registerTestObjects(array($objects)); |
223 | } | |
224 | return $objects; | |
225 | } | |
226 | ||
227 | /** | |
5a4f6742 CW |
228 | * @param array $objects |
229 | * DAO or BAO objects. | |
6a488035 | 230 | */ |
00be9182 | 231 | public function registerTestObjects($objects) { |
6a488035 TO |
232 | //if (is_object($objects)) { |
233 | // $objects = array($objects); | |
234 | //} | |
235 | foreach ($objects as $object) { | |
236 | $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object)); | |
237 | $this->_testObjects[$daoName][] = $object->id; | |
238 | } | |
239 | } | |
240 | ||
00be9182 | 241 | public function deleteTestObjects() { |
6a488035 TO |
242 | // Note: You might argue that the FK relations between test |
243 | // objects could make this problematic; however, it should | |
244 | // behave intuitively as long as we mentally split our | |
245 | // test-objects between the "manual/primary records" | |
246 | // and the "automatic/secondary records" | |
247 | foreach ($this->_testObjects as $daoName => $daoIds) { | |
248 | foreach ($daoIds as $daoId) { | |
249 | CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId)); | |
250 | } | |
251 | } | |
252 | $this->_testObjects = array(); | |
253 | } | |
254 | ||
255 | } |