AddSchoolEmailDomain action and test.
This commit is contained in:
parent
9ae4b0388a
commit
fa3df80e3c
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace App\Actions\Schools;
|
||||
|
||||
use App\Exceptions\AuditionAdminException;
|
||||
use App\Models\School;
|
||||
use App\Models\SchoolEmailDomain;
|
||||
|
||||
class AddSchoolEmailDomain
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
}
|
||||
|
||||
public function __invoke(School $school, string $domain): void
|
||||
{
|
||||
$this->addDomain($school, $domain);
|
||||
}
|
||||
|
||||
public function addDomain(School $school, string $domain): void
|
||||
{
|
||||
if (! School::where('id', $school->id)->exists()) {
|
||||
throw new AuditionAdminException('School does not exist');
|
||||
}
|
||||
|
||||
if (SchoolEmailDomain::where('domain', $domain)->where('school_id', $school->id)->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
SchoolEmailDomain::create([
|
||||
'domain' => $domain,
|
||||
'school_id' => $school->id,
|
||||
]);
|
||||
$message = 'Added the email domain '.$domain.' to school '.$school->name;
|
||||
$affected = ['schools' => [$school->id]];
|
||||
auditionLog($message, $affected);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
use App\Actions\Schools\AddSchoolEmailDomain;
|
||||
use App\Exceptions\AuditionAdminException;
|
||||
use App\Models\AuditLogEntry;
|
||||
use App\Models\School;
|
||||
use App\Models\SchoolEmailDomain;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
actAsAdmin();
|
||||
$this->secretary = app(AddSchoolEmailDomain::class);
|
||||
$this->school = School::factory()->create();
|
||||
$this->domain = fake()->domainName();
|
||||
});
|
||||
|
||||
it('adds a domain to a school', function () {
|
||||
($this->secretary)($this->school, $this->domain);
|
||||
expect(SchoolEmailDomain::where('school_id', $this->school->id)
|
||||
->where('domain', $this->domain)->exists())->toBeTrue();
|
||||
});
|
||||
|
||||
it('logs the addition of the domain', function () {
|
||||
($this->secretary)($this->school, $this->domain);
|
||||
$logEntry = AuditLogEntry::first();
|
||||
expect($logEntry->message)->toEqual('Added the email domain '.$this->domain.' to school '.$this->school->name);
|
||||
});
|
||||
|
||||
it('throws an exception if the school does not exist', function () {
|
||||
$newSchool = School::factory()->make();
|
||||
$this->secretary->addDomain($newSchool, $this->domain);
|
||||
})->throws(AuditionAdminException::class, 'School does not exist');
|
||||
|
||||
it('silently continues if the domain is already added to the school', function () {
|
||||
SchoolEmailDomain::create(['school_id' => $this->school->id, 'domain' => $this->domain]);
|
||||
($this->secretary)($this->school, $this->domain);
|
||||
expect(SchoolEmailDomain::where('school_id', $this->school->id)
|
||||
->where('domain', $this->domain)->exists())->toBeTrue()
|
||||
->and(AuditLogEntry::count())->toEqual(0);
|
||||
});
|
||||
Loading…
Reference in New Issue