From edf43801a91d5f807537342f5bbe23ab1c72de3a Mon Sep 17 00:00:00 2001 From: Bogdan Lambarski Date: Fri, 18 Sep 2026 10:20:23 +0200 Subject: [PATCH 1/3] fix: reset `is_windows()` mock state in `CIUnitTestCase::tearDown()` (#10554) --- system/Test/CIUnitTestCase.php | 10 ++++++++++ tests/system/CommonFunctionsTest.php | 2 +- tests/system/Test/TestCaseTest.php | 25 +++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/system/Test/CIUnitTestCase.php b/system/Test/CIUnitTestCase.php index d86e543a9a61..05e4c647f0ed 100644 --- a/system/Test/CIUnitTestCase.php +++ b/system/Test/CIUnitTestCase.php @@ -273,6 +273,8 @@ protected function tearDown(): void // Check for other trait methods $this->callTraitMethods('tearDown'); + + $this->resetIsWindowsMock(); } /** @@ -318,6 +320,14 @@ protected function resetServices(bool $initAutoloader = true) Services::reset($initAutoloader); } + /** + * Resets the mocked is_windows() function back to default state. + */ + protected function resetIsWindowsMock(): void + { + is_windows(null); + } + /** * Injects the mock Cache driver to prevent filesystem collisions. * diff --git a/tests/system/CommonFunctionsTest.php b/tests/system/CommonFunctionsTest.php index b4901a5e8d93..f1ce25c04aa1 100644 --- a/tests/system/CommonFunctionsTest.php +++ b/tests/system/CommonFunctionsTest.php @@ -863,7 +863,7 @@ public function testIsWindowsUsingMock(): void $this->assertFalse(is_windows()); $this->assertNotTrue(is_windows()); - is_windows(); + is_windows(null); $this->assertSame(str_contains(php_uname(), 'Windows'), is_windows()); $this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), is_windows()); } diff --git a/tests/system/Test/TestCaseTest.php b/tests/system/Test/TestCaseTest.php index 6905d825533a..611c3fe0ebd4 100644 --- a/tests/system/Test/TestCaseTest.php +++ b/tests/system/Test/TestCaseTest.php @@ -93,4 +93,29 @@ public function testCloseEnoughStringBadLength(): void $result = $this->assertCloseEnoughString('apples & oranges', 'apples'); $this->assertFalse($result, 'Different string lengths should have returned false'); } + + public function testTearDownResetsIsWindowsMock(): void + { + $testCase = new class ('test') extends CIUnitTestCase { + protected $tearDownMethods = ['customTearDown']; + + protected function customTearDown(): void + { + } + + public function triggerTearDown(): void + { + $this->tearDown(); + } + }; + + $default = DIRECTORY_SEPARATOR === '\\'; + + is_windows(! $default); + $this->assertSame(! $default, is_windows()); + + $testCase->triggerTearDown(); + + $this->assertSame($default, is_windows()); + } } From 014c45c3b5960b7bf631cd2a84ced61cadea3d23 Mon Sep 17 00:00:00 2001 From: Bogdan Lambarski Date: Fri, 18 Sep 2026 10:21:33 +0200 Subject: [PATCH 2/3] fix(Cache): set secure directory (0755) and file (0644) permissions in `FileVarExportHandler` (#10555) --- .../FactoriesCache/FileVarExportHandler.php | 4 +- ...FactoriesCacheFileVarExportHandlerTest.php | 37 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/system/Cache/FactoriesCache/FileVarExportHandler.php b/system/Cache/FactoriesCache/FileVarExportHandler.php index 023910c4b17d..241399a12035 100644 --- a/system/Cache/FactoriesCache/FileVarExportHandler.php +++ b/system/Cache/FactoriesCache/FileVarExportHandler.php @@ -23,7 +23,7 @@ public function save(string $key, mixed $val): void // Two processes may try to create the directory at the same time. // is_dir() confirms it exists, so suppressing the warning is safe. - if (! is_dir($this->path) && ! @mkdir($this->path, 0777, true) && ! is_dir($this->path)) { + if (! is_dir($this->path) && ! @mkdir($this->path, 0755, true) && ! is_dir($this->path)) { log_message('error', 'FactoriesCache: cannot create cache directory: ' . $this->path); return; @@ -37,6 +37,8 @@ public function save(string $key, mixed $val): void return; } + @chmod($tmp, 0644); + // Another process may have wiped the directory. Clean up on failure. if (! @rename($tmp, $this->path . "/{$key}")) { log_message('warning', 'FactoriesCache: failed to commit cache file for key: ' . $key); diff --git a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php index c71c756f5cdd..e09816dba2fa 100644 --- a/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php +++ b/tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php @@ -22,9 +22,46 @@ #[Group('Others')] final class FactoriesCacheFileVarExportHandlerTest extends AbstractFactoriesCacheHandlerTestCase { + public static function setUpBeforeClass(): void + { + parent::setUpBeforeClass(); + + helper('filesystem'); + } + protected function createFactoriesCache(): void { $this->handler = new FileVarExportHandler(); $this->cache = new FactoriesCache($this->handler); } + + public function testSaveCreatesDirectoryAndFileWithCorrectPermissions(): void + { + $dir = WRITEPATH . 'cache_test_dir_' . uniqid('', true); + $oldUmask = umask(0000); + + try { + $handler = new FileVarExportHandler(); + $this->setPrivateProperty($handler, 'path', $dir); + + $handler->save('test_key', ['data']); + + $this->assertDirectoryExists($dir); + + if (! is_windows()) { + $dirPerms = fileperms($dir) & 0777; + $this->assertSame(0755, $dirPerms); + + $filePerms = fileperms($dir . '/test_key') & 0777; + $this->assertSame(0644, $filePerms); + } + } finally { + umask($oldUmask); + + if (is_dir($dir)) { + delete_files($dir); + rmdir($dir); + } + } + } } From 404dcf561789c2e5fb569c57687a81a700629d02 Mon Sep 17 00:00:00 2001 From: John Paul E Balandan Date: Fri, 18 Sep 2026 16:22:32 +0800 Subject: [PATCH 3/3] fix: stop `FileLocatorCached` from restoring a deleted cache on shutdown (#10559) --- system/Autoloader/FileLocatorCached.php | 1 + system/Commands/Cache/ClearCache.php | 7 ++ system/Commands/Utilities/Optimize.php | 7 +- .../Autoloader/FileLocatorCachedTest.php | 19 +++- .../system/Commands/Cache/ClearCacheTest.php | 26 ++++++ .../Commands/Utilities/OptimizeTest.php | 88 +++++++++++++++++++ user_guide_src/source/changelogs/v4.7.5.rst | 1 + 7 files changed, 147 insertions(+), 2 deletions(-) create mode 100644 tests/system/Commands/Utilities/OptimizeTest.php diff --git a/system/Autoloader/FileLocatorCached.php b/system/Autoloader/FileLocatorCached.php index 3e31a33d9e84..ee1bb1577433 100644 --- a/system/Autoloader/FileLocatorCached.php +++ b/system/Autoloader/FileLocatorCached.php @@ -84,6 +84,7 @@ private function saveCache(): void */ public function deleteCache(): void { + $this->cache = []; $this->cacheUpdated = false; $this->cacheHandler->delete($this->cacheKey); } diff --git a/system/Commands/Cache/ClearCache.php b/system/Commands/Cache/ClearCache.php index 32f9466a4939..a5c60d315ac5 100644 --- a/system/Commands/Cache/ClearCache.php +++ b/system/Commands/Cache/ClearCache.php @@ -13,6 +13,7 @@ namespace CodeIgniter\Commands\Cache; +use CodeIgniter\Autoloader\FileLocatorCached; use CodeIgniter\CLI\BaseCommand; use CodeIgniter\CLI\CLI; use Config\Cache; @@ -81,6 +82,12 @@ public function run(array $params) return EXIT_ERROR; } + $locator = service('locator'); + + if ($handler === 'file' && $locator instanceof FileLocatorCached) { + $locator->deleteCache(); + } + CLI::write(CLI::color('Cache cleared.', 'green')); return EXIT_SUCCESS; diff --git a/system/Commands/Utilities/Optimize.php b/system/Commands/Utilities/Optimize.php index 1a103a1b9c52..9d3964ddda1d 100644 --- a/system/Commands/Utilities/Optimize.php +++ b/system/Commands/Utilities/Optimize.php @@ -74,7 +74,12 @@ public function run(array $params) private function clearCache(): void { - $locator = new FileLocatorCached(new FileLocator(service('autoloader'))); + $locator = service('locator'); + + if (! $locator instanceof FileLocatorCached) { + $locator = new FileLocatorCached(new FileLocator(service('autoloader'))); + } + $locator->deleteCache(); CLI::write('Removed FileLocatorCache.', 'green'); diff --git a/tests/system/Autoloader/FileLocatorCachedTest.php b/tests/system/Autoloader/FileLocatorCachedTest.php index f8b5b386818c..9700f88bdb33 100644 --- a/tests/system/Autoloader/FileLocatorCachedTest.php +++ b/tests/system/Autoloader/FileLocatorCachedTest.php @@ -75,10 +75,27 @@ protected function tearDown(): void public function testDeleteCache(): void { - $this->assertNotSame([], $this->handler->get('FileLocatorCache')); + $this->locator->search('Config/App'); + $this->locator->__destruct(); + $this->assertIsArray($this->handler->get('FileLocatorCache')); $this->locator->deleteCache(); $this->assertFalse($this->handler->get('FileLocatorCache')); } + + public function testDeleteCacheDiscardsDataHeldInMemory(): void + { + $this->locator->getClassname(SYSTEMPATH . 'CodeIgniter.php'); + + $this->locator->deleteCache(); + + $this->locator->search('Config/App'); + $this->locator->__destruct(); + + $cached = $this->handler->get('FileLocatorCache'); + + $this->assertArrayHasKey('search', $cached); + $this->assertArrayNotHasKey('getClassname', $cached); + } } diff --git a/tests/system/Commands/Cache/ClearCacheTest.php b/tests/system/Commands/Cache/ClearCacheTest.php index e64a24916e15..6674fff5a2c8 100644 --- a/tests/system/Commands/Cache/ClearCacheTest.php +++ b/tests/system/Commands/Cache/ClearCacheTest.php @@ -13,7 +13,10 @@ namespace CodeIgniter\Commands\Cache; +use CodeIgniter\Autoloader\FileLocator; +use CodeIgniter\Autoloader\FileLocatorCached; use CodeIgniter\Cache\CacheFactory; +use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; use CodeIgniter\Cache\Handlers\FileHandler; use CodeIgniter\CLI\CLI; use CodeIgniter\Config\Factories; @@ -93,6 +96,29 @@ public function testClearCacheWorks(): void $this->assertStringContainsString('Cache cleared.', $this->getStreamFilterBuffer()); } + public function testClearCacheDiscardsSharedLocatorCache(): void + { + $handler = new FileVarExportHandler(); + $handler->delete('FileLocatorCache'); + + $locator = new FileLocatorCached(new FileLocator(service('autoloader')), $handler); + $locator->search('Config/App'); + Services::injectMock('locator', $locator); + + command('cache:clear'); + + $locator->search('Config/Cache'); + $locator->__destruct(); + Services::resetSingle('locator'); + + $cached = $handler->get('FileLocatorCache'); + + $this->assertArrayNotHasKey('Config/App', $cached['search']); + $this->assertArrayHasKey('Config/Cache', $cached['search']); + + $handler->delete('FileLocatorCache'); + } + public function testClearCacheFails(): void { $cache = $this->getMockBuilder(FileHandler::class) diff --git a/tests/system/Commands/Utilities/OptimizeTest.php b/tests/system/Commands/Utilities/OptimizeTest.php new file mode 100644 index 000000000000..ccaf9403c613 --- /dev/null +++ b/tests/system/Commands/Utilities/OptimizeTest.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view + * the LICENSE file that was distributed with this source code. + */ + +namespace CodeIgniter\Commands\Utilities; + +use Closure; +use CodeIgniter\Autoloader\FileLocator; +use CodeIgniter\Autoloader\FileLocatorCached; +use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler; +use CodeIgniter\Test\CIUnitTestCase; +use CodeIgniter\Test\ReflectionHelper; +use CodeIgniter\Test\StreamFilterTrait; +use Config\Services; +use PHPUnit\Framework\Attributes\Group; + +/** + * @internal + */ +#[Group('Others')] +final class OptimizeTest extends CIUnitTestCase +{ + use ReflectionHelper; + use StreamFilterTrait; + + private FileVarExportHandler $handler; + + protected function setUp(): void + { + parent::setUp(); + + $this->handler = new FileVarExportHandler(); + $this->handler->delete('FileLocatorCache'); + } + + protected function tearDown(): void + { + parent::tearDown(); + + $this->handler->delete('FileLocatorCache'); + Services::resetSingle('locator'); + } + + /** + * @return Closure(): void + */ + private function getClearCache(): Closure + { + return self::getPrivateMethodInvoker(new Optimize(service('logger'), service('commands')), 'clearCache'); + } + + public function testClearCacheDiscardsSharedLocatorCache(): void + { + $locator = new FileLocatorCached(new FileLocator(service('autoloader')), $this->handler); + $locator->search('Config/App'); + Services::injectMock('locator', $locator); + + ($this->getClearCache())(); + + $locator->search('Config/Cache'); + $locator->__destruct(); + + $cached = $this->handler->get('FileLocatorCache'); + + $this->assertArrayNotHasKey('Config/App', $cached['search']); + $this->assertArrayHasKey('Config/Cache', $cached['search']); + $this->assertStringContainsString('Removed FileLocatorCache.', $this->getStreamFilterBuffer()); + } + + public function testClearCacheDeletesTheFileWithoutSharedLocatorCache(): void + { + $this->handler->save('FileLocatorCache', ['search' => []]); + + ($this->getClearCache())(); + + $this->assertFalse($this->handler->get('FileLocatorCache')); + $this->assertStringContainsString('Removed FileLocatorCache.', $this->getStreamFilterBuffer()); + } +} diff --git a/user_guide_src/source/changelogs/v4.7.5.rst b/user_guide_src/source/changelogs/v4.7.5.rst index 88a73f1a5e74..1f10e03cdf21 100644 --- a/user_guide_src/source/changelogs/v4.7.5.rst +++ b/user_guide_src/source/changelogs/v4.7.5.rst @@ -39,6 +39,7 @@ Deprecations Bugs Fixed ********** +- **Autoloader:** Fixed a bug where ``FileLocatorCached::deleteCache()`` left the deleted data in memory, so it could be written back to the cache file on shutdown. ``spark optimize`` and ``spark cache:clear`` now clear the shared locator's cache instead of a separate instance. - **CLI:** Fixed a bug where pressing backspace in a ``CLI::prompt()`` erased the prompt text when the ``readline`` extension is enabled. The prompt is now passed to ``readline()`` so line redraws repaint it. ANSI color codes in the prompt (e.g., option defaults) are wrapped in readline's non-printing markers under GNU readline so cursor positioning stays accurate. On Windows, where the ``readline`` extension is built on WinEditLine, the prompt is written to STDOUT first because WinEditLine reports no library version and prints ANSI sequences literally.