3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 require_once 'CiviTest/CiviUnitTestCase.php';
30 require_once 'CiviTest/Contact.php';
33 * Test class for CRM_Contact_BAO_GroupContact BAO
37 class CRM_Contact_BAO_GroupContactTest
extends CiviUnitTestCase
{
40 * Sets up the fixture, for example, opens a network connection.
41 * This method is called before a test is executed.
43 protected function setUp() {
48 * Tears down the fixture, for example, closes a network connection.
49 * This method is called after a test is executed.
51 protected function tearDown() {
55 * Test case for add( )
57 public function testAdd() {
59 //creates a test group contact by recursively creation
60 //lets create 10 groupContacts for fun
61 $groupContacts = CRM_Core_DAO
::createTestObject('CRM_Contact_DAO_GroupContact', NULL, 10);
63 //check the group contact id is not null for each of them
64 foreach ($groupContacts as $gc) {
65 $this->assertNotNull($gc->id
);
69 foreach ($groupContacts as $gc) {
70 $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
75 * Test case for getGroupId( )
77 public function testGetGroupId() {
79 //creates a test groupContact object
80 //force group_id to 1 so we can compare
81 $groupContact = CRM_Core_DAO
::createTestObject('CRM_Contact_DAO_GroupContact');
83 //check the group contact id is not null
84 $this->assertNotNull($groupContact->id
);
86 $groupId = CRM_Core_DAO
::singleValueQuery('select max(id) from civicrm_group');
88 $this->assertEquals($groupContact->group_id
, $groupId, 'Check for group_id');
91 $groupContact->deleteTestObjects('CRM_Contact_DAO_GroupContact');
95 * Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too.
97 public function testContactSearchByParentGroup() {
98 // create a parent group
99 // TODO: This is not an API test!!
100 $groupParams1 = array(
101 'title' => 'Parent Group',
102 'description' => 'Parent Group',
103 'visibility' => 'User and User Admin Only',
107 $parentGroup = CRM_Contact_BAO_Group
::create($groupParams1);
109 // create a child group
110 $groupParams2 = array(
111 'title' => 'Child Group',
112 'description' => 'Child Group',
113 'visibility' => 'User and User Admin Only',
114 'parents' => $parentGroup->id
,
117 $childGroup = CRM_Contact_BAO_Group
::create($groupParams2);
119 // Create a contact within parent group
120 $parentContactParams = array(
121 'first_name' => 'Parent1 Fname',
122 'last_name' => 'Parent1 Lname',
123 'group' => array($parentGroup->id
=> 1),
125 $parentContact = Contact
::createIndividual($parentContactParams);
127 // create a contact within child dgroup
128 $childContactParams = array(
129 'first_name' => 'Child1 Fname',
130 'last_name' => 'Child2 Lname',
131 'group' => array($childGroup->id
=> 1),
133 $childContact = Contact
::createIndividual($childContactParams);
135 // Check if searching by parent group returns both parent and child group contacts
136 $searchParams = array(
137 'group' => array($parentGroup->id
=> 1),
140 $result = civicrm_api('contact', 'get', $searchParams);
141 $validContactIds = array($parentContact, $childContact);
142 $resultContactIds = array();
143 foreach ($result['values'] as $k => $v) {
144 $resultContactIds[] = $v['contact_id'];
146 $this->assertEquals(2, count($resultContactIds), 'Check the count of returned values');
147 $this->assertEquals(array(), array_diff($validContactIds, $resultContactIds), 'Check that the difference between two arrays should be blank array');
149 // Check if searching by child group returns just child group contacts
150 $searchParams = array(
151 'group' => array($childGroup->id
=> 1),
154 $result = civicrm_api('contact', 'get', $searchParams);
155 $validChildContactIds = array($childContact);
156 $resultChildContactIds = array();
157 foreach ($result['values'] as $k => $v) {
158 $resultChildContactIds[] = $v['contact_id'];
160 $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
161 $this->assertEquals(array(), array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');