]> BookStack Code Mirror - system-cli/commitdiff
Addressed restore issues
authorDan Brown <redacted>
Sun, 7 May 2023 12:26:44 +0000 (13:26 +0100)
committerDan Brown <redacted>
Sun, 7 May 2023 12:26:44 +0000 (13:26 +0100)
- Actually added force option to update-url artisan command, after missed
  in last commit.
- Fixed lack of updating APP_URL in .env due to bad preg_replace.
- Added new test to cover different case of using the backup env URL.
- Updated existing test with correct expectations.

src/Commands/RestoreCommand.php
tests/Commands/RestoreCommandTest.php

index 84c7106d948eab47e43f8d7702f8db2742d2d1ac..2fa3c853c071931691c303b19ad53485f7f936d7 100644 (file)
@@ -104,9 +104,9 @@ class RestoreCommand extends Command
         }
 
         if ($envChanges && $envChanges['old_url'] !== $envChanges['new_url']) {
-            $output->writeln("<info>App URL change made, Updating database with URL change...</info>");
+            $output->writeln("<info>App URL change made, updating database with URL change...</info>");
             $artisan->run([
-                'bookstack:update-url',
+                'bookstack:update-url', '--force',
                 $envChanges['old_url'], $envChanges['new_url'],
             ]);
         }
@@ -165,9 +165,9 @@ class RestoreCommand extends Command
         ];
 
         if ($oldUrl !== $newUrl) {
-            $output->writeln("Found different APP_URL values:");
-            $changedUrl = $interactions->choice('Which would you like to use?', array_filter([$oldUrl, $newUrl]));
-            $envContents = preg_replace('/^APP_URL=.*?$/', 'APP_URL="' . $changedUrl . '"', $envContents);
+            $question = 'Found different APP_URL values, which would you like to use?';
+            $changedUrl = $interactions->choice($question, array_filter([$oldUrl, $newUrl]));
+            $envContents = preg_replace('/^APP_URL=.*?$/m', 'APP_URL="' . $changedUrl . '"', $envContents);
             $returnData['new_url'] = $changedUrl;
         }
 
index bcb799226ea6ec91d1a158d6d5795abc3380f696..288b0568d0773fcd9d7ca5712807e1b040faaf54 100644 (file)
@@ -17,7 +17,7 @@ class RestoreCommandTest extends TestCase
         $this->assertEquals(0, mysqli_num_rows($result));
 
         $zipFile = $this->buildZip(function (\ZipArchive $zip) {
-            $zip->addFromString('.env', "APP_KEY=abc123\nAPP_URL=https://example.com");
+            $zip->addFromString('.env', "APP_KEY=abc123\nAPP_URL=https://restore.example.com");
             $zip->addFromString('public/uploads/test.txt', 'hello-public-uploads');
             $zip->addFromString('storage/uploads/test.txt', 'hello-storage-uploads');
             $zip->addFromString('themes/test.txt', 'hello-themes');
@@ -29,8 +29,9 @@ class RestoreCommandTest extends TestCase
 
         $result = $this->runCommand('restore', [
             'backup-zip' => $zipFile,
-        ], ['yes', '1']);
+        ], ['yes', '1']); // This restore uses the existing (Non-backup) APP_URL
 
+        $result->dumpError();
         $result->assertSuccessfulExit();
         $result->assertStdoutContains('✔ .env Config File');
         $result->assertStdoutContains('✔ Themes Folder');
@@ -38,6 +39,7 @@ class RestoreCommandTest extends TestCase
         $result->assertStdoutContains('✔ Private File Uploads');
         $result->assertStdoutContains('✔ Database Dump');
         $result->assertStdoutContains('Restore operation complete!');
+        $result->assertStdoutContains('App URL change made, updating database with URL change');
 
         $result = $mysql->query('SELECT * FROM zz_testing where names = \'barry\';');
         $this->assertEquals(1, mysqli_num_rows($result));
@@ -47,14 +49,40 @@ class RestoreCommandTest extends TestCase
         $this->assertStringEqualsFile('/var/www/bookstack-restore/public/uploads/test.txt', 'hello-public-uploads');
         $this->assertStringEqualsFile('/var/www/bookstack-restore/storage/uploads/test.txt', 'hello-storage-uploads');
         $this->assertStringEqualsFile('/var/www/bookstack-restore/themes/test.txt', 'hello-themes');
+
         $env = file_get_contents('/var/www/bookstack-restore/.env');
         $this->assertStringContainsString('APP_KEY=abc123', $env);
-        $this->assertStringContainsString('APP_URL=https://example.com', $env);
+        $this->assertStringNotContainsString('APP_URL=https://restore.example.com', $env);
+        $this->assertStringContainsString('APP_URL="https://example.com"', $env);
 
         $mysql->query("DROP TABLE zz_testing;");
         exec('rm -rf /var/www/bookstack-restore');
     }
 
+    public function test_restore_using_backup_env_url()
+    {
+        $zipFile = $this->buildZip(function (\ZipArchive $zip) {
+            $zip->addFromString('.env', "APP_KEY=abc123\nAPP_URL=https://restore.example.com");
+        });
+
+        exec('cp -r /var/www/bookstack /var/www/bookstack-restore-backup-env');
+        chdir('/var/www/bookstack-restore-backup-env');
+
+        $result = $this->runCommand('restore', [
+            'backup-zip' => $zipFile,
+        ], ['yes', '0']); // This restore uses the old (Backup) APP_URL
+
+        $result->assertSuccessfulExit();
+        $result->assertStdoutContains('✔ .env Config File');
+        $result->assertStdoutContains('Restore operation complete!');
+
+        $env = file_get_contents('/var/www/bookstack-restore-backup-env/.env');
+        $this->assertStringContainsString('APP_KEY=abc123', $env);
+        $this->assertStringContainsString('APP_URL="https://restore.example.com"', $env);
+
+        exec('rm -rf /var/www/bookstack-restore-backup-env');
+    }
+
     public function test_command_fails_on_zip_with_no_expected_contents()
     {
         $zipFile = $this->buildZip(function (\ZipArchive $zip) {