Merge pull request #6421 from lcdservices/CRM-16968
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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_GroupContactTest extends CiviUnitTestCase {
38
39 /**
40 * Sets up the fixture, for example, opens a network connection.
41 * This method is called before a test is executed.
42 */
43 protected function setUp() {
44 parent::setUp();
45 }
46
47 /**
48 * Tears down the fixture, for example, closes a network connection.
49 *
50 * This method is called after a test is executed.
51 */
52 protected function tearDown() {
53 }
54
55 /**
56 * Test case for add( ).
57 */
58 public function testAdd() {
59
60 //creates a test group contact by recursively creation
61 //lets create 10 groupContacts for fun
62 $groupContacts = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_GroupContact', NULL, 10);
63
64 //check the group contact id is not null for each of them
65 foreach ($groupContacts as $gc) {
66 $this->assertNotNull($gc->id);
67 }
68
69 //cleanup
70 foreach ($groupContacts as $gc) {
71 $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
72 }
73 }
74
75 /**
76 * Test case for getGroupId( )
77 */
78 public function testGetGroupId() {
79
80 //creates a test groupContact object
81 //force group_id to 1 so we can compare
82 $groupContact = CRM_Core_DAO::createTestObject('CRM_Contact_DAO_GroupContact');
83
84 //check the group contact id is not null
85 $this->assertNotNull($groupContact->id);
86
87 $groupId = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_group');
88
89 $this->assertEquals($groupContact->group_id, $groupId, 'Check for group_id');
90
91 //cleanup
92 $groupContact->deleteTestObjects('CRM_Contact_DAO_GroupContact');
93 }
94
95 /**
96 * Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too.
97 */
98 public function testContactSearchByParentGroup() {
99 // create a parent group
100 // TODO: This is not an API test!!
101 $groupParams1 = array(
102 'title' => 'Parent Group',
103 'description' => 'Parent Group',
104 'visibility' => 'User and User Admin Only',
105 'parents' => '',
106 'is_active' => 1,
107 );
108 $parentGroup = CRM_Contact_BAO_Group::create($groupParams1);
109
110 // create a child group
111 $groupParams2 = array(
112 'title' => 'Child Group',
113 'description' => 'Child Group',
114 'visibility' => 'User and User Admin Only',
115 'parents' => $parentGroup->id,
116 'is_active' => 1,
117 );
118 $childGroup = CRM_Contact_BAO_Group::create($groupParams2);
119
120 // Create a contact within parent group
121 $parentContactParams = array(
122 'first_name' => 'Parent1 Fname',
123 'last_name' => 'Parent1 Lname',
124 'group' => array($parentGroup->id => 1),
125 );
126 $parentContact = Contact::createIndividual($parentContactParams);
127
128 // create a contact within child dgroup
129 $childContactParams = array(
130 'first_name' => 'Child1 Fname',
131 'last_name' => 'Child2 Lname',
132 'group' => array($childGroup->id => 1),
133 );
134 $childContact = Contact::createIndividual($childContactParams);
135
136 // Check if searching by parent group returns both parent and child group contacts
137 $searchParams = array(
138 'group' => $parentGroup->id,
139 'version' => 3,
140 );
141 $result = civicrm_api('contact', 'get', $searchParams);
142 $validContactIds = array($parentContact, $childContact);
143 $resultContactIds = array();
144 foreach ($result['values'] as $k => $v) {
145 $resultContactIds[] = $v['contact_id'];
146 }
147 $this->assertEquals(2, count($resultContactIds), 'Check the count of returned values');
148 $this->assertEquals(array(), array_diff($validContactIds, $resultContactIds), 'Check that the difference between two arrays should be blank array');
149
150 // Check if searching by child group returns just child group contacts
151 $searchParams = array(
152 'group' => $childGroup->id,
153 'version' => 3,
154 );
155 $result = civicrm_api('contact', 'get', $searchParams);
156 $validChildContactIds = array($childContact);
157 $resultChildContactIds = array();
158 foreach ($result['values'] as $k => $v) {
159 $resultChildContactIds[] = $v['contact_id'];
160 }
161 $this->assertEquals(1, count($resultChildContactIds), 'Check the count of returned values');
162 $this->assertEquals(array(), array_diff($validChildContactIds, $resultChildContactIds), 'Check that the difference between two arrays should be blank array');
163 }
164
165 }