Merge pull request #14138 from johncronan/drupal_login_url
[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
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 * @group headless
56 */
57 class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase {
58 protected $_tablesToTruncate = array(
59 'civicrm_group_contact',
60 'civicrm_group',
61 'civicrm_saved_search',
62 'civicrm_entity_tag',
63 'civicrm_tag',
64 'civicrm_contact',
65 'civicrm_option_value',
66 'civicrm_option_group',
67 );
68
69 /**
70 * @return CRM_Contact_Form_Search_Custom_GroupTestDataProvider
71 */
72 public function dataProvider() {
73 return new CRM_Contact_Form_Search_Custom_GroupTestDataProvider();
74 }
75
76 public function setUp() {
77 parent::setUp();
78 }
79
80 public function tearDown() {
81 }
82
83 /**
84 * Test CRM_Contact_Form_Search_Custom_Group::count()
85 * @dataProvider dataProvider
86 * @param $fv
87 * @param $count
88 * @param $ids
89 * @param $full
90 * @throws \Exception
91 */
92 public function testCount($fv, $count, $ids, $full) {
93 $this->quickCleanup($this->_tablesToTruncate);
94
95 $this->loadXMLDataSet(dirname(__FILE__) . '/datasets/group-dataset.xml');
96
97 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
98
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 }
110
111 /**
112 * Test CRM_Contact_Form_Search_Custom_Group::all()
113 * @dataProvider dataProvider
114 * @param $fv
115 * @param $count
116 * @param $ids
117 * @param $full
118 * @throws \Exception
119 */
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');
124
125 $obj = new CRM_Contact_Form_Search_Custom_Group($fv);
126 $sql = $obj->all();
127 $this->assertTrue(is_string($sql));
128 $dao = CRM_Core_DAO::executeQuery($sql);
129 $all = array();
130 while ($dao->fetch()) {
131 $all[] = array(
132 'contact_id' => $dao->contact_id,
133 'contact_type' => $dao->contact_type,
134 'sort_name' => $dao->sort_name,
135 );
136 }
137 asort($all);
138 $this->assertEquals($full, $all);
139 }
140
141 /**
142 * Test CRM_Contact_Form_Search_Custom_Group::contactIDs()
143 * @dataProvider dataProvider
144 * @param $fv
145 * @param $count
146 * @param $ids
147 * @param $full
148 * @throws \Exception
149 */
150 public function testContactIDs($fv, $count, $ids, $full) {
151 // Truncate affected tables
152 $this->quickCleanup($this->_tablesToTruncate);
153
154 $this->loadXMLDataSet(dirname(__FILE__) . '/datasets/group-dataset.xml');
155
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);
160 $contacts = array();
161 while ($dao->fetch()) {
162 $contacts[$dao->contact_id] = 1;
163 }
164 $contacts = array_keys($contacts);
165 sort($contacts, SORT_NUMERIC);
166 $this->assertEquals($ids, $contacts);
167 }
168
169 /**
170 * Test CRM_Contact_Form_Search_Custom_Group::columns()
171 * It returns an array of translated name => keys
172 */
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));
181 }
182 }
183
184 /**
185 * Test CRM_Contact_Form_Search_Custom_Group::from()
186 * @todo write this test
187 */
188 public function SKIPPED_testFrom() {
189 }
190
191 /**
192 * Test CRM_Contact_Form_Search_Custom_Group::summary()
193 * It returns NULL
194 */
195 public function testSummary() {
196 $formValues = array();
197 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
198 $this->assertNull($obj->summary());
199 }
200
201 /**
202 * Test CRM_Contact_Form_Search_Custom_Group::templateFile()
203 * Returns the path to the file as a string
204 */
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 ) );
212 }
213
214 /**
215 * Test CRM_Contact_Form_Search_Custom_Group::where( )
216 * With no arguments it returns '(1)'
217 */
218 public function testWhereNoArgs() {
219 $formValues = array(
220 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
221 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
222 );
223 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
224 $this->assertEquals(' (1) ', $obj->where());
225 }
226
227 /**
228 * Test CRM_Contact_Form_Search_Custom_Group::where( )
229 * With false argument it returns '(1)'
230 */
231 public function testWhereFalse() {
232 $formValues = array(
233 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
234 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
235 );
236 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
237 $this->assertEquals(' (1) ', $obj->where(FALSE),
238 'In line ' . __LINE__
239 );
240 }
241
242 /**
243 * Test CRM_Contact_Form_Search_Custom_Group::where( )
244 * With true argument it returns list of contact IDs
245 */
246 public function testWhereTrue() {
247 $formValues = array(
248 CRM_Core_Form::CB_PREFIX . '17' => TRUE,
249 CRM_Core_Form::CB_PREFIX . '23' => TRUE,
250 );
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__
254 );
255 }
256
257 }