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