Fix demoting event not being registered (again) - Complete #331

This commit is contained in:
Bubka 2024-04-29 13:11:23 +02:00
parent be3aaf319c
commit bdfc70732d
2 changed files with 15 additions and 14 deletions

View File

@ -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() protected $observables = ['demoting'];
{
static::creating(function (User $user) {
$user->addObservableEvents([
'demoting'
]);
});
}
/** /**
* Get the user's preferred locale. * Get the user's preferred locale.
@ -137,7 +132,9 @@ public function isAdministrator()
*/ */
public function promoteToAdministrator(bool $promote = true) : bool 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; return false;
} }

View File

@ -73,11 +73,15 @@ public function test_promoteToAdministrator_sets_administrator_status()
*/ */
public function test_promoteToAdministrator_demote_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());
} }
/** /**