From: Dan Brown Date: Tue, 11 Mar 2025 14:31:32 +0000 (+0000) Subject: Added test coverage for DownloadVendorCommand X-Git-Url: http://source.bookstackapp.com/system-cli/commitdiff_plain/b48fe53b80e6383d6bd109d6bde1ce95d4e3ac93 Added test coverage for DownloadVendorCommand --- diff --git a/tests/Commands/DownloadVendorCommandTest.php b/tests/Commands/DownloadVendorCommandTest.php new file mode 100644 index 0000000..855750f --- /dev/null +++ b/tests/Commands/DownloadVendorCommandTest.php @@ -0,0 +1,69 @@ +withOwnBookStackFolder(function (string $basePath) { + exec("rm -rf $basePath/vendor"); + file_put_contents("$basePath/version", 'v25.02'); + mkdir("$basePath/dev/checksums", '0777', true); + file_put_contents("$basePath/dev/checksums/vendor", '22e02ee72d21ff719c1073abbec8302f8e2096ba6d072e133051064ed24b45b1'); + + $this->assertDirectoryDoesNotExist("$basePath/vendor"); + + $result = $this->runCommand('download-vendor'); + + $result->assertSuccessfulExit(); + $result->assertStdoutContains('Downloading ZIP from files.bookstackapp.com...'); + $result->assertStdoutContains('Successfully downloaded & extracted vendor files into BookStack instance!'); + + $this->assertDirectoryExists("$basePath/vendor/composer"); + }); + } + + public function test_app_directory_option() + { + $this->withOwnBookStackFolder(function (string $basePath) { + chdir('/var'); + + exec("rm -rf $basePath/vendor"); + file_put_contents("$basePath/version", 'v25.02'); + mkdir("$basePath/dev/checksums", '0777', true); + file_put_contents("$basePath/dev/checksums/vendor", '22e02ee72d21ff719c1073abbec8302f8e2096ba6d072e133051064ed24b45b1'); + + $this->assertDirectoryDoesNotExist("$basePath/vendor"); + + $result = $this->runCommand('download-vendor', ['--app-directory' => $basePath]); + + $result->assertSuccessfulExit(); + $this->assertDirectoryExists("$basePath/vendor/composer"); + + $result = $this->runCommand('download-vendor', ['--app-directory' => '/var/beans']); + $result->assertErrorExit(); + $result->assertStderrContains('Could not find a valid BookStack installation'); + }); + } + + public function test_command_fails_on_checksum_mismatch() + { + $this->withOwnBookStackFolder(function (string $basePath) { + file_put_contents("$basePath/version", 'v25.02'); + mkdir("$basePath/dev/checksums", '0777', true); + file_put_contents("$basePath/dev/checksums/vendor", '42e02ee72d21ff719c1073abbec8302f8e2096ba6d072e133051064ed24b45b1'); + + $result = $this->runCommand('download-vendor'); + + $result->assertErrorExit(); + $result->assertStdoutContains('Downloading ZIP from files.bookstackapp.com...'); + $result->assertStderrContains('Checksum of downloaded ZIP does not match the expected checksum.'); + + $this->assertDirectoryExists("$basePath/vendor/composer"); + }); + } + +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index 65db06f..f546638 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -68,4 +68,18 @@ class TestCase extends \PHPUnit\Framework\TestCase return new CommandResult($commandTester, $err); } + + /** + * Create a clone of a BookStack install folder, then pass that path + * and run the given callback. + * Destroys the folder afterwards. + */ + protected function withOwnBookStackFolder(callable $callback) + { + $path = '/var/www/bookstack-' . md5(random_bytes(5)); + exec("cp -r /var/www/bookstack $path"); + chdir($path); + $callback($path); + exec("rm -rf $path"); + } } \ No newline at end of file