Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Refactor) Move welcome message to listener and add tests #4296

Merged
merged 1 commit into from
Oct 31, 2024
Merged
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
28 changes: 0 additions & 28 deletions app/Actions/Fortify/CreateNewUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use App\Models\Group;
use App\Models\Invite;
use App\Models\User;
use App\Repositories\ChatRepository;
use App\Rules\EmailBlacklist;
use Exception;
use Illuminate\Http\RedirectResponse;
Expand All @@ -33,10 +32,6 @@ class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;

public function __construct(private readonly ChatRepository $chatRepository)
{
}

/**
* Validate and create a newly registered user.
*
Expand Down Expand Up @@ -107,29 +102,6 @@ public function create(array $input): RedirectResponse | User
}
}

// Select A Random Welcome Message
$profileUrl = href_profile($user);

$welcomeArray = [
\sprintf('[url=%s]%s[/url], Welcome to ', $profileUrl, $user->username).config('other.title').'! Hope you enjoy the community.',
\sprintf("[url=%s]%s[/url], We've been expecting you.", $profileUrl, $user->username),
\sprintf("[url=%s]%s[/url] has arrived. Party's over.", $profileUrl, $user->username),
\sprintf("It's a bird! It's a plane! Nevermind, it's just [url=%s]%s[/url].", $profileUrl, $user->username),
\sprintf('Ready player [url=%s]%s[/url].', $profileUrl, $user->username),
\sprintf('A wild [url=%s]%s[/url] appeared.', $profileUrl, $user->username),
'Welcome to '.config('other.title').\sprintf(' [url=%s]%s[/url]. We were expecting you.', $profileUrl, $user->username),
];

$this->chatRepository->systemMessage(
$welcomeArray[array_rand($welcomeArray)]
);

// Send Welcome PM
$user->sendSystemNotification(
subject: config('welcomepm.subject'),
message: config('welcomepm.message'),
);

return $user;
}
}
47 changes: 47 additions & 0 deletions app/Listeners/RegisteredListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace App\Listeners;

use App\Models\User;
use App\Repositories\ChatRepository;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Arr;

readonly class RegisteredListener
{
public function __construct(private ChatRepository $chatRepository)
{
}

public function __invoke(Registered $event): void
{
/** @var User $user */
$user = $event->user;

$this->chatRepository->systemMessage($this->getWelcomeMessage($user));

// Send Welcome PM
$user->sendSystemNotification(
subject: config('welcomepm.subject'),
message: config('welcomepm.message'),
);
}

private function getWelcomeMessage(User $user): string
{
// Select A Random Welcome Message
$profileUrl = href_profile($user);

return Arr::random([
\sprintf('[url=%s]%s[/url], Welcome to ', $profileUrl, $user->username).config('other.title').'! Hope you enjoy the community.',
\sprintf("[url=%s]%s[/url], We've been expecting you.", $profileUrl, $user->username),
\sprintf("[url=%s]%s[/url] has arrived. Party's over.", $profileUrl, $user->username),
\sprintf("It's a bird! It's a plane! Nevermind, it's just [url=%s]%s[/url].", $profileUrl, $user->username),
\sprintf('Ready player [url=%s]%s[/url].', $profileUrl, $user->username),
\sprintf('A wild [url=%s]%s[/url] appeared.', $profileUrl, $user->username),
'Welcome to '.config('other.title').\sprintf(' [url=%s]%s[/url]. We were expecting you.', $profileUrl, $user->username),
]);
}
}
6 changes: 6 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
use App\Listeners\NotifyUserTicketWasClosed;
use App\Listeners\NotifyUserTicketWasCreated;
use App\Listeners\PasswordProtectBackup;
use App\Listeners\RegisteredListener;
use Assada\Achievements\Event\Unlocked;
use Illuminate\Auth\Events\Login;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Spatie\Backup\Events\BackupZipWasCreated;

Expand All @@ -51,6 +53,10 @@ class EventServiceProvider extends ServiceProvider
LoginListener::class,
],

Registered::class => [
RegisteredListener::class,
],

