]> BookStack Code Mirror - system-cli/commitdiff
Added test case for mysql usage with special chars
authorDan Brown <redacted>
Wed, 14 May 2025 10:52:12 +0000 (11:52 +0100)
committerDan Brown <redacted>
Wed, 14 May 2025 10:52:12 +0000 (11:52 +0100)
Updated docker environment to support this.
Updated compose volume mapping to support SELinux.

docker-compose.yml
docker-mysql-init.sql
tests/Commands/BackupCommandTest.php
tests/TestCase.php

index 53e9258c00b201bc0a900a1b17d7f848a929b978..a6f251838296536ccfc09a9e4895119afc9349d9 100644 (file)
@@ -3,7 +3,7 @@ services:
     build: .
     working_dir: /cli
     volumes:
-      - ./:/cli
+      - ./:/cli:z
     depends_on:
       db:
         condition: service_healthy
@@ -15,7 +15,7 @@ services:
       MYSQL_USER: bookstack
       MYSQL_PASSWORD: bookstack
     volumes:
-      - ./docker-mysql-init.sql:/docker-entrypoint-initdb.d/init.sql
+      - ./docker-mysql-init.sql:/docker-entrypoint-initdb.d/init.sql:z
     healthcheck:
       test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
       timeout: 20s
index e90c66dbd8aa5ccd37323ec1b6ac76fa69b06f92..1861dec9e968793b5dcd8e2c4b2fb946a2a26098 100644 (file)
@@ -1 +1,3 @@
-GRANT RELOAD ON *.* TO 'bookstack'@'%';
\ No newline at end of file
+GRANT RELOAD ON *.* TO 'bookstack'@'%';
+CREATE USER testuser@'%' IDENTIFIED BY 'a#pass${}with\\tspe\'cial\\nch"ars';
+GRANT ALL PRIVILEGES ON bookstack.* to testuser@'%';
\ No newline at end of file
index db5f116cf5f723c97fb0b05a4bafd26f880c813a..66527314f0634136152530cd244debf08f51a4fc 100644 (file)
@@ -157,4 +157,23 @@ class BackupCommandTest extends TestCase
         exec('rm -rf /var/www/bookstack-symlink-backup');
     }
 
+    public function test_backup_using_database_credentials_with_special_chars()
+    {
+        // The user details used here is created as part of the database docker-mysql-init.sql script.
+        chdir('/var/www/bookstack');
+        $this->assertCount(0, glob('storage/backups/bookstack-backup-*.zip'));
+
+        $result = $this->runCommand('backup', [], [], [
+            'DB_USERNAME' => 'testuser',
+            'DB_PASSWORD' => 'a#pass${}with\tspe\'cial\nch"ars',
+        ]);
+        $result->assertSuccessfulExit();
+        $result->assertStdoutContains("Backup finished.");
+
+        $this->assertCount(1, glob('storage/backups/bookstack-backup-*.zip'));
+
+        $zipFile = glob('storage/backups/bookstack-backup-*.zip')[0];
+        unlink($zipFile);
+    }
+
 }
\ No newline at end of file
index f5466384b9cc38d81b3352755ea2a8136e844878..e12cbdd8538558869f517950769c535009b3cad0 100644 (file)
@@ -48,7 +48,7 @@ class TestCase extends \PHPUnit\Framework\TestCase
         return require dirname(__DIR__) . '/src/app.php';
     }
 
-    protected function runCommand(string $command, array $args = [], array $inputs = []): CommandResult
+    protected function runCommand(string $command, array $args = [], array $inputs = [], array $env = []): CommandResult
     {
         $app = $this->getApp();
         $command = $app->find($command);
@@ -60,12 +60,20 @@ class TestCase extends \PHPUnit\Framework\TestCase
             $commandTester->setInputs($inputs);
         }
 
+        foreach ($env as $name => $value) {
+            $_SERVER[$name] = $value;
+        }
+
         try {
             $commandTester->execute($args);
         } catch (\Exception $exception) {
             $err = $exception;
         }
 
+        foreach ($env as $name => $value) {
+            unset($_SERVER[$name]);
+        }
+
         return new CommandResult($commandTester, $err);
     }