Skip to content

Commit

Permalink
Volunteer tests - admin platform
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Apr 11, 2024
1 parent 4ff1b8a commit 9097812
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 4 deletions.
17 changes: 17 additions & 0 deletions database/factories/OrganisationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,23 @@ public function withUserAndDocuments()
});
}

public function withUserAndVolunteers()
{
return $this->afterCreating(function (Organisation $organisation) {
// var_dump($organisation->email, $organisation->id, User::all()->pluck('email'));
// dd(Organisation::all()->pluck('email', 'id'), $organisation);
// User::factory(['email' => $organisation->email])
// ->orgAdmin()
// ->for($organisation)
// ->create();

Volunteer::factory()
->for($organisation)
->count(5)
->create();
});
}

protected function attachLocationByActivityArea(Organisation $organisation): void
{
$counties = null;
Expand Down
8 changes: 4 additions & 4 deletions tests/Feature/Documents/AdminPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
use App\Filament\Resources\DocumentResource\Pages\ViewDocument;
use App\Models\Document;
use App\Models\Organisation;
use App\Models\User;
use Livewire;
use Tests\Traits\ActingAsPlatformAdmin;

class AdminPlatformTest extends DocumentsBaseTest
{
use ActingAsPlatformAdmin;

protected function setUp(): void
{
parent::setUp();
$this->user = User::factory()
->platformAdmin()
->create();
$this->user = $this->getUser();
Livewire::actingAs($this->user);

$this->createOrganisations(3, 'random');
Expand Down
189 changes: 189 additions & 0 deletions tests/Feature/Volunteers/AdminPlatformTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Volunteers;

use App\Enum\VolunteerSpecialization;
use App\Filament\Resources\VolunteerResource\Pages\EditVolunteer;
use App\Filament\Resources\VolunteerResource\Pages\ListVolunteers;
use App\Filament\Resources\VolunteerResource\Pages\ViewVolunteer;
use App\Models\Organisation;
use App\Models\Volunteer;
use Tests\Traits\ActingAsPlatformAdmin;

class AdminPlatformTest extends VolunteersBaseTest
{
use ActingAsPlatformAdmin;

protected function setUp(): void
{
parent::setUp();
$this->actingAsPlatformAdmin();
}

public function testPlatformAdminCanViewVolunteers()
{
$volunteers = Volunteer::query()
->orderByDesc('id')
->limit(10)
->get();

$organisation = Organisation::query()
->with('volunteers')
->inRandomOrder()
->first();

$county = $volunteers->first()->county;
$volunteersFromCounty = Volunteer::query()
->where('county_id', $county->id)
->orderByDesc('id')
->limit(10)
->get();

$specialization = fake()->randomElements(VolunteerSpecialization::values());
$specializationVolunteers = Volunteer::query()
->whereJsonContains('specializations', $specialization)
->orderByDesc('id')
->limit(10)
->get();

$hasFirstAid = fake()->boolean;
$firstAidVolunteers = Volunteer::query()
->where('has_first_aid_accreditation', $hasFirstAid)
->orderByDesc('id')
->limit(10)
->get();

\Livewire::test(ListVolunteers::class)
->assertSuccessful()
->assertCountTableRecords(25)
->assertCanSeeTableRecords($volunteers)
->assertCanRenderTableColumn('organisation.name')
->assertCanRenderTableColumn('full_name')
->assertCanRenderTableColumn('email')
->assertCanRenderTableColumn('phone')
->assertCanRenderTableColumn('specializations')
->assertCanRenderTableColumn('has_first_aid_accreditation')
->filterTable('organisation', $organisation->id)
->assertCanSeeTableRecords($organisation->volunteers)
->resetTableFilters()
->filterTable('county', $county->id)
->assertCanSeeTableRecords($volunteersFromCounty)
->resetTableFilters()
->filterTable('specializations', $specialization)
->assertCanSeeTableRecords($specializationVolunteers)
->resetTableFilters()
->filterTable('has_first_aid_accreditation', $hasFirstAid)
->assertCanSeeTableRecords($firstAidVolunteers)
->assertPageActionVisible('create')
->assertPageActionEnabled('create');
}

public function testAdminPlatformCanViewVolunteer()
{
$volunteer = Volunteer::query()
->whereJsonDoesntContain('specializations', VolunteerSpecialization::translator)
->inRandomOrder()
->first();

\Livewire::test(ViewVolunteer::class, ['record' => $volunteer->id])
->assertSuccessful()
->assertFormFieldIsVisible('organisation_id')
->assertFormFieldIsDisabled('organisation_id')
->assertFormFieldIsVisible('first_name')
->assertFormFieldIsDisabled('first_name')
->assertFormFieldIsVisible('last_name')
->assertFormFieldIsDisabled('last_name')
->assertFormFieldIsVisible('email')
->assertFormFieldIsDisabled('email')
->assertFormFieldIsVisible('phone')
->assertFormFieldIsDisabled('phone')
->assertFormFieldIsVisible('cnp')
->assertFormFieldIsDisabled('cnp')
->assertFormFieldIsVisible('role')
->assertFormFieldIsDisabled('role')
->assertFormFieldIsVisible('specializations')
->assertFormFieldIsDisabled('specializations')
->assertFormFieldIsVisible('has_first_aid_accreditation')
->assertFormFieldIsDisabled('has_first_aid_accreditation')
->assertFormFieldIsVisible('county_id')
->assertFormFieldIsDisabled('county_id')
->assertFormFieldIsVisible('city_id')
->assertFormFieldIsDisabled('city_id')
->assertPageActionVisible('edit')
->assertPageActionEnabled('edit')
->assertFormFieldIsHidden('language');

$volunteerTranslator = Volunteer::query()
->whereJsonContains('specializations', VolunteerSpecialization::translator)
->inRandomOrder()
->first();

\Livewire::test(ViewVolunteer::class, ['record' => $volunteerTranslator->id])
->assertSuccessful()
->assertFormFieldIsVisible('organisation_id')
->assertFormFieldIsDisabled('organisation_id')
->assertFormFieldIsVisible('first_name')
->assertFormFieldIsDisabled('first_name')
->assertFormFieldIsVisible('last_name')
->assertFormFieldIsDisabled('last_name')
->assertFormFieldIsVisible('email')
->assertFormFieldIsDisabled('email')
->assertFormFieldIsVisible('phone')
->assertFormFieldIsDisabled('phone')
->assertFormFieldIsVisible('cnp')
->assertFormFieldIsDisabled('cnp')
->assertFormFieldIsVisible('role')
->assertFormFieldIsDisabled('role')
->assertFormFieldIsVisible('specializations')
->assertFormFieldIsDisabled('specializations')
->assertFormFieldIsVisible('language')
->assertFormFieldIsDisabled('language')
->assertFormFieldIsVisible('has_first_aid_accreditation')
->assertFormFieldIsDisabled('has_first_aid_accreditation')
->assertFormFieldIsVisible('county_id')
->assertFormFieldIsDisabled('county_id')
->assertFormFieldIsVisible('city_id')
->assertFormFieldIsDisabled('city_id')
->assertPageActionVisible('edit')
->assertPageActionEnabled('edit');
}

public function testAdminPlatformCanEditVolunteer()
{
$volunteer = Volunteer::query()
->whereJsonDoesntContain('specializations', VolunteerSpecialization::translator)
->inRandomOrder()
->first();

\Livewire::test(EditVolunteer::class, ['record' => $volunteer->id])
->assertSuccessful()
->assertFormFieldIsVisible('organisation_id')
->assertFormFieldIsEnabled('organisation_id')
->assertFormFieldIsVisible('first_name')
->assertFormFieldIsEnabled('first_name')
->assertFormFieldIsVisible('last_name')
->assertFormFieldIsEnabled('last_name')
->assertFormFieldIsVisible('email')
->assertFormFieldIsEnabled('email')
->assertFormFieldIsVisible('phone')
->assertFormFieldIsEnabled('phone')
->assertFormFieldIsVisible('cnp')
->assertFormFieldIsEnabled('cnp')
->assertFormFieldIsVisible('role')
->assertFormFieldIsEnabled('role')
->assertFormFieldIsVisible('specializations')
->assertFormFieldIsEnabled('specializations')
->assertFormFieldIsHidden('language')
// ->assertFormFieldIsDisabled('language')
->assertFormFieldIsVisible('has_first_aid_accreditation')
->assertFormFieldIsEnabled('has_first_aid_accreditation')
->assertFormFieldIsVisible('county_id')
->assertFormFieldIsEnabled('county_id')
->assertFormFieldIsVisible('city_id')
->assertFormFieldIsEnabled('city_id')

;
}
}
44 changes: 44 additions & 0 deletions tests/Feature/Volunteers/VolunteersBaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Tests\Feature\Volunteers;

use App\Enum\DocumentType;
use App\Enum\OrganisationStatus;
use App\Enum\UserRole;
use App\Filament\Resources\DocumentResource;
use App\Models\Document;
use App\Models\Organisation;
use App\Models\User;
use Database\Seeders\ResourceCategorySeed;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Notification;
use Livewire;
use Tests\TestCase;

abstract class VolunteersBaseTest extends TestCase
{
use RefreshDatabase;

protected string $seeder = ResourceCategorySeed::class;

protected User $user;

protected function setUp(): void
{
parent::setUp();

Notification::fake();
$this->createOrganisationsWithVolunteers();
}

protected function createOrganisationsWithVolunteers()
{
Organisation::factory()
->count(5)
->withUserAndVolunteers()
->create();
}
}
20 changes: 20 additions & 0 deletions tests/Traits/ActingAsPlatformAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Tests\Traits;

use App\Models\User;

trait ActingAsPlatformAdmin
{
public function getUser(): User
{
return User::factory()
->platformAdmin()
->create();
}

public function actingAsPlatformAdmin(): void
{
$this->actingAs($this->getUser());
}
}

0 comments on commit 9097812

Please sign in to comment.