// Achievements System
Unlocked::class => [
AchievementUnlocked::class,
Expand Down
55 changes: 17 additions & 38 deletions tests/Feature/Actions/Fortify/CreateNewUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,44 +14,37 @@
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

use App\Models\Bot;
use App\Models\User;
use App\Models\Chatroom;
use App\Models\Invite;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Event;

use function Pest\Laravel\assertDatabaseHas;
use function Pest\Laravel\assertDatabaseMissing;

beforeEach(function (): void {
Event::fake(Registered::class);
});

test('user registration is not available when disabled', function (): void {
User::factory()->system()->create();
$this->withoutMiddleware();
config(['other.invite-only' => true]);

$this->get('/register')
->assertOk()
->assertSeeText('Open Registration Is Disabled')
;
->assertSeeText('Open Registration Is Disabled');
Event::assertNotDispatched(Registered::class);
});

test('user registration is available when enabled', function (): void {
User::factory()->system()->create();
$this->withoutMiddleware();
config([
'other.invite-only' => false,
'captcha.enabled' => false,
]);

Bot::factory()->create([
'command' => 'Systembot',
]);
Chatroom::factory()->create([
'name' => config('chat.system_chatroom'),
]);

$this->get('/register')
->assertOk()
->assertDontSeeText('Open Registration Is Disabled')
;
->assertDontSeeText('Open Registration Is Disabled');

$this->post('/register', [
'username' => 'testuser',
Expand All @@ -64,10 +57,10 @@
'username' => 'testuser',
'email' => '[email protected]',
]);
Event::assertDispatched(Registered::class);
});

test('user can register using invite code', function (): void {
User::factory()->system()->create();
$this->withoutMiddleware();
config([
'other.invite-only' => true,
Expand All @@ -81,19 +74,11 @@
'expires_on' => now()->addDays(7),
]);

Bot::factory()->create([
'command' => 'Systembot',
]);
Chatroom::factory()->create([
'name' => config('chat.system_chatroom'),
]);

$email = fake()->safeEmail;

$this->get('/register?code=testcode')
->assertOk()
->assertDontSeeText('Open Registration Is Disabled')
;
->assertDontSeeText('Open Registration Is Disabled');

$this->post('/register?code=testcode', [
'username' => 'testuser',
Expand All @@ -108,6 +93,7 @@
'username' => 'testuser',
'email' => $email,
]);
Event::assertDispatched(Registered::class);
});

test('user cannot register using invalid invite code', function (): void {
Expand All @@ -121,8 +107,7 @@

$this->get('/register?code=testcode')
->assertOk()
->assertDontSeeText('Open Registration Is Disabled')
;
->assertDontSeeText('Open Registration Is Disabled');

$this->post('/register?code=testcode', [
'username' => 'testuser',
Expand All @@ -137,10 +122,10 @@
'username' => 'testuser',
'email' => $email,
]);
Event::assertNotDispatched(Registered::class);
});

test('user can register using invite code with internal note assigned', function (): void {
User::factory()->system()->create();
$this->withoutMiddleware();
config([
'other.invite-only' => true,
Expand All @@ -155,19 +140,11 @@
'internal_note' => 'This is a test note',
]);

Bot::factory()->create([
'command' => 'Systembot',
]);
Chatroom::factory()->create([
'name' => config('chat.system_chatroom'),
]);

$email = fake()->safeEmail;

$this->get('/register?code=testcode')
->assertOk()
->assertDontSeeText('Open Registration Is Disabled')
;
->assertDontSeeText('Open Registration Is Disabled');

$this->post('/register?code=testcode', [
'username' => 'testuser',
Expand All @@ -190,4 +167,6 @@
'staff_id' => $invite->user_id,
'user_id' => $invite->accepted_by,
]);

Event::assertDispatched(Registered::class);
});
55 changes: 55 additions & 0 deletions tests/Unit/Listeners/RegisteredListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <[email protected]>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/

use App\Models\Bot;
use App\Models\Chatroom;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Support\Facades\Event;

use function Pest\Laravel\assertDatabaseHas;

test('newly registered user is greeted in chat room', function (): void {
User::factory()->system()->create();
$bot = Bot::factory()->create([
'command' => 'Systembot',
]);
$chatroom = Chatroom::factory()->create([
'name' => config('chat.system_chatroom'),
]);

$user = User::factory()->create();

Event::dispatch(new Registered($user));

assertDatabaseHas('messages', [
'user_id' => User::SYSTEM_USER_ID,
'chatroom_id' => $chatroom->id,
'bot_id' => $bot->id,
'receiver_id' => null,
]);

$conversation = $user->conversations->first();

expect($conversation)->not->toBeNull()
->and($conversation->subject)->toBe(config('welcomepm.subject'));

$systemMessage = $conversation->messages->first();

expect($systemMessage)->not->toBeNull()
->and($systemMessage->message)->toBe(config('welcomepm.message'))
->and($systemMessage->sender_id)->toBe(User::SYSTEM_USER_ID);
});
Loading