3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 * Include parent class definition
33 * Include class under test
37 * Include form definitions
41 * Include DAO to do queries
45 * Include dataProvider for tests
49 * Test contact custom search functions
54 class CRM_Contact_Form_Search_Custom_SampleTest
extends CiviUnitTestCase
{
55 protected $_tablesToTruncate = array(
57 'civicrm_saved_search',
59 'civicrm_option_value',
60 'civicrm_option_group',
64 * @return CRM_Contact_Form_Search_Custom_SamplTestDataProvider
66 public function dataProvider() {
67 return new CRM_Contact_Form_Search_Custom_SampleTestDataProvider();
70 public function setUp() {
74 public function tearDown() {
78 * Test CRM_Contact_Form_Search_Custom_Sample::count()
79 * @dataProvider dataProvider
86 public function testCount($fv, $count, $ids, $full) {
87 $this->quickCleanup($this->_tablesToTruncate
);
89 $this->loadXMLDataSet(dirname(__FILE__
) . '/datasets/sample-dataset.xml');
91 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
93 $this->assertEquals($count, $obj->count());
97 * Test CRM_Contact_Form_Search_Custom_Sample::all()
98 * @dataProvider dataProvider
105 public function testAll($fv, $count, $ids, $full) {
106 // Truncate affected tables
107 $this->quickCleanup($this->_tablesToTruncate
);
109 $this->loadXMLDataSet(dirname(__FILE__
) . '/datasets/sample-dataset.xml');
111 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
112 $sql = $obj->all(0, 0, 'contact_id');
113 $this->assertTrue(is_string($sql));
114 $dao = CRM_Core_DAO
::executeQuery($sql);
116 while ($dao->fetch()) {
118 'contact_id' => $dao->contact_id
,
119 'contact_type' => $dao->contact_type
,
120 'household_name' => $dao->sort_name
,
124 $this->assertEquals($full, $all);
128 * Test CRM_Contact_Form_Search_Custom_Sample::contactIDs()
129 * @dataProvider dataProvider
136 public function testContactIDs($fv, $count, $ids, $full) {
137 // Truncate affected tables
138 $this->quickCleanup($this->_tablesToTruncate
);
140 $this->loadXMLDataSet(dirname(__FILE__
) . '/datasets/sample-dataset.xml');
141 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
142 $sql = $obj->contactIDs();
143 $this->assertTrue(is_string($sql));
144 $dao = CRM_Core_DAO
::executeQuery($sql);
146 while ($dao->fetch()) {
147 $contacts[$dao->contact_id
] = 1;
149 $contacts = array_keys($contacts);
150 sort($contacts, SORT_NUMERIC
);
151 $this->assertEquals($ids, $contacts);
155 * Test CRM_Contact_Form_Search_Custom_Group::columns()
156 * It returns an array of translated name => keys
158 public function testColumns() {
159 $formValues = array();
160 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
161 $columns = $obj->columns();
162 $this->assertTrue(is_array($columns));
163 foreach ($columns as $key => $value) {
164 $this->assertTrue(is_string($key));
165 $this->assertTrue(is_string($value));
170 * Test CRM_Contact_Form_Search_Custom_Group::from()
171 * @todo write this test
173 public function SKIPPED_testFrom() {
177 * Test CRM_Contact_Form_Search_Custom_Group::summary()
180 public function testSummary() {
181 $formValues = array();
182 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
183 $this->assertNull($obj->summary());
187 * Test CRM_Contact_Form_Search_Custom_Sample::templateFile()
188 * Returns the path to the file as a string
190 public function testTemplateFile() {
191 $formValues = array();
192 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
193 $fileName = $obj->templateFile();
194 $this->assertTrue(is_string($fileName));
195 //FIXME: we would need to search the include path to do the following
196 //$this->assertTrue( file_exists( $fileName ) );
200 * Test CRM_Contact_Form_Search_Custom_Sample with saved_search_id
201 * With true argument it returns list of contact IDs
203 public function testSavedSearch() {
204 $this->quickCleanup($this->_tablesToTruncate
);
206 $this->loadXMLDataSet(dirname(__FILE__
) . '/datasets/sample-dataset.xml');
208 $dataset[1] = array('id' => array(12));
209 $dataset[2] = array('id' => array(10, 11));
211 $ssdao = CRM_Core_DAO
::executeQuery("SELECT * FROM civicrm_saved_search");
212 while ($ssdao->fetch()) {
213 $fv = CRM_Contact_BAO_SavedSearch
::getFormValues($ssdao->id
);
214 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
215 $sql = $obj->contactIDs();
216 $this->assertTrue(is_string($sql));
217 $dao = CRM_Core_DAO
::executeQuery($sql);
219 while ($dao->fetch()) {
220 $contacts[] = $dao->contact_id
;
222 sort($contacts, SORT_NUMERIC
);
223 $this->assertEquals($dataset[$ssdao->id
]['id'], $contacts);