version fixes
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28
29require_once 'CiviTest/CiviUnitTestCase.php';
30require_once 'CiviTest/Contact.php';
31
32/**
33 * Test class for CRM_Contact_BAO_GroupContact BAO
34 *
6c6e6187 35 * @package CiviCRM
6a488035
TO
36 */
37class 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.
6a488035
TO
42 */
43 protected function setUp() {
44 parent::setUp();
45 }
46
47 /**
48 * Tears down the fixture, for example, closes a network connection.
49 * This method is called after a test is executed.
6a488035 50 */
6c6e6187
TO
51 protected function tearDown() {
52 }
6a488035
TO
53
54 /**
100fef9d 55 * Test case for add( )
6a488035 56 */
00be9182 57 public function testAdd() {
6a488035
TO
58
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);
62
63 //check the group contact id is not null for each of them
4f99ca55
TO
64 foreach ($groupContacts as $gc) {
65 $this->assertNotNull($gc->id);
6c6e6187 66 }
6a488035
TO
67
68 //cleanup
4f99ca55
TO
69 foreach ($groupContacts as $gc) {
70 $gc->deleteTestObjects('CRM_Contact_DAO_GroupContact');
6c6e6187 71 }
6a488035
TO
72 }
73
74 /**
100fef9d 75 * Test case for getGroupId( )
6a488035 76 */
00be9182 77 public function testGetGroupId() {
6a488035 78
6a488035
TO
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');
82
83 //check the group contact id is not null
84 $this->assertNotNull($groupContact->id);
85
86 $groupId = CRM_Core_DAO::singleValueQuery('select max(id) from civicrm_group');
87
88 $this->assertEquals($groupContact->group_id, $groupId, 'Check for group_id');
89
90 //cleanup
91 $groupContact->deleteTestObjects('CRM_Contact_DAO_GroupContact');
92 }
93
94 /**
95 * Test case for contact search: CRM-6706, CRM-6586 Parent Group search should return contacts from child groups too.
96 */
00be9182 97 public function testContactSearchByParentGroup() {
6a488035
TO
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',
104 'parents' => '',
105 'is_active' => 1,
106 );
107 $parentGroup = CRM_Contact_BAO_Group::create($groupParams1);
108
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,
115 'is_active' => 1,
116 );
117 $childGroup = CRM_Contact_BAO_Group::create($groupParams2);
118
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),
124 );
125 $parentContact = Contact::createIndividual($parentContactParams);
126
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),
132 );
133 $childContact = Contact::createIndividual($childContactParams);
134
135 // Check if searching by parent group returns both parent and child group contacts
136 $searchParams = array(
137 'group' => array($parentGroup->id => 1),
138 'version' => 3,
139 );
92915c55
TO
140 $result = civicrm_api('contact', 'get', $searchParams);
141 $validContactIds = array($parentContact, $childContact);
6a488035
TO
142 $resultContactIds = array();
143 foreach ($result['values'] as $k => $v) {
144 $resultContactIds[] = $v['contact_id'];
145 }
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');
148
149 // Check if searching by child group returns just child group contacts
150 $searchParams = array(
151 'group' => array($childGroup->id => 1),
152 'version' => 3,
153 );
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'];
159 }
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');
162 }
96025800 163
6a488035 164}