You are here

class SqlsrvConditionTest in Drupal driver for SQL Server and SQL Azure 8.2

Same name and namespace in other branches
  1. 4.2.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
  2. 3.0.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
  3. 3.1.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
  4. 4.0.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest
  5. 4.1.x tests/src/Unit/SqlsrvConditionTest.php \Drupal\Tests\sqlsrv\Unit\SqlsrvConditionTest

Test the behavior of the custom Condition class.

These tests are not expected to pass on other database drivers.

@group Database

Hierarchy

Expanded class hierarchy of SqlsrvConditionTest

File

tests/src/Unit/SqlsrvConditionTest.php, line 18

Namespace

Drupal\Tests\sqlsrv\Unit
View source
class SqlsrvConditionTest extends UnitTestCase {

  /**
   * Test the escaping strategy of the LIKE operator.
   *
   * Mysql, Postgres, and sqlite all use '\' to escape '%' and '_'
   * in the LIKE statement. SQL Server can also use a backslash with the syntax.
   * field LIKE :text ESCAPE '\'.
   * However, due to a bug in PDO (https://bugs.php.net/bug.php?id=79276), if a
   * SQL statement has multiple LIKE statements, parameters are not correctly
   * replaced if they are located between a pair of backslashes:
   *
   * "field1 LIKE :text1 ESCAPE '\' AND field2 LIKE :text2 ESCAPE '\'"
   * :text2 will not be replaced.
   *
   * If the PDO bug is fixed, this test and the LIKE customization within the
   * Condition class can be removed
   *
   * @dataProvider dataProviderForTestLike
   */
  public function testLike($given, $expected) {
    $connection = $this
      ->prophesize(Connection::class);
    $connection
      ->escapeField(Argument::any())
      ->will(function ($args) {
      return preg_replace('/[^A-Za-z0-9_.]+/', '', $args[0]);
    });
    $connection
      ->mapConditionOperator(Argument::any())
      ->willReturn([]);
    $connection = $connection
      ->reveal();
    $query_placeholder = $this
      ->prophesize(PlaceholderInterface::class);
    $counter = 0;
    $query_placeholder
      ->nextPlaceholder()
      ->will(function () use (&$counter) {
      return $counter++;
    });
    $query_placeholder
      ->uniqueIdentifier()
      ->willReturn(4);
    $query_placeholder = $query_placeholder
      ->reveal();
    $condition = new Condition('AND');
    $condition
      ->condition('name', $given, 'LIKE');
    $condition
      ->compile($connection, $query_placeholder);
    $this
      ->assertEquals('name LIKE :db_condition_placeholder_0', $condition
      ->__toString());
    $this
      ->assertEquals([
      ':db_condition_placeholder_0' => $expected,
    ], $condition
      ->arguments());
  }

  /**
   * Data Provider.
   */
  public function dataProviderForTestLike() {
    return [
      [
        '%',
        '%',
      ],
      [
        '\\%',
        '[%]',
      ],
      [
        '\\_',
        '[_]',
      ],
      [
        '\\\\',
        '\\',
      ],
      [
        '[\\%]',
        '[[][%]]',
      ],
    ];
  }

  /**
   * Test the REGEXP operator string replacement.
   *
   * @dataProvider dataProviderForTestRegexp
   */
  public function testRegexp($expected, $field_name, $operator, $pattern) {
    $connection = $this
      ->prophesize(Connection::class);
    $connection
      ->escapeField($field_name)
      ->will(function ($args) {
      return preg_replace('/[^A-Za-z0-9_.]+/', '', $args[0]);
    });
    $connection
      ->mapConditionOperator($operator)
      ->willReturn([
      'operator' => $operator,
    ]);
    $connection = $connection
      ->reveal();
    $query_placeholder = $this
      ->prophesize(PlaceholderInterface::class);
    $counter = 0;
    $query_placeholder
      ->nextPlaceholder()
      ->will(function () use (&$counter) {
      return $counter++;
    });
    $query_placeholder
      ->uniqueIdentifier()
      ->willReturn(4);
    $query_placeholder = $query_placeholder
      ->reveal();
    $condition = new Condition('AND');
    $condition
      ->condition($field_name, $pattern, $operator);
    $condition
      ->compile($connection, $query_placeholder);
    $this
      ->assertEquals($expected, $condition
      ->__toString());
    $this
      ->assertEquals([
      ':db_condition_placeholder_0' => $pattern,
    ], $condition
      ->arguments());
  }

  /**
   * Provides a list of known operations and the expected output.
   */
  public function dataProviderForTestRegexp() {
    return [
      [
        '(REGEXP(:db_condition_placeholder_0, name) = 1)',
        'name',
        'REGEXP',
        '^P',
      ],
      [
        '(REGEXP(:db_condition_placeholder_0, name123) = 1)',
        'name-123',
        'REGEXP',
        's$',
      ],
      [
        '(REGEXP(:db_condition_placeholder_0, name) = 0)',
        'name',
        'NOT REGEXP',
        '^\\$[a-z][a-zA-Z_]$',
      ],
      [
        '(REGEXP(:db_condition_placeholder_0, name123) = 0)',
        'name-123',
        'NOT REGEXP',
        '^[a-z].*$',
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
SqlsrvConditionTest::dataProviderForTestLike public function Data Provider.
SqlsrvConditionTest::dataProviderForTestRegexp public function Provides a list of known operations and the expected output.
SqlsrvConditionTest::testLike public function Test the escaping strategy of the LIKE operator.
SqlsrvConditionTest::testRegexp public function Test the REGEXP operator string replacement.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340