diff --git a/app/Actions/Schools/SetHeadDirector.php b/app/Actions/Schools/SetHeadDirector.php index 75377e7..e699061 100644 --- a/app/Actions/Schools/SetHeadDirector.php +++ b/app/Actions/Schools/SetHeadDirector.php @@ -3,7 +3,6 @@ namespace App\Actions\Schools; use App\Exceptions\AuditionAdminException; -use App\Models\School; use App\Models\User; use function auditionLog; @@ -15,9 +14,9 @@ class SetHeadDirector { } - public function __invoke(User $user, School $school): void + public function __invoke(User $user): void { - $this->setHeadDirector($user, $school); + $this->setHeadDirector($user); } /** @@ -25,6 +24,10 @@ class SetHeadDirector */ public function setHeadDirector(User $user): void { + if (! User::where('id', $user->id)->exists()) { + throw new AuditionAdminException('User does not exist'); + } + if (is_null($user->school_id)) { throw new AuditionAdminException('User is not associated with a school'); } diff --git a/tests/Feature/app/Actions/Schools/SetHeadDirectorTest.php b/tests/Feature/app/Actions/Schools/SetHeadDirectorTest.php new file mode 100644 index 0000000..5d6aa90 --- /dev/null +++ b/tests/Feature/app/Actions/Schools/SetHeadDirectorTest.php @@ -0,0 +1,61 @@ +school = School::factory()->create(); + $this->user = User::factory()->create(); +}); + +it('makes a user the head director at a school', function () { + $this->user->school_id = $this->school->id; + $this->user->save(); + $promoter = app(SetHeadDirector::class); + $promoter($this->user); + expect($this->user->hasFlag('head_director'))->toBeTrue(); +}); + +it('throws an exception if the user does not exist', function () { + $newUser = User::factory()->make(); + $promoter = app(SetHeadDirector::class); + $promoter($newUser); +})->throws(AuditionAdminException::class, 'User does not exist'); + +it('throws an exception if the user is not associated with a school', function () { + $promoter = app(SetHeadDirector::class); + $promoter($this->user); +})->throws(AuditionAdminException::class, 'User is not associated with a school'); + +it('removes any other head directors at their school', function () { + $this->user->school_id = $this->school->id; + $this->user->save(); + $otherUser = User::factory()->create(); + $otherUser->school_id = $this->school->id; + $otherUser->save(); + $promoter = app(SetHeadDirector::class); + $promoter($this->user); + $this->user->refresh(); + $otherUser->refresh(); + expect($otherUser->hasFlag('head_director'))->toBeFalse() + ->and($this->user->hasFlag('head_director'))->toBeTrue(); + $promoter($otherUser); + $this->user->refresh(); + $otherUser->refresh(); + expect($otherUser->hasFlag('head_director'))->toBeTrue() + ->and($this->user->hasFlag('head_director'))->toBeFalse(); +}); + +it('logs the head director promotion', function () { + $this->user->school_id = $this->school->id; + $this->user->save(); + $promoter = app(SetHeadDirector::class); + $promoter($this->user); + $logEntry = \App\Models\AuditLogEntry::latest()->first(); + expect($logEntry->message)->toEqual('Set '.$this->user->full_name().' as head director at '.$this->school->name); +});