|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backend\Tests\Models; |
| 4 | + |
| 5 | +use Backend\Models\User; |
| 6 | +use Backend\Models\UserRole; |
| 7 | +use System\Tests\Bootstrap\PluginTestCase; |
| 8 | +use Winter\Storm\Auth\AuthorizationException; |
| 9 | +use Winter\Storm\Database\Model as Eloquent; |
| 10 | + |
| 11 | +class UserTest extends PluginTestCase |
| 12 | +{ |
| 13 | + protected User $superuser; |
| 14 | + protected User $admin; |
| 15 | + protected User $lowPriv; |
| 16 | + |
| 17 | + public function setUp(): void |
| 18 | + { |
| 19 | + parent::setUp(); |
| 20 | + |
| 21 | + Eloquent::unguarded(function () { |
| 22 | + $developerRole = UserRole::where('code', UserRole::CODE_DEVELOPER)->first(); |
| 23 | + $publisherRole = UserRole::where('code', UserRole::CODE_PUBLISHER)->first(); |
| 24 | + |
| 25 | + $this->superuser = User::create([ |
| 26 | + 'email' => 'superuser@test.com', |
| 27 | + 'login' => 'superuser', |
| 28 | + 'password' => 'Testing123!', |
| 29 | + 'password_confirmation' => 'Testing123!', |
| 30 | + 'first_name' => 'Super', |
| 31 | + 'last_name' => 'User', |
| 32 | + 'is_superuser' => true, |
| 33 | + 'is_activated' => true, |
| 34 | + 'role_id' => $developerRole->id, |
| 35 | + 'permissions' => [], |
| 36 | + ]); |
| 37 | + |
| 38 | + $this->admin = User::create([ |
| 39 | + 'email' => 'admin@test.com', |
| 40 | + 'login' => 'admin_user', |
| 41 | + 'password' => 'Testing123!', |
| 42 | + 'password_confirmation' => 'Testing123!', |
| 43 | + 'first_name' => 'Admin', |
| 44 | + 'last_name' => 'User', |
| 45 | + 'is_superuser' => false, |
| 46 | + 'is_activated' => true, |
| 47 | + 'role_id' => $publisherRole->id, |
| 48 | + 'permissions' => ['backend.manage_users' => 1], |
| 49 | + ]); |
| 50 | + |
| 51 | + $this->lowPriv = User::create([ |
| 52 | + 'email' => 'lowpriv@test.com', |
| 53 | + 'login' => 'lowpriv', |
| 54 | + 'password' => 'Testing123!', |
| 55 | + 'password_confirmation' => 'Testing123!', |
| 56 | + 'first_name' => 'Low', |
| 57 | + 'last_name' => 'Priv', |
| 58 | + 'is_superuser' => false, |
| 59 | + 'is_activated' => true, |
| 60 | + 'role_id' => null, |
| 61 | + 'permissions' => [], |
| 62 | + ]); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + // ---- Denied operations ---- |
| 67 | + |
| 68 | + public function testSelfEditRoleThrows() |
| 69 | + { |
| 70 | + $this->actingAs($this->admin); |
| 71 | + |
| 72 | + $this->admin->role_id = $this->admin->role_id + 1; |
| 73 | + |
| 74 | + $this->expectException(AuthorizationException::class); |
| 75 | + $this->admin->save(); |
| 76 | + } |
| 77 | + |
| 78 | + public function testSelfEditSuperuserThrows() |
| 79 | + { |
| 80 | + $this->actingAs($this->admin); |
| 81 | + |
| 82 | + $this->admin->is_superuser = true; |
| 83 | + |
| 84 | + $this->expectException(AuthorizationException::class); |
| 85 | + $this->admin->save(); |
| 86 | + } |
| 87 | + |
| 88 | + public function testSelfEditPermissionsThrows() |
| 89 | + { |
| 90 | + $this->actingAs($this->admin); |
| 91 | + |
| 92 | + $this->admin->permissions = ['backend.manage_users' => 1, 'backend.access_dashboard' => 1]; |
| 93 | + |
| 94 | + $this->expectException(AuthorizationException::class); |
| 95 | + $this->admin->save(); |
| 96 | + } |
| 97 | + |
| 98 | + public function testNonSuperuserGrantSuperuserThrows() |
| 99 | + { |
| 100 | + $this->actingAs($this->admin); |
| 101 | + |
| 102 | + $this->lowPriv->is_superuser = true; |
| 103 | + |
| 104 | + $this->expectException(AuthorizationException::class); |
| 105 | + $this->lowPriv->save(); |
| 106 | + } |
| 107 | + |
| 108 | + public function testNonSuperuserRevokeSuperuserThrows() |
| 109 | + { |
| 110 | + $this->actingAs($this->admin); |
| 111 | + |
| 112 | + $this->superuser->is_superuser = false; |
| 113 | + |
| 114 | + $this->expectException(AuthorizationException::class); |
| 115 | + $this->superuser->save(); |
| 116 | + } |
| 117 | + |
| 118 | + public function testNonSuperuserEditingSuperuserThrows() |
| 119 | + { |
| 120 | + $this->actingAs($this->admin); |
| 121 | + |
| 122 | + $this->superuser->first_name = 'Changed'; |
| 123 | + |
| 124 | + $this->expectException(AuthorizationException::class); |
| 125 | + $this->superuser->save(); |
| 126 | + } |
| 127 | + |
| 128 | + public function testNoManageUsersEditingOtherThrows() |
| 129 | + { |
| 130 | + $this->actingAs($this->lowPriv); |
| 131 | + |
| 132 | + $this->admin->first_name = 'Changed'; |
| 133 | + |
| 134 | + $this->expectException(AuthorizationException::class); |
| 135 | + $this->admin->save(); |
| 136 | + } |
| 137 | + |
| 138 | + public function testNoManageUsersCreatingUserThrows() |
| 139 | + { |
| 140 | + $this->actingAs($this->lowPriv); |
| 141 | + |
| 142 | + $this->expectException(AuthorizationException::class); |
| 143 | + Eloquent::unguarded(function () { |
| 144 | + User::create([ |
| 145 | + 'email' => 'newuser@test.com', |
| 146 | + 'login' => 'newuser', |
| 147 | + 'password' => 'Testing123!', |
| 148 | + 'password_confirmation' => 'Testing123!', |
| 149 | + 'first_name' => 'New', |
| 150 | + 'last_name' => 'User', |
| 151 | + 'is_superuser' => false, |
| 152 | + 'is_activated' => true, |
| 153 | + 'permissions' => [], |
| 154 | + ]); |
| 155 | + }); |
| 156 | + } |
| 157 | + |
| 158 | + // ---- Allowed operations ---- |
| 159 | + |
| 160 | + public function testSelfEditNonProtectedFieldsAllowed() |
| 161 | + { |
| 162 | + $this->actingAs($this->admin); |
| 163 | + |
| 164 | + $this->admin->first_name = 'NewFirst'; |
| 165 | + $this->admin->last_name = 'NewLast'; |
| 166 | + $this->admin->email = 'newemail@test.com'; |
| 167 | + $this->admin->save(); |
| 168 | + |
| 169 | + $this->admin->refresh(); |
| 170 | + $this->assertEquals('NewFirst', $this->admin->first_name); |
| 171 | + $this->assertEquals('NewLast', $this->admin->last_name); |
| 172 | + $this->assertEquals('newemail@test.com', $this->admin->email); |
| 173 | + } |
| 174 | + |
| 175 | + public function testAdminCanChangeOtherUserRole() |
| 176 | + { |
| 177 | + $this->actingAs($this->admin); |
| 178 | + |
| 179 | + $publisherRole = UserRole::where('code', UserRole::CODE_PUBLISHER)->first(); |
| 180 | + $this->lowPriv->role_id = $publisherRole->id; |
| 181 | + $this->lowPriv->save(); |
| 182 | + |
| 183 | + $this->lowPriv->refresh(); |
| 184 | + $this->assertEquals($publisherRole->id, $this->lowPriv->role_id); |
| 185 | + } |
| 186 | + |
| 187 | + public function testSuperuserCanGrantSuperuser() |
| 188 | + { |
| 189 | + $this->actingAs($this->superuser); |
| 190 | + |
| 191 | + $this->lowPriv->is_superuser = true; |
| 192 | + $this->lowPriv->save(); |
| 193 | + |
| 194 | + $this->lowPriv->refresh(); |
| 195 | + $this->assertTrue((bool) $this->lowPriv->is_superuser); |
| 196 | + } |
| 197 | + |
| 198 | + public function testAdminCanChangeOtherUserPermissions() |
| 199 | + { |
| 200 | + $this->actingAs($this->admin); |
| 201 | + |
| 202 | + $this->lowPriv->permissions = ['backend.access_dashboard' => 1]; |
| 203 | + $this->lowPriv->save(); |
| 204 | + |
| 205 | + $this->lowPriv->refresh(); |
| 206 | + $this->assertEquals(['backend.access_dashboard' => 1], $this->lowPriv->permissions); |
| 207 | + } |
| 208 | + |
| 209 | + public function testNoAuthUserCanModifyAnyField() |
| 210 | + { |
| 211 | + // No actingAs — simulates CLI/artisan/queue context |
| 212 | + $developerRole = UserRole::where('code', UserRole::CODE_DEVELOPER)->first(); |
| 213 | + |
| 214 | + $this->lowPriv->role_id = $developerRole->id; |
| 215 | + $this->lowPriv->is_superuser = true; |
| 216 | + $this->lowPriv->permissions = ['backend.manage_users' => 1]; |
| 217 | + $this->lowPriv->save(); |
| 218 | + |
| 219 | + $this->lowPriv->refresh(); |
| 220 | + $this->assertEquals($developerRole->id, $this->lowPriv->role_id); |
| 221 | + $this->assertTrue((bool) $this->lowPriv->is_superuser); |
| 222 | + $this->assertEquals(['backend.manage_users' => 1], $this->lowPriv->permissions); |
| 223 | + } |
| 224 | +} |
0 commit comments