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