diff --git a/core/lib/Drupal/Core/Database/Query/Condition.php b/core/lib/Drupal/Core/Database/Query/Condition.php index 24a3582..9fe5b7f 100644 --- a/core/lib/Drupal/Core/Database/Query/Condition.php +++ b/core/lib/Drupal/Core/Database/Query/Condition.php @@ -191,7 +191,7 @@ public function compile(Connection $connection, PlaceholderInterface $queryPlace } else { // Left hand part is a normal field. Add it as is. - $field_fragment = $condition['field']; + $field_fragment = $connection->escapeField($condition['field']); $ignore_operator = FALSE; } diff --git a/core/tests/Drupal/Tests/Core/Database/ConditionTest.php b/core/tests/Drupal/Tests/Core/Database/ConditionTest.php index fd847b3..4e1c4e6 100644 --- a/core/tests/Drupal/Tests/Core/Database/ConditionTest.php +++ b/core/tests/Drupal/Tests/Core/Database/ConditionTest.php @@ -16,11 +16,26 @@ class ConditionTest extends UnitTestCase { /** + * Provides a list of known operations and the expected output. + * + * @return array + * - Expected result for the string version of the condition. + * - The field name to input in the condition. + */ + public function providerSimpleCondition() { + return [ + ['name = :db_condition_placeholder_0', 'name'], + ['name123 = :db_condition_placeholder_0', 'name-123'], + ]; + } + + /** * @covers ::compile + * @dataProvider providerSimpleCondition() */ - public function testSimpleCondition() { + public function testSimpleCondition($expected, $field_name) { $connection = $this->prophesize(Connection::class); - $connection->escapeField('name')->will(function ($args) { + $connection->escapeField($field_name)->will(function ($args) { return preg_replace('/[^A-Za-z0-9_.]+/', '', $args[0]); }); $connection->mapConditionOperator('=')->willReturn(['operator' => '=']); @@ -36,10 +51,10 @@ public function testSimpleCondition() { $query_placeholder = $query_placeholder->reveal(); $condition = new Condition('AND'); - $condition->condition('name', ['value']); + $condition->condition($field_name, ['value']); $condition->compile($connection, $query_placeholder); - $this->assertEquals('name = :db_condition_placeholder_0', $condition->__toString()); + $this->assertEquals($expected, $condition->__toString()); $this->assertEquals([':db_condition_placeholder_0' => 'value'], $condition->arguments()); }