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