3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Test class for CRM_Contact_BAO_GroupContact BAO
33 class CRM_Contact_BAO_GroupContactCacheTest
extends CiviUnitTestCase
{
36 * Manually add and remove contacts from a smart group.
38 public function testManualAddRemove() {
39 // Create smart group $g
41 'name' => 'Deceased Contacts',
42 'title' => 'Deceased Contacts',
44 'formValues' => array('is_deceased' => 1),
46 $group = CRM_Contact_BAO_Group
::createSmartGroup($params);
47 $this->registerTestObjects(array($group));
49 // Create contacts $y1, $y2, $y3 which do match $g; create $n1, $n2, $n3 which do not match $g
50 $living = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 0), 3);
51 $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3);
52 $this->assertEquals(3, count($deceased));
53 $this->assertEquals(3, count($living));
55 // Assert: $g cache has exactly $y1, $y2, $y3
56 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
57 $this->assertCacheMatches(
58 array($deceased[0]->id
, $deceased[1]->id
, $deceased[2]->id
),
63 $this->callAPISuccess('group_contact', 'create', array(
64 'contact_id' => $living[0]->id
,
65 'group_id' => $group->id
,
68 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
69 $this->assertCacheMatches(
70 array($deceased[0]->id
, $deceased[1]->id
, $deceased[2]->id
, $living[0]->id
),
75 $this->callAPISuccess('group_contact', 'create', array(
76 'contact_id' => $deceased[0]->id
,
77 'group_id' => $group->id
,
78 'status' => 'Removed',
81 CRM_Contact_BAO_GroupContactCache
::load($group, TRUE);
82 $this->assertCacheMatches(
93 * Allow removing contact from a parent group even if contact is in a child group. (CRM-8858).
95 public function testRemoveFromParentSmartGroup() {
96 // Create smart group $parent
98 'name' => 'Deceased Contacts',
99 'title' => 'Deceased Contacts',
101 'formValues' => array('is_deceased' => 1),
103 $parent = CRM_Contact_BAO_Group
::createSmartGroup($params);
104 $this->registerTestObjects(array($parent));
106 // Create group $child in $parent
108 'name' => 'Child Group',
109 'title' => 'Child Group',
111 'parents' => array($parent->id
=> 1),
113 $child = CRM_Contact_BAO_Group
::create($params);
114 $this->registerTestObjects(array($child));
116 // Create $c1, $c2, $c3
117 $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3);
119 // Add $c1, $c2, $c3 to $child
120 foreach ($deceased as $contact) {
121 $this->callAPISuccess('group_contact', 'create', array(
122 'contact_id' => $contact->id
,
123 'group_id' => $child->id
,
127 CRM_Contact_BAO_GroupContactCache
::load($parent, TRUE);
128 $this->assertCacheMatches(
129 array($deceased[0]->id
, $deceased[1]->id
, $deceased[2]->id
),
133 // Remove $c1 from $parent
134 $this->callAPISuccess('group_contact', 'create', array(
135 'contact_id' => $deceased[0]->id
,
136 'group_id' => $parent->id
,
137 'status' => 'Removed',
140 // Assert $c1 not in $parent
141 CRM_Contact_BAO_GroupContactCache
::load($parent, TRUE);
142 $this->assertCacheMatches(
150 // Assert $c1 still in $child
151 $this->assertDBQuery(1,
152 'select count(*) from civicrm_group_contact where group_id=%1 and contact_id=%2 and status=%3',
154 1 => array($child->id
, 'Integer'),
155 2 => array($deceased[0]->id
, 'Integer'),
156 3 => array('Added', 'String'),
162 * Assert that the cache for a group contains exactly the listed contacts.
164 * @param array $expectedContactIds
166 * @param int $groupId
168 public function assertCacheMatches($expectedContactIds, $groupId) {
169 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
170 $params = array(1 => array($groupId, 'Integer'));
171 $dao = CRM_Core_DAO
::executeQuery($sql, $params);
172 $actualContactIds = array();
173 while ($dao->fetch()) {
174 $actualContactIds[] = $dao->contact_id
;
177 sort($expectedContactIds);
178 sort($actualContactIds);
179 $this->assertEquals($expectedContactIds, $actualContactIds);
182 // *** Everything below this should be moved to parent class ****
185 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
187 private $_testObjects;
190 * Sets up the fixture, for example, opens a network connection.
192 * This method is called before a test is executed.
194 protected function setUp() {
195 $this->_testObjects
= array();
200 * Tears down the fixture, for example, closes a network connection.
202 * This method is called after a test is executed.
204 protected function tearDown() {
206 $this->deleteTestObjects();
210 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks created entities.
212 * @see CRM_Core_DAO::createTestObject
214 * @param string $daoName
215 * @param array $params
216 * @param int $numObjects
217 * @param bool $createOnly
219 * @return array|NULL|object
221 public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
222 $objects = CRM_Core_DAO
::createTestObject($daoName, $params, $numObjects, $createOnly);
223 if (is_array($objects)) {
224 $this->registerTestObjects($objects);
227 $this->registerTestObjects(array($objects));
233 * Register test objects.
235 * @param array $objects
236 * DAO or BAO objects.
238 public function registerTestObjects($objects) {
239 foreach ($objects as $object) {
240 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
241 $this->_testObjects
[$daoName][] = $object->id
;
246 * Delete test objects.
248 * Note: You might argue that the FK relations between test
249 * objects could make this problematic; however, it should
250 * behave intuitively as long as we mentally split our
251 * test-objects between the "manual/primary records"
252 * and the "automatic/secondary records"
254 public function deleteTestObjects() {
255 foreach ($this->_testObjects
as $daoName => $daoIds) {
256 foreach ($daoIds as $daoId) {
257 CRM_Core_DAO
::deleteTestObjects($daoName, array('id' => $daoId));
260 $this->_testObjects
= array();