]> BookStack Code Mirror - system-cli/commitdiff
Added test coverage for DownloadVendorCommand
authorDan Brown <redacted>
Tue, 11 Mar 2025 14:31:32 +0000 (14:31 +0000)
committerDan Brown <redacted>
Tue, 11 Mar 2025 14:31:32 +0000 (14:31 +0000)
tests/Commands/DownloadVendorCommandTest.php [new file with mode: 0644]
tests/TestCase.php

diff --git a/tests/Commands/DownloadVendorCommandTest.php b/tests/Commands/DownloadVendorCommandTest.php
new file mode 100644 (file)
index 0000000..855750f
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+
+namespace Commands;
+
+use Tests\TestCase;
+
+class DownloadVendorCommandTest extends TestCase
+{
+    public function test_command_downloads_vendor_folder()
+    {
+        $this->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
index 65db06fd12a6e92fb9568d94535466adf6ede7e9..f5466384b9cc38d81b3352755ea2a8136e844878 100644 (file)
@@ -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