Issue #2823910 by daffie, pwolanin, smustgrave, dawehner, larowlan: DBTNG/EQ condition works inconsistently with arrays

merge-requests/3748/head
Lee Rowlands 2023-03-29 13:42:41 +10:00
parent 6cdc56148a
commit 330d393ec2
No known key found for this signature in database
GPG Key ID: 2B829A3DF9204DC4
4 changed files with 42 additions and 2 deletions

View File

@ -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,

View File

@ -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();

View File

@ -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();
}
}

View File

@ -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());