From d9bd98e8cba232fa32df292c71885c2b21c2e9a0 Mon Sep 17 00:00:00 2001 From: Johan Vervloet Date: Thu, 19 Mar 2015 13:50:25 +0100 Subject: [PATCH] CRM-16036 - Contact reference custom values are returned incorrectly. Here is a unit test. ---------------------------------------- * CRM-16036: API: searching on custom fields does not work https://issues.civicrm.org/jira/browse/CRM-16036 --- tests/phpunit/api/v3/EventTest.php | 63 ++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/tests/phpunit/api/v3/EventTest.php b/tests/phpunit/api/v3/EventTest.php index 3239613887..0471c10629 100644 --- a/tests/phpunit/api/v3/EventTest.php +++ b/tests/phpunit/api/v3/EventTest.php @@ -386,6 +386,69 @@ class api_v3_EventTest extends CiviUnitTestCase { $this->customGroupDelete($ids['custom_group_id']); } + /** + * Test searching on custom fields returning a contact reference. + * + * https://issues.civicrm.org/jira/browse/CRM-16036 + */ + public function testEventGetCustomContactRefFieldCRM16036() { + // Create some contact. + $test_contact_name = 'Contact, Test'; + $contact_save_result = $this->callAPISuccess('contact', 'create', array( + 'sort_name' => $test_contact_name, + 'contact_type' => 'Individual', + 'display_name' => $test_contact_name, + )); + $contact_id = $contact_save_result['id']; + + // I have no clue what this $subfile is about. I just copied it from another + // unit test. + $subfile = 'ContactRefCustomField'; + $description = "Demonstrates get with Contact Reference Custom Field."; + + // Create a custom group, and add a custom contact reference field. + $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, __FILE__); + $params = array( + 'custom_group_id' => $ids['custom_group_id'], + 'name' => 'Worker_Lookup', + 'label' => 'Worker Lookup', + 'html_type' => 'Autocomplete-Select', + 'data_type' => 'ContactReference', + 'weight' => 4, + 'is_searchable' => 1, + 'is_active' => 1, + ); + $customField = $this->callAPISuccess('custom_field', 'create', $params); + + // Create an event, and add the contact as custom value. + $params = $this->_params; + $params['title'] = "My test event."; + $params['start_date'] = "2015-03-14"; + // Just assume that an event type 1 exists. + $params['event_type_id'] = 1; + $params['custom_' . $customField['id']] = "$contact_id"; + + $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__, $description, $subfile); + + // Retrieve the activity, search for the contact. + $result = $this->callAPIAndDocument($this->_entity, 'get', array( + 'return.custom_' . $customField['id'] => 1, + 'custom_' . $customField['id'] => $contact_id, + ), __FUNCTION__, __FILE__, $description, $subfile); + + $this->assertEquals($test_contact_name, $result['values'][$result['id']]['custom_' . $customField['id']]); + $this->assertEquals($contact_id, $result['values'][$result['id']]['custom_' . $customField['id'] . "_id"], ' in line ' . __LINE__); + // Not sure whether I should test for custom_X_1 and custom_X_1_id as well. + // (1 being the id of the record in the custom value table) + + $this->customFieldDelete($ids['custom_field_id']); + $this->customGroupDelete($ids['custom_group_id']); + $this->callAPISuccess('contact', 'delete', array( + 'id' => $contact_id, + 'skip_undelete' => TRUE, + )); + } + /** * Test that an event with a price set can be created. */ -- 2.25.1