Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / Tests / TestSuites / TestSuiteBaseTest.php
1 <?php
2
3 namespace Drupal\Tests\TestSuites;
4
5 use org\bovigo\vfs\vfsStream;
6
7 // The test suite class is not part of the autoloader, we need to include it
8 // manually.
9 require_once __DIR__ . '/../../../TestSuites/TestSuiteBase.php';
10
11 /**
12  * @coversDefaultClass \Drupal\Tests\TestSuites\TestSuiteBase
13  *
14  * @group TestSuite
15  */
16 class TestSuiteBaseTest extends \PHPUnit_Framework_TestCase {
17
18   /**
19    * Helper method to set up the file system.
20    *
21    * @return array[]
22    *   A Drupal filesystem suitable for use with vfsStream.
23    */
24   protected function getFilesystem() {
25     return [
26       'core' => [
27         'modules' => [],
28         'profiles' => [],
29         'tests' => [
30           'Drupal' => [
31             'NotUnitTests' => [
32               'CoreNotUnitTest.php' => '<?php',
33             ],
34             'Tests' => [
35               'CoreUnitTest.php' => '<?php',
36             ],
37           ],
38         ],
39       ],
40     ];
41   }
42
43   /**
44    * @return array[]
45    *   Test data for testAddTestsBySuiteNamespaceCore(). An array of arrays:
46    *   - A filesystem array for vfsStream.
47    *   - The sub-namespace of the test suite.
48    *   - The array of tests expected to be discovered.
49    */
50   public function provideCoreTests() {
51     $filesystem = $this->getFilesystem();
52     return [
53       'unit-tests' => [
54         $filesystem,
55         'Unit',
56         [
57           'Drupal\Tests\CoreUnitTest' => 'vfs://root/core/tests/Drupal/Tests/CoreUnitTest.php',
58         ],
59       ],
60       'not-unit-tests' => [
61         $filesystem,
62         'NotUnit',
63         [
64           'Drupal\NotUnitTests\CoreNotUnitTest' => 'vfs://root/core/tests/Drupal/NotUnitTests/CoreNotUnitTest.php',
65         ],
66       ],
67     ];
68   }
69
70   /**
71    * Tests for special case behavior of unit test suite namespaces in core.
72    *
73    * @covers ::addTestsBySuiteNamespace
74    *
75    * @dataProvider provideCoreTests
76    */
77   public function testAddTestsBySuiteNamespaceCore($filesystem, $suite_namespace, $expected_tests) {
78     // Set up the file system.
79     $vfs = vfsStream::setup('root');
80     vfsStream::create($filesystem, $vfs);
81
82     // Make a stub suite base to test.
83     $stub = new StubTestSuiteBase('test_me');
84
85     // Access addTestsBySuiteNamespace().
86     $ref_add_tests = new \ReflectionMethod($stub, 'addTestsBySuiteNamespace');
87     $ref_add_tests->setAccessible(TRUE);
88
89     // Invoke addTestsBySuiteNamespace().
90     $ref_add_tests->invokeArgs($stub, [vfsStream::url('root'), $suite_namespace]);
91
92     // Determine if we loaded the expected test files.
93     $this->assertNotEmpty($stub->testFiles);
94     $this->assertEmpty(array_diff_assoc($expected_tests, $stub->testFiles));
95   }
96
97   /**
98    * Tests the assumption that local time is in 'Australia/Sydney'.
99    */
100   public function testLocalTimeZone() {
101     // The 'Australia/Sydney' time zone is set in core/tests/bootstrap.php
102     $this->assertEquals('Australia/Sydney', date_default_timezone_get());
103   }
104
105 }
106
107 /**
108  * Stub subclass of TestSuiteBase.
109  *
110  * We use this class to alter the behavior of TestSuiteBase so it can be
111  * testable.
112  */
113 class StubTestSuiteBase extends TestSuiteBase {
114
115   /**
116    * Test files discovered by addTestsBySuiteNamespace().
117    *
118    * @var string[]
119    */
120   public $testFiles = [];
121
122   /**
123    * {@inheritdoc}
124    */
125   protected function findExtensionDirectories($root) {
126     // We have to stub findExtensionDirectories() because we can't inject a
127     // vfsStream filesystem into drupal_phpunit_find_extension_directories(),
128     // which uses \SplFileInfo->getRealPath(). getRealPath() resolves
129     // stream-based paths to an empty string. See
130     // https://github.com/mikey179/vfsStream/wiki/Known-Issues
131     return [];
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   public function addTestFiles($filenames) {
138     // We stub addTestFiles() because the parent implementation can't deal with
139     // vfsStream-based filesystems due to an error in
140     // stream_resolve_include_path(). See
141     // https://github.com/mikey179/vfsStream/issues/5 Here we just store the
142     // test file being added in $this->testFiles.
143     $this->testFiles = array_merge($this->testFiles, $filenames);
144   }
145
146 }