From 01cd01a765f76c5a80da9b19b212d379f666abcd Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Fri, 13 Jul 2018 14:26:56 +0100 Subject: [PATCH] added unit test to search contact based on contact sub type --- .../Contact/Form/Search/SearchContactTest.php | 94 +++++++++++++++++++ 1 file changed, 94 insertions(+) create mode 100644 tests/phpunit/CRM/Contact/Form/Search/SearchContactTest.php diff --git a/tests/phpunit/CRM/Contact/Form/Search/SearchContactTest.php b/tests/phpunit/CRM/Contact/Form/Search/SearchContactTest.php new file mode 100644 index 0000000000..f503b048be --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Search/SearchContactTest.php @@ -0,0 +1,94 @@ +callAPISuccess('ContactType', 'create', [ + 'name' => $contactSubType, + 'label' => $contactSubType, + 'is_active' => 1, + 'parent_id' => "Individual", + ]); + // Contact Type api munge name in create mode + // Therefore updating the name in update mode + $this->callAPISuccess('ContactType', 'create', [ + 'name' => $contactSubType, + 'id' => $subType['id'], + ]); + } + $this->searchContacts('Contact_sub_type'); + $this->searchContacts('Contact2__sub__type'); + } + + protected function searchContacts($contactSubType) { + // create contact + $params = [ + 'first_name' => 'Peter' . substr(sha1(rand()), 0, 4), + 'last_name' => 'Lastname', + 'contact_type' => 'Individual', + 'contact_sub_type' => $contactSubType, + ]; + $contacts = $this->callAPISuccess('Contact', 'create', $params); + $contactTypes = CRM_Contact_BAO_ContactType::getSelectElements(TRUE); + foreach ($contactTypes as $contactType => $ignore) { + if (strpos($contactType, $contactSubType) !== FALSE) { + $formValues = [ + 'contact_type' => $contactType, + ]; + break; + } + } + CRM_Contact_BAO_Query::convertFormValues($formValues); + $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($formValues)); + list($select, $from, $where, $having) = $query->query(); + // get and assert contact count + $contactsResult = CRM_Core_DAO::executeQuery(sprintf('SELECT DISTINCT contact_a.id %s %s', $from, $where))->fetchAll(); + foreach ($contactsResult as $key => $value) { + $contactsResult[$key] = $value['id']; + } + // assert the contacts count + $this->assertEquals(1, count($contactsResult)); + // assert the contact IDs + $expectedResult = [$contacts['id']]; + $this->checkArrayEquals($expectedResult, $contactsResult); + // get and assert qill string + $qill = trim(implode($query->getOperator(), CRM_Utils_Array::value(0, $query->qill()))); + $this->assertEquals("Contact Type In IndividualANDContact Subtype Like {$contactSubType}", $qill); + } + +} -- 2.25.1