From e833cb896393dc953b9bfc69a2e04ccef4c3db7d Mon Sep 17 00:00:00 2001 From: Mathias Elle Date: Thu, 12 Nov 2026 23:09:46 +0100 Subject: [PATCH 1/4] fix(symlink-cleaner): detect symlinks via resolved path instead of stat mode SymlinkCleaner checked the S_IFLNK bit of FileDriver::stat(), but stat() follows links and reports the target's mode, so symlinks in web/css were never detected or removed. Compare the entry's real path with its expected location in the resolved css directory instead; unresolvable (broken) links are removed as well. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Service/SymlinkCleaner.php | 25 ++++++-- tests/Unit/Service/SymlinkCleanerTest.php | 78 +++++++++++++++++++---- 2 files changed, 87 insertions(+), 16 deletions(-) diff --git a/src/Service/SymlinkCleaner.php b/src/Service/SymlinkCleaner.php index 18dfbfb0..921d279d 100644 --- a/src/Service/SymlinkCleaner.php +++ b/src/Service/SymlinkCleaner.php @@ -50,12 +50,18 @@ public function cleanSymlinks(string $themePath, SymfonyStyle $io, bool $isVerbo return true; } + $realCssPath = $this->fileDriver->getRealPath($cssPath); + if (!is_string($realCssPath)) { + return true; + } + $realCssPath = rtrim($realCssPath, '/'); + $items = $this->fileDriver->readDirectory($cssPath); $deletedCount = 0; foreach ($items as $item) { // Check if item is a symlink - if ($this->isSymlink($item)) { + if ($this->isSymlink($item, $realCssPath)) { $this->fileDriver->deleteFile($item); $deletedCount++; @@ -81,20 +87,29 @@ public function cleanSymlinks(string $themePath, SymfonyStyle $io, bool $isVerbo } /** - * Check if a path is a symlink using stat info. + * Check if a directory entry is a symlink by comparing its resolved path with its expected location. + * + * The file driver offers no lstat(), and stat() follows links, so the file mode cannot be used. + * An entry whose real path differs from "/" (or cannot be resolved, + * i.e. a broken link) is a symlink. * * @param string $path + * @param string $realDirectory Resolved path of the directory containing the entry * @return bool */ - private function isSymlink(string $path): bool + private function isSymlink(string $path, string $realDirectory): bool { try { - $stat = $this->fileDriver->stat($path); + $realPath = $this->fileDriver->getRealPath($path); } catch (\Exception $e) { return false; } - return ((int) ($stat['mode'] ?? 0) & 0o12_0000) === 0o12_0000; + if (!is_string($realPath)) { + return true; + } + + return $realPath !== $realDirectory . '/' . $this->getBasename($path); } /** diff --git a/tests/Unit/Service/SymlinkCleanerTest.php b/tests/Unit/Service/SymlinkCleanerTest.php index a33804b7..771d8cb2 100644 --- a/tests/Unit/Service/SymlinkCleanerTest.php +++ b/tests/Unit/Service/SymlinkCleanerTest.php @@ -13,9 +13,6 @@ class SymlinkCleanerTest extends TestCase { - private const SYMLINK_MODE = 0o12_0777; - private const REGULAR_FILE_MODE = 0o10_0644; - /** * @var File&Stub */ @@ -69,10 +66,11 @@ public function testDeletesOnlySymlinks(): void ->method('readDirectory') ->willReturn(['/theme/web/css/styles.css', '/theme/web/css/link.css']); $fileDriver - ->method('stat') + ->method('getRealPath') ->willReturnMap([ - ['/theme/web/css/styles.css', ['mode' => self::REGULAR_FILE_MODE]], - ['/theme/web/css/link.css', ['mode' => self::SYMLINK_MODE]], + ['/theme/web/css', '/theme/web/css'], + ['/theme/web/css/styles.css', '/theme/web/css/styles.css'], + ['/theme/web/css/link.css', '/shared/link.css'], ]); $fileDriver->expects($this->once())->method('deleteFile')->with('/theme/web/css/link.css'); $io = $this->createMock(SymfonyStyle::class); @@ -87,31 +85,85 @@ public function testVerboseModeWithoutSymlinksPrintsNoSuccess(): void $this->state->method('getMode')->willReturn(State::MODE_DEVELOPER); $this->fileDriver->method('isDirectory')->willReturn(true); $this->fileDriver->method('readDirectory')->willReturn(['/theme/web/css/styles.css']); - $this->fileDriver->method('stat')->willReturn(['mode' => self::REGULAR_FILE_MODE]); + $this->fileDriver->method('getRealPath')->willReturnArgument(0); $io = $this->createMock(SymfonyStyle::class); $io->expects($this->never())->method('success'); $this->assertTrue($this->cleaner->cleanSymlinks('/theme', $io, true)); } - public function testTreatsUnstatableItemsAsRegularFiles(): void + public function testDeletesBrokenSymlinks(): void { $this->state->method('getMode')->willReturn(State::MODE_DEVELOPER); $fileDriver = $this->createMock(File::class); $fileDriver->method('isDirectory')->willReturn(true); $fileDriver->method('readDirectory')->willReturn(['/theme/web/css/broken.css']); - $fileDriver->method('stat')->willThrowException(new \RuntimeException('stat failed')); + $fileDriver + ->method('getRealPath') + ->willReturnMap([ + ['/theme/web/css', '/theme/web/css'], + ['/theme/web/css/broken.css', false], + ]); + $fileDriver->expects($this->once())->method('deleteFile')->with('/theme/web/css/broken.css'); + + $this->assertTrue($this->createCleaner(fileDriver: $fileDriver)->cleanSymlinks('/theme', $this->io, false)); + } + + public function testKeepsRegularFilesInsideSymlinkedThemeDirectory(): void + { + $this->state->method('getMode')->willReturn(State::MODE_DEVELOPER); + $fileDriver = $this->createMock(File::class); + $fileDriver->method('isDirectory')->willReturn(true); + $fileDriver->method('readDirectory')->willReturn(['/theme/web/css/styles.css']); + $fileDriver + ->method('getRealPath') + ->willReturnMap([ + ['/theme/web/css', '/real/theme/web/css'], + ['/theme/web/css/styles.css', '/real/theme/web/css/styles.css'], + ]); $fileDriver->expects($this->never())->method('deleteFile'); $this->assertTrue($this->createCleaner(fileDriver: $fileDriver)->cleanSymlinks('/theme', $this->io, false)); } + public function testRemovesRealSymlinksFromFilesystem(): void + { + $state = $this->createStub(State::class); + $state->method('getMode')->willReturn(State::MODE_DEVELOPER); + $themePath = sys_get_temp_dir() . '/mageforge-symlink-' . uniqid(); + $cssPath = $themePath . '/web/css'; + mkdir($cssPath, 0o777, true); + file_put_contents($cssPath . '/styles.css', 'a{}'); + file_put_contents($themePath . '/target.css', 'b{}'); + symlink($themePath . '/target.css', $cssPath . '/link.css'); + symlink($themePath . '/missing.css', $cssPath . '/broken.css'); + + try { + $cleaner = $this->createCleaner(fileDriver: new File(), state: $state); + $this->assertTrue($cleaner->cleanSymlinks($themePath, $this->io, false)); + + $this->assertFileExists($cssPath . '/styles.css'); + $this->assertFileExists($themePath . '/target.css'); + $this->assertFalse(is_link($cssPath . '/link.css')); + $this->assertFalse(is_link($cssPath . '/broken.css')); + } finally { + array_map('unlink', glob($cssPath . '/*') ?: []); + unlink($themePath . '/target.css'); + rmdir($cssPath); + rmdir($themePath . '/web'); + rmdir($themePath); + } + } + public function testReportsRemovedSymlinksInVerboseMode(): void { $this->state->method('getMode')->willReturn(State::MODE_DEVELOPER); $this->fileDriver->method('isDirectory')->willReturn(true); $this->fileDriver->method('readDirectory')->willReturn(['/theme/web/css/link.css']); - $this->fileDriver->method('stat')->willReturn(['mode' => self::SYMLINK_MODE]); + $this->fileDriver->method('getRealPath')->willReturnMap([ + ['/theme/web/css', '/theme/web/css'], + ['/theme/web/css/link.css', '/shared/link.css'], + ]); $io = $this->createMock(SymfonyStyle::class); $io->expects($this->once())->method('writeln')->with(' ⚠ Removed symlink: link.css'); $io->expects($this->once())->method('success')->with('Removed 1 symlink(s) from web/css/'); @@ -123,6 +175,7 @@ public function testSucceedsWithWarningWhenDirectoryCannotBeRead(): void { $this->state->method('getMode')->willReturn(State::MODE_DEVELOPER); $this->fileDriver->method('isDirectory')->willReturn(true); + $this->fileDriver->method('getRealPath')->willReturnArgument(0); $this->fileDriver->method('readDirectory')->willThrowException(new \RuntimeException('permission denied')); $io = $this->createMock(SymfonyStyle::class); $io->expects($this->once())->method('warning')->with('Could not clean symlinks: permission denied'); @@ -135,7 +188,10 @@ public function testReportsBasenameForItemWithoutDirectorySeparator(): void $this->state->method('getMode')->willReturn(State::MODE_DEVELOPER); $this->fileDriver->method('isDirectory')->willReturn(true); $this->fileDriver->method('readDirectory')->willReturn(['link.css']); - $this->fileDriver->method('stat')->willReturn(['mode' => self::SYMLINK_MODE]); + $this->fileDriver->method('getRealPath')->willReturnMap([ + ['/theme/web/css', '/theme/web/css'], + ['link.css', '/shared/link.css'], + ]); $io = $this->createMock(SymfonyStyle::class); $io->expects($this->once())->method('writeln')->with(' ⚠ Removed symlink: link.css'); From 67b002d24a61bda19e6ad5950182086e363c7570 Mon Sep 17 00:00:00 2001 From: Mathias Elle Date: Thu, 12 Nov 2026 23:14:36 +0100 Subject: [PATCH 2/4] style(tests): wrap overlong line in TemplateFallbackResolverTest Fixes the Generic.Files.LineLength.TooLong warning that fails the PHPCS check. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Service/TemplateOverride/TemplateFallbackResolverTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Service/TemplateOverride/TemplateFallbackResolverTest.php b/tests/Unit/Service/TemplateOverride/TemplateFallbackResolverTest.php index 0844a888..eb0930be 100644 --- a/tests/Unit/Service/TemplateOverride/TemplateFallbackResolverTest.php +++ b/tests/Unit/Service/TemplateOverride/TemplateFallbackResolverTest.php @@ -340,7 +340,8 @@ public function testFindDirectoryFilesUsesDirectoryRootWithoutSubPathAndSkipsMis ->method('isFile') ->willReturnCallback(static fn(string $path): bool => str_ends_with($path, '.phtml')); - $files = $this->createResolver(fileDriver: $fileDriver)->findDirectoryFiles(['/missing', '/module/templates'], ''); + $resolver = $this->createResolver(fileDriver: $fileDriver); + $files = $resolver->findDirectoryFiles(['/missing', '/module/templates'], ''); $this->assertSame( ['b.phtml' => '/module/templates/b.phtml', 'z.phtml' => '/module/templates/z.phtml'], From 1b657c63f7e53869d66d7a8360bd88cbf72a1a69 Mon Sep 17 00:00:00 2001 From: Mathias Elle Date: Thu, 12 Nov 2026 23:20:13 +0100 Subject: [PATCH 3/4] ci(phpstan): ignore getStubBuilder() missing in Magento's PHPUnit 10.5 The Static Analysis workflow analyses the tests inside the Magento install, which pins PHPUnit 10.5 where getStubBuilder() does not exist. The tests run with the PHPUnit 12 required by composer.json, so ignore only this message. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- phpstan.neon | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 7fb98e03..a9253d8d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,3 +6,10 @@ parameters: paths: - src - tests + ignoreErrors: + # CI analyses the tests inside the Magento install, which pins PHPUnit 10.5 (getStubBuilder() requires + # PHPUnit 12). The tests themselves run with the PHPUnit version required by composer.json. + - + identifier: method.notFound + message: '#^Call to an undefined method .+::getStubBuilder\(\)\.$#' + reportUnmatched: false From b3c8129fb6853993dbb65f28e08e1cd3d36913c4 Mon Sep 17 00:00:00 2001 From: Mathias Elle Date: Thu, 12 Nov 2026 23:36:27 +0100 Subject: [PATCH 4/4] fix(symlink-cleaner): treat backslash as separator only on Windows Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/Service/SymlinkCleaner.php | 21 ++++++++++++++++++--- tests/Unit/Service/SymlinkCleanerTest.php | 16 ++++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/Service/SymlinkCleaner.php b/src/Service/SymlinkCleaner.php index 921d279d..617e5c8f 100644 --- a/src/Service/SymlinkCleaner.php +++ b/src/Service/SymlinkCleaner.php @@ -109,23 +109,38 @@ private function isSymlink(string $path, string $realDirectory): bool return true; } - return $realPath !== $realDirectory . '/' . $this->getBasename($path); + return ( + $this->normalizeSeparators($realPath) + !== $this->normalizeSeparators($realDirectory) . '/' . $this->getBasename($path) + ); } /** * Get basename without using basename(). * + * A backslash only separates path segments on Windows; on other platforms it is a valid filename character. + * * @param string $path * @return string */ private function getBasename(string $path): string { - $normalized = str_replace('\\', '/', $path); - $trimmed = rtrim($normalized, '/'); + $trimmed = rtrim($this->normalizeSeparators($path), '/'); $pos = strrpos($trimmed, '/'); if ($pos === false) { return $trimmed; } return substr($trimmed, $pos + 1); } + + /** + * Convert platform-specific directory separators to forward slashes + * + * @param string $path + * @return string + */ + private function normalizeSeparators(string $path): string + { + return DIRECTORY_SEPARATOR === '\\' ? str_replace('\\', '/', $path) : $path; + } } diff --git a/tests/Unit/Service/SymlinkCleanerTest.php b/tests/Unit/Service/SymlinkCleanerTest.php index 771d8cb2..d8dbc322 100644 --- a/tests/Unit/Service/SymlinkCleanerTest.php +++ b/tests/Unit/Service/SymlinkCleanerTest.php @@ -126,6 +126,22 @@ public function testKeepsRegularFilesInsideSymlinkedThemeDirectory(): void $this->assertTrue($this->createCleaner(fileDriver: $fileDriver)->cleanSymlinks('/theme', $this->io, false)); } + public function testKeepsRegularFileWithBackslashInName(): void + { + if (DIRECTORY_SEPARATOR === '\\') { + $this->markTestSkipped('A backslash is a path separator on Windows.'); + } + + $this->state->method('getMode')->willReturn(State::MODE_DEVELOPER); + $fileDriver = $this->createMock(File::class); + $fileDriver->method('isDirectory')->willReturn(true); + $fileDriver->method('readDirectory')->willReturn(['/theme/web/css/vendor\\styles.css']); + $fileDriver->method('getRealPath')->willReturnArgument(0); + $fileDriver->expects($this->never())->method('deleteFile'); + + $this->assertTrue($this->createCleaner(fileDriver: $fileDriver)->cleanSymlinks('/theme', $this->io, false)); + } + public function testRemovesRealSymlinksFromFilesystem(): void { $state = $this->createStub(State::class);