From 78998b1b2bc9015dcbfddb7afce8a8ef305fde18 Mon Sep 17 00:00:00 2001 From: Ian Wilson Date: Thu, 17 Feb 2022 16:48:52 -0500 Subject: [PATCH] dev/core#1340 - Test contact get using custom field param + special character. --- tests/phpunit/api/v3/ContactTest.php | 55 ++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index e3631bea74..b72766b860 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -5267,4 +5267,59 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->assertEquals($expected, $api->execute()->first()[$fieldName]); } + /** + * Test fetching custom field value that needs to be escaped. + */ + public function testGetEscapedCustomField(): void { + $testValues = [ + 'test-value', + // This value will be escaped. + "test-'value", + ]; + + // Create a custom contact field group + field. + $customGroupId = $this->customGroupCreate([ + 'title' => 'testGetEscapedCustomField', + ])['id']; + $customFieldId = $this->customFieldCreate([ + 'custom_group_id' => $customGroupId, + 'label' => 'testGetEscapedCustomField', + ])['id']; + $customFieldParam = 'custom_' . $customFieldId; + + // Create and fetch contacts using the test values. + foreach ($testValues as $value) { + // Create a new contact and insert the test value into the custom field. + $contactId = $this->callAPISuccess('Contact', 'create', [ + 'contact_type' => 'Individual', + 'first_name' => 'Test', + 'last_name' => 'Contact', + $customFieldParam => $value, + ])['id']; + + // Verify the test value was inserted correctly. + $contactData = $this->callAPISuccess('Contact', 'getsingle', [ + 'id' => $contactId, + 'return' => [$customFieldParam], + ]); + $this->assertEquals($value, $contactData[$customFieldParam]); + + // All of these comparison operators should return a result. + $comparisonQueries = [ + ['LIKE' => $value], + ['IN' => [$value]], + // Equals. + $value, + ]; + + // Fetch the new contact using different comparison operators. + foreach ($comparisonQueries as $query) { + $this->callAPISuccess('Contact', 'getsingle', [ + 'id' => $contactId, + $customFieldParam => $query, + ]); + } + } + } + } -- 2.25.1