Merge pull request #13473 from deb1990/C51-384-add-case-token-in-email
[civicrm-core.git] / tests / phpunit / CRM / Contact / Form / Search / SearchContactTest.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 * Test class for CRM_Contact_BAO_Group BAO
30 *
31 * @package CiviCRM
32 * @group headless
33 */
34 class CRM_Contact_Form_Search_SearchContactTest extends CiviUnitTestCase {
35
36 /**
37 * Test contact sub type search.
38 */
39 public function testContactSubtype() {
40 foreach (['Contact_sub_type', 'Contact2__sub__type'] as $contactSubType) {
41 $subType = $this->callAPISuccess('ContactType', 'create', [
42 'name' => $contactSubType,
43 'label' => $contactSubType,
44 'is_active' => 1,
45 'parent_id' => "Individual",
46 ]);
47 // Contact Type api munge name in create mode
48 // Therefore updating the name in update mode
49 $this->callAPISuccess('ContactType', 'create', [
50 'name' => $contactSubType,
51 'id' => $subType['id'],
52 ]);
53 }
54 $this->searchContacts('Contact_sub_type');
55 $this->searchContacts('Contact2__sub__type');
56 }
57
58 protected function searchContacts($contactSubType) {
59 // create contact
60 $params = [
61 'first_name' => 'Peter' . substr(sha1(rand()), 0, 4),
62 'last_name' => 'Lastname',
63 'contact_type' => 'Individual',
64 'contact_sub_type' => $contactSubType,
65 ];
66 $contacts = $this->callAPISuccess('Contact', 'create', $params);
67 $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(TRUE);
68 foreach ($contactTypes as $contactType => $ignore) {
69 if (strpos($contactType, $contactSubType) !== FALSE) {
70 $formValues = [
71 'contact_type' => $contactType,
72 ];
73 break;
74 }
75 }
76 CRM_Contact_BAO_Query::convertFormValues($formValues);
77 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($formValues));
78 list($select, $from, $where, $having) = $query->query();
79 // get and assert contact count
80 $contactsResult = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT contact_a.id %s %s', $from, $where))->fetchAll();
81 foreach ($contactsResult as $key => $value) {
82 $contactsResult[$key] = $value['id'];
83 }
84 // assert the contacts count
85 $this->assertEquals(1, count($contactsResult));
86 // assert the contact IDs
87 $expectedResult = [$contacts['id']];
88 $this->checkArrayEquals($expectedResult, $contactsResult);
89 // get and assert qill string
90 $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill())));
91 $this->assertEquals("Contact Type In IndividualANDContact Subtype Like {$contactSubType}", $qill);
92 }
93
94 }