6ce1ff8679d4e88a0911996eb855cdb8b57e5293
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Search / Custom / GroupTest.php
1 <?php
2 /**
3 * File for the CRM_Contact_Form_Search_Custom_GroupTest class
4 *
5 * (PHP 5)
6 *
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
11 * @package CiviCRM
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 */
33 require_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 *
54 * @package CiviCRM
55 */
56 class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase {
57 protected $_tablesToTruncate = array(
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 );
67 function get_info() {
68 return array(
69 'name' => 'Contact Custom Search Group',
70 'description' => 'Test all CRM_Contact_Form_Search_Custom_Group methods.',
71 'group' => 'CiviCRM Custom Search Tests',
72 );
73 }
74
75 public function dataProvider() {
76 return new CRM_Contact_Form_Search_Custom_GroupTestDataProvider;
77 }
78
79 function setUp() {
80 parent::setUp();
81 }
82
83 function tearDown() {}
84
85 /**
86 * Test CRM_Contact_Form_Search_Custom_Group::count()
87 * @dataProvider dataProvider
88 */
89 public function testCount($fv, $count, $ids, $full) {
90 $this->foreignKeyChecksOff();
91
92 $this->quickCleanup($this->_tablesToTruncate);
93
94 // echo "testCount\n";
95 $op = new PHPUnit_Extensions_Database_Operation_Insert();
96 $op->execute($this->_dbconn,
97 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
98 dirname(__FILE__) . '/dataset.xml'
99 )
100 );
101
102 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
103 $sql = $obj->all();
104 $dao = CRM_Core_DAO::executeQuery($sql);
105
106 /**
107 echo "Count: $count, OBJ: ", $obj->count( ) . "\n";
108 while ( $dao->fetch( ) ) {
109 echo "{$dao->contact_id}, {$dao->contact_type}, {$dao->sort_name}, {$dao->group_names}\n";
110 }
111 **/
112 $this->assertEquals($count, $obj->count(),
113 'In line ' . __LINE__
114 );
115 }
116
117 /**
118 * Test CRM_Contact_Form_Search_Custom_Group::all()
119 * @dataProvider dataProvider
120 */
121 public function testAll($fv, $count, $ids, $full) {
122 // Truncate affected tables
123 $this->quickCleanup(self::$_tablesToTruncate);
124
125 // echo "testAll\n";
126 $op = new PHPUnit_Extensions_Database_Operation_Insert();
127 $op->execute($this->_dbconn,
128 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
129 dirname(__FILE__) . '/dataset.xml'
130 )
131 );
132 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
133 $sql = $obj->all();
134 $this->assertTrue(is_string($sql), 'In line ' . __LINE__);
135 $dao = CRM_Core_DAO::executeQuery($sql);
136 $all = array();
137 while ($dao->fetch()) {
138 $all[] = array(
139 'contact_id' => $dao->contact_id,
140 'contact_type' => $dao->contact_type,
141 'sort_name' => $dao->sort_name,
142 );
143 }
144 asort($all);
145 $this->assertEquals($full, $all, 'In line ' . __LINE__);
146 }
147
148 /**
149 * Test CRM_Contact_Form_Search_Custom_Group::contactIDs()
150 * @dataProvider dataProvider
151 */
152 public function testContactIDs($fv, $count, $ids, $full) {
153 // Truncate affected tables
154 $this->quickCleanup(self::$_tablesToTruncate);
155
156 // echo "testContactIDs\n";
157 $op = new PHPUnit_Extensions_Database_Operation_Insert();
158 $op->execute($this->_dbconn,
159 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
160 dirname(__FILE__) . '/dataset.xml'
161 )
162 );
163 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
164 $sql = $obj->contactIDs();
165 $this->assertTrue(is_string($sql), 'In line ' . __LINE__);
166 $dao = CRM_Core_DAO::executeQuery($sql);
167 $contacts = array();
168 while ($dao->fetch()) {
169 $contacts[$dao->contact_id] = 1;
170 }
171 $contacts = array_keys($contacts);
172 sort($contacts, SORT_NUMERIC);
173 $this->assertEquals($ids, $contacts, 'In line ' . __LINE__);
174 }
175
176 /**
177 * Test CRM_Contact_Form_Search_Custom_Group::columns()
178 * It returns an array of translated name => keys
179 */
180 public function testColumns() {
181 $formValues = array();
182 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
183 $columns = $obj->columns();
184 $this->assertTrue(is_array($columns), 'In line ' . __LINE__);
185 foreach ($columns as $key => $value) {
186 $this->assertTrue(is_string($key), 'In line ' . __LINE__);
187 $this->assertTrue(is_string($value), 'In line ' . __LINE__);
188 }
189 }
190
191 /**
192 * Test CRM_Contact_Form_Search_Custom_Group::from()
193 * @todo write this test
194 */
195 public function SKIPPED_testFrom() {}
196
197 /**
198 * Test CRM_Contact_Form_Search_Custom_Group::summary()
199 * It returns NULL
200 */
201 public function testSummary() {
202 $formValues = array();
203 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
204 $this->assertNull($obj->summary(), 'In line ' . __LINE__);
205 }
206
207 /**
208 * Test CRM_Contact_Form_Search_Custom_Group::templateFile()
209 * Returns the path to the file as a string
210 */
211 public function testTemplateFile() {
212 $formValues = array();
213 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
214 $fileName = $obj->templateFile();
215 $this->assertTrue(is_string($fileName), 'In line ' . __LINE__);
216 //FIXME: we would need to search the include path to do the following
217 //$this->assertTrue( file_exists( $fileName ), 'In line ' . __LINE__ );
218 }
219
220 /**
221 * Test CRM_Contact_Form_Search_Custom_Group::where( )
222 * With no arguments it returns '(1)'
223 */
224 public function testWhereNoArgs() {
225 $formValues = array(
226 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
227 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
228 );
229 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
230 $this->assertEquals(' (1) ', $obj->where(), 'In line ' . __LINE__);
231 }
232
233 /**
234 * Test CRM_Contact_Form_Search_Custom_Group::where( )
235 * With false argument it returns '(1)'
236 */
237 public function testWhereFalse() {
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);
243 $this->assertEquals(' (1) ', $obj->where(FALSE),
244 'In line ' . __LINE__
245 );
246 }
247
248 /**
249 * Test CRM_Contact_Form_Search_Custom_Group::where( )
250 * With true argument it returns list of contact IDs
251 */
252 public function testWhereTrue() {
253 $formValues = array(
254 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
255 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
256 );
257 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
258 $this->assertEquals(' (1) AND contact_a.id IN ( 17, 23 )', $obj->where(TRUE),
259 'In line ' . __LINE__
260 );
261 }
262 }