diff --git a/app/Console/Commands/Admin/UserVerifyCommand.php b/app/Console/Commands/Admin/UserVerifyCommand.php new file mode 100644 index 00000000..aa588a18 --- /dev/null +++ b/app/Console/Commands/Admin/UserVerifyCommand.php @@ -0,0 +1,59 @@ +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; + } +} diff --git a/tests/Unit/Console/Commands/Admin/UserVerifyCommandTest.php b/tests/Unit/Console/Commands/Admin/UserVerifyCommandTest.php new file mode 100644 index 00000000..bc0a6bdf --- /dev/null +++ b/tests/Unit/Console/Commands/Admin/UserVerifyCommandTest.php @@ -0,0 +1,73 @@ +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); + } +}