public function SelectQueryTest::testEscapeLike in Drupal driver for SQL Server and SQL Azure 8
Test LIKE statement wildcards are properly escaped.
File
- src/
Tests/ SelectQueryTest.php, line 211 - Definition of Drupal\sqlsrv\Tests\SelectQueryTest.
Class
- SelectQueryTest
- General tests for SQL Server database driver.
Namespace
Drupal\sqlsrv\TestsCode
public function testEscapeLike() {
// Test expected escaped characters
$string = 't[e%s]t_\\';
$expected = 't[[]e[%]s[]]t[_]\\';
$actual = db_like($string);
$this
->assertEqual($actual, $expected, 'Properly escaped LIKE statement wildcards.');
db_insert('test_task')
->fields(array(
'task' => 'T\\est',
))
->execute();
$query = db_select('test_task', 't');
$query
->fields('t');
$query
->condition('t.task', db_like('T\\est'), 'LIKE');
$result = $query
->execute()
->fetchAll();
$this
->assertEqual(count($result), 1, t('db_select returned the correct number of total rows.'));
db_insert('test_task')
->fields(array(
'task' => 'T\'est',
))
->execute();
$query = db_select('test_task', 't');
$query
->fields('t');
$query
->condition('t.task', db_like('T\'est'), 'LIKE');
$result = $query
->execute()
->fetchAll();
$this
->assertEqual(count($result), 1, t('db_select returned the correct number of total rows.'));
// db_select: Test unescaped wildcard.
$query = db_select('test_task', 't');
$query
->condition('t.task', '[s]leep', 'LIKE');
$query
->fields('t');
$result = $query
->execute()
->fetchAll();
$this
->assertEqual(count($result), 2, t('db_select returned the correct number of total rows.'));
// db_select: Test unescaped wildcard.
$query = db_select('test_task', 't');
$query
->condition('t.task', '[s]leep', 'LIKE');
$query
->fields('t');
$result = $query
->execute()
->fetchAll();
$this
->assertEqual(count($result), 2, t('db_select returned the correct number of total rows.'));
// db_select: Test escaped wildcard.
$query = db_select('test_task', 't');
$query
->condition('t.task', db_like('[s]leep'), 'LIKE');
$query
->fields('t');
$result = $query
->execute()
->fetchAll();
$this
->assertEqual(count($result), 0, t('db_select returned the correct number of total rows.'));
// db_select->where: Test unescaped wildcard.
$query = db_select('test_task', 't');
$query
->where('t.task LIKE :task', array(
':task' => '[s]leep',
));
$query
->fields('t');
$result = $query
->execute()
->fetchAll();
$this
->assertEqual(count($result), 2, t('db_select returned the correct number of total rows.'));
// db_select->where: Test escaped wildcard.
$query = db_select('test_task', 't');
$query
->where('t.task LIKE :task', array(
':task' => db_like('[s]leep'),
));
$query
->fields('t');
$result = $query
->execute()
->fetchAll();
$this
->assertEqual(count($result), 0, t('db_select returned the correct number of total rows.'));
// db_query: Test unescaped wildcard.
$query = db_query('SELECT COUNT(*) FROM {test_task} WHERE task LIKE :task', array(
':task' => '[s]leep',
));
$result = $query
->fetchField();
$this
->assertEqual($result, 2, t('db_query returned the correct number of total rows.'));
// db_query: Test escaped wildcard.
$query = db_query('SELECT COUNT(*) FROM {test_task} WHERE task LIKE :task', array(
':task' => db_like('[s]leep'),
));
$result = $query
->fetchField();
$this
->assertEqual($result, 0, t('db_query returned the correct number of total rows.'));
}