db backup prior to drupal security update
[yaffs-website] / vendor / webflo / drupal-finder / tests / DrupalFinderTest.php
1 <?php
2
3 use org\bovigo\vfs\vfsStream;
4
5 class DrupalFinderTest extends PHPUnit_Framework_TestCase
6 {
7     /**
8      * @var \DrupalFinder\DrupalFinder
9      */
10     protected $finder;
11
12     protected static $fileStructure = [
13       'autoload.php' => '',
14       'composer.json' => '',
15       'core' => [
16         'includes' => [
17           'common.inc' => '',
18         ],
19         'misc' => [
20           'drupal.js' => '',
21         ],
22         'core.services.yml' => '',
23       ],
24       'modules' => [],
25       'vendor' => [],
26     ];
27
28     /**
29      * @return array
30      */
31     protected function getDrupalComposerStructure()
32     {
33         $fileStructure = [
34           'web' => static::$fileStructure,
35           'composer.json' => json_encode([
36             'require' => [
37               'drupal/core' => '*',
38             ],
39             'extra' => [
40               'installer-paths' => [
41                 'web/core' => [
42                   'type:drupal-core',
43                 ],
44               ],
45             ],
46           ]),
47           'vendor' => [],
48         ];
49         unset($fileStructure['web']['composer.json']);
50         unset($fileStructure['web']['vendor']);
51
52         return $fileStructure;
53     }
54
55     protected function setUp()
56     {
57         parent::setUp();
58         $this->finder = new \DrupalFinder\DrupalFinder();
59     }
60
61     public function testDrupalDefaultStructure()
62     {
63         $root = vfsStream::setup('root', null, static::$fileStructure);
64
65         $this->assertTrue($this->finder->locateRoot($root->url()));
66         $this->assertSame('vfs://root', $this->finder->getDrupalRoot());
67         $this->assertSame('vfs://root', $this->finder->getComposerRoot());
68         $this->assertSame('vfs://root/vendor', $this->finder->getVendorDir());
69
70         $this->assertTrue($this->finder->locateRoot($root->url() . '/misc'));
71         $this->assertSame('vfs://root', $this->finder->getDrupalRoot());
72         $this->assertSame('vfs://root', $this->finder->getComposerRoot());
73         $this->assertSame('vfs://root/vendor', $this->finder->getVendorDir());
74
75         $root = vfsStream::setup(
76             'root',
77             null,
78             ['project' => static::$fileStructure]
79         );
80         $this->assertFalse(
81             $this->finder->locateRoot($root->url()),
82             'Not in the scope of the project'
83         );
84         $this->assertFalse($this->finder->getDrupalRoot());
85         $this->assertFalse($this->finder->getComposerRoot());
86         $this->assertFalse($this->finder->getVendorDir());
87     }
88
89     public function testDrupalComposerStructure()
90     {
91         $fileStructure = $this->getDrupalComposerStructure();
92         $this->assertComposerStructure($fileStructure);
93     }
94
95     public function testDrupalComposerStructureWithoutRequire()
96     {
97         $fileStructure = [
98           'web' => static::$fileStructure,
99           'composer.json' => json_encode([
100             'extra' => [
101               'installer-paths' => [
102                 'web/core' => [
103                   'drupal/core',
104                 ],
105               ],
106             ],
107           ]),
108         ];
109         unset($fileStructure['web']['composer.json']);
110         $this->assertComposerStructure($fileStructure);
111     }
112
113     public function testNoDrupalRootWithRealFilesystem()
114     {
115         $root = $this->tempdir(sys_get_temp_dir());
116
117         $this->assertFalse($this->finder->locateRoot($root));
118         $this->assertFalse($this->finder->getDrupalRoot());
119         $this->assertFalse($this->finder->getComposerRoot());
120         $this->assertFalse($this->finder->getVendorDir());
121     }
122
123     public function testDrupalDefaultStructureWithRealFilesystem()
124     {
125         $root = $this->tempdir(sys_get_temp_dir());
126         $this->dumpToFileSystem(static::$fileStructure, $root);
127
128         $this->assertTrue($this->finder->locateRoot($root));
129         $this->assertSame($root, $this->finder->getDrupalRoot());
130         $this->assertSame($root, $this->finder->getComposerRoot());
131         $this->assertSame($root . '/vendor', $this->finder->getVendorDir());
132
133         // Test symlink implementation
134         $symlink = $this->tempdir(sys_get_temp_dir());
135         $this->symlink($root, $symlink . '/foo');
136
137         $this->assertTrue($this->finder->locateRoot($symlink . '/foo'));
138         $this->assertSame($root, $this->finder->getDrupalRoot());
139         $this->assertSame($root, $this->finder->getComposerRoot());
140         $this->assertSame($root . '/vendor', $this->finder->getVendorDir());
141     }
142
143     public function testDrupalComposerStructureWithRealFilesystem()
144     {
145         $root = $this->tempdir(sys_get_temp_dir());
146         $this->dumpToFileSystem($this->getDrupalComposerStructure(), $root);
147
148         $this->assertTrue($this->finder->locateRoot($root));
149         $this->assertSame($root . '/web', $this->finder->getDrupalRoot());
150         $this->assertSame($root, $this->finder->getComposerRoot());
151         $this->assertSame($root . '/vendor', $this->finder->getVendorDir());
152
153         // Test symlink implementation
154         $symlink = $this->tempdir(sys_get_temp_dir());
155         $this->symlink($root, $symlink . '/foo');
156
157         $this->assertTrue($this->finder->locateRoot($symlink . '/foo'));
158         $this->assertSame($root . '/web', $this->finder->getDrupalRoot());
159         $this->assertSame($root, $this->finder->getComposerRoot());
160         $this->assertSame($root . '/vendor', $this->finder->getVendorDir());
161     }
162
163     public function testDrupalWithLinkedModule()
164     {
165         $root = $this->tempdir(sys_get_temp_dir());
166         $this->dumpToFileSystem(static::$fileStructure, $root);
167
168         $module = $this->tempdir(sys_get_temp_dir());
169         $module_link = $root . '/modules/foo';
170         $this->symlink($module, $module_link);
171
172         $this->assertTrue($this->finder->locateRoot($module_link));
173         $this->assertSame($root, realpath($this->finder->getDrupalRoot()));
174         $this->assertSame($root, realpath($this->finder->getComposerRoot()));
175         $this->assertSame($root . '/vendor', realpath($this->finder->getVendorDir()));
176     }
177
178     public function testDrupalWithCustomVendor()
179     {
180         $root = $this->tempdir(sys_get_temp_dir());
181         $fileStructure = static::$fileStructure;
182         $fileStructure['composer.json'] = json_encode([
183             'config' => [
184                 'vendor-dir' => 'vendor-foo'
185             ]
186         ], JSON_UNESCAPED_SLASHES);
187         $fileStructure['vendor-foo'] = [];
188         $this->dumpToFileSystem($fileStructure, $root);
189
190         $this->assertTrue($this->finder->locateRoot($root));
191         $this->assertSame($root, realpath($this->finder->getDrupalRoot()));
192         $this->assertSame($root, realpath($this->finder->getComposerRoot()));
193         $this->assertSame($root . '/vendor-foo', realpath($this->finder->getVendorDir()));
194     }
195
196     protected function dumpToFileSystem($fileStructure, $root)
197     {
198         foreach ($fileStructure as $name => $content) {
199             if (is_array($content)) {
200                 mkdir($root . '/' . $name);
201                 $this->dumpToFileSystem($content, $root . '/' . $name);
202             } else {
203                 file_put_contents($root . '/' . $name, $content);
204             }
205         }
206     }
207
208     protected function tempdir($dir, $prefix = '', $mode = 0700)
209     {
210         if (substr($dir, -1) != '/') {
211             $dir .= '/';
212         }
213         do {
214             $path = $dir . $prefix . mt_rand(0, 9999999);
215         } while (!mkdir($path, $mode));
216         register_shutdown_function(
217             ['DrupalFinderTest', 'tempdir_remove'],
218             $path
219         );
220
221         return realpath($path);
222     }
223
224     public static function tempdir_remove($path)
225     {
226         if (is_link($path)) {
227             if (defined('PHP_WINDOWS_VERSION_BUILD')) {
228                 rmdir($path);
229             } else {
230                 unlink($path);
231             }
232
233             return;
234         }
235
236         foreach (scandir($path) as $child) {
237             if (in_array($child, ['.', '..'])) {
238                 continue;
239             }
240             $child = "$path/$child";
241             is_dir($child) ? static::tempdir_remove($child) : unlink($child);
242         }
243         rmdir($path);
244     }
245
246     /**
247      * @param $target
248      * @param $link
249      *
250      * @throws PHPUnit_Framework_SkippedTestError
251      */
252     private function symlink($target, $link)
253     {
254         try {
255             return symlink($target, $link);
256         } catch (Exception $e) {
257             if (defined('PHP_WINDOWS_VERSION_BUILD')
258               && strstr($e->getMessage(), WIN_ERROR_PRIVILEGE_NOT_HELD)
259             ) {
260                 $this->markTestSkipped(<<<'MESSAGE'
261 No privilege to create symlinks. Run test as Administrator (elevated process).
262 MESSAGE
263                 );
264             }
265             throw $e;
266         }
267     }
268
269     /**
270      * @param $fileStructure
271      */
272     protected function assertComposerStructure($fileStructure)
273     {
274         $root = vfsStream::setup('root', null, $fileStructure);
275         $this->assertTrue($this->finder->locateRoot($root->url() . '/web'));
276         $this->assertSame('vfs://root/web', $this->finder->getDrupalRoot());
277         $this->assertSame('vfs://root', $this->finder->getComposerRoot());
278         $this->assertSame('vfs://root/vendor', $this->finder->getVendorDir());
279
280         $this->assertTrue($this->finder->locateRoot($root->url() . '/web/misc'));
281         $this->assertSame('vfs://root/web', $this->finder->getDrupalRoot());
282         $this->assertSame('vfs://root', $this->finder->getComposerRoot());
283         $this->assertSame('vfs://root/vendor', $this->finder->getVendorDir());
284
285         $this->assertTrue($this->finder->locateRoot($root->url()));
286         $this->assertSame('vfs://root/web', $this->finder->getDrupalRoot());
287         $this->assertSame('vfs://root', $this->finder->getComposerRoot());
288         $this->assertSame('vfs://root/vendor', $this->finder->getVendorDir());
289
290         $root = vfsStream::setup(
291           'root',
292           null,
293           ['nested_folder' => $fileStructure]
294         );
295         $this->assertFalse($this->finder->locateRoot($root->url()));
296         $this->assertFalse($this->finder->getDrupalRoot());
297         $this->assertFalse($this->finder->getComposerRoot());
298         $this->assertFalse($this->finder->getVendorDir());
299     }
300 }
301
302 define('WIN_ERROR_PRIVILEGE_NOT_HELD', '1314');