From: nirodaonline Date: Fri, 12 Nov 2021 15:55:17 +0000 (+0100) Subject: Contact/BAO/Query.php: fix searching for whitespace X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=1962d3d5c09a40e4c7fd4549da4bd533b376c387;p=civicrm-core.git Contact/BAO/Query.php: fix searching for whitespace Condition was never true Add Unit test for space wildcard replacement on sort_name fix failing test --- diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 54e6c641e2..09281671fc 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -3478,7 +3478,7 @@ WHERE $smartGroupClause // Replace spaces with wildcards for a LIKE operation // UNLESS string contains a comma (this exception is a tiny bit questionable) // Also need to check if there is space in between sort name. - elseif ($op == 'LIKE' && strpos($value, ',') === FALSE && strpos($value, ' ') === TRUE) { + elseif ($op == 'LIKE' && strpos($value, ',') === FALSE && strpos($value, ' ') !== FALSE) { $value = str_replace(' ', '%', $value); } $value = CRM_Core_DAO::escapeString(trim($value)); diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index 82e1f77941..cbd75314e5 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -1376,4 +1376,33 @@ civicrm_relationship.is_active = 1 AND $this->assertEquals('Smith', $rows[0]['last_name']); } + /** + * Tests if a space is replaced by the wildcard on sort_name when operation is 'LIKE' and there is no comma + * + * CRM-22060 fix if condition + * + * @throws \CRM_Core_Exception + */ + public function testReplaceSpaceByWildcardCondition() { + //Check for wildcard + $params = [ + 0 => [ + 0 => 'sort_name', + 1 => 'LIKE', + 2 => 'John Doe', + 3 => 0, + 4 => 1, + ], + ]; + $query = new CRM_Contact_BAO_Query($params); + list($select, $from, $where) = $query->query(); + $this->assertStringContainsString("contact_a.sort_name LIKE '%John%Doe%'", $where); + + //Check for NO wildcard due to comma + $params[0][2] = 'Doe, John'; + $query = new CRM_Contact_BAO_Query($params); + list($select, $from, $where) = $query->query(); + $this->assertStringContainsString("contact_a.sort_name LIKE '%Doe, John%'", $where); + } + } diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index af6e0df138..040ef4ad32 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -2275,7 +2275,8 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->individualCreate($contactParams_2); $result = $this->callAPISuccess('contact', 'get', ['sort_name' => 'Blackwell F']); - $this->assertEquals(1, $result['count']); + // Since #22060 we expect both results as space is being replaced with wildcard + $this->assertEquals(2, $result['count']); } /**