From 56fadcf86de007b96135336f3145dc8c1cdefeb0 Mon Sep 17 00:00:00 2001 From: Kevin Woblick Date: Thu, 18 Dec 2025 00:01:35 +0100 Subject: [PATCH] Allow passing a password to registeruser command via CLI (#1046) --- app/Console/Commands/RegisterUserCommand.php | 7 +++++-- tests/Commands/RegisterUserCommandTest.php | 22 ++++++++++++++++++-- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/app/Console/Commands/RegisterUserCommand.php b/app/Console/Commands/RegisterUserCommand.php index d06583c98..6b46c92b2 100644 --- a/app/Console/Commands/RegisterUserCommand.php +++ b/app/Console/Commands/RegisterUserCommand.php @@ -10,7 +10,7 @@ class RegisterUserCommand extends Command { - protected $signature = 'registeruser {name? : Username} {email? : User email address} {--admin}'; + protected $signature = 'registeruser {name? : Username} {email? : User email address} {password? : Password for the user} {--admin}'; protected $description = 'Register a new user with a given user name and an email address.'; private ?string $userName; @@ -27,6 +27,7 @@ public function handle(): void $this->userName = $this->argument('name'); $this->userEmail = $this->argument('email'); + $this->userPassword = $this->argument('password'); do { $this->askForUserDetails(); @@ -68,6 +69,8 @@ protected function askForUserDetails(): void $this->userEmail = $this->ask('Please enter the user email address', $this->userEmail); } - $this->userPassword = $this->secret('Please enter a password for ' . $this->userName); + if (empty($this->userPassword) || $this->validationFailed) { + $this->userPassword = $this->ask('Please enter a password for ' . $this->userName, $this->userPassword); + } } } diff --git a/tests/Commands/RegisterUserCommandTest.php b/tests/Commands/RegisterUserCommandTest.php index a7d6d4b72..7b4bc2fc9 100644 --- a/tests/Commands/RegisterUserCommandTest.php +++ b/tests/Commands/RegisterUserCommandTest.php @@ -28,6 +28,25 @@ public function test_command_with_input(): void $this->assertEquals('test@linkace.org', $databaseUser->email); } + public function test_command_with_input_including_password(): void + { + User::factory()->create(); // Create admin dummy user + + $this->artisan('registeruser', [ + 'name' => 'Test', + 'email' => 'test@linkace.org', + 'password' => 'testpassword', + ]) + ->doesntExpectOutput('Please enter a password for Test') + ->expectsOutput('User Test registered.') + ->assertExitCode(0); + + $databaseUser = User::latest('id')->first(); + + $this->assertEquals('Test', $databaseUser->name); + $this->assertEquals('test@linkace.org', $databaseUser->email); + } + public function test_command_without_input(): void { User::factory()->create(); // Create admin dummy user @@ -60,7 +79,6 @@ public function test_command_with_duplicate_user(): void ->expectsQuestion('Please enter the user email address', 'test2@linkace.org') ->expectsQuestion('Please enter a password for Test2', 'testpassword') ->expectsOutput('User Test2 registered.') - ->assertExitCode(0) - ; + ->assertExitCode(0); } }