Ian province abbreviation patch - issue 724
[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 */
33require_once 'CiviTest/CiviUnitTestCase.php';
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
6a488035
TO
55 */
56class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase {
8d54cdc9 57 protected $_tablesToTruncate = array(
6a488035
TO
58 'civicrm_group_contact',
59 'civicrm_group',
60 'civicrm_saved_search',
61 'civicrm_entity_tag',
62 'civicrm_tag',
63 'civicrm_contact',
64 'civicrm_option_value',
65 'civicrm_option_group',
66 );
4cbe18b8 67
4cbe18b8
EM
68 /**
69 * @return CRM_Contact_Form_Search_Custom_GroupTestDataProvider
70 */
6a488035 71 public function dataProvider() {
87a890cc 72 return new CRM_Contact_Form_Search_Custom_GroupTestDataProvider();
6a488035
TO
73 }
74
00be9182 75 public function setUp() {
6a488035
TO
76 parent::setUp();
77 }
78
6c6e6187
TO
79 public function tearDown() {
80 }
6a488035
TO
81
82 /**
83 * Test CRM_Contact_Form_Search_Custom_Group::count()
6c6e6187 84 * @dataProvider dataProvider
1e1fdcf6
EM
85 * @param $fv
86 * @param $count
87 * @param $ids
88 * @param $full
89 * @throws \Exception
6a488035
TO
90 */
91 public function testCount($fv, $count, $ids, $full) {
92 $this->foreignKeyChecksOff();
93
8d54cdc9 94 $this->quickCleanup($this->_tablesToTruncate);
6a488035
TO
95
96 // echo "testCount\n";
97 $op = new PHPUnit_Extensions_Database_Operation_Insert();
98 $op->execute($this->_dbconn,
bbfd46a5 99 $this->createFlatXMLDataSet(
6a488035
TO
100 dirname(__FILE__) . '/dataset.xml'
101 )
102 );
103
104 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
105 $sql = $obj->all();
106 $dao = CRM_Core_DAO::executeQuery($sql);
107
108 /**
92915c55
TO
109 * echo "Count: $count, OBJ: ", $obj->count( ) . "\n";
110 * while ( $dao->fetch( ) ) {
111 * echo "{$dao->contact_id}, {$dao->contact_type}, {$dao->sort_name}, {$dao->group_names}\n";
112 * }
6a488035
TO
113 **/
114 $this->assertEquals($count, $obj->count(),
115 'In line ' . __LINE__
116 );
117 }
118
119 /**
120 * Test CRM_Contact_Form_Search_Custom_Group::all()
6c6e6187 121 * @dataProvider dataProvider
1e1fdcf6
EM
122 * @param $fv
123 * @param $count
124 * @param $ids
125 * @param $full
126 * @throws \Exception
6a488035
TO
127 */
128 public function testAll($fv, $count, $ids, $full) {
129 // Truncate affected tables
745bc660 130 $this->quickCleanup($this->_tablesToTruncate);
6a488035
TO
131
132 // echo "testAll\n";
133 $op = new PHPUnit_Extensions_Database_Operation_Insert();
134 $op->execute($this->_dbconn,
bbfd46a5 135 $this->createFlatXMLDataSet(
6a488035
TO
136 dirname(__FILE__) . '/dataset.xml'
137 )
138 );
139 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
140 $sql = $obj->all();
ba4a1892 141 $this->assertTrue(is_string($sql));
6a488035
TO
142 $dao = CRM_Core_DAO::executeQuery($sql);
143 $all = array();
144 while ($dao->fetch()) {
145 $all[] = array(
146 'contact_id' => $dao->contact_id,
147 'contact_type' => $dao->contact_type,
148 'sort_name' => $dao->sort_name,
149 );
150 }
151 asort($all);
a15773db 152 $this->assertEquals($full, $all);
6a488035
TO
153 }
154
155 /**
156 * Test CRM_Contact_Form_Search_Custom_Group::contactIDs()
6c6e6187 157 * @dataProvider dataProvider
1e1fdcf6
EM
158 * @param $fv
159 * @param $count
160 * @param $ids
161 * @param $full
162 * @throws \Exception
6a488035
TO
163 */
164 public function testContactIDs($fv, $count, $ids, $full) {
165 // Truncate affected tables
29d41c01 166 $this->quickCleanup($this->_tablesToTruncate);
6a488035
TO
167
168 // echo "testContactIDs\n";
169 $op = new PHPUnit_Extensions_Database_Operation_Insert();
170 $op->execute($this->_dbconn,
bbfd46a5 171 $this->createFlatXMLDataSet(
6a488035
TO
172 dirname(__FILE__) . '/dataset.xml'
173 )
174 );
175 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
176 $sql = $obj->contactIDs();
ba4a1892 177 $this->assertTrue(is_string($sql));
6a488035
TO
178 $dao = CRM_Core_DAO::executeQuery($sql);
179 $contacts = array();
180 while ($dao->fetch()) {
181 $contacts[$dao->contact_id] = 1;
182 }
183 $contacts = array_keys($contacts);
184 sort($contacts, SORT_NUMERIC);
a15773db 185 $this->assertEquals($ids, $contacts);
6a488035
TO
186 }
187
188 /**
189 * Test CRM_Contact_Form_Search_Custom_Group::columns()
190 * It returns an array of translated name => keys
191 */
192 public function testColumns() {
193 $formValues = array();
92915c55
TO
194 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
195 $columns = $obj->columns();
ba4a1892 196 $this->assertTrue(is_array($columns));
6a488035 197 foreach ($columns as $key => $value) {
ba4a1892
TM
198 $this->assertTrue(is_string($key));
199 $this->assertTrue(is_string($value));
6a488035
TO
200 }
201 }
202
203 /**
204 * Test CRM_Contact_Form_Search_Custom_Group::from()
6c6e6187 205 * @todo write this test
6a488035 206 */
6c6e6187
TO
207 public function SKIPPED_testFrom() {
208 }
6a488035
TO
209
210 /**
211 * Test CRM_Contact_Form_Search_Custom_Group::summary()
212 * It returns NULL
213 */
214 public function testSummary() {
215 $formValues = array();
216 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
ba4a1892 217 $this->assertNull($obj->summary());
6a488035
TO
218 }
219
220 /**
221 * Test CRM_Contact_Form_Search_Custom_Group::templateFile()
222 * Returns the path to the file as a string
223 */
224 public function testTemplateFile() {
225 $formValues = array();
92915c55
TO
226 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
227 $fileName = $obj->templateFile();
ba4a1892 228 $this->assertTrue(is_string($fileName));
6a488035 229 //FIXME: we would need to search the include path to do the following
ba4a1892 230 //$this->assertTrue( file_exists( $fileName ) );
6a488035
TO
231 }
232
233 /**
234 * Test CRM_Contact_Form_Search_Custom_Group::where( )
235 * With no arguments it returns '(1)'
236 */
237 public function testWhereNoArgs() {
238 $formValues = array(
239 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
240 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
241 );
242 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
ba4a1892 243 $this->assertEquals(' (1) ', $obj->where());
6a488035
TO
244 }
245
246 /**
247 * Test CRM_Contact_Form_Search_Custom_Group::where( )
248 * With false argument it returns '(1)'
249 */
250 public function testWhereFalse() {
251 $formValues = array(
252 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
253 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
254 );
255 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
256 $this->assertEquals(' (1) ', $obj->where(FALSE),
257 'In line ' . __LINE__
258 );
259 }
260
261 /**
262 * Test CRM_Contact_Form_Search_Custom_Group::where( )
263 * With true argument it returns list of contact IDs
264 */
265 public function testWhereTrue() {
266 $formValues = array(
267 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
268 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
269 );
270 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
271 $this->assertEquals(' (1) AND contact_a.id IN ( 17, 23 )', $obj->where(TRUE),
272 'In line ' . __LINE__
273 );
274 }
96025800 275
6a488035 276}