You are here

public function BasicSyntaxTest::testLikeEscape in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php \Drupal\KernelTests\Core\Database\BasicSyntaxTest::testLikeEscape()
  2. 10 core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php \Drupal\KernelTests\Core\Database\BasicSyntaxTest::testLikeEscape()

Tests escaping of LIKE wildcards.

File

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

Class

BasicSyntaxTest
Tests SQL syntax interpretation.

Namespace

Drupal\KernelTests\Core\Database

Code

public function testLikeEscape() {
  $this->connection
    ->insert('test')
    ->fields([
    'name' => 'Ring_',
  ])
    ->execute();

  // Match both "Ringo" and "Ring_".
  $num_matches = $this->connection
    ->select('test', 't')
    ->condition('name', 'Ring_', 'LIKE')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertIdentical($num_matches, '2', 'Found 2 records.');

  // Match only "Ring_" using a LIKE expression with no wildcards.
  $num_matches = $this->connection
    ->select('test', 't')
    ->condition('name', $this->connection
    ->escapeLike('Ring_'), 'LIKE')
    ->countQuery()
    ->execute()
    ->fetchField();
  $this
    ->assertIdentical($num_matches, '1', 'Found 1 record.');
}