Merge pull request #14326 from civicrm/5.14
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Search / Custom / SampleTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Include parent class definition
30 */
31
32 /**
33 * Include class under test
34 */
35
36 /**
37 * Include form definitions
38 */
39
40 /**
41 * Include DAO to do queries
42 */
43
44 /**
45 * Include dataProvider for tests
46 */
47
48 /**
49 * Test contact custom search functions
50 *
51 * @package CiviCRM
52 * @group headless
53 */
54 class CRM_Contact_Form_Search_Custom_SampleTest extends CiviUnitTestCase {
55 protected $_tablesToTruncate = array(
56 'civicrm_address',
57 'civicrm_saved_search',
58 'civicrm_contact',
59 'civicrm_option_value',
60 'civicrm_option_group',
61 );
62
63 /**
64 * @return CRM_Contact_Form_Search_Custom_SamplTestDataProvider
65 */
66 public function dataProvider() {
67 return new CRM_Contact_Form_Search_Custom_SampleTestDataProvider();
68 }
69
70 public function setUp() {
71 parent::setUp();
72 }
73
74 public function tearDown() {
75 }
76
77 /**
78 * Test CRM_Contact_Form_Search_Custom_Sample::count()
79 * @dataProvider dataProvider
80 * @param $fv
81 * @param $count
82 * @param $ids
83 * @param $full
84 * @throws \Exception
85 */
86 public function testCount($fv, $count, $ids, $full) {
87 $this->quickCleanup($this->_tablesToTruncate);
88
89 $this->loadXMLDataSet(dirname(__FILE__) . '/datasets/sample-dataset.xml');
90
91 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
92
93 $this->assertEquals($count, $obj->count());
94 }
95
96 /**
97 * Test CRM_Contact_Form_Search_Custom_Sample::all()
98 * @dataProvider dataProvider
99 * @param $fv
100 * @param $count
101 * @param $ids
102 * @param $full
103 * @throws \Exception
104 */
105 public function testAll($fv, $count, $ids, $full) {
106 // Truncate affected tables
107 $this->quickCleanup($this->_tablesToTruncate);
108
109 $this->loadXMLDataSet(dirname(__FILE__) . '/datasets/sample-dataset.xml');
110
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);
115 $all = array();
116 while ($dao->fetch()) {
117 $all[] = array(
118 'contact_id' => $dao->contact_id,
119 'contact_type' => $dao->contact_type,
120 'household_name' => $dao->sort_name,
121 );
122 }
123 asort($all);
124 $this->assertEquals($full, $all);
125 }
126
127 /**
128 * Test CRM_Contact_Form_Search_Custom_Sample::contactIDs()
129 * @dataProvider dataProvider
130 * @param $fv
131 * @param $count
132 * @param $ids
133 * @param $full
134 * @throws \Exception
135 */
136 public function testContactIDs($fv, $count, $ids, $full) {
137 // Truncate affected tables
138 $this->quickCleanup($this->_tablesToTruncate);
139
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);
145 $contacts = array();
146 while ($dao->fetch()) {
147 $contacts[$dao->contact_id] = 1;
148 }
149 $contacts = array_keys($contacts);
150 sort($contacts, SORT_NUMERIC);
151 $this->assertEquals($ids, $contacts);
152 }
153
154 /**
155 * Test CRM_Contact_Form_Search_Custom_Group::columns()
156 * It returns an array of translated name => keys
157 */
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));
166 }
167 }
168
169 /**
170 * Test CRM_Contact_Form_Search_Custom_Group::from()
171 * @todo write this test
172 */
173 public function SKIPPED_testFrom() {
174 }
175
176 /**
177 * Test CRM_Contact_Form_Search_Custom_Group::summary()
178 * It returns NULL
179 */
180 public function testSummary() {
181 $formValues = array();
182 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
183 $this->assertNull($obj->summary());
184 }
185
186 /**
187 * Test CRM_Contact_Form_Search_Custom_Sample::templateFile()
188 * Returns the path to the file as a string
189 */
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 ) );
197 }
198
199 /**
200 * Test CRM_Contact_Form_Search_Custom_Sample with saved_search_id
201 * With true argument it returns list of contact IDs
202 */
203 public function testSavedSearch() {
204 $this->quickCleanup($this->_tablesToTruncate);
205
206 $this->loadXMLDataSet(dirname(__FILE__) . '/datasets/sample-dataset.xml');
207
208 $dataset[1] = array('id' => array(12));
209 $dataset[2] = array('id' => array(10, 11));
210
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);
218 $contacts = array();
219 while ($dao->fetch()) {
220 $contacts[] = $dao->contact_id;
221 }
222 sort($contacts, SORT_NUMERIC);
223 $this->assertEquals($dataset[$ssdao->id]['id'], $contacts);
224 }
225 }
226
227 }