189 lines
6.8 KiB
PHP
189 lines
6.8 KiB
PHP
<?php
|
|
|
|
namespace Modules\Documentation\Tests\Feature;
|
|
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Tests\TestCase;
|
|
use Modules\Documentation\Models\DocumentationCategory;
|
|
use Modules\Documentation\Models\DocumentationDocument;
|
|
use Modules\Documentation\Models\DocumentationAttachment;
|
|
|
|
class DocumentationModuleTest extends TestCase
|
|
{
|
|
use RefreshDatabase;
|
|
|
|
/**
|
|
* Add a CSRF token for projects that keep web middleware enabled during tests.
|
|
*
|
|
* @param array<string, mixed> $payload
|
|
* @return array<string, mixed>
|
|
*/
|
|
private function csrfPayload(array $payload): array
|
|
{
|
|
return array_merge(['_token' => 'test-token'], $payload);
|
|
}
|
|
|
|
/**
|
|
* The documentation dashboard renders for authenticated users.
|
|
*/
|
|
public function test_documentation_dashboard_renders(): void
|
|
{
|
|
$user = \App\Models\User::factory()->create();
|
|
|
|
$this->actingAs($user)
|
|
->get(route('documentation.documents.index'))
|
|
->assertOk()
|
|
->assertSee('Documentation Centre');
|
|
}
|
|
|
|
/**
|
|
* Documents can be created through the CRUD endpoint.
|
|
*/
|
|
public function test_document_can_be_created(): void
|
|
{
|
|
$user = \App\Models\User::factory()->create();
|
|
$category = DocumentationCategory::query()->create([
|
|
'name' => 'Operations',
|
|
'slug' => 'operations',
|
|
'colour' => 'emerald',
|
|
'is_active' => true,
|
|
]);
|
|
|
|
$response = $this->actingAs($user)->withSession(['_token' => 'test-token'])->post(route('documentation.documents.store'), $this->csrfPayload([
|
|
'documentation_category_id' => $category->id,
|
|
'title' => 'Goods In Process',
|
|
'slug' => 'goods-in-process',
|
|
'summary' => 'Inbound warehouse process.',
|
|
'body' => 'Step-by-step goods in process.',
|
|
'status' => DocumentationDocument::STATUS_PUBLISHED,
|
|
'version' => '1.0.0',
|
|
'reference' => 'SOP-001',
|
|
'is_featured' => '1',
|
|
]));
|
|
|
|
$response->assertRedirect();
|
|
$this->assertDatabaseHas('documentation_documents', [
|
|
'title' => 'Goods In Process',
|
|
'status' => DocumentationDocument::STATUS_PUBLISHED,
|
|
'reference' => 'SOP-001',
|
|
'is_featured' => true,
|
|
]);
|
|
}
|
|
|
|
|
|
/**
|
|
* Markdown attachments are stored and imported into the document body.
|
|
*/
|
|
public function test_markdown_attachment_is_imported_and_rendered(): void
|
|
{
|
|
Storage::fake('public');
|
|
|
|
$user = \App\Models\User::factory()->create();
|
|
$file = UploadedFile::fake()->createWithContent('goods-in.md', "# Goods In\n\n- Scan delivery note\n- Confirm quantities");
|
|
|
|
$response = $this->actingAs($user)->withSession(['_token' => 'test-token'])->post(route('documentation.documents.store'), $this->csrfPayload([
|
|
'title' => 'Goods In Markdown',
|
|
'slug' => 'goods-in-markdown',
|
|
'summary' => 'Markdown controlled document.',
|
|
'body' => '',
|
|
'status' => DocumentationDocument::STATUS_PUBLISHED,
|
|
'version' => '1.0.0',
|
|
'attachment' => $file,
|
|
]));
|
|
|
|
$response->assertRedirect();
|
|
|
|
$document = DocumentationDocument::query()->where('slug', 'goods-in-markdown')->firstOrFail();
|
|
|
|
$this->assertStringContainsString('# Goods In', (string) $document->body);
|
|
$this->assertStringContainsString('<h1>Goods In</h1>', $document->rendered_body);
|
|
Storage::disk('public')->assertExists($document->attachment_path);
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* Multiple supporting attachments can be uploaded, downloaded and removed.
|
|
*/
|
|
public function test_document_supports_multiple_attachments(): void
|
|
{
|
|
Storage::fake('public');
|
|
|
|
$user = \App\Models\User::factory()->create();
|
|
|
|
$response = $this->actingAs($user)->withSession(['_token' => 'test-token'])->post(route('documentation.documents.store'), $this->csrfPayload([
|
|
'title' => 'Attachment Pack',
|
|
'slug' => 'attachment-pack',
|
|
'summary' => 'Document with supporting files.',
|
|
'body' => 'Attach evidence and supporting material.',
|
|
'status' => DocumentationDocument::STATUS_PUBLISHED,
|
|
'version' => '1.0.0',
|
|
'attachments' => [
|
|
UploadedFile::fake()->createWithContent('setup.md', "```php\necho 'ok';\n```"),
|
|
UploadedFile::fake()->create('process.pdf', 64, 'application/pdf'),
|
|
],
|
|
]));
|
|
|
|
$response->assertRedirect();
|
|
|
|
$document = DocumentationDocument::query()->where('slug', 'attachment-pack')->firstOrFail();
|
|
|
|
$this->assertCount(2, $document->attachments()->get());
|
|
|
|
$attachment = $document->attachments()->where('original_name', 'setup.md')->firstOrFail();
|
|
Storage::disk('public')->assertExists($attachment->path);
|
|
|
|
$this->actingAs($user)
|
|
->get(route('documentation.documents.attachments.download', [$document, $attachment]))
|
|
->assertOk();
|
|
|
|
$this->actingAs($user)
|
|
->withSession(['_token' => 'test-token'])
|
|
->delete(route('documentation.documents.attachments.destroy', [$document, $attachment]), ['_token' => 'test-token'])
|
|
->assertRedirect(route('documentation.documents.edit', $document));
|
|
|
|
$this->assertDatabaseMissing('documentation_attachments', ['id' => $attachment->id]);
|
|
Storage::disk('public')->assertMissing($attachment->path);
|
|
}
|
|
|
|
/**
|
|
* The editor renders Markdown controls for code blocks and tables.
|
|
*/
|
|
public function test_document_editor_has_markdown_code_controls(): void
|
|
{
|
|
$user = \App\Models\User::factory()->create();
|
|
|
|
$this->actingAs($user)
|
|
->get(route('documentation.documents.create'))
|
|
->assertOk()
|
|
->assertSee('data-documentation-editor', false)
|
|
->assertSee('Code block')
|
|
->assertSee('Markdown is supported');
|
|
}
|
|
|
|
/**
|
|
* Categories can be created through the CRUD endpoint.
|
|
*/
|
|
public function test_category_can_be_created(): void
|
|
{
|
|
$user = \App\Models\User::factory()->create();
|
|
|
|
$this->actingAs($user)->withSession(['_token' => 'test-token'])->post(route('documentation.categories.store'), $this->csrfPayload([
|
|
'name' => 'Policies',
|
|
'slug' => 'policies',
|
|
'colour' => 'emerald',
|
|
'description' => 'Controlled business policies.',
|
|
'sort_order' => 1,
|
|
'is_active' => '1',
|
|
]))->assertRedirect(route('documentation.categories.index'));
|
|
|
|
$this->assertDatabaseHas('documentation_categories', [
|
|
'name' => 'Policies',
|
|
'slug' => 'policies',
|
|
'is_active' => true,
|
|
]);
|
|
}
|
|
}
|