]> BookStack Code Mirror - system-cli/commitdiff
Covered backup command options with testing
authorDan Brown <redacted>
Tue, 4 Apr 2023 15:10:50 +0000 (16:10 +0100)
committerDan Brown <redacted>
Tue, 4 Apr 2023 15:10:50 +0000 (16:10 +0100)
src/Commands/BackupCommand.php
tests/Commands/BackupCommandTest.php

index 05d90c72bf3afc06ce6e35bac83319c0e776a940..915c582ce281bc36c6ddf671e9af1af1e9559db2 100644 (file)
@@ -21,9 +21,9 @@ final class BackupCommand extends Command
         $this->setName('backup');
         $this->setDescription('Backup a BookStack installation to a single compressed ZIP file.');
         $this->addArgument('backup-path', InputArgument::OPTIONAL, 'Outfile file or directory to store the resulting backup file.', '');
-        $this->addOption('no-database', null, null, "Skip adding a database dump to the backup");
-        $this->addOption('no-uploads', null, null, "Skip adding uploaded files to the backup");
-        $this->addOption('no-themes', null, null, "Skip adding the themes folder to the backup");
+        $this->addOption('no-database', null, InputOption::VALUE_NONE, "Skip adding a database dump to the backup");
+        $this->addOption('no-uploads', null, InputOption::VALUE_NONE, "Skip adding uploaded files to the backup");
+        $this->addOption('no-themes', null, InputOption::VALUE_NONE, "Skip adding the themes folder to the backup");
         $this->addOption('app-directory', null, InputOption::VALUE_OPTIONAL, 'BookStack install directory to backup', '');
     }
 
index 7f598d7970f35303498283c3bf722dec15d701b5..58b0446ee7ecc0e8325034f7a81f395eef918687 100644 (file)
@@ -16,10 +16,10 @@ class BackupCommandTest extends TestCase
         exec('php artisan migrate --force');
         exec('php artisan bookstack:create-admin --email="' . static::$uniqueUserEmail . '" --name="Bazza" --password="hunter200"');
     }
+
     public function test_command_does_full_backup_of_cwd_instance_by_default()
     {
         chdir('/var/www/bookstack');
-
         $this->assertCount(0, glob('bookstack-backup-*.zip'));
 
         file_put_contents('/var/www/bookstack/themes/test.txt', static::$uniqueUserEmail . '-themes');
@@ -27,7 +27,6 @@ class BackupCommandTest extends TestCase
         file_put_contents('/var/www/bookstack/storage/uploads/test.txt', static::$uniqueUserEmail . '-storage-uploads');
 
         $result = $this->runCommand('backup');
-        $result->dumpError();
         $result->assertSuccessfulExit();
         $result->assertStdoutContains("Backup finished.");
 
@@ -43,5 +42,80 @@ class BackupCommandTest extends TestCase
         $this->assertStringContainsString(static::$uniqueUserEmail . '-public-uploads', $zip->getFromName('public/uploads/test.txt'));
         $this->assertStringContainsString(static::$uniqueUserEmail . '-storage-uploads', $zip->getFromName('storage/uploads/test.txt'));
         $this->assertStringContainsString(static::$uniqueUserEmail . '-themes', $zip->getFromName('themes/test.txt'));
+
+        unlink($zipFile);
     }
+
+    public function test_no_options()
+    {
+        chdir('/var/www/bookstack');
+        $this->assertCount(0, glob('bookstack-backup-*.zip'));
+
+        $result = $this->runCommand('backup', [
+            '--no-database' => true,
+            '--no-uploads' => true,
+            '--no-themes' => true,
+        ]);
+        $result->assertSuccessfulExit();
+
+        $zipFile = glob('bookstack-backup-*.zip')[0];
+
+        $zip = new \ZipArchive();
+        $zip->open($zipFile);
+        $this->assertLessThan(3, $zip->numFiles);
+        $this->assertFalse($zip->getFromName('db.sql'));
+        $this->assertFalse($zip->getFromName('themes/.gitignore'));
+        $this->assertFalse($zip->getFromName('public/uploads/.gitignore'));
+        $this->assertFalse($zip->getFromName('storage/uploads/.gitignore'));
+
+        unlink($zipFile);
+    }
+
+    public function test_app_directory_option()
+    {
+        chdir('/var');
+        $this->assertCount(0, glob('bookstack-backup-*.zip'));
+
+        $result = $this->runCommand('backup', [
+            '--no-database' => true,
+            '--no-uploads' => true,
+            '--no-themes' => true,
+            '--app-directory' => '/var/www/bookstack'
+        ]);
+        $result->assertSuccessfulExit();
+
+        $zipFile = glob('bookstack-backup-*.zip')[0] ?? null;
+        $this->assertNotNull($zipFile);
+
+        unlink($zipFile);
+    }
+
+    public function test_backup_path_argument()
+    {
+        chdir('/var/www/bookstack');
+        $this->assertCount(0, glob('/home/bookstack-backup-*.zip'));
+        $this->assertFileDoesNotExist('/home/my-cool-backup.zip');
+
+        $result = $this->runCommand('backup', [
+            'backup-path' => '/home/my-cool-backup.zip',
+            '--no-database' => true,
+            '--no-uploads' => true,
+            '--no-themes' => true,
+        ]);
+        $result->assertSuccessfulExit();
+        $this->assertFileExists('/home/my-cool-backup.zip');
+        unlink('/home/my-cool-backup.zip');
+
+        $result = $this->runCommand('backup', [
+            'backup-path' => '/home/',
+            '--no-database' => true,
+            '--no-uploads' => true,
+            '--no-themes' => true,
+        ]);
+        $result->assertSuccessfulExit();
+        $this->assertCount(1, glob('/home/bookstack-backup-*.zip'));
+        $zipFile = glob('/home/bookstack-backup-*.zip')[0];
+        unlink($zipFile);
+    }
+
 }
\ No newline at end of file