3 use org\bovigo\vfs\vfsStream;
5 class DrupalFinderTest extends PHPUnit_Framework_TestCase
8 * @var \DrupalFinder\DrupalFinder
12 protected static $fileStructure = [
14 'composer.json' => '',
22 'core.services.yml' => '',
31 protected function getDrupalComposerStructure()
34 'web' => static::$fileStructure,
35 'composer.json' => json_encode([
40 'installer-paths' => [
49 unset($fileStructure['web']['composer.json']);
50 unset($fileStructure['web']['vendor']);
52 return $fileStructure;
55 protected function setUp()
58 $this->finder = new \DrupalFinder\DrupalFinder();
61 public function testDrupalDefaultStructure()
63 $root = vfsStream::setup('root', null, static::$fileStructure);
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());
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());
75 $root = vfsStream::setup(
78 ['project' => static::$fileStructure]
81 $this->finder->locateRoot($root->url()),
82 'Not in the scope of the project'
84 $this->assertFalse($this->finder->getDrupalRoot());
85 $this->assertFalse($this->finder->getComposerRoot());
86 $this->assertFalse($this->finder->getVendorDir());
89 public function testDrupalComposerStructure()
91 $fileStructure = $this->getDrupalComposerStructure();
92 $this->assertComposerStructure($fileStructure);
95 public function testDrupalComposerStructureWithoutRequire()
98 'web' => static::$fileStructure,
99 'composer.json' => json_encode([
101 'installer-paths' => [
109 unset($fileStructure['web']['composer.json']);
110 $this->assertComposerStructure($fileStructure);
113 public function testNoDrupalRootWithRealFilesystem()
115 $root = $this->tempdir(sys_get_temp_dir());
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());
123 public function testDrupalDefaultStructureWithRealFilesystem()
125 $root = $this->tempdir(sys_get_temp_dir());
126 $this->dumpToFileSystem(static::$fileStructure, $root);
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());
133 // Test symlink implementation
134 $symlink = $this->tempdir(sys_get_temp_dir());
135 $this->symlink($root, $symlink . '/foo');
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());
143 public function testDrupalComposerStructureWithRealFilesystem()
145 $root = $this->tempdir(sys_get_temp_dir());
146 $this->dumpToFileSystem($this->getDrupalComposerStructure(), $root);
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());
153 // Test symlink implementation
154 $symlink = $this->tempdir(sys_get_temp_dir());
155 $this->symlink($root, $symlink . '/foo');
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());
163 public function testDrupalWithLinkedModule()
165 $root = $this->tempdir(sys_get_temp_dir());
166 $this->dumpToFileSystem(static::$fileStructure, $root);
168 $module = $this->tempdir(sys_get_temp_dir());
169 $module_link = $root . '/modules/foo';
170 $this->symlink($module, $module_link);
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()));
178 public function testDrupalWithCustomVendor()
180 $root = $this->tempdir(sys_get_temp_dir());
181 $fileStructure = static::$fileStructure;
182 $fileStructure['composer.json'] = json_encode([
184 'vendor-dir' => 'vendor-foo'
186 ], JSON_UNESCAPED_SLASHES);
187 $fileStructure['vendor-foo'] = [];
188 $this->dumpToFileSystem($fileStructure, $root);
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()));
196 protected function dumpToFileSystem($fileStructure, $root)
198 foreach ($fileStructure as $name => $content) {
199 if (is_array($content)) {
200 mkdir($root . '/' . $name);
201 $this->dumpToFileSystem($content, $root . '/' . $name);
203 file_put_contents($root . '/' . $name, $content);
208 protected function tempdir($dir, $prefix = '', $mode = 0700)
210 if (substr($dir, -1) != '/') {
214 $path = $dir . $prefix . mt_rand(0, 9999999);
215 } while (!mkdir($path, $mode));
216 register_shutdown_function(
217 ['DrupalFinderTest', 'tempdir_remove'],
221 return realpath($path);
224 public static function tempdir_remove($path)
226 if (is_link($path)) {
227 if (defined('PHP_WINDOWS_VERSION_BUILD')) {
236 foreach (scandir($path) as $child) {
237 if (in_array($child, ['.', '..'])) {
240 $child = "$path/$child";
241 is_dir($child) ? static::tempdir_remove($child) : unlink($child);
250 * @throws PHPUnit_Framework_SkippedTestError
252 private function symlink($target, $link)
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)
260 $this->markTestSkipped(<<<'MESSAGE'
261 No privilege to create symlinks. Run test as Administrator (elevated process).
270 * @param $fileStructure
272 protected function assertComposerStructure($fileStructure)
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());
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());
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());
290 $root = vfsStream::setup(
293 ['nested_folder' => $fileStructure]
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());
302 define('WIN_ERROR_PRIVILEGE_NOT_HELD', '1314');