Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio committed Sep 9, 2024
1 parent e418e8b commit 5efe70b
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 61 deletions.
30 changes: 18 additions & 12 deletions tests/Feature/Documents/DocumentsBaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
use App\Enum\DocumentType;
use App\Enum\OrganisationStatus;
use App\Enum\UserRole;
use App\Filament\Resources\DocumentResource;
use App\Filament\Resources\DocumentResource\Pages\EditDocument;
use App\Filament\Resources\DocumentResource\Pages\ListDocuments;
use App\Filament\Resources\DocumentResource\Pages\ViewDocument;
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 Livewire\Livewire;
use Livewire\Testing\TestableLivewire;
use Tests\TestCase;

abstract class DocumentsBaseTest extends TestCase
Expand All @@ -33,15 +36,15 @@ protected function setUp(): void
Notification::fake();
}

protected function getOrgAdmin(): Collection
protected function getOrganisationAdmin(): Collection
{
return User::query()
->role(UserRole::ORG_ADMIN)
->inRandomOrder()
->get();
}

protected function getOrgAdminWithInactiveOrg(): User
protected function getOrganisationAdminWithInactiveOrganisation(): User
{
return Organisation::query()
->whereStatus(OrganisationStatus::inactive)
Expand All @@ -60,8 +63,10 @@ protected function createOrganisations(int $count = 3, string $status = 'active'
->inactive()
->withUserAndDocuments()
->createQuietly();

return;
}

if ($status === 'random') {
Organisation::factory()
->count($count)
Expand All @@ -71,17 +76,18 @@ protected function createOrganisations(int $count = 3, string $status = 'active'

return;
}

Organisation::factory()
->count($count)
->withUserAndDocuments()
->createQuietly();
}

public function viewDocuments(): Livewire\Testing\TestableLivewire
public function viewDocuments(): TestableLivewire
{
$documents = Document::all();

return Livewire::test(DocumentResource\Pages\ListDocuments::class)
return Livewire::test(ListDocuments::class)
->assertSuccessful()
->assertCountTableRecords(9)
->assertCanSeeTableRecords($documents)
Expand All @@ -103,9 +109,9 @@ public function viewDocuments(): Livewire\Testing\TestableLivewire
->assertCanSeeTableRecords($documents->sortBy('expires_at'), inOrder: true);
}

public function viewProtocolDocumentByUser(Document $document): Livewire\Testing\TestableLivewire
public function viewProtocolDocumentByUser(Document $document): TestableLivewire
{
return Livewire::test(DocumentResource\Pages\ViewDocument::class, ['record' => $document->id])
return Livewire::test(ViewDocument::class, ['record' => $document->id])
->assertSuccessful()
->assertFormFieldIsVisible('organisation_id')
->assertFormFieldIsVisible('name')
Expand All @@ -116,9 +122,9 @@ public function viewProtocolDocumentByUser(Document $document): Livewire\Testing
->assertFormFieldIsVisible('document');
}

public function viewDocumentByUser(Document $document): Livewire\Testing\TestableLivewire
public function viewDocumentByUser(Document $document): TestableLivewire
{
return Livewire::test(DocumentResource\Pages\ViewDocument::class, ['record' => $document->id])
return Livewire::test(ViewDocument::class, ['record' => $document->id])
->assertSuccessful()
->assertFormFieldIsVisible('organisation_id')
->assertFormFieldIsVisible('name')
Expand All @@ -129,9 +135,9 @@ public function viewDocumentByUser(Document $document): Livewire\Testing\Testabl
->assertFormFieldIsVisible('document');
}

public function editDocument(Document $document): Livewire\Testing\TestableLivewire
public function editDocument(Document $document): TestableLivewire
{
return Livewire::test(DocumentResource\Pages\EditDocument::class, ['record' => $document->id])
return Livewire::test(EditDocument::class, ['record' => $document->id])
->assertSuccessful()
->assertFormFieldIsVisible('organisation_id')
->assertFormFieldIsEnabled('organisation_id')
Expand Down
41 changes: 21 additions & 20 deletions tests/Feature/Documents/OrganisationAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use App\Filament\Resources\DocumentResource;
use App\Models\Document;
use App\Models\User;
use Livewire;
use Livewire\Livewire;

class OrganisationAdminTest extends DocumentsBaseTest
{
Expand All @@ -26,7 +26,7 @@ protected function setUp(): void
// $this->actingAs($this->user);
}

public function testOrgAdminCanViewDocuments(): void
public function testOrganisationAdminCanViewDocuments(): void
{
$this->actingAs($this->user);
$userOganisationID = $this->user->organisation_id;
Expand All @@ -36,6 +36,7 @@ public function testOrgAdminCanViewDocuments(): void
$documentsFromAnotherOrg = Document::query()
->whereNot('organisation_id', $userOganisationID)
->get();

Livewire::test(DocumentResource\Pages\ListDocuments::class)
->assertSuccessful()
->assertPageActionHidden('create')
Expand Down Expand Up @@ -66,13 +67,13 @@ public function testViewDocumentsByInactiveOrgAdmin(): void
$url = DocumentResource::getUrl('index');

$this->createOrganisations(2, 'inactive');
$orgAdmin = $this->getOrgAdminWithInactiveOrg(2);
$orgAdmin = $this->getOrganisationAdminWithInactiveOrganisation(2);
$this->actingAs($orgAdmin)
->get($url)
->assertRedirect('/login');
}

public function testOrgAdminCanViewDocument(): void
public function testOrganisationAdminCanViewDocument(): void
{
$document = $this->user
->organisation
Expand Down Expand Up @@ -103,7 +104,7 @@ public function testOrgAdminCanViewDocument(): void
public function testInactiveOrgAdminCanNotViewDocument(): void
{
$this->createOrganisations(2, 'inactive');
$orgAdmins = $this->getOrgAdminWithInactiveOrg();
$orgAdmins = $this->getOrganisationAdminWithInactiveOrganisation();

$document = Document::query()
->inRandomOrder()
Expand All @@ -116,7 +117,7 @@ public function testInactiveOrgAdminCanNotViewDocument(): void
->assertRedirect('/login');
}

public function testAnotherOrgAdminCanNotViewDocument(): void
public function testOtherOrganisationAdminCanNotViewDocument(): void
{
// dd(Document::all()->count());
$document = Document::query()
Expand All @@ -129,10 +130,10 @@ public function testAnotherOrgAdminCanNotViewDocument(): void
->assertForbidden();
}

public function testAnotherInactiveOrgAdminCanNotViewDocument(): void
public function testOtherInactiveOrganisationAdminCanNotViewDocument(): void
{
$this->createOrganisations(2, 'inactive');
$orgAdmin = $this->getOrgAdminWithInactiveOrg();
$orgAdmin = $this->getOrganisationAdminWithInactiveOrganisation();

$document = Document::query()
->whereNot('organisation_id', $orgAdmin->organisation_id)
Expand All @@ -147,7 +148,7 @@ public function testAnotherInactiveOrgAdminCanNotViewDocument(): void
->assertRedirect('/login');
}

public function testOrgAdminCanNotEditDocument(): void
public function testOrganisationAdminCanNotEditDocument(): void
{
$document = $this->user
->organisation
Expand All @@ -158,10 +159,10 @@ public function testOrgAdminCanNotEditDocument(): void
->assertForbidden();
}

public function testInactiveOrgAdminCanNotEditDocument(): void
public function testInactiveOrganisationAdminCanNotEditDocument(): void
{
$this->createOrganisations(2, 'inactive');
$orgAdmin = $this->getOrgAdminWithInactiveOrg();
$orgAdmin = $this->getOrganisationAdminWithInactiveOrganisation();

$document = Document::query()
->where('organisation_id', $orgAdmin->organisation_id)
Expand All @@ -175,7 +176,7 @@ public function testInactiveOrgAdminCanNotEditDocument(): void
->assertRedirect('/login');
}

public function testAnotherOrgAdminCanNotEditDocument(): void
public function testOtherOrganisationAdminCanNotEditDocument(): void
{
$document = Document::query()
->whereNot('organisation_id', $this->user->organisation_id)
Expand All @@ -190,10 +191,10 @@ public function testAnotherOrgAdminCanNotEditDocument(): void
->assertNotFound();
}

public function testAnotherInactiveOrgAdminCanNotEditDocument(): void
public function testOtherInactiveOrganisationAdminCanNotEditDocument(): void
{
$this->createOrganisations(2, 'inactive');
$orgAdmin = $this->getOrgAdminWithInactiveOrg();
$orgAdmin = $this->getOrganisationAdminWithInactiveOrganisation();

$document = Document::query()
->whereNot('organisation_id', $orgAdmin->organisation_id)
Expand All @@ -208,10 +209,10 @@ public function testAnotherInactiveOrgAdminCanNotEditDocument(): void
->assertRedirect('/login');
}

public function testInactiveOrgAdminCanNotDeleteDocument(): void
public function testInactiveOrganisationAdminCanNotDeleteDocument(): void
{
$this->createOrganisations(2, 'inactive');
$orgAdmin = $this->getOrgAdminWithInactiveOrg();
$orgAdmin = $this->getOrganisationAdminWithInactiveOrganisation();

$document = Document::query()
->where('organisation_id', $orgAdmin->organisation_id)
Expand All @@ -223,19 +224,19 @@ public function testInactiveOrgAdminCanNotDeleteDocument(): void
->assertPageActionDisabled('delete');
}

public function testOrgAdminCanNotCreateDocument()
public function testOrganisationAdminCanNotCreateDocument()
{
$orgAdmins = $this->getOrgAdmin()->random();
$orgAdmins = $this->getOrganisationAdmin()->random();

Livewire::actingAs($orgAdmins);
Livewire::test(DocumentResource\Pages\CreateDocument::class)
->assertForbidden();
}

public function testInactiveOrgAdminCanNotCreateDocument()
public function testInactiveOrganisationAdminCanNotCreateDocument()
{
$this->createOrganisations(2, 'inactive');
$orgAdmins = $this->getOrgAdminWithInactiveOrg();
$orgAdmins = $this->getOrganisationAdminWithInactiveOrganisation();

Livewire::actingAs($orgAdmins);
Livewire::test(DocumentResource\Pages\CreateDocument::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
use App\Filament\Resources\DocumentResource\Pages\ViewDocument;
use App\Models\Document;
use App\Models\Organisation;
use Livewire;
use Livewire\Livewire;
use Tests\Traits\ActingAsPlatformAdmin;

class AdminPlatformTest extends DocumentsBaseTest
class PlatformAdminTest extends DocumentsBaseTest
{
use ActingAsPlatformAdmin;

Expand All @@ -25,14 +25,14 @@ protected function setUp(): void
$this->createOrganisations(3, 'random');
}

public function testAdminPlatformCanViewDocuments(): void
public function testPlatformAdminCanViewDocuments(): void
{
$this->viewDocuments()
->assertPageActionVisible('create')
->assertPageActionEnabled('create');
}

public function testViewAdminPlatformCanViewDocument(): void
public function testViewPlatformAdminCanViewDocument(): void
{
$document = Document::query()
->whereType(DocumentType::protocol)
Expand All @@ -51,7 +51,7 @@ public function testViewAdminPlatformCanViewDocument(): void
->assertPageActionVisible('delete');
}

public function testAdminPlatformCanEditDocument(): void
public function testPlatformAdminCanEditDocument(): void
{
$protocolDocument = Document::query()
->whereType(DocumentType::protocol)
Expand All @@ -75,7 +75,7 @@ public function testAdminPlatformCanEditDocument(): void
->assertFormFieldIsHidden('never_expires');
}

public function testAdminPlatformCanDeleteDocument(): void
public function testPlatformAdminCanDeleteDocument(): void
{
$document = Document::query()
->inRandomOrder()
Expand All @@ -87,7 +87,7 @@ public function testAdminPlatformCanDeleteDocument(): void
$this->assertNull(Document::find($document->id));
}

public function testAdminPlatformCanCreateDocument()
public function testPlatformAdminCanCreateDocument()
{
$file = \Illuminate\Http\UploadedFile::fake()
->image(fake()->word() . '.jpg')
Expand Down
4 changes: 3 additions & 1 deletion tests/Feature/Documents/PlatformCoordinatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@
use App\Filament\Resources\DocumentResource;
use App\Models\Document;
use App\Models\User;
use Livewire;
use Livewire\Livewire;

class PlatformCoordinatorTest extends DocumentsBaseTest
{
protected function setUp(): void
{
parent::setUp();

$this->user = User::factory()
->platformCoordinator()
->create();

Livewire::actingAs($this->user);

$this->createOrganisations(3, 'random');
Expand Down
Loading

0 comments on commit 5efe70b

Please sign in to comment.