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