From 3ba70bea893cb5e7d5a27835abee139b7bf150d9 Mon Sep 17 00:00:00 2001 From: Matt Young Date: Mon, 1 Jul 2024 14:07:22 -0500 Subject: [PATCH] Model User Test --- app/Models/User.php | 9 +- tests/Feature/Models/UserTest.php | 148 ++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 tests/Feature/Models/UserTest.php diff --git a/app/Models/User.php b/app/Models/User.php index eb96143..361da51 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -10,6 +10,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasManyThrough; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Collection; class User extends Authenticatable implements MustVerifyEmail { @@ -131,7 +132,7 @@ class User extends Authenticatable implements MustVerifyEmail * * @return SchoolEmailDomain[] */ - public function possibleSchools() + public function possibleSchools(): Collection { if ($this->school_id) { $return[] = $this->school; @@ -148,7 +149,11 @@ class User extends Authenticatable implements MustVerifyEmail return true; } - return $this->is_tab; + if ($this->is_tab) { + return true; + } + + return false; } public function scoreSheets(): HasMany diff --git a/tests/Feature/Models/UserTest.php b/tests/Feature/Models/UserTest.php new file mode 100644 index 0000000..82baa6d --- /dev/null +++ b/tests/Feature/Models/UserTest.php @@ -0,0 +1,148 @@ +user = User::factory()->create([ + 'first_name' => 'Bandit', + 'last_name' => 'Heeler', + 'email' => 'bheeler@calypso.null', + ]); +}); + +it('formats a full name and can put last first if needed', function () { + expect($this->user->full_name())->toBe('Bandit Heeler') + ->and($this->user->full_name(true))->toBe('Heeler, Bandit'); +}); + +it('formats a short name with the first initial of first name and full last name', function () { + expect($this->user->short_name())->toBe('B. Heeler'); +}); + +it('checks if it has a school', function () { + expect($this->user->has_school())->toBeFalse(); + $school = School::factory()->create(['name' => 'Calypso School']); + $this->user->school_id = $school->id; + $this->user->save(); + expect($this->user->has_school())->toBeTrue(); +}); + +it('has a school', function () { + // Arrange + $school = School::factory()->create(['name' => 'Calypso School']); + $this->user->school_id = $school->id; + $this->user->save(); + // Act & Assert + expect($this->user->school->name)->toBe('Calypso School') + ->and($this->user->school)->toBeInstanceOf(School::class); + +}); + +it('has an email domain', function () { + expect($this->user->emailDomain())->toBe('calypso.null'); +}); + +it('has students', function () { + // Arrange + $school = School::factory()->create(['name' => 'Calypso School']); + $this->user->school_id = $school->id; + $this->user->save(); + Student::factory()->count(5)->create(['school_id' => $school->id]); + // Act & Assert + expect($this->user->students->count())->toBe(5) + ->and($this->user->students->first())->toBeInstanceOf(Student::class); + +}); + +it('has entries', function () { + // Arrange + $school = School::factory()->create(['name' => 'Calypso School']); + $this->user->school_id = $school->id; + $this->user->save(); + $students = Student::factory()->count(5)->create(['school_id' => $school->id]); + foreach ($students as $student) { + Entry::factory()->count(2)->create(['student_id' => $student->id]); + } + // Act & Assert + expect($this->user->entries->count())->toBe(10) + ->and($this->user->entries->first())->toBeInstanceOf(Entry::class); +}); + +it('has rooms also called judgingAssignments', function () { + // Arrange + $rooms = Room::factory()->count(3)->create(); + foreach ($rooms as $room) { + $room->addJudge($this->user); + } + // Act & Assert + expect($this->user->rooms->count())->toBe(3) + ->and($this->user->rooms->first())->toBeInstanceOf(Room::class) + ->and($this->user->judgingAssignments->count())->toBe(3) + ->and($this->user->judgingAssignments->first())->toBeInstanceOf(Room::class); +}); + +it('has advancement votes', function () { + // Arrange + $entries = Entry::factory()->count(5)->create(); + foreach ($entries as $entry) { + JudgeAdvancementVote::create([ + 'user_id' => $this->user->id, + 'entry_id' => $entry->id, + 'vote' => fake()->randomElement(['yes', 'no', 'dq']), + ]); + } + // Act & Assert + expect($this->user->advancementVotes->count())->toBe(5) + ->and($this->user->advancementVotes->first())->toBeInstanceOf(JudgeAdvancementVote::class); +}); + +it('knows if it is a judge', function () { + expect($this->user->isJudge())->toBeFalse(); + Room::factory()->create()->addJudge($this->user); + $this->user->load('judgingAssignments'); + expect($this->user->isJudge())->toBeTrue(); +}); + +it('knows what schools share its email domain', function () { + // Arrange + $school1 = School::factory()->create(['name' => 'Calypso Elementary School']); + $school2 = School::factory()->create(['name' => 'Calypso Middle School']); + $otherSchools = School::factory()->count(4)->create(); + SchoolEmailDomain::create([ + 'school_id' => $school1->id, + 'domain' => 'calypso.null', + ]); + SchoolEmailDomain::create([ + 'school_id' => $school2->id, + 'domain' => 'calypso.null', + ]); + foreach ($otherSchools as $school) { + SchoolEmailDomain::create([ + 'school_id' => $school->id, + 'domain' => fake()->domainName, + ]); + } + // Act & Assert + expect($this->user->possibleSchools())->toBeInstanceOf(Collection::class) + ->and($this->user->possibleSchools()->count())->toBe(2) + ->and($this->user->possibleSchools()->first())->toBeInstanceOf(SchoolEmailDomain::class) + ->and($this->user->possibleSchools()->first()->school)->toBeInstanceOf(School::class) + ->and($this->user->possibleSchools()->first()->school->name)->toBe('Calypso Elementary School'); +}); + +it('knows if it is a score tabulator', function () { + expect($this->user->canTab())->toBeFalse() + ->and(User::factory()->tab()->create())->canTab()->toBe(true) + ->and(User::factory()->admin()->create())->canTab()->toBe(true); + +});