Pull merge.
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Database / ConnectionTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Database;
4
5 use Drupal\Tests\Core\Database\Stub\StubConnection;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * Tests the Connection class.
10  *
11  * @group Database
12  */
13 class ConnectionTest extends UnitTestCase {
14
15   /**
16    * Dataprovider for testPrefixRoundTrip().
17    *
18    * @return array
19    *   Array of arrays with the following elements:
20    *   - Arguments to pass to Connection::setPrefix().
21    *   - Expected result from Connection::tablePrefix().
22    */
23   public function providerPrefixRoundTrip() {
24     return [
25       [
26         ['' => 'test_'],
27         'test_',
28       ],
29       [
30         [
31           'fooTable' => 'foo_',
32           'barTable' => 'bar_',
33         ],
34         [
35           'fooTable' => 'foo_',
36           'barTable' => 'bar_',
37         ],
38       ],
39     ];
40   }
41
42   /**
43    * Exercise setPrefix() and tablePrefix().
44    *
45    * @dataProvider providerPrefixRoundTrip
46    */
47   public function testPrefixRoundTrip($expected, $prefix_info) {
48     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
49     $connection = new StubConnection($mock_pdo, []);
50
51     // setPrefix() is protected, so we make it accessible with reflection.
52     $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\StubConnection');
53     $set_prefix = $reflection->getMethod('setPrefix');
54     $set_prefix->setAccessible(TRUE);
55
56     // Set the prefix data.
57     $set_prefix->invokeArgs($connection, [$prefix_info]);
58     // Check the round-trip.
59     foreach ($expected as $table => $prefix) {
60       $this->assertEquals($prefix, $connection->tablePrefix($table));
61     }
62   }
63
64   /**
65    * Dataprovider for testPrefixTables().
66    *
67    * @return array
68    *   Array of arrays with the following elements:
69    *   - Expected result.
70    *   - Table prefix.
71    *   - Query to be prefixed.
72    */
73   public function providerTestPrefixTables() {
74     return [
75       [
76         'SELECT * FROM test_table',
77         'test_',
78         'SELECT * FROM {table}',
79       ],
80       [
81         'SELECT * FROM first_table JOIN second_thingie',
82         [
83           'table' => 'first_',
84           'thingie' => 'second_',
85         ],
86         'SELECT * FROM {table} JOIN {thingie}',
87       ],
88     ];
89   }
90
91   /**
92    * Exercise the prefixTables() method.
93    *
94    * @dataProvider providerTestPrefixTables
95    */
96   public function testPrefixTables($expected, $prefix_info, $query) {
97     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
98     $connection = new StubConnection($mock_pdo, ['prefix' => $prefix_info]);
99     $this->assertEquals($expected, $connection->prefixTables($query));
100   }
101
102   /**
103    * Dataprovider for testEscapeMethods().
104    *
105    * @return array
106    *   Array of arrays with the following elements:
107    *   - Expected escaped string.
108    *   - String to escape.
109    */
110   public function providerEscapeMethods() {
111     return [
112       ['thing', 'thing'],
113       ['_item', '_item'],
114       ['item_', 'item_'],
115       ['_item_', '_item_'],
116       ['', '!@#$%^&*()-=+'],
117       ['123', '!1@2#3'],
118     ];
119   }
120
121   /**
122    * Test the various escaping methods.
123    *
124    * All tested together since they're basically the same method
125    * with different names.
126    *
127    * @dataProvider providerEscapeMethods
128    * @todo Separate test method for each escape method?
129    */
130   public function testEscapeMethods($expected, $name) {
131     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
132     $connection = new StubConnection($mock_pdo, []);
133     $this->assertEquals($expected, $connection->escapeDatabase($name));
134     $this->assertEquals($expected, $connection->escapeTable($name));
135     $this->assertEquals($expected, $connection->escapeField($name));
136     $this->assertEquals($expected, $connection->escapeAlias($name));
137   }
138
139   /**
140    * Dataprovider for testGetDriverClass().
141    *
142    * @return array
143    *   Array of arrays with the following elements:
144    *   - Expected namespaced class name.
145    *   - Driver.
146    *   - Namespace.
147    *   - Class name without namespace.
148    */
149   public function providerGetDriverClass() {
150     return [
151       [
152         'nonexistent_class',
153         '\\',
154         'nonexistent_class',
155       ],
156       [
157         'Drupal\Tests\Core\Database\Stub\Select',
158         NULL,
159         'Select',
160       ],
161       [
162         'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
163         'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
164         'Schema',
165       ],
166     ];
167   }
168
169   /**
170    * Test getDriverClass().
171    *
172    * @dataProvider providerGetDriverClass
173    */
174   public function testGetDriverClass($expected, $namespace, $class) {
175     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
176     $connection = new StubConnection($mock_pdo, ['namespace' => $namespace]);
177     // Set the driver using our stub class' public property.
178     $this->assertEquals($expected, $connection->getDriverClass($class));
179   }
180
181   /**
182    * Dataprovider for testSchema().
183    *
184    * @return array
185    *   Array of arrays with the following elements:
186    *   - Expected namespaced class of schema object.
187    *   - Driver for PDO connection.
188    *   - Namespace for connection.
189    */
190   public function providerSchema() {
191     return [
192       [
193         'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
194         'stub',
195         'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
196       ],
197     ];
198   }
199
200   /**
201    * Test Connection::schema().
202    *
203    * @dataProvider providerSchema
204    */
205   public function testSchema($expected, $driver, $namespace) {
206     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
207     $connection = new StubConnection($mock_pdo, ['namespace' => $namespace]);
208     $connection->driver = $driver;
209     $this->assertInstanceOf($expected, $connection->schema());
210   }
211
212   /**
213    * Test Connection::destroy().
214    */
215   public function testDestroy() {
216     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
217     // Mocking StubConnection gives us access to the $schema attribute.
218     $connection = $this->getMock(
219       'Drupal\Tests\Core\Database\Stub\StubConnection',
220       NULL,
221       [$mock_pdo, ['namespace' => 'Drupal\\Tests\\Core\\Database\\Stub\\Driver']]
222     );
223     // Generate a schema object in order to verify that we've NULLed it later.
224     $this->assertInstanceOf(
225       'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
226       $connection->schema()
227     );
228     $connection->destroy();
229     $this->assertAttributeEquals(NULL, 'schema', $connection);
230   }
231
232   /**
233    * Dataprovider for testMakeComments().
234    *
235    * @return array
236    *   Array of arrays with the following elements:
237    *   - Expected filtered comment.
238    *   - Arguments for Connection::makeComment().
239    */
240   public function providerMakeComments() {
241     return [
242       [
243         '/*  */ ',
244         [''],
245       ],
246       [
247         '/* Exploit  *  / DROP TABLE node. -- */ ',
248         ['Exploit * / DROP TABLE node; --'],
249       ],
250       [
251         '/* Exploit  *  / DROP TABLE node. --. another comment */ ',
252         ['Exploit * / DROP TABLE node; --', 'another comment'],
253       ],
254     ];
255   }
256
257   /**
258    * Test Connection::makeComments().
259    *
260    * @dataProvider providerMakeComments
261    */
262   public function testMakeComments($expected, $comment_array) {
263     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
264     $connection = new StubConnection($mock_pdo, []);
265     $this->assertEquals($expected, $connection->makeComment($comment_array));
266   }
267
268   /**
269    * Dataprovider for testFilterComments().
270    *
271    * @return array
272    *   Array of arrays with the following elements:
273    *   - Expected filtered comment.
274    *   - Comment to filter.
275    */
276   public function providerFilterComments() {
277     return [
278       ['', ''],
279       ['Exploit  *  / DROP TABLE node. --', 'Exploit * / DROP TABLE node; --'],
280       ['Exploit  * / DROP TABLE node. --', 'Exploit */ DROP TABLE node; --'],
281     ];
282   }
283
284   /**
285    * Test Connection::filterComments().
286    *
287    * @dataProvider providerFilterComments
288    */
289   public function testFilterComments($expected, $comment) {
290     $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
291     $connection = new StubConnection($mock_pdo, []);
292
293     // filterComment() is protected, so we make it accessible with reflection.
294     $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\StubConnection');
295     $filter_comment = $reflection->getMethod('filterComment');
296     $filter_comment->setAccessible(TRUE);
297
298     $this->assertEquals(
299       $expected,
300       $filter_comment->invokeArgs($connection, [$comment])
301     );
302   }
303
304 }