]> BookStack Code Mirror - system-cli/commitdiff
Developed out restore command testing
authorDan Brown <redacted>
Thu, 6 Apr 2023 11:20:38 +0000 (12:20 +0100)
committerDan Brown <redacted>
Thu, 6 Apr 2023 11:20:38 +0000 (12:20 +0100)
composer.json
composer.lock
src/Commands/RestoreCommand.php
tests/Commands/RestoreCommandTest.php

index 085e1ab883a4f196ff6bfd3ecbe340b6ec0d222d..db5437c0217beac021105efb10afc2b1d1ab5159 100644 (file)
@@ -7,7 +7,11 @@
     },
     "autoload": {
         "psr-4": {
     },
     "autoload": {
         "psr-4": {
-            "Cli\\": "src/",
+            "Cli\\": "src/"
+        }
+    },
+    "autoload-dev": {
+        "psr-4": {
             "Tests\\": "tests/"
         }
     },
             "Tests\\": "tests/"
         }
     },
index b3e3578a7e6677e975058e86fbe4450019a443c2..bd7dac3b97f5b9327a8ddf84b646b217d56ee02a 100644 (file)
@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "cd0ebe51e12d0c9de2a4ee3199c6fb24",
+    "content-hash": "fb5a3ea6a800b352e80acd925c56d8f4",
     "packages": [
         {
             "name": "graham-campbell/result-type",
     "packages": [
         {
             "name": "graham-campbell/result-type",
     "platform": {
         "ext-zip": "*"
     },
     "platform": {
         "ext-zip": "*"
     },
-    "platform-dev": [],
+    "platform-dev": {
+        "ext-mysqli": "*"
+    },
     "platform-overrides": {
         "php": "8.0.2"
     },
     "platform-overrides": {
         "php": "8.0.2"
     },
index c5dc6f24b4db40febf84d2b63c6a0cdb4022cb18..b38589cb5c46b4684bd067d641d19ade9f79bb75 100644 (file)
@@ -10,6 +10,7 @@ use Cli\Services\InteractiveConsole;
 use Cli\Services\MySqlRunner;
 use Cli\Services\ProgramRunner;
 use Cli\Services\RequirementsValidator;
 use Cli\Services\MySqlRunner;
 use Cli\Services\ProgramRunner;
 use Cli\Services\RequirementsValidator;
+use Exception;
 use RecursiveDirectoryIterator;
 use RecursiveIteratorIterator;
 use Symfony\Component\Console\Command\Command;
 use RecursiveDirectoryIterator;
 use RecursiveIteratorIterator;
 use Symfony\Component\Console\Command\Command;
@@ -30,7 +31,7 @@ class RestoreCommand extends Command
 
     /**
      * @throws CommandError
 
     /**
      * @throws CommandError
-     * @throws \Exception
+     * @throws Exception
      */
     protected function execute(InputInterface $input, OutputInterface $output): int
     {
      */
     protected function execute(InputInterface $input, OutputInterface $output): int
     {
@@ -61,7 +62,7 @@ class RestoreCommand extends Command
         }
 
         if (!$hasContent) {
         }
 
         if (!$hasContent) {
-            throw new CommandError("Provided ZIP backup [{$zipPath}] does not have any expected restore-able content.");
+            throw new CommandError("Provided ZIP backup [{$zipPath}] does not have any expected restorable content.");
         }
 
         $output->writeln("<info>The checked elements will be restored into [{$appDir}].</info>");
         }
 
         $output->writeln("<info>The checked elements will be restored into [{$appDir}].</info>");
@@ -180,7 +181,7 @@ class RestoreCommand extends Command
         rename($extractDir . DIRECTORY_SEPARATOR . $folderSubPath, $fullAppFolderPath);
     }
 
         rename($extractDir . DIRECTORY_SEPARATOR . $folderSubPath, $fullAppFolderPath);
     }
 
-    protected function deleteDirectoryAndContents(string $dir)
+    protected function deleteDirectoryAndContents(string $dir): void
     {
         $files = new RecursiveIteratorIterator(
             new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
     {
         $files = new RecursiveIteratorIterator(
             new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
@@ -206,7 +207,7 @@ class RestoreCommand extends Command
         file_put_contents($dropSqlTempFile, $mysql->dropTablesSql());
         $mysql->importSqlFile($dropSqlTempFile);
 
         file_put_contents($dropSqlTempFile, $mysql->dropTablesSql());
         $mysql->importSqlFile($dropSqlTempFile);
 
-
+        // Import MySQL dump
         $mysql->importSqlFile($dbDump);
     }
 }
         $mysql->importSqlFile($dbDump);
     }
 }
index c985abd41f57bd97d58fc8c47645a9eb0a5c0962..a8ff2b45a78e142f2d3843b30b859189bd8fc710 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace Tests\Commands;
 
 
 namespace Tests\Commands;
 
+use mysqli;
 use Tests\TestCase;
 
 class RestoreCommandTest extends TestCase
 use Tests\TestCase;
 
 class RestoreCommandTest extends TestCase
@@ -9,7 +10,7 @@ class RestoreCommandTest extends TestCase
 
     public function test_restore_into_cwd_by_default_with_all_content_types()
     {
 
     public function test_restore_into_cwd_by_default_with_all_content_types()
     {
-        $mysql = new \mysqli('db', 'bookstack', 'bookstack', 'bookstack');
+        $mysql = new mysqli('db', 'bookstack', 'bookstack', 'bookstack');
         $mysql->query('CREATE TABLE xx_testing (labels varchar(255));');
 
         $result = $mysql->query('SHOW TABLES LIKE \'zz_testing\';');
         $mysql->query('CREATE TABLE xx_testing (labels varchar(255));');
 
         $result = $mysql->query('SHOW TABLES LIKE \'zz_testing\';');
@@ -34,6 +35,11 @@ class RestoreCommandTest extends TestCase
 
         $result->dumpError();
         $result->assertSuccessfulExit();
 
         $result->dumpError();
         $result->assertSuccessfulExit();
+        $result->assertStdoutContains('✔ .env Config File');
+        $result->assertStdoutContains('✔ Themes Folder');
+        $result->assertStdoutContains('✔ Public File Uploads');
+        $result->assertStdoutContains('✔ Private File Uploads');
+        $result->assertStdoutContains('✔ Database Dump');
         $result->assertStdoutContains('Restore operation complete!');
 
         $result = $mysql->query('SELECT * FROM zz_testing where names = \'barry\';');
         $result->assertStdoutContains('Restore operation complete!');
 
         $result = $mysql->query('SELECT * FROM zz_testing where names = \'barry\';');
@@ -49,6 +55,49 @@ class RestoreCommandTest extends TestCase
         exec('rm -rf /var/www/bookstack-restore');
     }
 
         exec('rm -rf /var/www/bookstack-restore');
     }
 
+    public function test_command_fails_on_zip_with_no_expected_contents()
+    {
+        $zipFile = $this->buildZip(function (\ZipArchive $zip) {
+            $zip->addFromString('spaghetti', "Hello world!");
+        });
+
+        chdir('/var/www/bookstack');
+
+        $result = $this->runCommand('restore', [
+            'backup-zip' => $zipFile,
+        ]);
+
+        $result->assertErrorExit();
+        $result->assertStderrContains("Provided ZIP backup [{$zipFile}] does not have any expected restorable content.");
+    }
+
+    public function test_limited_restore_using_app_directory_option()
+    {
+        $zipFile = $this->buildZip(function (\ZipArchive $zip) {
+            $zip->addFromString('db.sql', "CREATE TABLE zz_testing (names varchar(255));");
+            $zip->addFromString('themes/hello.txt', "limited restore test!");
+        });
+
+        chdir('/home');
+
+        $result = $this->runCommand('restore', [
+            'backup-zip' => $zipFile,
+            '--app-directory' => '/var/www/bookstack'
+        ], ['yes']);
+
+        $result->assertSuccessfulExit();
+        $result->assertStdoutContains('❌ .env Config File');
+        $result->assertStdoutContains('✔ Themes Folder');
+        $result->assertStdoutContains('❌ Public File Uploads');
+        $result->assertStdoutContains('❌ Private File Uploads');
+        $result->assertStdoutContains('✔ Database Dump');
+        $this->assertStringEqualsFile('/var/www/bookstack/themes/hello.txt', 'limited restore test!');
+
+        unlink('/var/www/bookstack/themes/hello.txt');
+        $mysql = new mysqli('db', 'bookstack', 'bookstack', 'bookstack');
+        $mysql->query("DROP TABLE zz_testing;");
+    }
+
     protected function buildZip(callable $builder): string
     {
         $zipFile = tempnam(sys_get_temp_dir(), 'cli-test');
     protected function buildZip(callable $builder): string
     {
         $zipFile = tempnam(sys_get_temp_dir(), 'cli-test');