From 7712a4a9aba76be310058bf966f9b3f2f5a9feaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrei=20Ioni=C8=9B=C4=83?= Date: Tue, 2 Apr 2024 11:00:30 +0100 Subject: [PATCH] wip documents test --- tests/Feature/DocumentsTest.php | 149 ++++++++++++++++---------------- 1 file changed, 75 insertions(+), 74 deletions(-) diff --git a/tests/Feature/DocumentsTest.php b/tests/Feature/DocumentsTest.php index 23d7a5a..4514a12 100644 --- a/tests/Feature/DocumentsTest.php +++ b/tests/Feature/DocumentsTest.php @@ -14,11 +14,9 @@ use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Notification; -use Livewire; +use Livewire\Livewire; use Tests\TestCase; -use function Pest\Livewire\livewire; - class DocumentsTest extends TestCase { use RefreshDatabase; @@ -37,7 +35,6 @@ protected function getPlatformAdmins(): User return User::factory() ->platformAdmin() ->create(); - } protected function getPlatformCoordinator(): User @@ -53,6 +50,7 @@ protected function getOrgAdminWithActiveOrg(): Collection ->count(5) ->withRelated() ->createQuietly(); + return User::query() ->withoutGlobalScopes() ->role(UserRole::ORG_ADMIN) @@ -66,6 +64,7 @@ protected function getOrgAdminWithInactiveOrg(): Collection ->inactive() ->withRelated() ->createQuietly(); + return User::query() ->withoutGlobalScopes() ->role(UserRole::ORG_ADMIN) @@ -81,13 +80,54 @@ protected function createOrganisations(): void ->createQuietly(); } - public function testViewDocumentsWithoutUser() + public function testPlatformAdminsCanListAllDocuments(): void { - $url = DocumentResource::getUrl('index'); + $user = User::factory() + ->platformAdmin() + ->create(); - $this->withSession(['banned' => false]) - ->get($url) - ->assertFound(); + $organisation = Organisation::factory() + ->withRelated() + ->createQuietly(); + + $documents = Document::all(); + + $this->actingAs($user); + + Livewire::test(DocumentResource\Pages\ListDocuments::class) + ->assertSuccessful() + ->assertCountTableRecords(3) + ->assertCanSeeTableRecords($documents) + ->assertCanRenderTableColumn('name') + ->filterTable('type', DocumentType::contract->value) + ->assertCanSeeTableRecords($documents->where('type', DocumentType::contract)) + ->resetTableFilters() + ->sortTable('name') + ->assertCanSeeTableRecords($documents->sortBy('name'), inOrder: true) + ->assertCanRenderTableColumn('organisation.name') + ->sortTable('organisation.name') + ->assertCanSeeTableRecords($documents->sortBy('organisation.name'), inOrder: true) + ->assertCanRenderTableColumn('media.file_name') + ->sortTable('media.file_name') + ->assertCanSeeTableRecords($documents->sortBy('media.file_name'), inOrder: true) + ->assertCanRenderTableColumn('signed_at') + ->sortTable('signed_at') + ->assertCanSeeTableRecords($documents->sortBy('signed_at'), inOrder: true) + ->assertCanRenderTableColumn('expires_at') + ->sortTable('expires_at') + ->assertCanSeeTableRecords($documents->sortBy('expires_at'), inOrder: true); + } + + public function testPlatformCoordinatorCanListAllDocuments(): void + { + } + + public function testOrganisationAdminCanListOwnDocuments(): void + { + } + + public function testOrganisationAdminCannotListOtherDocuments(): void + { } public function testViewDocumentsByOrgAdmin(): void @@ -130,18 +170,6 @@ public function testViewDocumentsByPlatformAdmin(): void ->assertSuccessful(); } - public function testViewDocumentWithoutUser() - { - $this->createOrganisations(); - $document = Document::query() - ->inRandomOrder() - ->first(); - - $url = DocumentResource::getUrl('view', ['record' => $document->id]); - $this->withSession(['banned' => false]) - ->get($url) - ->assertFound(); - } public function testViewDocumentByPlatformAdmin(): void { @@ -236,19 +264,6 @@ public function testViewDocumentByAnotherInactiveOrgAdmin(): void ->assertFound(); } - public function testEditDocumentWithoutUser() - { - $this->createOrganisations(); - $document = Document::query() - ->inRandomOrder() - ->first(); - - $url = DocumentResource::getUrl('edit', ['record' => $document->id]); - $this->withSession(['banned' => false]) - ->get($url) - ->assertFound(); - } - public function testEditDocumentByPlatformAdmin(): void { $this->createOrganisations(); @@ -342,18 +357,6 @@ public function testEditDocumentByAnotherInactiveOrgAdmin(): void ->assertFound(); } - public function testDeleteDocumentWithoutUser() - { - $this->assertTrue(true); -// $this->createOrganisations(); -// $document = Document::query() -// ->inRandomOrder() -// ->first(); -// -// \Livewire::test(DocumentResource\Pages\ViewDocument::class, ['record' => $document->id]) -// ->assertPageActionDisabled('delete'); - } - public function testDeleteDocumentByPlatformAdmin(): void { $this->createOrganisations(); @@ -411,37 +414,37 @@ public function testDeleteDocumentByAnotherOrgAdmin(): void { $this->assertTrue(true); -// $orgAdmins = $this->getOrgAdminWithActiveOrg(); -// -// $document = Document::query() -// ->inRandomOrder() -// ->first(); -// -// $documentUser = $orgAdmins->filter(fn($item) => $item->organisation_id != $document->organisation_id)->first(); -// $params = ['record' => $document->id]; -// -// \Livewire::actingAs($documentUser); -// \Livewire::test(DocumentResource\Pages\ViewDocument::class, $params) -// ->assertPageActionDisabled('delete'); + // $orgAdmins = $this->getOrgAdminWithActiveOrg(); + // + // $document = Document::query() + // ->inRandomOrder() + // ->first(); + // + // $documentUser = $orgAdmins->filter(fn($item) => $item->organisation_id != $document->organisation_id)->first(); + // $params = ['record' => $document->id]; + // + // \Livewire::actingAs($documentUser); + // \Livewire::test(DocumentResource\Pages\ViewDocument::class, $params) + // ->assertPageActionDisabled('delete'); } public function testDeleteDocumentByAnotherInactiveOrgAdmin(): void { $this->assertTrue(true); -// $orgAdmins = $this->getOrgAdminWithInactiveOrg(); -// -// $document = Document::query() -// ->inRandomOrder() -// ->first(); -// -// $url = DocumentResource::getUrl('edit', ['record' => $document->id]); -// -// $documentUser = $orgAdmins->filter(fn($item) => $item->organisation_id != $document->organisation_id)->first(); -// $params = ['record' => $document->id]; -// \Livewire::actingAs($documentUser); -// \Livewire::test(DocumentResource\Pages\ViewDocument::class, $params) -// ->assertPageActionDisabled('delete'); + // $orgAdmins = $this->getOrgAdminWithInactiveOrg(); + // + // $document = Document::query() + // ->inRandomOrder() + // ->first(); + // + // $url = DocumentResource::getUrl('edit', ['record' => $document->id]); + // + // $documentUser = $orgAdmins->filter(fn($item) => $item->organisation_id != $document->organisation_id)->first(); + // $params = ['record' => $document->id]; + // \Livewire::actingAs($documentUser); + // \Livewire::test(DocumentResource\Pages\ViewDocument::class, $params) + // ->assertPageActionDisabled('delete'); } public function testCreateDocumentByPlatformAdmin(): void @@ -476,7 +479,6 @@ public function testCreateDocumentByInactiveOrgAdmin() ->assertForbidden(); } - public function testCreateDocument() { $this->createOrganisations(); @@ -560,7 +562,7 @@ public function testCreateDocument() 'document' => [$file]]) ->call('create') ->assertHasFormErrors(['signed_at' => 'required', - 'expires_at' => 'required' + 'expires_at' => 'required', ]); // document type protocol with never expires and without start/end date @@ -615,5 +617,4 @@ public function testCreateDocument() ->call('create') ->assertHasFormErrors(['expires_at']); } - }