From fa3df80e3cc352270d1c204cedc5fae7443748e3 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Tue, 1 Jul 2025 17:33:44 -0500 Subject: [PATCH] AddSchoolEmailDomain action and test. --- app/Actions/Schools/AddSchoolEmailDomain.php | 38 +++++++++++++++++ .../Schools/AddSchoolEmailDomainTest.php | 42 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 app/Actions/Schools/AddSchoolEmailDomain.php create mode 100644 tests/Feature/app/Actions/Schools/AddSchoolEmailDomainTest.php diff --git a/app/Actions/Schools/AddSchoolEmailDomain.php b/app/Actions/Schools/AddSchoolEmailDomain.php new file mode 100644 index 0000000..586fa7f --- /dev/null +++ b/app/Actions/Schools/AddSchoolEmailDomain.php @@ -0,0 +1,38 @@ +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); + } +} diff --git a/tests/Feature/app/Actions/Schools/AddSchoolEmailDomainTest.php b/tests/Feature/app/Actions/Schools/AddSchoolEmailDomainTest.php new file mode 100644 index 0000000..00a1b12 --- /dev/null +++ b/tests/Feature/app/Actions/Schools/AddSchoolEmailDomainTest.php @@ -0,0 +1,42 @@ +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); +});