1. 8.2.x core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php
  2. 8.0.x core/modules/system/src/Tests/Database/BasicSyntaxTest.php
  3. 8.1.x core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php
  4. 8.3.x core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php

Namespace

Drupal\KernelTests\Core\Database

File

core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php
View source
  1. <?php
  2. namespace Drupal\KernelTests\Core\Database;
  3. /**
  4. * Tests SQL syntax interpretation.
  5. *
  6. * In order to ensure consistent SQL handling throughout Drupal
  7. * across multiple kinds of database systems, we test that the
  8. * database system interprets SQL syntax in an expected fashion.
  9. *
  10. * @group Database
  11. */
  12. class BasicSyntaxTest extends DatabaseTestBase {
  13. /**
  14. * Tests string concatenation.
  15. */
  16. function testConcatLiterals() {
  17. $result = db_query('SELECT CONCAT(:a1, CONCAT(:a2, CONCAT(:a3, CONCAT(:a4, :a5))))', array(
  18. ':a1' => 'This',
  19. ':a2' => ' ',
  20. ':a3' => 'is',
  21. ':a4' => ' a ',
  22. ':a5' => 'test.',
  23. ));
  24. $this->assertIdentical($result->fetchField(), 'This is a test.', 'Basic CONCAT works.');
  25. }
  26. /**
  27. * Tests string concatenation with field values.
  28. */
  29. function testConcatFields() {
  30. $result = db_query('SELECT CONCAT(:a1, CONCAT(name, CONCAT(:a2, CONCAT(age, :a3)))) FROM {test} WHERE age = :age', array(
  31. ':a1' => 'The age of ',
  32. ':a2' => ' is ',
  33. ':a3' => '.',
  34. ':age' => 25,
  35. ));
  36. $this->assertIdentical($result->fetchField(), 'The age of John is 25.', 'Field CONCAT works.');
  37. }
  38. /**
  39. * Tests string concatenation with separator.
  40. */
  41. function testConcatWsLiterals() {
  42. $result = db_query("SELECT CONCAT_WS(', ', :a1, NULL, :a2, :a3, :a4)", array(
  43. ':a1' => 'Hello',
  44. ':a2' => NULL,
  45. ':a3' => '',
  46. ':a4' => 'world.',
  47. ));
  48. $this->assertIdentical($result->fetchField(), 'Hello, , world.');
  49. }
  50. /**
  51. * Tests string concatenation with separator, with field values.
  52. */
  53. function testConcatWsFields() {
  54. $result = db_query("SELECT CONCAT_WS('-', :a1, name, :a2, age) FROM {test} WHERE age = :age", array(
  55. ':a1' => 'name',
  56. ':a2' => 'age',
  57. ':age' => 25,
  58. ));
  59. $this->assertIdentical($result->fetchField(), 'name-John-age-25');
  60. }
  61. /**
  62. * Tests escaping of LIKE wildcards.
  63. */
  64. function testLikeEscape() {
  65. db_insert('test')
  66. ->fields(array(
  67. 'name' => 'Ring_',
  68. ))
  69. ->execute();
  70. // Match both "Ringo" and "Ring_".
  71. $num_matches = db_select('test', 't')
  72. ->condition('name', 'Ring_', 'LIKE')
  73. ->countQuery()
  74. ->execute()
  75. ->fetchField();
  76. $this->assertIdentical($num_matches, '2', 'Found 2 records.');
  77. // Match only "Ring_" using a LIKE expression with no wildcards.
  78. $num_matches = db_select('test', 't')
  79. ->condition('name', db_like('Ring_'), 'LIKE')
  80. ->countQuery()
  81. ->execute()
  82. ->fetchField();
  83. $this->assertIdentical($num_matches, '1', 'Found 1 record.');
  84. }
  85. /**
  86. * Tests a LIKE query containing a backslash.
  87. */
  88. function testLikeBackslash() {
  89. db_insert('test')
  90. ->fields(array('name'))
  91. ->values(array(
  92. 'name' => 'abcde\f',
  93. ))
  94. ->values(array(
  95. 'name' => 'abc%\_',
  96. ))
  97. ->execute();
  98. // Match both rows using a LIKE expression with two wildcards and a verbatim
  99. // backslash.
  100. $num_matches = db_select('test', 't')
  101. ->condition('name', 'abc%\\\\_', 'LIKE')
  102. ->countQuery()
  103. ->execute()
  104. ->fetchField();
  105. $this->assertIdentical($num_matches, '2', 'Found 2 records.');
  106. // Match only the former using a LIKE expression with no wildcards.
  107. $num_matches = db_select('test', 't')
  108. ->condition('name', db_like('abc%\_'), 'LIKE')
  109. ->countQuery()
  110. ->execute()
  111. ->fetchField();
  112. $this->assertIdentical($num_matches, '1', 'Found 1 record.');
  113. }
  114. /**
  115. * Tests \Drupal\Core\Database\Connection::getFullQualifiedTableName().
  116. */
  117. public function testGetFullQualifiedTableName() {
  118. $database = \Drupal::database();
  119. $num_matches = $database->select($database->getFullQualifiedTableName('test'), 't')
  120. ->countQuery()
  121. ->execute()
  122. ->fetchField();
  123. $this->assertIdentical($num_matches, '4', 'Found 4 records.');
  124. }
  125. }

Classes

Namesort descending Description
BasicSyntaxTest Tests SQL syntax interpretation.