Skip to content
This repository was archived by the owner on Jul 26, 2024. It is now read-only.

Commit df21bb3

Browse files
committed
Addressed restore issues
- 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.
1 parent 681df67 commit df21bb3

File tree

2 files changed

+36
-8
lines changed

2 files changed

+36
-8
lines changed

src/Commands/RestoreCommand.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
104104
}
105105

106106
if ($envChanges && $envChanges['old_url'] !== $envChanges['new_url']) {
107-
$output->writeln("<info>App URL change made, Updating database with URL change...</info>");
107+
$output->writeln("<info>App URL change made, updating database with URL change...</info>");
108108
$artisan->run([
109-
'bookstack:update-url',
109+
'bookstack:update-url', '--force',
110110
$envChanges['old_url'], $envChanges['new_url'],
111111
]);
112112
}
@@ -165,9 +165,9 @@ protected function restoreEnv(string $extractDir, string $appDir, OutputInterfac
165165
];
166166

167167
if ($oldUrl !== $newUrl) {
168-
$output->writeln("Found different APP_URL values:");
169-
$changedUrl = $interactions->choice('Which would you like to use?', array_filter([$oldUrl, $newUrl]));
170-
$envContents = preg_replace('/^APP_URL=.*?$/', 'APP_URL="' . $changedUrl . '"', $envContents);
168+
$question = 'Found different APP_URL values, which would you like to use?';
169+
$changedUrl = $interactions->choice($question, array_filter([$oldUrl, $newUrl]));
170+
$envContents = preg_replace('/^APP_URL=.*?$/m', 'APP_URL="' . $changedUrl . '"', $envContents);
171171
$returnData['new_url'] = $changedUrl;
172172
}
173173

tests/Commands/RestoreCommandTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public function test_restore_into_cwd_by_default_with_all_content_types()
1717
$this->assertEquals(0, mysqli_num_rows($result));
1818

1919
$zipFile = $this->buildZip(function (\ZipArchive $zip) {
20-
$zip->addFromString('.env', "APP_KEY=abc123\nAPP_URL=https://example.com");
20+
$zip->addFromString('.env', "APP_KEY=abc123\nAPP_URL=https://restore.example.com");
2121
$zip->addFromString('public/uploads/test.txt', 'hello-public-uploads');
2222
$zip->addFromString('storage/uploads/test.txt', 'hello-storage-uploads');
2323
$zip->addFromString('themes/test.txt', 'hello-themes');
@@ -29,15 +29,17 @@ public function test_restore_into_cwd_by_default_with_all_content_types()
2929

3030
$result = $this->runCommand('restore', [
3131
'backup-zip' => $zipFile,
32-
], ['yes', '1']);
32+
], ['yes', '1']); // This restore uses the existing (Non-backup) APP_URL
3333

34+
$result->dumpError();
3435
$result->assertSuccessfulExit();
3536
$result->assertStdoutContains('✔ .env Config File');
3637
$result->assertStdoutContains('✔ Themes Folder');
3738
$result->assertStdoutContains('✔ Public File Uploads');
3839
$result->assertStdoutContains('✔ Private File Uploads');
3940
$result->assertStdoutContains('✔ Database Dump');
4041
$result->assertStdoutContains('Restore operation complete!');
42+
$result->assertStdoutContains('App URL change made, updating database with URL change');
4143

4244
$result = $mysql->query('SELECT * FROM zz_testing where names = \'barry\';');
4345
$this->assertEquals(1, mysqli_num_rows($result));
@@ -47,14 +49,40 @@ public function test_restore_into_cwd_by_default_with_all_content_types()
4749
$this->assertStringEqualsFile('/var/www/bookstack-restore/public/uploads/test.txt', 'hello-public-uploads');
4850
$this->assertStringEqualsFile('/var/www/bookstack-restore/storage/uploads/test.txt', 'hello-storage-uploads');
4951
$this->assertStringEqualsFile('/var/www/bookstack-restore/themes/test.txt', 'hello-themes');
52+
5053
$env = file_get_contents('/var/www/bookstack-restore/.env');
5154
$this->assertStringContainsString('APP_KEY=abc123', $env);
52-
$this->assertStringContainsString('APP_URL=https://example.com', $env);
55+
$this->assertStringNotContainsString('APP_URL=https://restore.example.com', $env);
56+
$this->assertStringContainsString('APP_URL="https://example.com"', $env);
5357

5458
$mysql->query("DROP TABLE zz_testing;");
5559
exec('rm -rf /var/www/bookstack-restore');
5660
}
5761

62+
public function test_restore_using_backup_env_url()
63+
{
64+
$zipFile = $this->buildZip(function (\ZipArchive $zip) {
65+
$zip->addFromString('.env', "APP_KEY=abc123\nAPP_URL=https://restore.example.com");
66+
});
67+
68+
exec('cp -r /var/www/bookstack /var/www/bookstack-restore-backup-env');
69+
chdir('/var/www/bookstack-restore-backup-env');
70+
71+
$result = $this->runCommand('restore', [
72+
'backup-zip' => $zipFile,
73+
], ['yes', '0']); // This restore uses the old (Backup) APP_URL
74+
75+
$result->assertSuccessfulExit();
76+
$result->assertStdoutContains('✔ .env Config File');
77+
$result->assertStdoutContains('Restore operation complete!');
78+
79+
$env = file_get_contents('/var/www/bookstack-restore-backup-env/.env');
80+
$this->assertStringContainsString('APP_KEY=abc123', $env);
81+
$this->assertStringContainsString('APP_URL="https://restore.example.com"', $env);
82+
83+
exec('rm -rf /var/www/bookstack-restore-backup-env');
84+
}
85+
5886
public function test_command_fails_on_zip_with_no_expected_contents()
5987
{
6088
$zipFile = $this->buildZip(function (\ZipArchive $zip) {

0 commit comments

Comments
 (0)