8.3.x BasicSyntaxTest.php public BasicSyntaxTest::testLikeBackslash()
8.0.x BasicSyntaxTest.php BasicSyntaxTest::testLikeBackslash()
8.1.x BasicSyntaxTest.php BasicSyntaxTest::testLikeBackslash()
8.2.x BasicSyntaxTest.php BasicSyntaxTest::testLikeBackslash()
8.4.x BasicSyntaxTest.php public BasicSyntaxTest::testLikeBackslash()

Tests a LIKE query containing a backslash.

File

core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php, line 96

Class

BasicSyntaxTest
Tests SQL syntax interpretation.

Namespace

Drupal\KernelTests\Core\Database

Code

function testLikeBackslash() {
  db_insert('test')
    ->fields(array('name'))
    ->values(array(
      'name' => 'abcde\f',
    ))
    ->values(array(
      'name' => 'abc%\_',
    ))
    ->execute();

  // Match both rows using a LIKE expression with two wildcards and a verbatim
  // backslash.
  $num_matches = db_select('test', 't')
    ->condition('name', 'abc%\\\\_', 'LIKE')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this->assertIdentical($num_matches, '2', 'Found 2 records.');
  // Match only the former using a LIKE expression with no wildcards.
  $num_matches = db_select('test', 't')
    ->condition('name', db_like('abc%\_'), 'LIKE')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this->assertIdentical($num_matches, '1', 'Found 1 record.');
}