Backup of db before drupal security update
[yaffs-website] / web / core / modules / simpletest / tests / src / Unit / TestInfoParsingTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\Tests\simpletest\Unit\TestInfoParsingTest.
6  */
7
8 namespace Drupal\Tests\simpletest\Unit;
9
10 use Composer\Autoload\ClassLoader;
11 use Drupal\Core\Extension\Extension;
12 use Drupal\Core\Extension\ModuleHandlerInterface;
13 use Drupal\simpletest\Exception\MissingGroupException;
14 use Drupal\simpletest\TestDiscovery;
15 use Drupal\Tests\UnitTestCase;
16 use org\bovigo\vfs\vfsStream;
17
18 /**
19  * @coversDefaultClass \Drupal\simpletest\TestDiscovery
20  * @group simpletest
21  */
22 class TestInfoParsingTest extends UnitTestCase {
23
24   /**
25    * @covers ::getTestInfo
26    * @dataProvider infoParserProvider
27    */
28   public function testTestInfoParser($expected, $classname, $doc_comment = NULL) {
29     $info = TestDiscovery::getTestInfo($classname, $doc_comment);
30     $this->assertEquals($expected, $info);
31   }
32
33   public function infoParserProvider() {
34     // A module provided unit test.
35     $tests[] = [
36       // Expected result.
37       [
38         'name' => 'Drupal\Tests\simpletest\Unit\TestInfoParsingTest',
39         'group' => 'simpletest',
40         'description' => 'Tests \Drupal\simpletest\TestDiscovery.',
41         'type' => 'PHPUnit-Unit',
42       ],
43       // Classname.
44       'Drupal\Tests\simpletest\Unit\TestInfoParsingTest',
45     ];
46
47     // A core unit test.
48     $tests[] = [
49       // Expected result.
50       [
51         'name' => 'Drupal\Tests\Core\DrupalTest',
52         'group' => 'DrupalTest',
53         'description' => 'Tests \Drupal.',
54         'type' => 'PHPUnit-Unit',
55       ],
56       // Classname.
57       'Drupal\Tests\Core\DrupalTest',
58     ];
59
60     // Functional PHPUnit test.
61     $tests[] = [
62       // Expected result.
63       [
64         'name' => 'Drupal\FunctionalTests\BrowserTestBaseTest',
65         'group' => 'browsertestbase',
66         'description' => 'Tests BrowserTestBase functionality.',
67         'type' => 'PHPUnit-Functional',
68       ],
69       // Classname.
70       'Drupal\FunctionalTests\BrowserTestBaseTest',
71     ];
72
73     // kernel PHPUnit test.
74     $tests['phpunit-kernel'] = [
75       // Expected result.
76       [
77         'name' => '\Drupal\Tests\file\Kernel\FileItemValidationTest',
78         'group' => 'file',
79         'description' => 'Tests that files referenced in file and image fields are always validated.',
80         'type' => 'PHPUnit-Kernel',
81       ],
82       // Classname.
83       '\Drupal\Tests\file\Kernel\FileItemValidationTest',
84     ];
85
86     // Simpletest classes can not be autoloaded in a PHPUnit test, therefore
87     // provide a docblock.
88     $tests[] = [
89       // Expected result.
90       [
91         'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
92         'group' => 'simpletest',
93         'description' => 'Tests the Simpletest UI internal browser.',
94         'type' => 'Simpletest',
95       ],
96       // Classname.
97       'Drupal\simpletest\Tests\ExampleSimpleTest',
98       // Doc block.
99       "/**
100  * Tests the Simpletest UI internal browser.
101  *
102  * @group simpletest
103  */
104  ",
105     ];
106
107     // Test with a different amount of leading spaces.
108     $tests[] = [
109       // Expected result.
110       [
111         'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
112         'group' => 'simpletest',
113         'description' => 'Tests the Simpletest UI internal browser.',
114         'type' => 'Simpletest',
115       ],
116       // Classname.
117       'Drupal\simpletest\Tests\ExampleSimpleTest',
118       // Doc block.
119       "/**
120    * Tests the Simpletest UI internal browser.
121    *
122    * @group simpletest
123    */
124    */
125  ",
126     ];
127
128     // Make sure that a "* @" inside a string does not get parsed as an
129     // annotation.
130     $tests[] = [
131       // Expected result.
132       [
133         'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
134         'group' => 'simpletest',
135         'description' => 'Tests the Simpletest UI internal browser. * @',
136         'type' => 'Simpletest',
137       ],
138       // Classname.
139       'Drupal\simpletest\Tests\ExampleSimpleTest',
140       // Doc block.
141       "/**
142    * Tests the Simpletest UI internal browser. * @
143    *
144    * @group simpletest
145    */
146  ",
147     ];
148
149     // Multiple @group annotations.
150     $tests[] = [
151       // Expected result.
152       [
153         'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
154         'group' => 'Test',
155         'description' => 'Tests the Simpletest UI internal browser.',
156         'type' => 'Simpletest',
157       ],
158       // Classname.
159       'Drupal\simpletest\Tests\ExampleSimpleTest',
160       // Doc block.
161       "/**
162  * Tests the Simpletest UI internal browser.
163  *
164  * @group Test
165  * @group simpletest
166  */
167  ",
168     ];
169
170     // @dependencies annotation.
171     $tests[] = [
172       // Expected result.
173       [
174         'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
175         'description' => 'Tests the Simpletest UI internal browser.',
176         'type' => 'Simpletest',
177         'requires' => ['module' => ['test']],
178         'group' => 'simpletest',
179       ],
180       // Classname.
181       'Drupal\simpletest\Tests\ExampleSimpleTest',
182       // Doc block.
183       "/**
184  * Tests the Simpletest UI internal browser.
185  *
186  * @dependencies test
187  * @group simpletest
188  */
189  ",
190     ];
191
192     // Multiple @dependencies annotation.
193     $tests[] = [
194       // Expected result.
195       [
196         'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
197         'description' => 'Tests the Simpletest UI internal browser.',
198         'type' => 'Simpletest',
199         'requires' => ['module' => ['test', 'test1', 'test2']],
200         'group' => 'simpletest',
201       ],
202       // Classname.
203       'Drupal\simpletest\Tests\ExampleSimpleTest',
204       // Doc block.
205       "/**
206  * Tests the Simpletest UI internal browser.
207  *
208  * @dependencies test, test1, test2
209  * @group simpletest
210  */
211  ",
212     ];
213
214     // Multi-line summary line.
215     $tests[] = [
216       // Expected result.
217       [
218         'name' => 'Drupal\simpletest\Tests\ExampleSimpleTest',
219         'description' => 'Tests the Simpletest UI internal browser. And the summary line continues an there is no gap to the annotation.',
220         'type' => 'Simpletest',
221         'group' => 'simpletest',
222       ],
223       // Classname.
224       'Drupal\simpletest\Tests\ExampleSimpleTest',
225       // Doc block.
226       "/**
227  * Tests the Simpletest UI internal browser. And the summary line continues an
228  * there is no gap to the annotation.
229  *
230  * @group simpletest
231  */
232  ",
233     ];
234     return $tests;
235   }
236
237   /**
238    * @covers ::getTestInfo
239    */
240   public function testTestInfoParserMissingGroup() {
241     $classname = 'Drupal\KernelTests\field\BulkDeleteTest';
242     $doc_comment = <<<EOT
243 /**
244  * Bulk delete storages and fields, and clean up afterwards.
245  */
246 EOT;
247     $this->setExpectedException(MissingGroupException::class, 'Missing @group annotation in Drupal\KernelTests\field\BulkDeleteTest');
248     TestDiscovery::getTestInfo($classname, $doc_comment);
249   }
250
251   /**
252    * @covers ::getTestInfo
253    */
254   public function testTestInfoParserMissingSummary() {
255     $classname = 'Drupal\KernelTests\field\BulkDeleteTest';
256     $doc_comment = <<<EOT
257 /**
258  * @group field
259  */
260 EOT;
261     $info = TestDiscovery::getTestInfo($classname, $doc_comment);
262     $this->assertEmpty($info['description']);
263   }
264
265   protected function setupVfsWithTestClasses() {
266     vfsStream::setup('drupal');
267
268     $test_file = <<<EOF
269 <?php
270
271 /**
272  * Test description
273  * @group example
274  */
275 class FunctionalExampleTest {}
276 EOF;
277
278     vfsStream::create([
279       'modules' => [
280         'test_module' => [
281           'tests' => [
282             'src' => [
283               'Functional' => [
284                 'FunctionalExampleTest.php' => $test_file,
285                 'FunctionalExampleTest2.php' => str_replace(['FunctionalExampleTest', '@group example'], ['FunctionalExampleTest2', '@group example2'], $test_file),
286               ],
287               'Kernel' => [
288                 'KernelExampleTest3.php' => str_replace(['FunctionalExampleTest', '@group example'], ['KernelExampleTest3', '@group example2'], $test_file),
289               ],
290             ],
291           ],
292         ],
293       ],
294     ]);
295   }
296
297   /**
298    * @covers ::getTestClasses
299    */
300   public function testGetTestClasses() {
301     $this->setupVfsWithTestClasses();
302     $class_loader = $this->prophesize(ClassLoader::class);
303     $module_handler = $this->prophesize(ModuleHandlerInterface::class);
304
305     $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal());
306
307     $extensions = [
308       'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'),
309     ];
310     $test_discovery->setExtensions($extensions);
311     $result = $test_discovery->getTestClasses();
312     $this->assertCount(2, $result);
313     $this->assertEquals([
314       'example' => [
315         'Drupal\Tests\test_module\Functional\FunctionalExampleTest' => [
316           'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest',
317           'description' => 'Test description',
318           'group' => 'example',
319           'type' => 'PHPUnit-Functional',
320         ],
321       ],
322       'example2' => [
323         'Drupal\Tests\test_module\Functional\FunctionalExampleTest2' => [
324           'name' => 'Drupal\Tests\test_module\Functional\FunctionalExampleTest2',
325           'description' => 'Test description',
326           'group' => 'example2',
327           'type' => 'PHPUnit-Functional',
328         ],
329         'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [
330           'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3',
331           'description' => 'Test description',
332           'group' => 'example2',
333           'type' => 'PHPUnit-Kernel',
334         ],
335       ],
336     ], $result);
337   }
338
339   /**
340    * @covers ::getTestClasses
341    */
342   public function testGetTestClassesWithSelectedTypes() {
343     $this->setupVfsWithTestClasses();
344     $class_loader = $this->prophesize(ClassLoader::class);
345     $module_handler = $this->prophesize(ModuleHandlerInterface::class);
346
347     $test_discovery = new TestTestDiscovery('vfs://drupal', $class_loader->reveal(), $module_handler->reveal());
348
349     $extensions = [
350       'test_module' => new Extension('vfs://drupal', 'module', 'modules/test_module/test_module.info.yml'),
351     ];
352     $test_discovery->setExtensions($extensions);
353     $result = $test_discovery->getTestClasses(NULL, ['PHPUnit-Kernel']);
354     $this->assertCount(2, $result);
355     $this->assertEquals([
356       'example' => [
357       ],
358       'example2' => [
359         'Drupal\Tests\test_module\Kernel\KernelExampleTest3' => [
360           'name' => 'Drupal\Tests\test_module\Kernel\KernelExampleTest3',
361           'description' => 'Test description',
362           'group' => 'example2',
363           'type' => 'PHPUnit-Kernel',
364         ],
365       ],
366     ], $result);
367   }
368
369 }
370
371 class TestTestDiscovery extends TestDiscovery {
372
373   /**
374    * @var \Drupal\Core\Extension\Extension[]
375    */
376   protected $extensions = [];
377
378   public function setExtensions(array $extensions) {
379     $this->extensions = $extensions;
380   }
381
382   /**
383    * {@inheritdoc}
384    */
385   protected function getExtensions() {
386     return $this->extensions;
387   }
388
389   /**
390    * @covers ::getPhpunitTestSuite
391    * @dataProvider providerTestGetPhpunitTestSuite
392    */
393   public function testGetPhpunitTestSuite($classname, $expected) {
394     $this->assertEquals($expected, TestDiscovery::getPhpunitTestSuite($classname));
395   }
396
397   public function providerTestGetPhpunitTestSuite() {
398     $data = [];
399     $data['simpletest-webtest'] = ['\Drupal\rest\Tests\NodeTest', FALSE];
400     $data['simpletest-kerneltest'] = ['\Drupal\hal\Tests\FileNormalizeTest', FALSE];
401     $data['module-unittest'] = [static::class, 'Unit'];
402     $data['module-kerneltest'] = ['\Drupal\KernelTests\Core\Theme\TwigMarkupInterfaceTest', 'Kernel'];
403     $data['module-functionaltest'] = ['\Drupal\FunctionalTests\BrowserTestBaseTest', 'Functional'];
404     $data['module-functionaljavascripttest'] = ['\Drupal\Tests\toolbar\FunctionalJavascript\ToolbarIntegrationTest', 'FunctionalJavascript'];
405     $data['core-unittest'] = ['\Drupal\Tests\ComposerIntegrationTest', 'Unit'];
406     $data['core-unittest2'] = ['Drupal\Tests\Core\DrupalTest', 'Unit'];
407     $data['core-kerneltest'] = ['\Drupal\KernelTests\KernelTestBaseTest', 'Kernel'];
408     $data['core-functionaltest'] = ['\Drupal\FunctionalTests\ExampleTest', 'Functional'];
409     $data['core-functionaljavascripttest'] = ['\Drupal\FunctionalJavascriptTests\ExampleTest', 'FunctionalJavascript'];
410
411     return $data;
412   }
413
414 }
415
416 namespace Drupal\simpletest\Tests;
417
418 use Drupal\simpletest\WebTestBase;
419
420 /**
421  * Tests the Simpletest UI internal browser.
422  *
423  * @group simpletest
424  */
425 class ExampleSimpleTest extends WebTestBase {
426 }