ee546ab6895b053d2d29434310c68fb01aad979d
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactCacheTest.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 require_once 'CiviTest/Contact.php';
31
32 /**
33 * Test class for CRM_Contact_BAO_GroupContact BAO
34 *
35 * @package CiviCRM
36 */
37 class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase {
38
39 /**
40 * Manually add and remove contacts from a smart group
41 */
42 public function testManualAddRemove() {
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 }
93
94 /**
95 * Allow removing contact from a parent group even if contact is in
96 * a child group. (CRM-8858)
97 */
98 public function testRemoveFromParentSmartGroup() {
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) {
124 $result = $this->callAPISuccess('group_contact', 'create', array(
125 'contact_id' => $contact->id,
126 'group_id' => $child->id,
127 ));
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 );
136
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);
145
146 // Assert $c1 not in $parent
147 CRM_Contact_BAO_GroupContactCache::load($parent, TRUE);
148 $this->assertCacheMatches(
149 array( /* deceased[0], */ $deceased[1]->id, $deceased[2]->id),
150 $parent->id
151 );
152
153 // Assert $c1 still in $child
154 $this->assertDBQuery(1,
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 *
167 * @param $expectedContactIds
168 * Array(int).
169 * @param $groupId
170 * Int.
171 */
172 public function assertCacheMatches($expectedContactIds, $groupId) {
173 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
174 $params = array(1 => array($groupId, 'Integer'));
175 $dao = CRM_Core_DAO::executeQuery($sql, $params);
176 $actualContactIds = array();
177 while ($dao->fetch()) {
178 $actualContactIds[] = $dao->contact_id;
179 }
180
181 sort($expectedContactIds);
182 sort($actualContactIds);
183 $this->assertEquals($expectedContactIds, $actualContactIds);
184 }
185
186 // *** Everything below this should be moved to parent class ****
187
188 /**
189 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
190 */
191 private $_testObjects;
192
193 /**
194 * Sets up the fixture, for example, opens a network connection.
195 * This method is called before a test is executed.
196 *
197 */
198 protected function setUp() {
199 $this->_testObjects = array();
200 parent::setUp();
201 }
202
203 /**
204 * Tears down the fixture, for example, closes a network connection.
205 * This method is called after a test is executed.
206 *
207 */
208 protected function tearDown() {
209 parent::tearDown();
210 $this->deleteTestObjects();
211 }
212
213 /**
214 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks
215 * created entities and provides for brainless clenaup.
216 *
217 * @see CRM_Core_DAO::createTestObject
218 */
219 function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
220 $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly);
221 if (is_array($objects)) {
222 $this->registerTestObjects($objects);
223 } else {
224 $this->registerTestObjects(array($objects));
225 }
226 return $objects;
227 }
228
229 /**
230 * @param $objects
231 * Array DAO or BAO objects.
232 */
233 public function registerTestObjects($objects) {
234 //if (is_object($objects)) {
235 // $objects = array($objects);
236 //}
237 foreach ($objects as $object) {
238 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
239 $this->_testObjects[$daoName][] = $object->id;
240 }
241 }
242
243 public function deleteTestObjects() {
244 // Note: You might argue that the FK relations between test
245 // objects could make this problematic; however, it should
246 // behave intuitively as long as we mentally split our
247 // test-objects between the "manual/primary records"
248 // and the "automatic/secondary records"
249 foreach ($this->_testObjects as $daoName => $daoIds) {
250 foreach ($daoIds as $daoId) {
251 CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId));
252 }
253 }
254 $this->_testObjects = array();
255 }
256
257 }