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