From 23378d7668b28fd9cdce89cd3d7ed57ede4d2d8f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 25 Feb 2026 08:40:04 +0000 Subject: [PATCH] Fix: prevent key generation if APP_KEY already exists This commit prevents the `key:generate` command from running during installation if `APP_KEY` is already set in the configuration. This avoids potential issues with duplicate keys in `.env` or overwriting existing keys. Added a new test case `FinalInstallManagerTest` to verify this behavior. Co-authored-by: juzaweb <47020363+juzaweb@users.noreply.github.com> --- src/Helpers/FinalInstallManager.php | 2 +- tests/Feature/FinalInstallManagerTest.php | 77 +++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/FinalInstallManagerTest.php diff --git a/src/Helpers/FinalInstallManager.php b/src/Helpers/FinalInstallManager.php index 6666ade..bffc4be 100644 --- a/src/Helpers/FinalInstallManager.php +++ b/src/Helpers/FinalInstallManager.php @@ -33,7 +33,7 @@ public function runFinal() private static function generateKey(BufferedOutput $outputLog) { try { - if (config('installer.final.key')) { + if (config('installer.final.key') && !config('app.key')) { Artisan::call('key:generate', ['--force'=> true], $outputLog); } } catch (Throwable $e) { diff --git a/tests/Feature/FinalInstallManagerTest.php b/tests/Feature/FinalInstallManagerTest.php new file mode 100644 index 0000000..cc08946 --- /dev/null +++ b/tests/Feature/FinalInstallManagerTest.php @@ -0,0 +1,77 @@ + \Juzaweb\Modules\Core\Facades\Field::class, + 'Module' => \Juzaweb\Modules\Core\Facades\Module::class, + 'Theme' => \Juzaweb\Modules\Core\Facades\Theme::class, + 'Widget' => \Juzaweb\Modules\Core\Facades\Widget::class, + 'Sidebar' => \Juzaweb\Modules\Core\Facades\Sidebar::class, + 'PageTemplate' => \Juzaweb\Modules\Core\Facades\PageTemplate::class, + 'PageBlock' => \Juzaweb\Modules\Core\Facades\PageBlock::class, + 'Chart' => \Juzaweb\Modules\Core\Facades\Chart::class, + ]; + } + + public function testGenerateKeySkippedIfKeyExists() + { + // Set a dummy key + config(['app.key' => 'base64:dummykey']); + config(['installer.final.key' => true]); + + $kernel = Mockery::mock(\Illuminate\Contracts\Console\Kernel::class); + $this->app->instance(\Illuminate\Contracts\Console\Kernel::class, $kernel); + + // Expect key:generate to NOT be called + $kernel->shouldReceive('call') + ->never() + ->with('key:generate', ['--force' => true], Mockery::any()); + + // Expect other calls + $kernel->shouldReceive('call')->with('vendor:publish', Mockery::any(), Mockery::any()); + $kernel->shouldReceive('call')->with('storage:link', Mockery::any(), Mockery::any()); + + $manager = new FinalInstallManager(); + $manager->runFinal(); + } + + public function testGenerateKeyIsCalledIfKeyMissing() + { + // Set key to null + config(['app.key' => null]); + config(['installer.final.key' => true]); + + $kernel = Mockery::mock(\Illuminate\Contracts\Console\Kernel::class); + $this->app->instance(\Illuminate\Contracts\Console\Kernel::class, $kernel); + + // Expect key:generate to be called + $kernel->shouldReceive('call') + ->once() + ->with('key:generate', ['--force' => true], Mockery::any()); + + // Expect other calls + $kernel->shouldReceive('call')->with('vendor:publish', Mockery::any(), Mockery::any()); + $kernel->shouldReceive('call')->with('storage:link', Mockery::any(), Mockery::any()); + + $manager = new FinalInstallManager(); + $manager->runFinal(); + } +}