]> BookStack Code Mirror - system-cli/blob - tests/Commands/RestoreCommandTest.php
Added main-path restore command testing
[system-cli] / tests / Commands / RestoreCommandTest.php
1 <?php declare(strict_types=1);
2
3 namespace Tests\Commands;
4
5 use Tests\TestCase;
6
7 class RestoreCommandTest extends TestCase
8 {
9
10     public function test_restore_into_cwd_by_default_with_all_content_types()
11     {
12         $mysql = new \mysqli('db', 'bookstack', 'bookstack', 'bookstack');
13         $mysql->query('CREATE TABLE xx_testing (labels varchar(255));');
14
15         $result = $mysql->query('SHOW TABLES LIKE \'zz_testing\';');
16         $this->assertEquals(0, mysqli_num_rows($result));
17
18         $zipFile = $this->buildZip(function (\ZipArchive $zip) {
19             $zip->addFromString('.env', "APP_KEY=abc123\nAPP_URL=https://example.com");
20             $zip->addFromString('public/uploads/test.txt', 'hello-public-uploads');
21             $zip->addFromString('storage/uploads/test.txt', 'hello-storage-uploads');
22             $zip->addFromString('themes/test.txt', 'hello-themes');
23             $zip->addFromString('db.sql', "CREATE TABLE zz_testing (names varchar(255));\nINSERT INTO zz_testing values ('barry');");
24         });
25
26         exec('cp -r /var/www/bookstack /var/www/bookstack-restore');
27         chdir('/var/www/bookstack-restore');
28
29         $result = $this->runCommand('restore', [
30             'backup-zip' => $zipFile,
31         ], [
32             'yes', '1'
33         ]);
34
35         $result->dumpError();
36         $result->assertSuccessfulExit();
37         $result->assertStdoutContains('Restore operation complete!');
38
39         $result = $mysql->query('SELECT * FROM zz_testing where names = \'barry\';');
40         $this->assertEquals(1, mysqli_num_rows($result));
41         $result = $mysql->query('SHOW TABLES LIKE \'xx_testing\';');
42         $this->assertEquals(0, mysqli_num_rows($result));
43
44         $this->assertStringEqualsFile('/var/www/bookstack-restore/public/uploads/test.txt', 'hello-public-uploads');
45         $this->assertStringEqualsFile('/var/www/bookstack-restore/storage/uploads/test.txt', 'hello-storage-uploads');
46         $this->assertStringEqualsFile('/var/www/bookstack-restore/themes/test.txt', 'hello-themes');
47
48         $mysql->query("DROP TABLE zz_testing;");
49         exec('rm -rf /var/www/bookstack-restore');
50     }
51
52     protected function buildZip(callable $builder): string
53     {
54         $zipFile = tempnam(sys_get_temp_dir(), 'cli-test');
55         $testZip = new \ZipArchive('');
56         $testZip->open($zipFile);
57         $builder($testZip);
58         $testZip->close();
59
60         return $zipFile;
61     }
62 }