You are here

public function ConditionTest::testPdoBugExists in Drupal driver for SQL Server and SQL Azure 8.2

Same name and namespace in other branches
  1. 4.2.x tests/src/Kernel/ConditionTest.php \Drupal\Tests\sqlsrv\Kernel\ConditionTest::testPdoBugExists()
  2. 3.0.x tests/src/Kernel/ConditionTest.php \Drupal\Tests\sqlsrv\Kernel\ConditionTest::testPdoBugExists()
  3. 3.1.x tests/src/Kernel/ConditionTest.php \Drupal\Tests\sqlsrv\Kernel\ConditionTest::testPdoBugExists()
  4. 4.0.x tests/src/Kernel/ConditionTest.php \Drupal\Tests\sqlsrv\Kernel\ConditionTest::testPdoBugExists()
  5. 4.1.x tests/src/Kernel/ConditionTest.php \Drupal\Tests\sqlsrv\Kernel\ConditionTest::testPdoBugExists()

Test presence of PDO Bug.

Bug Report.

This test will throw an exception while the PDO bug exists. When it is fixed, the LIKE operator can safely use "ESCAPE '\'" and custom code within the Condition class can be removed.

File

tests/src/Kernel/ConditionTest.php, line 51

Class

ConditionTest
Test the functions of the custom Condition class.

Namespace

Drupal\Tests\sqlsrv\Kernel

Code

public function testPdoBugExists() {

  // Extend Connection with new $sqlsrvConditionOperatorMap array.
  $connection = $this->connection;
  $reflection = new \ReflectionClass($connection);
  $reflection_property = $reflection
    ->getProperty('sqlsrvConditionOperatorMap');
  $reflection_property
    ->setAccessible(TRUE);
  $desired_operator_map = [
    'LIKE' => [
      'postfix' => " ESCAPE '\\'",
    ],
  ];
  $reflection_property
    ->setValue($connection, $desired_operator_map);

  // Set Condition to use parent::compile()
  $condition = new CoreCondition('AND');
  $query = new Select('test', 't', $connection);
  $reflection = new \ReflectionClass($query);
  $reflection_property = $reflection
    ->getProperty('condition');
  $reflection_property
    ->setAccessible(TRUE);
  $reflection_property
    ->setValue($query, $condition);

  // Expect exception when executing query;
  // Should specify what type.
  $this
    ->expectException(\Exception::class);

  // Create and execute buggy query.
  $query
    ->addField('t', 'job');
  $query
    ->condition('job', '%i%', 'LIKE');
  $query
    ->condition('name', '%o%', 'LIKE');
  $query
    ->execute();
}