Add command admin:user:verify

This commit is contained in:
Constantin Graf
2024-09-13 17:39:39 +02:00
committed by Constantin Graf
parent 50902e7705
commit 37400d239c
2 changed files with 132 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands\Admin;
use App\Models\User;
use Illuminate\Auth\Events\Verified;
use Illuminate\Console\Command;
class UserVerifyCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'admin:user:verify
{ email : The email of the user to verify }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Verify the email address of an user';
/**
* Execute the console command.
*/
public function handle(): int
{
$email = $this->argument('email');
$this->info('Start verifying user with email "'.$email.'"');
/** @var User|null $user */
$user = User::where('email', $email)->first();
if ($user === null) {
$this->error('User with email "'.$email.'" not found.');
return self::FAILURE;
}
if ($user->hasVerifiedEmail()) {
$this->info('User with email "'.$email.'" already verified.');
return self::FAILURE;
}
$user->markEmailAsVerified();
event(new Verified($user));
$this->info('User with email "'.$email.'" has been verified.');
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
namespace Tests\Unit\Console\Commands\Admin;
use App\Console\Commands\Admin\UserVerifyCommand;
use App\Models\Member;
use App\Models\Organization;
use App\Models\User;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use Tests\TestCaseWithDatabase;
#[CoversClass(UserVerifyCommand::class)]
#[UsesClass(UserVerifyCommand::class)]
class UserVerifyCommandTest extends TestCaseWithDatabase
{
public function test_it_verifies_user_email(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create([
'email_verified_at' => null,
]);
$member = Member::factory()->forUser($user)->forOrganization($organization)->create();
// Act
$command = $this->artisan('admin:user:verify', ['email' => $user->email]);
// Assert
$command->expectsOutput('Start verifying user with email "'.$user->email.'"')
->expectsOutput('User with email "'.$user->email.'" has been verified.')
->assertExitCode(0);
}
public function test_it_fails_if_user_does_not_exist(): void
{
// Arrange
$email = 'test@test.test';
$organization = Organization::factory()->create();
$user = User::factory()->create([
'email' => 'other@test.test',
'email_verified_at' => null,
]);
$member = Member::factory()->forUser($user)->forOrganization($organization)->create();
// Act
$command = $this->artisan('admin:user:verify', ['email' => $email]);
// Assert
$command->expectsOutput('User with email "'.$email.'" not found.')
->assertExitCode(1);
}
public function test_it_fails_if_user_email_is_already_verified(): void
{
// Arrange
$organization = Organization::factory()->create();
$user = User::factory()->create([
'email_verified_at' => now(),
]);
$member = Member::factory()->forUser($user)->forOrganization($organization)->create();
// Act
$command = $this->artisan('admin:user:verify', ['email' => $user->email]);
// Assert
$command->expectsOutput('User with email "'.$user->email.'" already verified.')
->assertExitCode(1);
}
}