Skip to content

Commit d4e9146

Browse files
authored
[6.x] Fix non-interactive make:user with Eloquent users (#15943)
1 parent b9627fc commit d4e9146

4 files changed

Lines changed: 83 additions & 7 deletions

File tree

‎src/Console/Commands/MakeUser.php‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ public function handle()
8585
$this->password = $password;
8686
}
8787

88+
$this->data = collect([
89+
'name' => $this->option('name'),
90+
'first_name' => $this->option('first-name'),
91+
'last_name' => $this->option('last-name'),
92+
])->filter()->all();
93+
8894
// If email argument exists, non-interactively create user.
8995
if ($this->email = $this->argument('email')) {
9096
return $this->createUser();
@@ -126,7 +132,7 @@ protected function promptName()
126132
return $this->promptSeparateNameFields();
127133
}
128134

129-
$this->data['name'] = text(label: 'Name');
135+
$this->data['name'] ??= text(label: 'Name');
130136

131137
return $this;
132138
}
@@ -138,8 +144,8 @@ protected function promptName()
138144
*/
139145
protected function promptSeparateNameFields()
140146
{
141-
$this->data['first_name'] = text(label: 'First Name');
142-
$this->data['last_name'] = text(label: 'Last Name');
147+
$this->data['first_name'] ??= text(label: 'First Name');
148+
$this->data['last_name'] ??= text(label: 'Last Name');
143149

144150
return $this;
145151
}
@@ -194,8 +200,11 @@ protected function createUser()
194200

195201
$user = User::make()
196202
->email($this->email)
197-
->data($this->data)
198-
->password($this->password);
203+
->data($this->data);
204+
205+
if ($this->password) {
206+
$user->password($this->password);
207+
}
199208

200209
if ($this->super || $this->option('super')) {
201210
$user->makeSuper();
@@ -265,6 +274,9 @@ protected function getOptions()
265274
return array_merge(parent::getOptions(), [
266275
['super', '', InputOption::VALUE_NONE, 'Generate a super user with permission to do everything'],
267276
['password', '', InputOption::VALUE_OPTIONAL, 'Generate a user with given password'],
277+
['name', '', InputOption::VALUE_OPTIONAL, 'Generate a user with given name'],
278+
['first-name', '', InputOption::VALUE_OPTIONAL, 'Generate a user with given first name'],
279+
['last-name', '', InputOption::VALUE_OPTIONAL, 'Generate a user with given last name'],
268280
]);
269281
}
270282
}

‎src/Console/Commands/stubs/auth/create_users_table.php.stub‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
Schema::create('USERS_TABLE', function (Blueprint $table) {
22
$table->id();
3-
$table->string('name');
3+
$table->string('name')->nullable();
44
$table->string('email')->unique();
55
$table->timestamp('email_verified_at')->nullable();
6-
$table->string('password');
6+
$table->string('password')->nullable();
77
$table->rememberToken();
88
$table->timestamps();
99
$table->boolean('super')->default(false);

‎src/Console/Commands/stubs/auth/update_users_table.php.stub‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Schema::table('USERS_TABLE', function (Blueprint $table) {
33
$table->string('avatar')->nullable();
44
$table->json('preferences')->nullable();
55
$table->timestamp('last_login')->nullable();
6+
$table->string('name')->nullable()->change();
67
$table->string('password')->nullable()->change();
78
$table->text('two_factor_secret')->nullable();
89
$table->text('two_factor_recovery_codes')->nullable();

‎tests/Console/Commands/MakeUserTest.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace Tests\Console\Commands;
44

5+
use Illuminate\Database\Schema\Blueprint;
56
use Illuminate\Filesystem\Filesystem;
7+
use Illuminate\Support\Facades\Schema;
68
use PHPUnit\Framework\Attributes\Test;
79
use Statamic\Facades\User;
10+
use Tests\Auth\Eloquent\User as EloquentUser;
811
use Tests\TestCase;
912

1013
class MakeUserTest extends TestCase
@@ -37,6 +40,30 @@ public function it_can_make_a_user()
3740
$this->assertEquals('jason@tellmewhatyouchasin.com', $user->email());
3841
}
3942

43+
#[Test]
44+
public function it_can_make_an_eloquent_user_non_interactively()
45+
{
46+
config([
47+
'auth.providers.users.model' => EloquentUser::class,
48+
'statamic.users.repository' => 'eloquent',
49+
]);
50+
51+
$this->loadMigrationsFrom(__DIR__.'/../../Auth/Eloquent/__migrations__');
52+
53+
Schema::table('users', fn (Blueprint $table) => $table->string('name')->nullable()->change());
54+
55+
collect(['role', 'group'])->each(fn ($type) => Schema::create("{$type}_user", function (Blueprint $table) use ($type) {
56+
$table->id();
57+
$table->foreignId('user_id');
58+
$table->string("{$type}_id");
59+
}));
60+
61+
$this->artisan('statamic:make:user', ['email' => 'jason@tellmewhatyouchasin.com'])
62+
->expectsOutputToContain('User created successfully.');
63+
64+
$this->assertEquals('jason@tellmewhatyouchasin.com', User::all()->first()->email());
65+
}
66+
4067
#[Test]
4168
public function it_can_make_a_super_user_interactively()
4269
{
@@ -145,4 +172,40 @@ public function if_password_option_is_passed_it_will_not_prompt_for_password()
145172
$this->assertNotEmpty($user->password());
146173
$this->assertTrue(password_verify($password, $user->password()));
147174
}
175+
176+
#[Test]
177+
public function it_can_make_a_user_with_name_option()
178+
{
179+
$this->artisan('statamic:make:user', ['email' => 'jason@tellmewhatyouchasin.com', '--name' => 'Jason'])
180+
->expectsOutputToContain('User created successfully.');
181+
182+
$this->assertEquals('Jason', User::all()->first()->get('name'));
183+
}
184+
185+
#[Test]
186+
public function it_can_make_a_user_with_first_and_last_name_options()
187+
{
188+
$this->artisan('statamic:make:user', [
189+
'email' => 'jason@tellmewhatyouchasin.com',
190+
'--first-name' => 'Jason',
191+
'--last-name' => 'Varga',
192+
])->expectsOutputToContain('User created successfully.');
193+
194+
$user = User::all()->first();
195+
196+
$this->assertEquals('Jason', $user->get('first_name'));
197+
$this->assertEquals('Varga', $user->get('last_name'));
198+
}
199+
200+
#[Test]
201+
public function if_name_option_is_passed_it_will_not_prompt_for_name()
202+
{
203+
$this->artisan('statamic:make:user', ['--name' => 'Duncan'])
204+
->expectsQuestion('Email', 'duncan@likesteatime.com')
205+
->expectsQuestion('Password', 'PacManMoonwalk#84')
206+
->expectsQuestion('Super user?', false)
207+
->assertExitCode(0);
208+
209+
$this->assertEquals('Duncan', User::all()->first()->get('name'));
210+
}
148211
}

0 commit comments

Comments
 (0)