3 * File for the CRM_Contact_Form_Search_Custom_GroupTest class
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
10 * GNU Affero General Public License version 3
13 * This file is part of CiviCRM
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.
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.
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/>.
31 * Include parent class definition
36 * Include class under test
40 * Include form definitions
44 * Include DAO to do queries
48 * Include dataProvider for tests
52 * Test contact custom search functions
57 class CRM_Contact_Form_Search_Custom_GroupTest
extends CiviUnitTestCase
{
58 protected $_tablesToTruncate = array(
59 'civicrm_group_contact',
61 'civicrm_saved_search',
65 'civicrm_option_value',
66 'civicrm_option_group',
70 * @return CRM_Contact_Form_Search_Custom_GroupTestDataProvider
72 public function dataProvider() {
73 return new CRM_Contact_Form_Search_Custom_GroupTestDataProvider();
76 public function setUp() {
80 public function tearDown() {
84 * Test CRM_Contact_Form_Search_Custom_Group::count()
85 * @dataProvider dataProvider
92 public function testCount($fv, $count, $ids, $full) {
93 $this->quickCleanup($this->_tablesToTruncate
);
95 $this->loadXMLDataSet(dirname(__FILE__
) . '/datasets/group-dataset.xml');
97 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
100 $dao = CRM_Core_DAO
::executeQuery($sql);
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";
108 $this->assertEquals($count, $obj->count());
112 * Test CRM_Contact_Form_Search_Custom_Group::all()
113 * @dataProvider dataProvider
120 public function testAll($fv, $count, $ids, $full) {
121 // Truncate affected tables
122 $this->quickCleanup($this->_tablesToTruncate
);
123 $this->loadXMLDataSet(dirname(__FILE__
) . '/datasets/group-dataset.xml');
125 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
127 $this->assertTrue(is_string($sql));
128 $dao = CRM_Core_DAO
::executeQuery($sql);
130 while ($dao->fetch()) {
132 'contact_id' => $dao->contact_id
,
133 'contact_type' => $dao->contact_type
,
134 'sort_name' => $dao->sort_name
,
138 $this->assertEquals($full, $all);
142 * Test CRM_Contact_Form_Search_Custom_Group::contactIDs()
143 * @dataProvider dataProvider
150 public function testContactIDs($fv, $count, $ids, $full) {
151 // Truncate affected tables
152 $this->quickCleanup($this->_tablesToTruncate
);
154 $this->loadXMLDataSet(dirname(__FILE__
) . '/datasets/group-dataset.xml');
156 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
157 $sql = $obj->contactIDs();
158 $this->assertTrue(is_string($sql));
159 $dao = CRM_Core_DAO
::executeQuery($sql);
161 while ($dao->fetch()) {
162 $contacts[$dao->contact_id
] = 1;
164 $contacts = array_keys($contacts);
165 sort($contacts, SORT_NUMERIC
);
166 $this->assertEquals($ids, $contacts);
170 * Test CRM_Contact_Form_Search_Custom_Group::columns()
171 * It returns an array of translated name => keys
173 public function testColumns() {
174 $formValues = array();
175 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
176 $columns = $obj->columns();
177 $this->assertTrue(is_array($columns));
178 foreach ($columns as $key => $value) {
179 $this->assertTrue(is_string($key));
180 $this->assertTrue(is_string($value));
185 * Test CRM_Contact_Form_Search_Custom_Group::from()
186 * @todo write this test
188 public function SKIPPED_testFrom() {
192 * Test CRM_Contact_Form_Search_Custom_Group::summary()
195 public function testSummary() {
196 $formValues = array();
197 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
198 $this->assertNull($obj->summary());
202 * Test CRM_Contact_Form_Search_Custom_Group::templateFile()
203 * Returns the path to the file as a string
205 public function testTemplateFile() {
206 $formValues = array();
207 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
208 $fileName = $obj->templateFile();
209 $this->assertTrue(is_string($fileName));
210 //FIXME: we would need to search the include path to do the following
211 //$this->assertTrue( file_exists( $fileName ) );
215 * Test CRM_Contact_Form_Search_Custom_Group::where( )
216 * With no arguments it returns '(1)'
218 public function testWhereNoArgs() {
220 CRM_Core_Form
::CB_PREFIX
. '17' => TRUE,
221 CRM_Core_Form
::CB_PREFIX
. '23' => TRUE,
223 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
224 $this->assertEquals(' (1) ', $obj->where());
228 * Test CRM_Contact_Form_Search_Custom_Group::where( )
229 * With false argument it returns '(1)'
231 public function testWhereFalse() {
233 CRM_Core_Form
::CB_PREFIX
. '17' => TRUE,
234 CRM_Core_Form
::CB_PREFIX
. '23' => TRUE,
236 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
237 $this->assertEquals(' (1) ', $obj->where(FALSE),
238 'In line ' . __LINE__
243 * Test CRM_Contact_Form_Search_Custom_Group::where( )
244 * With true argument it returns list of contact IDs
246 public function testWhereTrue() {
248 CRM_Core_Form
::CB_PREFIX
. '17' => TRUE,
249 CRM_Core_Form
::CB_PREFIX
. '23' => TRUE,
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__