function BasicSyntaxTest::testLikeBackslash

Same name in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php \Drupal\KernelTests\Core\Database\BasicSyntaxTest::testLikeBackslash()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php \Drupal\KernelTests\Core\Database\BasicSyntaxTest::testLikeBackslash()
  3. 11.x core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php \Drupal\KernelTests\Core\Database\BasicSyntaxTest::testLikeBackslash()

Tests a LIKE query containing a backslash.

File

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

Class

BasicSyntaxTest
Tests SQL syntax interpretation.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testLikeBackslash() {
    $this->connection
        ->insert('test')
        ->fields([
        'name',
    ])
        ->values([
        'name' => 'abcde\\f',
    ])
        ->values([
        'name' => 'abc%\\_',
    ])
        ->execute();
    // Match both rows using a LIKE expression with two wildcards and a verbatim
    // backslash.
    $num_matches = $this->connection
        ->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 = $this->connection
        ->select('test', 't')
        ->condition('name', $this->connection
        ->escapeLike('abc%\\_'), 'LIKE')
        ->countQuery()
        ->execute()
        ->fetchField();
    $this->assertIdentical($num_matches, '1', 'Found 1 record.');
}

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