Merge pull request #14734 from yashodha/dev-1104
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Search / Custom / GroupTest.php
CommitLineData
6a488035
TO
1<?php
2/**
3 * File for the CRM_Contact_Form_Search_Custom_GroupTest class
4 *
5 * (PHP 5)
6 *
6c6e6187
TO
7 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
8 * @copyright Copyright CiviCRM LLC (C) 2009
9 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
6a488035 10 * GNU Affero General Public License version 3
6c6e6187 11 * @package CiviCRM
6a488035
TO
12 *
13 * This file is part of CiviCRM
14 *
15 * CiviCRM is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Affero General Public License
17 * as published by the Free Software Foundation; either version 3 of
18 * the License, or (at your option) any later version.
19 *
20 * CiviCRM is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Affero General Public License for more details.
24 *
25 * You should have received a copy of the GNU Affero General Public
26 * License along with this program. If not, see
27 * <http://www.gnu.org/licenses/>.
28 */
29
30/**
31 * Include parent class definition
32 */
0eea664b 33
6a488035
TO
34
35/**
36 * Include class under test
37 */
38
39/**
40 * Include form definitions
41 */
42
43/**
44 * Include DAO to do queries
45 */
46
47/**
48 * Include dataProvider for tests
49 */
50
51/**
52 * Test contact custom search functions
53 *
6c6e6187 54 * @package CiviCRM
acb109b7 55 * @group headless
6a488035
TO
56 */
57class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase {
9099cab3 58 protected $_tablesToTruncate = [
6a488035
TO
59 'civicrm_group_contact',
60 'civicrm_group',
61 'civicrm_saved_search',
62 'civicrm_entity_tag',
63 'civicrm_tag',
64 'civicrm_contact',
65 'civicrm_option_value',
66 'civicrm_option_group',
9099cab3 67 ];
4cbe18b8 68
4cbe18b8
EM
69 /**
70 * @return CRM_Contact_Form_Search_Custom_GroupTestDataProvider
71 */
6a488035 72 public function dataProvider() {
87a890cc 73 return new CRM_Contact_Form_Search_Custom_GroupTestDataProvider();
6a488035
TO
74 }
75
00be9182 76 public function setUp() {
6a488035
TO
77 parent::setUp();
78 }
79
6c6e6187
TO
80 public function tearDown() {
81 }
6a488035
TO
82
83 /**
84 * Test CRM_Contact_Form_Search_Custom_Group::count()
6c6e6187 85 * @dataProvider dataProvider
1e1fdcf6
EM
86 * @param $fv
87 * @param $count
88 * @param $ids
89 * @param $full
90 * @throws \Exception
6a488035
TO
91 */
92 public function testCount($fv, $count, $ids, $full) {
8d54cdc9 93 $this->quickCleanup($this->_tablesToTruncate);
6a488035 94
1d291c60 95 $this->loadXMLDataSet(dirname(__FILE__) . '/datasets/group-dataset.xml');
6a488035
TO
96
97 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
fcfb2732 98
6a488035
TO
99 $sql = $obj->all();
100 $dao = CRM_Core_DAO::executeQuery($sql);
101
102 /**
92915c55
TO
103 * echo "Count: $count, OBJ: ", $obj->count( ) . "\n";
104 * while ( $dao->fetch( ) ) {
105 * echo "{$dao->contact_id}, {$dao->contact_type}, {$dao->sort_name}, {$dao->group_names}\n";
106 * }
6a488035 107 **/
1d291c60 108 $this->assertEquals($count, $obj->count());
6a488035
TO
109 }
110
111 /**
112 * Test CRM_Contact_Form_Search_Custom_Group::all()
6c6e6187 113 * @dataProvider dataProvider
1e1fdcf6
EM
114 * @param $fv
115 * @param $count
116 * @param $ids
117 * @param $full
118 * @throws \Exception
6a488035
TO
119 */
120 public function testAll($fv, $count, $ids, $full) {
121 // Truncate affected tables
745bc660 122 $this->quickCleanup($this->_tablesToTruncate);
1d291c60 123 $this->loadXMLDataSet(dirname(__FILE__) . '/datasets/group-dataset.xml');
6a488035 124
6a488035
TO
125 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
126 $sql = $obj->all();
ba4a1892 127 $this->assertTrue(is_string($sql));
6a488035 128 $dao = CRM_Core_DAO::executeQuery($sql);
9099cab3 129 $all = [];
6a488035 130 while ($dao->fetch()) {
9099cab3 131 $all[] = [
6a488035
TO
132 'contact_id' => $dao->contact_id,
133 'contact_type' => $dao->contact_type,
134 'sort_name' => $dao->sort_name,
9099cab3 135 ];
6a488035
TO
136 }
137 asort($all);
a15773db 138 $this->assertEquals($full, $all);
6a488035
TO
139 }
140
141 /**
142 * Test CRM_Contact_Form_Search_Custom_Group::contactIDs()
6c6e6187 143 * @dataProvider dataProvider
1e1fdcf6
EM
144 * @param $fv
145 * @param $count
146 * @param $ids
147 * @param $full
148 * @throws \Exception
6a488035
TO
149 */
150 public function testContactIDs($fv, $count, $ids, $full) {
151 // Truncate affected tables
29d41c01 152 $this->quickCleanup($this->_tablesToTruncate);
6a488035 153
1d291c60 154 $this->loadXMLDataSet(dirname(__FILE__) . '/datasets/group-dataset.xml');
155
6a488035
TO
156 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
157 $sql = $obj->contactIDs();
ba4a1892 158 $this->assertTrue(is_string($sql));
6a488035 159 $dao = CRM_Core_DAO::executeQuery($sql);
9099cab3 160 $contacts = [];
6a488035
TO
161 while ($dao->fetch()) {
162 $contacts[$dao->contact_id] = 1;
163 }
164 $contacts = array_keys($contacts);
165 sort($contacts, SORT_NUMERIC);
a15773db 166 $this->assertEquals($ids, $contacts);
6a488035
TO
167 }
168
169 /**
170 * Test CRM_Contact_Form_Search_Custom_Group::columns()
171 * It returns an array of translated name => keys
172 */
173 public function testColumns() {
9099cab3 174 $formValues = [];
92915c55
TO
175 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
176 $columns = $obj->columns();
ba4a1892 177 $this->assertTrue(is_array($columns));
6a488035 178 foreach ($columns as $key => $value) {
ba4a1892
TM
179 $this->assertTrue(is_string($key));
180 $this->assertTrue(is_string($value));
6a488035
TO
181 }
182 }
183
184 /**
185 * Test CRM_Contact_Form_Search_Custom_Group::from()
6c6e6187 186 * @todo write this test
6a488035 187 */
6c6e6187
TO
188 public function SKIPPED_testFrom() {
189 }
6a488035
TO
190
191 /**
192 * Test CRM_Contact_Form_Search_Custom_Group::summary()
193 * It returns NULL
194 */
195 public function testSummary() {
9099cab3 196 $formValues = [];
6a488035 197 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
ba4a1892 198 $this->assertNull($obj->summary());
6a488035
TO
199 }
200
201 /**
202 * Test CRM_Contact_Form_Search_Custom_Group::templateFile()
203 * Returns the path to the file as a string
204 */
205 public function testTemplateFile() {
9099cab3 206 $formValues = [];
92915c55
TO
207 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
208 $fileName = $obj->templateFile();
ba4a1892 209 $this->assertTrue(is_string($fileName));
6a488035 210 //FIXME: we would need to search the include path to do the following
ba4a1892 211 //$this->assertTrue( file_exists( $fileName ) );
6a488035
TO
212 }
213
214 /**
215 * Test CRM_Contact_Form_Search_Custom_Group::where( )
216 * With no arguments it returns '(1)'
217 */
218 public function testWhereNoArgs() {
9099cab3 219 $formValues = [
6a488035
TO
220 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
221 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
9099cab3 222 ];
6a488035 223 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
ba4a1892 224 $this->assertEquals(' (1) ', $obj->where());
6a488035
TO
225 }
226
227 /**
228 * Test CRM_Contact_Form_Search_Custom_Group::where( )
229 * With false argument it returns '(1)'
230 */
231 public function testWhereFalse() {
9099cab3 232 $formValues = [
6a488035
TO
233 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
234 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
9099cab3 235 ];
6a488035
TO
236 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
237 $this->assertEquals(' (1) ', $obj->where(FALSE),
238 'In line ' . __LINE__
239 );
240 }
241
242 /**
243 * Test CRM_Contact_Form_Search_Custom_Group::where( )
244 * With true argument it returns list of contact IDs
245 */
246 public function testWhereTrue() {
9099cab3 247 $formValues = [
6a488035
TO
248 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
249 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
9099cab3 250 ];
6a488035
TO
251 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
252 $this->assertEquals(' (1) AND contact_a.id IN ( 17, 23 )', $obj->where(TRUE),
253 'In line ' . __LINE__
254 );
255 }
96025800 256
6a488035 257}