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
33 require_once 'CiviTest/CiviUnitTestCase.php';
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
56 class CRM_Contact_Form_Search_Custom_GroupTest
extends CiviUnitTestCase
{
57 protected $_tablesToTruncate = array(
58 'civicrm_group_contact',
60 'civicrm_saved_search',
64 'civicrm_option_value',
65 'civicrm_option_group',
69 * @return CRM_Contact_Form_Search_Custom_GroupTestDataProvider
71 public function dataProvider() {
72 return new CRM_Contact_Form_Search_Custom_GroupTestDataProvider
;
75 public function setUp() {
79 public function tearDown() {
83 * Test CRM_Contact_Form_Search_Custom_Group::count()
84 * @dataProvider dataProvider
86 public function testCount($fv, $count, $ids, $full) {
87 $this->foreignKeyChecksOff();
89 $this->quickCleanup($this->_tablesToTruncate
);
91 // echo "testCount\n";
92 $op = new PHPUnit_Extensions_Database_Operation_Insert();
93 $op->execute($this->_dbconn
,
94 $this->createFlatXMLDataSet(
95 dirname(__FILE__
) . '/dataset.xml'
99 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
101 $dao = CRM_Core_DAO
::executeQuery($sql);
104 * echo "Count: $count, OBJ: ", $obj->count( ) . "\n";
105 * while ( $dao->fetch( ) ) {
106 * echo "{$dao->contact_id}, {$dao->contact_type}, {$dao->sort_name}, {$dao->group_names}\n";
109 $this->assertEquals($count, $obj->count(),
110 'In line ' . __LINE__
115 * Test CRM_Contact_Form_Search_Custom_Group::all()
116 * @dataProvider dataProvider
118 public function testAll($fv, $count, $ids, $full) {
119 // Truncate affected tables
120 $this->quickCleanup($this->_tablesToTruncate
);
123 $op = new PHPUnit_Extensions_Database_Operation_Insert();
124 $op->execute($this->_dbconn
,
125 $this->createFlatXMLDataSet(
126 dirname(__FILE__
) . '/dataset.xml'
129 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
131 $this->assertTrue(is_string($sql), 'In line ' . __LINE__
);
132 $dao = CRM_Core_DAO
::executeQuery($sql);
134 while ($dao->fetch()) {
136 'contact_id' => $dao->contact_id
,
137 'contact_type' => $dao->contact_type
,
138 'sort_name' => $dao->sort_name
,
142 $this->assertEquals($full, $all, 'In line ' . __LINE__
);
146 * Test CRM_Contact_Form_Search_Custom_Group::contactIDs()
147 * @dataProvider dataProvider
149 public function testContactIDs($fv, $count, $ids, $full) {
150 // Truncate affected tables
151 $this->quickCleanup($this->_tablesToTruncate
);
153 // echo "testContactIDs\n";
154 $op = new PHPUnit_Extensions_Database_Operation_Insert();
155 $op->execute($this->_dbconn
,
156 $this->createFlatXMLDataSet(
157 dirname(__FILE__
) . '/dataset.xml'
160 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
161 $sql = $obj->contactIDs();
162 $this->assertTrue(is_string($sql), 'In line ' . __LINE__
);
163 $dao = CRM_Core_DAO
::executeQuery($sql);
165 while ($dao->fetch()) {
166 $contacts[$dao->contact_id
] = 1;
168 $contacts = array_keys($contacts);
169 sort($contacts, SORT_NUMERIC
);
170 $this->assertEquals($ids, $contacts, 'In line ' . __LINE__
);
174 * Test CRM_Contact_Form_Search_Custom_Group::columns()
175 * It returns an array of translated name => keys
177 public function testColumns() {
178 $formValues = array();
179 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
180 $columns = $obj->columns();
181 $this->assertTrue(is_array($columns), 'In line ' . __LINE__
);
182 foreach ($columns as $key => $value) {
183 $this->assertTrue(is_string($key), 'In line ' . __LINE__
);
184 $this->assertTrue(is_string($value), 'In line ' . __LINE__
);
189 * Test CRM_Contact_Form_Search_Custom_Group::from()
190 * @todo write this test
192 public function SKIPPED_testFrom() {
196 * Test CRM_Contact_Form_Search_Custom_Group::summary()
199 public function testSummary() {
200 $formValues = array();
201 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
202 $this->assertNull($obj->summary(), 'In line ' . __LINE__
);
206 * Test CRM_Contact_Form_Search_Custom_Group::templateFile()
207 * Returns the path to the file as a string
209 public function testTemplateFile() {
210 $formValues = array();
211 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
212 $fileName = $obj->templateFile();
213 $this->assertTrue(is_string($fileName), 'In line ' . __LINE__
);
214 //FIXME: we would need to search the include path to do the following
215 //$this->assertTrue( file_exists( $fileName ), 'In line ' . __LINE__ );
219 * Test CRM_Contact_Form_Search_Custom_Group::where( )
220 * With no arguments it returns '(1)'
222 public function testWhereNoArgs() {
224 CRM_Core_Form
::CB_PREFIX
. '17' => TRUE,
225 CRM_Core_Form
::CB_PREFIX
. '23' => TRUE,
227 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
228 $this->assertEquals(' (1) ', $obj->where(), 'In line ' . __LINE__
);
232 * Test CRM_Contact_Form_Search_Custom_Group::where( )
233 * With false argument it returns '(1)'
235 public function testWhereFalse() {
237 CRM_Core_Form
::CB_PREFIX
. '17' => TRUE,
238 CRM_Core_Form
::CB_PREFIX
. '23' => TRUE,
240 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
241 $this->assertEquals(' (1) ', $obj->where(FALSE),
242 'In line ' . __LINE__
247 * Test CRM_Contact_Form_Search_Custom_Group::where( )
248 * With true argument it returns list of contact IDs
250 public function testWhereTrue() {
252 CRM_Core_Form
::CB_PREFIX
. '17' => TRUE,
253 CRM_Core_Form
::CB_PREFIX
. '23' => TRUE,
255 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
256 $this->assertEquals(' (1) AND contact_a.id IN ( 17, 23 )', $obj->where(TRUE),
257 'In line ' . __LINE__