(801) 534-1262 * @copyright Copyright CiviCRM LLC (C) 2009 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html * GNU Affero General Public License version 3 * @package CiviCRM * * This file is part of CiviCRM * * CiviCRM is free software; you can redistribute it and/or * modify it under the terms of the GNU Affero General Public License * as published by the Free Software Foundation; either version 3 of * the License, or (at your option) any later version. * * CiviCRM is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public * License along with this program. If not, see * . */ /** * Include parent class definition */ require_once 'CiviTest/CiviUnitTestCase.php'; /** * Include class under test */ /** * Include form definitions */ /** * Include DAO to do queries */ /** * Include dataProvider for tests */ /** * Test contact custom search functions * * @package CiviCRM */ class CRM_Contact_Form_Search_Custom_GroupTest extends CiviUnitTestCase { protected $_tablesToTruncate = array( 'civicrm_group_contact', 'civicrm_group', 'civicrm_saved_search', 'civicrm_entity_tag', 'civicrm_tag', 'civicrm_contact', 'civicrm_option_value', 'civicrm_option_group', ); function get_info() { return array( 'name' => 'Contact Custom Search Group', 'description' => 'Test all CRM_Contact_Form_Search_Custom_Group methods.', 'group' => 'CiviCRM Custom Search Tests', ); } public function dataProvider() { return new CRM_Contact_Form_Search_Custom_GroupTestDataProvider; } function setUp() { parent::setUp(); } function tearDown() {} /** * Test CRM_Contact_Form_Search_Custom_Group::count() * @dataProvider dataProvider */ public function testCount($fv, $count, $ids, $full) { $this->foreignKeyChecksOff(); $this->quickCleanup($this->_tablesToTruncate); // echo "testCount\n"; $op = new PHPUnit_Extensions_Database_Operation_Insert(); $op->execute($this->_dbconn, new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet( dirname(__FILE__) . '/dataset.xml' ) ); $obj = new CRM_Contact_Form_Search_Custom_Group($fv); $sql = $obj->all(); $dao = CRM_Core_DAO::executeQuery($sql); /** echo "Count: $count, OBJ: ", $obj->count( ) . "\n"; while ( $dao->fetch( ) ) { echo "{$dao->contact_id}, {$dao->contact_type}, {$dao->sort_name}, {$dao->group_names}\n"; } **/ $this->assertEquals($count, $obj->count(), 'In line ' . __LINE__ ); } /** * Test CRM_Contact_Form_Search_Custom_Group::all() * @dataProvider dataProvider */ public function testAll($fv, $count, $ids, $full) { // Truncate affected tables $this->quickCleanup($this->_tablesToTruncate); // echo "testAll\n"; $op = new PHPUnit_Extensions_Database_Operation_Insert(); $op->execute($this->_dbconn, new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet( dirname(__FILE__) . '/dataset.xml' ) ); $obj = new CRM_Contact_Form_Search_Custom_Group($fv); $sql = $obj->all(); $this->assertTrue(is_string($sql), 'In line ' . __LINE__); $dao = CRM_Core_DAO::executeQuery($sql); $all = array(); while ($dao->fetch()) { $all[] = array( 'contact_id' => $dao->contact_id, 'contact_type' => $dao->contact_type, 'sort_name' => $dao->sort_name, ); } asort($all); $this->assertEquals($full, $all, 'In line ' . __LINE__); } /** * Test CRM_Contact_Form_Search_Custom_Group::contactIDs() * @dataProvider dataProvider */ public function testContactIDs($fv, $count, $ids, $full) { // Truncate affected tables $this->quickCleanup($this->_tablesToTruncate); // echo "testContactIDs\n"; $op = new PHPUnit_Extensions_Database_Operation_Insert(); $op->execute($this->_dbconn, new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet( dirname(__FILE__) . '/dataset.xml' ) ); $obj = new CRM_Contact_Form_Search_Custom_Group($fv); $sql = $obj->contactIDs(); $this->assertTrue(is_string($sql), 'In line ' . __LINE__); $dao = CRM_Core_DAO::executeQuery($sql); $contacts = array(); while ($dao->fetch()) { $contacts[$dao->contact_id] = 1; } $contacts = array_keys($contacts); sort($contacts, SORT_NUMERIC); $this->assertEquals($ids, $contacts, 'In line ' . __LINE__); } /** * Test CRM_Contact_Form_Search_Custom_Group::columns() * It returns an array of translated name => keys */ public function testColumns() { $formValues = array(); $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); $columns = $obj->columns(); $this->assertTrue(is_array($columns), 'In line ' . __LINE__); foreach ($columns as $key => $value) { $this->assertTrue(is_string($key), 'In line ' . __LINE__); $this->assertTrue(is_string($value), 'In line ' . __LINE__); } } /** * Test CRM_Contact_Form_Search_Custom_Group::from() * @todo write this test */ public function SKIPPED_testFrom() {} /** * Test CRM_Contact_Form_Search_Custom_Group::summary() * It returns NULL */ public function testSummary() { $formValues = array(); $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); $this->assertNull($obj->summary(), 'In line ' . __LINE__); } /** * Test CRM_Contact_Form_Search_Custom_Group::templateFile() * Returns the path to the file as a string */ public function testTemplateFile() { $formValues = array(); $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); $fileName = $obj->templateFile(); $this->assertTrue(is_string($fileName), 'In line ' . __LINE__); //FIXME: we would need to search the include path to do the following //$this->assertTrue( file_exists( $fileName ), 'In line ' . __LINE__ ); } /** * Test CRM_Contact_Form_Search_Custom_Group::where( ) * With no arguments it returns '(1)' */ public function testWhereNoArgs() { $formValues = array( CRM_Core_Form::CB_PREFIX . '17' => TRUE, CRM_Core_Form::CB_PREFIX . '23' => TRUE, ); $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); $this->assertEquals(' (1) ', $obj->where(), 'In line ' . __LINE__); } /** * Test CRM_Contact_Form_Search_Custom_Group::where( ) * With false argument it returns '(1)' */ public function testWhereFalse() { $formValues = array( CRM_Core_Form::CB_PREFIX . '17' => TRUE, CRM_Core_Form::CB_PREFIX . '23' => TRUE, ); $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); $this->assertEquals(' (1) ', $obj->where(FALSE), 'In line ' . __LINE__ ); } /** * Test CRM_Contact_Form_Search_Custom_Group::where( ) * With true argument it returns list of contact IDs */ public function testWhereTrue() { $formValues = array( CRM_Core_Form::CB_PREFIX . '17' => TRUE, CRM_Core_Form::CB_PREFIX . '23' => TRUE, ); $obj = new CRM_Contact_Form_Search_Custom_Group($formValues); $this->assertEquals(' (1) AND contact_a.id IN ( 17, 23 )', $obj->where(TRUE), 'In line ' . __LINE__ ); } }