From e4ff26385f3cfa2889a8b774f2c272ce03c11ae1 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Sat, 21 Nov 2026 16:53:54 -0500 Subject: [PATCH 1/3] fix(installer): skip a configured test data folder the module does not ship --- CHANGELOG.md | 4 ++ src/Module/Installer.php | 6 ++ tests/Module/InstallerTestFoldersTest.php | 71 +++++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 tests/Module/InstallerTestFoldersTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index aefda69..caa7b4e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,10 @@ The format is based on Keep a Changelog and this project follows Semantic Versio method no longer carries on after the failed query. - `Common\Db::fieldExists()`, `cloneRecord()` and `enumerate()` refuse a table or column name that ends in a line feed. `fieldExists()` still takes a table name in backticks. +- `Module\Installer::copyTestFolders()` skips a configured sample folder that the module does + not ship. Installing such a module used to raise + `Warning: Test data folder "…/testdata/uploads" was not copied to "…"`. A folder that exists + and cannot be copied still raises the warning. ## [1.7.2] — 2026-11-18 diff --git a/src/Module/Installer.php b/src/Module/Installer.php index 467d9f1..81cb56c 100644 --- a/src/Module/Installer.php +++ b/src/Module/Installer.php @@ -81,11 +81,17 @@ public static function copyBlankFiles(Configurator $configurator): void /** * Recursively copy each configured `[source, destination]` test-data folder pair. + * A pair whose source folder the module does not ship is skipped. */ public static function copyTestFolders(Configurator $configurator): void { foreach (self::plan($configurator)->testFolderCopies as $pair) { [$src, $dest] = $pair; + // Sample data is optional: most modules keep the skeleton's `testdata/uploads` + // entry without shipping that folder. Nothing to copy is not a failed copy. + if (!\is_dir((string)$src)) { + continue; + } if (!SysUtility::rcopy($src, $dest)) { \trigger_error(\sprintf('Test data folder "%s" was not copied to "%s"', $src, $dest), \E_USER_WARNING); } diff --git a/tests/Module/InstallerTestFoldersTest.php b/tests/Module/InstallerTestFoldersTest.php new file mode 100644 index 0000000..64119c6 --- /dev/null +++ b/tests/Module/InstallerTestFoldersTest.php @@ -0,0 +1,71 @@ + */ + private array $warnings = []; + + protected function setUp(): void + { + $this->dir = sys_get_temp_dir() . '/moduletools-installer-' . bin2hex(random_bytes(6)); + self::assertTrue(Filesystem::mkdir($this->dir . '/module/config')); + self::assertTrue(Filesystem::mkdir($this->dir . '/module/testdata/images')); + file_put_contents($this->dir . '/module/testdata/images/a.txt', 'a'); + + // The skeleton's config: one sample folder the module ships, one it does not. + $config = ['copyTestFolders' => [ + [$this->dir . '/module/testdata/uploads', $this->dir . '/uploads'], + [$this->dir . '/module/testdata/images', $this->dir . '/uploads/images'], + ]]; + file_put_contents($this->dir . '/module/config/config.php', 'warnings = []; + set_error_handler(function (int $level, string $message): bool { + $this->warnings[] = $message; + + return true; + }, E_USER_WARNING); + } + + protected function tearDown(): void + { + restore_error_handler(); + unset($GLOBALS['xoopsUser']); + if (is_dir($this->dir)) { + Filesystem::deleteDirectory($this->dir); + } + } + + public function testADeclaredSampleFolderTheModuleDoesNotShipIsSkippedQuietly(): void + { + $GLOBALS['xoopsUser'] = new \XoopsUser(false, ['2', '1']); + + Installer::copyTestFolders(new Configurator($this->dir . '/module')); + + self::assertSame([], $this->warnings); + self::assertFileExists($this->dir . '/uploads/images/a.txt'); + } + + public function testAFolderThatExistsButIsNotCopiedStillWarns(): void + { + // A module-only admin is not a webmaster, so the copy of the existing folder is refused. + $GLOBALS['xoopsUser'] = new \XoopsUser(true, ['2', '4']); + + Installer::copyTestFolders(new Configurator($this->dir . '/module')); + + self::assertCount(1, $this->warnings); + self::assertStringContainsString('/module/testdata/images" was not copied', $this->warnings[0]); + self::assertFileDoesNotExist($this->dir . '/uploads/images/a.txt'); + } +} From 462b53bf4cbcb8f0c7d4a57f3c0279f947b60995 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Sat, 21 Nov 2026 17:04:58 -0500 Subject: [PATCH 2/3] fix(installer): share the sample folder copy with the sample data import, skip only a confirmed absent source --- CHANGELOG.md | 10 +-- src/Common/TestdataSample.php | 12 +--- src/Internal/SampleFolders.php | 73 +++++++++++++++++++ src/Module/Installer.php | 13 +--- tests/Internal/SampleFoldersTest.php | 102 +++++++++++++++++++++++++++ 5 files changed, 187 insertions(+), 23 deletions(-) create mode 100644 src/Internal/SampleFolders.php create mode 100644 tests/Internal/SampleFoldersTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index caa7b4e..51458eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,10 +18,12 @@ The format is based on Keep a Changelog and this project follows Semantic Versio method no longer carries on after the failed query. - `Common\Db::fieldExists()`, `cloneRecord()` and `enumerate()` refuse a table or column name that ends in a line feed. `fieldExists()` still takes a table name in backticks. -- `Module\Installer::copyTestFolders()` skips a configured sample folder that the module does - not ship. Installing such a module used to raise - `Warning: Test data folder "…/testdata/uploads" was not copied to "…"`. A folder that exists - and cannot be copied still raises the warning. +- `Module\Installer::copyTestFolders()` and `Common\TestdataSample::loadData()` skip a configured + sample folder that the module does not ship. Installing such a module used to raise + `Warning: Test data folder "…/testdata/uploads" was not copied to "…"`, and "Import Sample + Data" reported the folder as not copied. A source is skipped only when a readable parent folder + confirms it is absent; a folder that exists and cannot be copied, a file in its place, and a + path the copier refuses are reported as before. ## [1.7.2] — 2026-11-18 diff --git a/src/Common/TestdataSample.php b/src/Common/TestdataSample.php index b8182e9..9993d97 100644 --- a/src/Common/TestdataSample.php +++ b/src/Common/TestdataSample.php @@ -19,6 +19,7 @@ use Xmf\Database\TableLoad; use Xoops\Helpers\Utility\HtmlBuilder; use Xoops\ModuleTools\Internal\PackageLanguage; +use Xoops\ModuleTools\Internal\SampleFolders; use Xoops\ModuleTools\Utility; /** @@ -64,7 +65,6 @@ private function dataFile(string $table): string public function loadData(): void { $this->assertAuthorized(); - $utility = new Utility(); $configurator = new Configurator($this->modHelper->path()); $tables = $this->modHelper->getModule()->getInfo('tables'); @@ -83,14 +83,8 @@ public function loadData(): void // --- COPY test folder files --------------- if (\is_array($configurator->copyTestFolders) && \count($configurator->copyTestFolders) > 0) { - $failed = []; - foreach (\array_keys($configurator->copyTestFolders) as $i) { - $src = $configurator->copyTestFolders[$i][0]; - $dest = $configurator->copyTestFolders[$i][1]; - if (!$utility::rcopy($src, $dest)) { - $failed[] = (string) $dest; - } - } + // A sample folder the module does not ship is skipped, as at install time. + $failed = \array_column(SampleFolders::copy($configurator->copyTestFolders), 1); // rcopy() is webmaster-only: a module-only admin gets the rows but not the files, // and must be told so instead of seeing a success message. if ([] !== $failed) { diff --git a/src/Internal/SampleFolders.php b/src/Internal/SampleFolders.php new file mode 100644 index 0000000..2349edd --- /dev/null +++ b/src/Internal/SampleFolders.php @@ -0,0 +1,73 @@ + $pairs + * + * @return list the pairs that were not copied + */ + public static function copy(array $pairs): array + { + $failed = []; + foreach ($pairs as $pair) { + [$source, $destination] = $pair; + if (self::isAbsent((string)$source)) { + continue; + } + if (!SysUtility::rcopy($source, $destination)) { + $failed[] = [(string)$source, (string)$destination]; + } + } + + return $failed; + } + + /** + * Whether $source is a plain local path that a readable ancestor folder confirms is not there. + * + * A false is_dir() alone does not say so: it is also false for a folder that exists below a + * parent PHP may not search. Such a source goes to the copier, whose refusal is reported. + */ + private static function isAbsent(string $source): bool + { + // A NUL byte or a URI is rcopy()'s to refuse: is_dir() throws on the first as of + // PHP 8.6 and would ask a stream wrapper about the second. + if ('' === $source || \str_contains($source, "\0") || \str_contains($source, '://')) { + return false; + } + if (\is_dir($source)) { + return false; + } + + // Walk up to the nearest ancestor that is a folder and ask it for the missing entry. + $path = \rtrim(\str_replace('\\', '/', $source), '/'); + for ($parent = \dirname($path); $parent !== $path; $parent = \dirname($path)) { + if (\is_dir($parent)) { + $entries = \is_readable($parent) ? \scandir($parent) : false; + + return \is_array($entries) && !\in_array(\basename($path), $entries, true); + } + $path = $parent; + } + + return false; + } +} diff --git a/src/Module/Installer.php b/src/Module/Installer.php index 81cb56c..33a8e70 100644 --- a/src/Module/Installer.php +++ b/src/Module/Installer.php @@ -7,6 +7,7 @@ use Xoops\ModuleTools\Common\Configurator; use Xoops\ModuleTools\Common\SysUtility; use Xoops\ModuleTools\Internal\Installation\InstallationPlan; +use Xoops\ModuleTools\Internal\SampleFolders; /** * Shared module install/update filesystem helper. @@ -85,16 +86,8 @@ public static function copyBlankFiles(Configurator $configurator): void */ public static function copyTestFolders(Configurator $configurator): void { - foreach (self::plan($configurator)->testFolderCopies as $pair) { - [$src, $dest] = $pair; - // Sample data is optional: most modules keep the skeleton's `testdata/uploads` - // entry without shipping that folder. Nothing to copy is not a failed copy. - if (!\is_dir((string)$src)) { - continue; - } - if (!SysUtility::rcopy($src, $dest)) { - \trigger_error(\sprintf('Test data folder "%s" was not copied to "%s"', $src, $dest), \E_USER_WARNING); - } + foreach (SampleFolders::copy(self::plan($configurator)->testFolderCopies) as [$src, $dest]) { + \trigger_error(\sprintf('Test data folder "%s" was not copied to "%s"', $src, $dest), \E_USER_WARNING); } } diff --git a/tests/Internal/SampleFoldersTest.php b/tests/Internal/SampleFoldersTest.php new file mode 100644 index 0000000..c08db34 --- /dev/null +++ b/tests/Internal/SampleFoldersTest.php @@ -0,0 +1,102 @@ +dir = sys_get_temp_dir() . '/moduletools-sample-' . bin2hex(random_bytes(6)); + self::assertTrue(Filesystem::mkdir($this->dir . '/module/testdata/images')); + file_put_contents($this->dir . '/module/testdata/images/a.txt', 'a'); + $GLOBALS['xoopsUser'] = new \XoopsUser(false, ['2', '1']); // a webmaster + } + + protected function tearDown(): void + { + unset($GLOBALS['xoopsUser']); + if (is_dir($this->dir . '/module/locked')) { + chmod($this->dir . '/module/locked', 0700); + } + if (is_dir($this->dir)) { + Filesystem::deleteDirectory($this->dir); + } + } + + public function testAFolderTheModuleShipsIsCopied(): void + { + $failed = SampleFolders::copy([[$this->dir . '/module/testdata/images', $this->dir . '/uploads/images']]); + + self::assertSame([], $failed); + self::assertFileExists($this->dir . '/uploads/images/a.txt'); + } + + public function testAFolderTheModuleDoesNotShipIsSkipped(): void + { + $failed = SampleFolders::copy([ + // the skeleton's entry, beside a testdata folder that holds other things + [$this->dir . '/module/testdata/uploads', $this->dir . '/uploads'], + // a module without the parent folder either + [$this->dir . '/module/samples/deep/uploads', $this->dir . '/uploads'], + [$this->dir . '/module/testdata/uploads/', $this->dir . '/uploads'], + ]); + + self::assertSame([], $failed); + self::assertDirectoryDoesNotExist($this->dir . '/uploads'); + } + + public function testARefusedCopyOfAnExistingFolderIsReported(): void + { + // A module-only admin is not a webmaster, so the copier refuses. + $GLOBALS['xoopsUser'] = new \XoopsUser(true, ['2', '4']); + $pair = [$this->dir . '/module/testdata/images', $this->dir . '/uploads/images']; + + self::assertSame([$pair], SampleFolders::copy([$pair])); + } + + public function testAFileWhereAFolderIsExpectedIsReported(): void + { + $pair = [$this->dir . '/module/testdata/images/a.txt', $this->dir . '/uploads/images']; + + self::assertSame([$pair], SampleFolders::copy([$pair])); + } + + public function testAPathTheCopierRefusesIsReportedAndNeverProbed(): void + { + // is_dir() throws on a NUL byte as of PHP 8.6 and would ask a stream wrapper about a URI. + $pairs = [ + [$this->dir . "/module/testdata/up\0loads", $this->dir . '/uploads'], + ['ftp://example.invalid/testdata/uploads', $this->dir . '/uploads'], + ['', $this->dir . '/uploads'], + ]; + + self::assertSame($pairs, SampleFolders::copy($pairs)); + } + + public function testAFolderBelowAParentThatCannotBeSearchedIsReported(): void + { + if ('\\' === \DIRECTORY_SEPARATOR || (\function_exists('posix_geteuid') && 0 === posix_geteuid())) { + self::markTestSkipped('Needs POSIX permissions and a user they apply to.'); + } + self::assertTrue(Filesystem::mkdir($this->dir . '/module/locked/uploads')); + $pair = [$this->dir . '/module/locked/uploads', $this->dir . '/uploads']; + + // Readable but not searchable: the entry is listed, is_dir() on it is false. + chmod($this->dir . '/module/locked', 0400); + clearstatcache(); + self::assertSame([$pair], SampleFolders::copy([$pair])); + + // Neither readable nor searchable: nothing confirms the folder is absent. + chmod($this->dir . '/module/locked', 0000); + clearstatcache(); + self::assertSame([$pair], SampleFolders::copy([$pair])); + } +} From 7fb8002ab871865990cf75e830a5948dbad78d13 Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Sat, 21 Nov 2026 17:14:31 -0500 Subject: [PATCH 3/3] fix(installer): keep the configured path as written in the absence check, satisfy two Sonar rules --- src/Internal/SampleFolders.php | 23 ++++++++++------------- tests/Internal/SampleFoldersTest.php | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/src/Internal/SampleFolders.php b/src/Internal/SampleFolders.php index 2349edd..c381f97 100644 --- a/src/Internal/SampleFolders.php +++ b/src/Internal/SampleFolders.php @@ -50,24 +50,21 @@ private static function isAbsent(string $source): bool { // A NUL byte or a URI is rcopy()'s to refuse: is_dir() throws on the first as of // PHP 8.6 and would ask a stream wrapper about the second. - if ('' === $source || \str_contains($source, "\0") || \str_contains($source, '://')) { - return false; - } - if (\is_dir($source)) { + if ('' === $source || \str_contains($source, "\0") || \str_contains($source, '://') || \is_dir($source)) { return false; } // Walk up to the nearest ancestor that is a folder and ask it for the missing entry. - $path = \rtrim(\str_replace('\\', '/', $source), '/'); - for ($parent = \dirname($path); $parent !== $path; $parent = \dirname($path)) { - if (\is_dir($parent)) { - $entries = \is_readable($parent) ? \scandir($parent) : false; - - return \is_array($entries) && !\in_array(\basename($path), $entries, true); - } - $path = $parent; + // The path stays as configured: dirname() and basename() split it the way the copier + // will, and both ignore a trailing separator. + $path = $source; + $parent = \dirname($path); + while ($parent !== $path && !\is_dir($parent)) { + $path = $parent; + $parent = \dirname($path); } + $entries = $parent !== $path && \is_readable($parent) ? \scandir($parent) : false; - return false; + return \is_array($entries) && !\in_array(\basename($path), $entries, true); } } diff --git a/tests/Internal/SampleFoldersTest.php b/tests/Internal/SampleFoldersTest.php index c08db34..b814d0f 100644 --- a/tests/Internal/SampleFoldersTest.php +++ b/tests/Internal/SampleFoldersTest.php @@ -74,13 +74,26 @@ public function testAPathTheCopierRefusesIsReportedAndNeverProbed(): void // is_dir() throws on a NUL byte as of PHP 8.6 and would ask a stream wrapper about a URI. $pairs = [ [$this->dir . "/module/testdata/up\0loads", $this->dir . '/uploads'], - ['ftp://example.invalid/testdata/uploads', $this->dir . '/uploads'], + ['sample://example.invalid/testdata/uploads', $this->dir . '/uploads'], ['', $this->dir . '/uploads'], ]; self::assertSame($pairs, SampleFolders::copy($pairs)); } + public function testABackslashInTheConfiguredPathIsNotReadAsASeparatorWhereItIsNone(): void + { + if ('\\' === \DIRECTORY_SEPARATOR) { + self::markTestSkipped('A backslash separates folders on Windows.'); + } + + // "testdata\images" names one entry of the module folder here, and there is none. + $failed = SampleFolders::copy([[$this->dir . '/module/testdata\\images', $this->dir . '/uploads/images']]); + + self::assertSame([], $failed); + self::assertDirectoryDoesNotExist($this->dir . '/uploads'); + } + public function testAFolderBelowAParentThatCannotBeSearchedIsReported(): void { if ('\\' === \DIRECTORY_SEPARATOR || (\function_exists('posix_geteuid') && 0 === posix_geteuid())) {