Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/Commands/InitCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -428,22 +428,26 @@ protected function runMigrations(): void

protected function createAdminUser(string $kebabName, string $serviceKey = '', string $serviceName = ''): array
{
$adminEmail = when(empty($serviceKey), "admin@{$kebabName}.com", "admin.{$serviceKey}@{$kebabName}.com");
$isServiceAdmin = (!empty($serviceKey) && !empty($serviceName));

$adminEmail = when($isServiceAdmin, "admin.{$serviceKey}@{$kebabName}.com", "admin@{$kebabName}.com");
$defaultPassword = substr(md5(uniqid()), 0, 8);

$serviceLabel = when(!empty($serviceName), " for {$serviceName}");
$serviceLabel = when($isServiceAdmin, " for {$serviceName}");
Copy link

Copilot AI Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The when() helper on this line is missing the third parameter (default value). When $isServiceAdmin is false, this will return null instead of an empty string. This could result in the service label being null rather than empty, which may cause type inconsistencies or unexpected output.

Consider changing to:

$serviceLabel = when($isServiceAdmin, " for {$serviceName}", '');
Suggested change
$serviceLabel = when($isServiceAdmin, " for {$serviceName}");
$serviceLabel = when($isServiceAdmin, " for {$serviceName}", '');

Copilot uses AI. Check for mistakes.

$adminCredentials = [
'email' => $this->ask("Please enter admin email{$serviceLabel}", $adminEmail),
'password' => $this->ask("Please enter admin password{$serviceLabel}", $defaultPassword),
];

$adminName = when($isServiceAdmin, "{$serviceName} Admin", 'Admin');

if ($this->authType === AuthTypeEnum::None) {
$adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", "{$serviceName} Admin");
$adminCredentials['name'] = $this->ask("Please enter admin name{$serviceLabel}", $adminName);
$adminCredentials['role_id'] = $this->ask("Please enter admin role id{$serviceLabel}", RoleEnum::Admin->value);
}

if (empty($serviceName)) {
if (!$isServiceAdmin) {
$this->adminCredentials = $adminCredentials;
}

Expand Down
50 changes: 50 additions & 0 deletions tests/InitCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

namespace RonasIT\ProjectInitializator\Tests;

use ReflectionMethod;
use ReflectionProperty;
use RonasIT\ProjectInitializator\Commands\InitCommand;
use RonasIT\ProjectInitializator\Enums\AuthTypeEnum;
use RonasIT\ProjectInitializator\Tests\Support\Traits\InitCommandMockTrait;

class InitCommandTest extends TestCase
Expand Down Expand Up @@ -926,4 +930,50 @@ public function testRunWithClerkAdditionalAdminsWithoutDefaultAdmin(): void
->expectsConfirmation('Do you want to uninstall project-initializator package?')
->assertExitCode(0);
}

/**
* @runInSeparateProcess
*/
public function testDefaultAdminsCredentials()
{
$this->mockNativeFunction(
'RonasIT\ProjectInitializator\Commands',
$this->mockAdminDefaultPassword('123456'),
$this->callFilePutContent('database/migrations/2018_11_11_111111_add_default_admin.php', $this->getFixture('users_default_admin_default_credentials.php')),
$this->mockAdminDefaultPassword('654321'),
$this->callFilePutContent('database/migrations/2018_11_11_111111_add_telescope_admin.php', $this->getFixture('telescope_users_default_credentials.php')),
$this->mockAdminDefaultPassword('123456'),
$this->callFilePutContent('database/migrations/2018_11_11_111111_add_nova_admin.php', $this->getFixture('nova_users_default_credentials.php')),
$this->mockAdminDefaultPassword('123456'),
$this->callFilePutContent('database/migrations/2018_11_11_111111_add_default_admin.php', $this->getFixture('admins_add_default_admin_default_credentials.php')),
$this->mockAdminDefaultPassword('654321'),
$this->callFilePutContent('database/migrations/2018_11_11_111111_add_telescope_admin.php', $this->getFixture('admins_add_telescope_admin_default_credentials.php')),
$this->mockAdminDefaultPassword('123456'),
$this->callFilePutContent('database/migrations/2018_11_11_111111_add_nova_admin.php', $this->getFixture('admins_add_nova_admin_default_credentials.php')),
);

$commandMock = $this
->getMockBuilder(InitCommand::class)
->disableOriginalConstructor()
->getMock();

$commandMock->method('ask')->willReturnCallback(fn ($question, $default) => $default);

$authTypeProperty = new ReflectionProperty(InitCommand::class, 'authType');
$authTypeProperty->setAccessible(true);

$createAdminMethod = new ReflectionMethod(InitCommand::class, 'createAdminUser');

$authTypeProperty->setValue($commandMock, AuthTypeEnum::None);

$createAdminMethod->invokeArgs($commandMock, ['my-app']);
$createAdminMethod->invokeArgs($commandMock, ['my-app', 'telescope', 'Laravel Telescope']);
$createAdminMethod->invokeArgs($commandMock, ['my-app', 'nova', 'Laravel Nova']);

$authTypeProperty->setValue($commandMock, AuthTypeEnum::Clerk);

$createAdminMethod->invokeArgs($commandMock, ['my-app']);
$createAdminMethod->invokeArgs($commandMock, ['my-app', 'telescope', 'Laravel Telescope']);
$createAdminMethod->invokeArgs($commandMock, ['my-app', 'nova', 'Laravel Nova']);
}
}
9 changes: 9 additions & 0 deletions tests/Support/Traits/InitCommandMockTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,13 @@ protected function changeConfigFileCall(string $fileName, string $sourceFixture,
$this->callFilePutContent(base_path($fileName), $this->getFixture($resultFixture)),
];
}

