diff --git a/core/lib/Drupal/Core/Database/Query/Condition.php b/core/lib/Drupal/Core/Database/Query/Condition.php index ad64622303d..5cfd1a1100d 100644 --- a/core/lib/Drupal/Core/Database/Query/Condition.php +++ b/core/lib/Drupal/Core/Database/Query/Condition.php @@ -105,6 +105,10 @@ class Condition implements ConditionInterface, \Countable { if (empty($value) && is_array($value)) { throw new InvalidQueryException(sprintf("Query condition '%s %s ()' cannot be empty.", $field, $operator)); } + if (is_array($value) && in_array($operator, ['=', '<', '>', '<=', '>=', 'IS NULL', 'IS NOT NULL'], TRUE)) { + $value = implode(', ', $value); + throw new InvalidQueryException(sprintf("Query condition '%s %s %s' must have an array compatible operator.", $field, $operator, $value)); + } $this->conditions[] = [ 'field' => $field, diff --git a/core/modules/user/src/UserStorage.php b/core/modules/user/src/UserStorage.php index 5ca52d5e99c..9d24d4c1b4d 100644 --- a/core/modules/user/src/UserStorage.php +++ b/core/modules/user/src/UserStorage.php @@ -70,7 +70,7 @@ class UserStorage extends SqlContentEntityStorage implements UserStorageInterfac public function deleteRoleReferences(array $rids) { // Remove the role from all users. $this->database->delete('user__roles') - ->condition('roles_target_id', $rids) + ->condition('roles_target_id', $rids, 'IN') ->execute(); $this->resetCache(); diff --git a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php index fe85345d9b9..798b4dc5640 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php @@ -591,4 +591,40 @@ class SelectTest extends DatabaseTestBase { } } + /** + * Data provider for testNonArrayOperatorWithArrayValueCondition(). + * + * @return array[] + * Array of non array compatible operators and its value in the expected + * exception message. + */ + public function providerNonArrayOperatorWithArrayValueCondition() { + return [ + '=' => ['=', '='], + '>' => ['>', '>'], + '<' => ['<', '<'], + '>=' => ['>=', '>='], + '<=' => ['<=', '<='], + 'IS NULL' => ['IS NULL', 'IS NULL'], + 'IS NOT NULL' => ['IS NOT NULL', 'IS NOT NULL'], + 'Empty string' => ['', '='], + 'Not set' => [NULL, '='], + ]; + } + + /** + * Tests thrown exception for non array operator conditions with array value. + * + * @dataProvider providerNonArrayOperatorWithArrayValueCondition + */ + public function testNonArrayOperatorWithArrayValueCondition($operator, $operator_in_exception_message) { + $this->expectException(InvalidQueryException::class); + $this->expectExceptionMessage("Query condition 'age " . $operator_in_exception_message . " 26, 27' must have an array compatible operator."); + + $this->connection->select('test', 't') + ->fields('t') + ->condition('age', [26, 27], $operator) + ->execute(); + } + } diff --git a/core/tests/Drupal/Tests/Core/Database/ConditionTest.php b/core/tests/Drupal/Tests/Core/Database/ConditionTest.php index 8f19768a7e0..46f6f8972a5 100644 --- a/core/tests/Drupal/Tests/Core/Database/ConditionTest.php +++ b/core/tests/Drupal/Tests/Core/Database/ConditionTest.php @@ -54,7 +54,7 @@ class ConditionTest extends UnitTestCase { $query_placeholder = $query_placeholder->reveal(); $condition = $connection->condition('AND'); - $condition->condition($field_name, ['value']); + $condition->condition($field_name, 'value'); $condition->compile($connection, $query_placeholder); $this->assertEquals($expected, $condition->__toString());