3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 // echo "testCount\n";
90 $op = new PHPUnit_Extensions_Database_Operation_Insert();
91 $op->execute($this->_dbconn
,
92 $this->createFlatXMLDataSet(
93 dirname(__FILE__
) . '/datasets/sample-dataset.xml'
97 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
99 $this->assertEquals($count, $obj->count(),
100 'In line ' . __LINE__
105 * Test CRM_Contact_Form_Search_Custom_Sample::all()
106 * @dataProvider dataProvider
113 public function testAll($fv, $count, $ids, $full) {
114 // Truncate affected tables
115 $this->quickCleanup($this->_tablesToTruncate
);
118 $op = new PHPUnit_Extensions_Database_Operation_Insert();
119 $op->execute($this->_dbconn
,
120 $this->createFlatXMLDataSet(
121 dirname(__FILE__
) . '/datasets/sample-dataset.xml'
124 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
125 $sql = $obj->all(0, 0, 'contact_id');
126 $this->assertTrue(is_string($sql));
127 $dao = CRM_Core_DAO
::executeQuery($sql);
129 while ($dao->fetch()) {
131 'contact_id' => $dao->contact_id
,
132 'contact_type' => $dao->contact_type
,
133 'household_name' => $dao->sort_name
,
137 $this->assertEquals($full, $all);
141 * Test CRM_Contact_Form_Search_Custom_Sample::contactIDs()
142 * @dataProvider dataProvider
149 public function testContactIDs($fv, $count, $ids, $full) {
150 // Truncate affected tables
151 $this->quickCleanup($this->_tablesToTruncate
);
153 // echo "testContactIDs\n";
154 $op = new PHPUnit_Extensions_Database_Operation_Insert();
155 $op->execute($this->_dbconn
,
156 $this->createFlatXMLDataSet(
157 dirname(__FILE__
) . '/datasets/sample-dataset.xml'
160 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
161 $sql = $obj->contactIDs();
162 $this->assertTrue(is_string($sql));
163 $dao = CRM_Core_DAO
::executeQuery($sql);
165 while ($dao->fetch()) {
166 $contacts[$dao->contact_id
] = 1;
168 $contacts = array_keys($contacts);
169 sort($contacts, SORT_NUMERIC
);
170 $this->assertEquals($ids, $contacts);
174 * Test CRM_Contact_Form_Search_Custom_Group::columns()
175 * It returns an array of translated name => keys
177 public function testColumns() {
178 $formValues = array();
179 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
180 $columns = $obj->columns();
181 $this->assertTrue(is_array($columns));
182 foreach ($columns as $key => $value) {
183 $this->assertTrue(is_string($key));
184 $this->assertTrue(is_string($value));
189 * Test CRM_Contact_Form_Search_Custom_Group::from()
190 * @todo write this test
192 public function SKIPPED_testFrom() {
196 * Test CRM_Contact_Form_Search_Custom_Group::summary()
199 public function testSummary() {
200 $formValues = array();
201 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
202 $this->assertNull($obj->summary());
206 * Test CRM_Contact_Form_Search_Custom_Sample::templateFile()
207 * Returns the path to the file as a string
209 public function testTemplateFile() {
210 $formValues = array();
211 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
212 $fileName = $obj->templateFile();
213 $this->assertTrue(is_string($fileName));
214 //FIXME: we would need to search the include path to do the following
215 //$this->assertTrue( file_exists( $fileName ) );
219 * Test CRM_Contact_Form_Search_Custom_Sample with saved_search_id
220 * With true argument it returns list of contact IDs
222 public function testSavedSearch() {
223 $this->quickCleanup($this->_tablesToTruncate
);
225 $op = new PHPUnit_Extensions_Database_Operation_Insert();
226 $op->execute($this->_dbconn
,
227 $this->createFlatXMLDataSet(
228 dirname(__FILE__
) . '/datasets/sample-dataset.xml'
232 $dataset[1] = array('id' => array(12));
233 $dataset[2] = array('id' => array(10, 11));
235 $ssdao = CRM_Core_DAO
::executeQuery("SELECT * FROM civicrm_saved_search");
236 while ($ssdao->fetch()) {
237 $fv = CRM_Contact_BAO_SavedSearch
::getFormValues($ssdao->id
);
238 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
239 $sql = $obj->contactIDs();
240 $this->assertTrue(is_string($sql));
241 $dao = CRM_Core_DAO
::executeQuery($sql);
243 while ($dao->fetch()) {
244 $contacts[] = $dao->contact_id
;
246 sort($contacts, SORT_NUMERIC
);
247 $this->assertEquals($dataset[$ssdao->id
]['id'], $contacts);