Merge pull request #13473 from deb1990/C51-384-add-case-token-in-email
[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 // 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'
94 )
95 );
96
97 $obj = new CRM_Contact_Form_Search_Custom_Sample($fv);
98
99 $this->assertEquals($count, $obj->count(),
100 'In line ' . __LINE__
101 );
102 }
103
104 /**
105 * Test CRM_Contact_Form_Search_Custom_Sample::all()
106 * @dataProvider dataProvider
107 * @param $fv
108 * @param $count
109 * @param $ids
110 * @param $full
111 * @throws \Exception
112 */
113 public function testAll($fv, $count, $ids, $full) {
114 // Truncate affected tables
115 $this->quickCleanup($this->_tablesToTruncate);
116
117 // echo "testAll\n";
118 $op = new PHPUnit_Extensions_Database_Operation_Insert();
119 $op->execute($this->_dbconn,
120 $this->createFlatXMLDataSet(
121 dirname(__FILE__) . '/datasets/sample-dataset.xml'
122 )
123 );
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);
128 $all = array();
129 while ($dao->fetch()) {
130 $all[] = array(
131 'contact_id' => $dao->contact_id,
132 'contact_type' => $dao->contact_type,
133 'household_name' => $dao->sort_name,
134 );
135 }
136 asort($all);
137 $this->assertEquals($full, $all);
138 }
139
140 /**
141 * Test CRM_Contact_Form_Search_Custom_Sample::contactIDs()
142 * @dataProvider dataProvider
143 * @param $fv
144 * @param $count
145 * @param $ids
146 * @param $full
147 * @throws \Exception
148 */
149 public function testContactIDs($fv, $count, $ids, $full) {
150 // Truncate affected tables
151 $this->quickCleanup($this->_tablesToTruncate);
152
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'
158 )
159 );
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);
164 $contacts = array();
165 while ($dao->fetch()) {
166 $contacts[$dao->contact_id] = 1;
167 }
168 $contacts = array_keys($contacts);
169 sort($contacts, SORT_NUMERIC);
170 $this->assertEquals($ids, $contacts);
171 }
172
173 /**
174 * Test CRM_Contact_Form_Search_Custom_Group::columns()
175 * It returns an array of translated name => keys
176 */
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));
185 }
186 }
187
188 /**
189 * Test CRM_Contact_Form_Search_Custom_Group::from()
190 * @todo write this test
191 */
192 public function SKIPPED_testFrom() {
193 }
194
195 /**
196 * Test CRM_Contact_Form_Search_Custom_Group::summary()
197 * It returns NULL
198 */
199 public function testSummary() {
200 $formValues = array();
201 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
202 $this->assertNull($obj->summary());
203 }
204
205 /**
206 * Test CRM_Contact_Form_Search_Custom_Sample::templateFile()
207 * Returns the path to the file as a string
208 */
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 ) );
216 }
217
218 /**
219 * Test CRM_Contact_Form_Search_Custom_Sample with saved_search_id
220 * With true argument it returns list of contact IDs
221 */
222 public function testSavedSearch() {
223 $this->quickCleanup($this->_tablesToTruncate);
224
225 $op = new PHPUnit_Extensions_Database_Operation_Insert();
226 $op->execute($this->_dbconn,
227 $this->createFlatXMLDataSet(
228 dirname(__FILE__) . '/datasets/sample-dataset.xml'
229 )
230 );
231
232 $dataset[1] = array('id' => array(12));
233 $dataset[2] = array('id' => array(10, 11));
234
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);
242 $contacts = array();
243 while ($dao->fetch()) {
244 $contacts[] = $dao->contact_id;
245 }
246 sort($contacts, SORT_NUMERIC);
247 $this->assertEquals($dataset[$ssdao->id]['id'], $contacts);
248 }
249 }
250
251 }