class ConditionTest

Same name in this branch
  1. main core/tests/Drupal/Tests/Core/Database/ConditionTest.php \Drupal\Tests\Core\Database\ConditionTest
Same name and namespace in other branches
  1. 11.x core/tests/Drupal/Tests/Core/Database/ConditionTest.php \Drupal\Tests\Core\Database\ConditionTest
  2. 10 core/tests/Drupal/Tests/Core/Database/ConditionTest.php \Drupal\Tests\Core\Database\ConditionTest
  3. 9 core/tests/Drupal/Tests/Core/Database/ConditionTest.php \Drupal\Tests\Core\Database\ConditionTest
  4. 8.9.x core/tests/Drupal/Tests/Core/Database/ConditionTest.php \Drupal\Tests\Core\Database\ConditionTest
  5. 11.x core/modules/pgsql/tests/src/Unit/EntityQuery/ConditionTest.php \Drupal\Tests\pgsql\Unit\EntityQuery\ConditionTest

Tests \Drupal\pgsql\EntityQuery\Condition.

Attributes

#[CoversClass(Condition::class)] #[Group('Database')]

Hierarchy

Expanded class hierarchy of ConditionTest

File

core/modules/pgsql/tests/src/Unit/EntityQuery/ConditionTest.php, line 17

Namespace

Drupal\Tests\pgsql\Unit\EntityQuery
View source
class ConditionTest extends UnitTestCase {
  
  /**
   * Tests that valid operators are accepted for case-insensitive array values.
   */
  public function testValidOperatorsAreAccepted(string $operator) : void {
    $sql_query = $this->createStub(SelectInterface::class);
    $sql_query->method('escapeField')
      ->willReturn('field');
    $condition = [
      'real_field' => 'field',
      'operator' => $operator,
      'value' => [
        'foo',
        'bar',
      ],
    ];
    Condition::translateCondition($condition, $sql_query, FALSE);
    $this->assertStringContainsString($operator, $condition['where']);
  }
  
  /**
   * Data provider for ::testValidOperatorsAreAccepted().
   */
  public static function providerValidOperators() : array {
    return [
      'IN operator' => [
        'IN',
      ],
      'NOT IN operator' => [
        'NOT IN',
      ],
    ];
  }
  
  /**
   * Tests that an invalid operator throws an exception.
   */
  public function testInvalidOperatorThrowsException() : void {
    $sql_query = $this->createStub(SelectInterface::class);
    $condition = [
      'real_field' => 'field',
      'operator' => 'LIKE',
      'value' => [
        'foo',
        'bar',
      ],
    ];
    $this->expectException(\InvalidArgumentException::class);
    $this->expectExceptionMessage('Invalid operator "LIKE"');
    Condition::translateCondition($condition, $sql_query, FALSE);
  }
  
  /**
   * Tests that case-sensitive conditions are not affected by the validation.
   */
  public function testCaseSensitiveArrayConditionIsNotValidated() : void {
    $sql_query = $this->createStub(SelectInterface::class);
    $condition = [
      'real_field' => 'field',
      'operator' => 'LIKE',
      'value' => [
        'foo',
        'bar',
      ],
    ];
    // No exception should be thrown for case-sensitive conditions.
    Condition::translateCondition($condition, $sql_query, TRUE);
    $this->assertArrayNotHasKey('where', $condition);
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.