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