diff --git a/app/Models/User.php b/app/Models/User.php index 3c0e6fce..958901c1 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -90,18 +90,13 @@ class User extends Authenticatable implements HasLocalePreference, WebAuthnAuthe ]; /** - * Perform any actions required after the model boots. + * User exposed observable events. * - * @return void + * These are extra user-defined events observers may subscribe to. + * + * @var array */ - protected static function booted() - { - static::creating(function (User $user) { - $user->addObservableEvents([ - 'demoting' - ]); - }); - } + protected $observables = ['demoting']; /** * Get the user's preferred locale. @@ -137,7 +132,9 @@ public function isAdministrator() */ public function promoteToAdministrator(bool $promote = true) : bool { - if ($promote == false && $this->fireModelEvent('demoting') === false) { + $eventResult = $promote ? $this->fireModelEvent('promoting') : $this->fireModelEvent('demoting'); + + if ($promote == false && $eventResult === false) { return false; } diff --git a/tests/Feature/Models/UserModelTest.php b/tests/Feature/Models/UserModelTest.php index b779a3f2..5b78bcdf 100644 --- a/tests/Feature/Models/UserModelTest.php +++ b/tests/Feature/Models/UserModelTest.php @@ -73,11 +73,15 @@ public function test_promoteToAdministrator_sets_administrator_status() */ public function test_promoteToAdministrator_demote_administrator_status() { - $user = User::factory()->administrator()->create(); + $admin = User::factory()->administrator()->create(); + // We need another admin to prevent demoting event returning false + // and blocking the demotion + $another_admin = User::factory()->administrator()->create(); - $user->promoteToAdministrator(false); + $admin->promoteToAdministrator(false); + $admin->save(); - $this->assertEquals($user->isAdministrator(), false); + $this->assertFalse($admin->isAdministrator()); } /**