protected function mockAdminDefaultPassword(string $resultPassword): array
{
return [
$this->functionCall('substr', ['0058a062', 0, 8], $resultPassword),
$this->functionCall('md5', ['0058a062'], '0058a062'),
$this->functionCall('uniqid', [], '0058a062'),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

return new class extends Migration
{
public function up(): void
{
if (!App::environment('testing')) {
DB::table('admins')->insert([
'email' => 'admin@my-app.com',
'password' => Hash::make('123456'),
]);
}
}

public function down(): void
{
if (!App::environment('testing')) {
DB::table('admins')
->where('email', 'admin@my-app.com')
->delete();
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

return new class extends Migration
{
public function up(): void
{
if (!App::environment('testing')) {
DB::table('admins')->insert([
'email' => 'admin.nova@my-app.com',
'password' => Hash::make('123456'),
]);
}
}

public function down(): void
{
if (!App::environment('testing')) {
DB::table('admins')
->where('email', 'admin.nova@my-app.com')
->delete();
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

return new class extends Migration
{
public function up(): void
{
if (!App::environment('testing')) {
DB::table('admins')->insert([
'email' => 'admin.telescope@my-app.com',
'password' => Hash::make('654321'),
]);
}
}

public function down(): void
{
if (!App::environment('testing')) {
DB::table('admins')
->where('email', 'admin.telescope@my-app.com')
->delete();
}
}
};
30 changes: 30 additions & 0 deletions tests/fixtures/InitCommandTest/nova_users_default_credentials.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

return new class extends Migration
{
public function up(): void
{
if (!App::environment('testing')) {
DB::table('users')->insert([
'name' => 'Laravel Nova Admin',
'email' => 'admin.nova@my-app.com',
'password' => Hash::make('123456'),
'role_id' => 1,
]);
}
}

public function down(): void
{
if (!App::environment('testing')) {
DB::table('users')
->where('email', 'admin.nova@my-app.com')
->delete();
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

return new class extends Migration
{
public function up(): void
{
if (!App::environment('testing')) {
DB::table('users')->insert([
'name' => 'Laravel Telescope Admin',
'email' => 'admin.telescope@my-app.com',
'password' => Hash::make('654321'),
'role_id' => 1,
]);
}
}

public function down(): void
{
if (!App::environment('testing')) {
DB::table('users')
->where('email', 'admin.telescope@my-app.com')
->delete();
}
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

return new class extends Migration
{
public function up(): void
{
if (!App::environment('testing')) {
DB::table('users')->insert([
'name' => 'Admin',
'email' => 'admin@my-app.com',
'password' => Hash::make('123456'),
'role_id' => 1,
]);
}
}

public function down(): void
{
if (!App::environment('testing')) {
DB::table('users')
->where('email', 'admin@my-app.com')
->delete();
}
}
};