1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace Tests\Libraries;
7
8use App\Exceptions\ValidationException;
9use App\Libraries\UserRegistration;
10use App\Models\Count;
11use App\Models\User;
12use App\Models\UserCoverPreset;
13use Tests\TestCase;
14
15class UserRegistrationTest extends TestCase
16{
17 public function testBasicFunctionality()
18 {
19 $attrs = $this->basicAttributes();
20 UserCoverPreset::create(['active' => true, 'filename' => 'test']);
21 app('user-cover-presets')->resetMemoized();
22
23 $this->expectCountChange(fn () => User::count(), 1);
24 $this->expectCountChange(fn () => Count::totalUsers()->count, 1);
25 $reg = new UserRegistration($attrs);
26 $thrown = $this->runSubject($reg);
27
28 $this->assertFalse($thrown);
29
30 $user = $reg->user()->fresh();
31 $this->assertNotNull($user->cover()->presetId());
32 $this->assertTrue($user->userGroups->every(fn ($userGroup) =>
33 $userGroup->user_pending === false));
34 }
35
36 public function testRequiresUsername()
37 {
38 $attrs = $this->basicAttributes();
39 unset($attrs['username']);
40
41 $origCount = User::count();
42 $reg = new UserRegistration($attrs);
43 $thrown = $this->runSubject($reg);
44
45 $this->assertTrue($thrown);
46 $this->assertArraySubset(
47 $reg->user()->validationErrors()->all(),
48 [
49 'username' => [osu_trans('model_validation.required', [
50 'attribute' => osu_trans('model_validation.user.attributes.username'),
51 ])],
52 ]
53 );
54 $this->assertSame($origCount, User::count());
55 }
56
57 public function testStoreRequiresEmail()
58 {
59 $attrs = $this->basicAttributes();
60 unset($attrs['user_email']);
61
62 $origCount = User::count();
63 $reg = new UserRegistration($attrs);
64 $thrown = $this->runSubject($reg);
65
66 $this->assertTrue($thrown);
67 $this->assertArraySubset(
68 $reg->user()->validationErrors()->all(),
69 [
70 'user_email' => [osu_trans('model_validation.required', [
71 'attribute' => osu_trans('model_validation.user.attributes.user_email'),
72 ])],
73 ]
74 );
75 $this->assertSame($origCount, User::count());
76 }
77
78 public function testStoreRequiresPassword()
79 {
80 $attrs = $this->basicAttributes();
81 unset($attrs['password']);
82
83 $origCount = User::count();
84 $reg = new UserRegistration($attrs);
85 $thrown = $this->runSubject($reg);
86
87 $this->assertTrue($thrown);
88 $this->assertArraySubset(
89 $reg->user()->validationErrors()->all(),
90 [
91 'password' => [osu_trans('model_validation.required', [
92 'attribute' => osu_trans('model_validation.user.attributes.password'),
93 ])],
94 ]
95 );
96 $this->assertSame($origCount, User::count());
97 }
98
99 // wrapper to catch the exception
100 // so that the contents of validationErrors can be examined.
101 private function runSubject($subject)
102 {
103 try {
104 $subject->save();
105 } catch (ValidationException $e) {
106 return true;
107 }
108
109 return false;
110 }
111
112 private function basicAttributes()
113 {
114 return [
115 'username' => 'user1',
116 'password' => 'hunter22',
117 'user_email' => 'user1@example.com',
118 ];
119 }
120}