auditionadmin/tests/Feature/app/Actions/Schools/SetHeadDirectorTest.php

63 lines
2.2 KiB
PHP

<?php
use App\Actions\Schools\SetHeadDirector;
use App\Exceptions\AuditionAdminException;
use App\Models\AuditLogEntry;
use App\Models\School;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$this->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 = AuditLogEntry::orderBy('id', 'desc')->first();
expect($logEntry->message)->toEqual('Set '.$this->user->full_name().' as head director at '.$this->school->name);
});