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); });