3 namespace Civi\Searches
;
8 use Civi\Test\HeadlessInterface
;
9 use Civi\Test\HookInterface
;
10 use Civi\Test\TransactionalInterface
;
11 use Civi\Api4\SavedSearch
;
12 use Civi\Api4\OptionValue
;
13 use CRM_Contact_BAO_SavedSearch
;
14 use CRM_Contact_Form_Search_Custom_Group
;
15 use CRM_Contact_Form_Search_Custom_Sample
;
17 use PHPUnit\Framework\TestCase
;
20 * FIXME - Add test description.
23 * - With HookInterface, you may implement CiviCRM hooks directly in the test
24 * class. Simply create corresponding functions (e.g. "hook_civicrm_post(...)"
26 * - With TransactionalInterface, any data changes made by setUp() or
27 * test****() functions will rollback automatically -- as long as you don't
28 * manipulate schema or truncate tables. If this test needs to manipulate
29 * schema or truncate tables, then either: a. Do all that using setupHeadless()
30 * and Civi\Test. b. Disable TransactionalInterface, and handle all
31 * setup/teardown yourself.
35 class SampleTest
extends TestCase
implements HeadlessInterface
, HookInterface
, TransactionalInterface
{
38 * Civi\Test has many helpers, like install(), uninstall(), sql(), and
40 * https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
42 public function setUpHeadless(): Test\CiviEnvBuilder
{
43 return Test
::headless()
44 ->install(['legacycustomsearches'])
51 * @throws \API_Exception
52 * @throws \Civi\API\Exception\UnauthorizedException
54 public function setUp(): void
{
55 OptionValue
::create()->setValues([
56 'option_group_id:name' => 'custom_search',
57 'label' => 'CRM_Contact_Form_Search_Custom_Sample',
59 'name' => 'CRM_Contact_Form_Search_Custom_Sample',
60 'description' => 'Household Name and State',
69 public function dataProvider(): array {
71 // Search by Household name: 'Household 9'
73 'form_values' => ['household_name' => 'Household - No state'],
75 'Household - No state',
78 // Search by Household name: 'Household'
80 'form_values' => ['household_name' => 'Household'],
82 'Household - No state',
88 // Search by State: California
90 'form_values' => ['state_province_id' => '1004'],
96 // Search by State: New York
98 'form_values' => ['state_province_id' => '1031'],
107 * Test CRM_Contact_Form_Search_Custom_Sample::count()
109 * @dataProvider dataProvider
111 * @param array $formValues
112 * @param array $names
114 * @throws \API_Exception
116 public function testCount(array $formValues, array $names): void
{
117 $this->setupSampleData();
118 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
119 $this->assertEquals(count($names), $obj->count());
123 * Test CRM_Contact_Form_Search_Custom_Sample::all()
125 * @dataProvider dataProvider
127 * @param array $formValues
128 * @param array $names
130 * @throws \API_Exception
132 public function testAll(array $formValues, array $names): void
{
133 $this->setupSampleData();
134 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
135 $sql = $obj->all(0, 0, 'contact_id');
136 $this->assertIsString($sql);
137 $dao = CRM_Core_DAO
::executeQuery($sql);
139 while ($dao->fetch()) {
141 'contact_id' => $dao->contact_id
,
142 'contact_type' => $dao->contact_type
,
143 'household_name' => $dao->sort_name
,
147 foreach ($names as $name) {
149 'contact_type' => 'Household',
150 'household_name' => $name,
151 'contact_id' => Contact
::get()
152 ->addWhere('household_name', '=', $name)
158 $this->assertEquals($full, $all);
162 * Test CRM_Contact_Form_Search_Custom_Sample::contactIDs().
164 * @dataProvider dataProvider
166 * @param array $formValues
167 * @param array $names
169 * @throws \API_Exception
171 public function testContactIDs(array $formValues, array $names): void
{
172 $this->setupSampleData();
173 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
174 $sql = $obj->contactIDs();
175 $this->assertIsString($sql);
176 $dao = CRM_Core_DAO
::executeQuery($sql);
178 while ($dao->fetch()) {
179 $contacts[$dao->contact_id
] = 1;
181 $contacts = array_keys($contacts);
182 sort($contacts, SORT_NUMERIC
);
183 $this->assertEquals($this->getContactIDs($names), $contacts);
187 * Test CRM_Contact_Form_Search_Custom_Group::columns().
189 * It returns an array of translated name => keys
191 public function testColumns(): void
{
193 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
194 $columns = $obj->columns();
195 $this->assertIsArray($columns);
196 foreach ($columns as $key => $value) {
197 $this->assertIsString($key);
198 $this->assertIsString($value);
203 * Test CRM_Contact_Form_Search_Custom_Group::summary()
206 public function testSummary(): void
{
208 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
209 $this->assertNull($obj->summary());
213 * Test CRM_Contact_Form_Search_Custom_Sample::templateFile()
214 * Returns the path to the file as a string
216 public function testTemplateFile(): void
{
218 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
219 $fileName = $obj->templateFile();
220 $this->assertIsString($fileName);
224 * Test CRM_Contact_Form_Search_Custom_Sample with saved_search_id
225 * With true argument it returns list of contact IDs
227 * @throws \API_Exception
228 * @throws \CRM_Core_Exception
229 * @throws \CiviCRM_API3_Exception
230 * @throws \Civi\API\Exception\UnauthorizedException
232 public function testSavedSearch(): void
{
233 $this->setupSampleData();
234 $this->setupSavedSearches();
235 $dataset[1] = ['id' => $this->getContactIDs(['Household - NY'])];
237 'id' => $this->getContactIDs([
239 'Household - CA - 2',
242 $searches = SavedSearch
::get()->addSelect('*')->execute();
243 foreach ($searches as $search) {
244 $formValues = CRM_Contact_BAO_SavedSearch
::getFormValues($search['id']);
245 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
246 $sql = $obj->contactIDs();
247 $this->assertIsString($sql);
248 $dao = CRM_Core_DAO
::executeQuery($sql);
250 while ($dao->fetch()) {
251 $contacts[] = $dao->contact_id
;
253 sort($contacts, SORT_NUMERIC
);
254 $this->assertEquals($dataset[$search['id']]['id'], $contacts);
259 * Set up our sample data.
261 * @throws \API_Exception
263 public function setupSampleData(): void
{
265 'Household - No state' => '',
266 'Household - CA' => 1004,
267 'Household - CA - 2' => 1004,
268 'Household - NY' => 1031,
270 foreach ($households as $household => $state) {
271 $create = Contact
::create(FALSE)->setValues([
272 'contact_type' => 'Household',
273 'household_name' => $household,
278 Address
::create()->setValues([
279 'contact_id' => '$id',
280 'location_type_id' => 1,
281 'state_province_id' => $state,
289 * Get the ids for the relevant contacts.@
292 * IDs of the contacts.
294 * @throws \API_Exception
296 protected function getContactIDs($names): array {
297 return array_keys((array) Contact
::get()->addWhere(
298 'display_name', 'IN', $names
299 )->addOrderBy('id')->execute()->indexBy('id'));
303 * Set up saved searches.
305 protected function setupSavedSearches(): void
{
306 SavedSearch
::create()->setValues([
316 0 => 'household_name',
318 2 => 'Household - NY',
323 0 => 'state_province_id',
331 0 => 'customSearchID',
339 0 => 'customSearchClass',
341 2 => 'CRM_Contact_Form_Search_Custom_Sample',
348 SavedSearch
::create()->setValues([
351 'household_name' => '',
352 'state_province_id' => '1004',