rob-revisited/ide/tests/DocsLibraryTest.php

79 lines
3.5 KiB
PHP

<?php
namespace JumPHP\Ide\Tests;
use JumPHP\Ide\Docs\DocsLibrary;
use JumPHP\Ide\Ide;
use JumPHP\Ide\Workspace\MemoryWorkspace;
use PHPUnit\Framework\TestCase;
/** The IDE's documentation as the Doc browser reads it (docs.index, docs.image): every page, and nothing outside its folder. */
final class DocsLibraryTest extends TestCase
{
private string $dir;
protected function setUp(): void
{
$this->dir = sys_get_temp_dir() . '/jumphp-docs-' . bin2hex(random_bytes(4));
mkdir($this->dir . '/docs/images', 0777, true);
mkdir($this->dir . '/docs/fr', 0777, true);
file_put_contents($this->dir . '/docs/toc.json', '{"groups": [{"id": "a", "title": {"en": "A", "fr": "A"}, "pages": ["index"]}]}');
file_put_contents($this->dir . '/docs/index.md', "# Overview\n\nText.");
file_put_contents($this->dir . '/docs/fr/index.md', "# Aperçu\n\nTexte.");
file_put_contents($this->dir . '/docs/Bad Name.md', '# no');
file_put_contents($this->dir . '/docs/images/shot.png', "\x89PNG\r\n\x1a\nfake");
file_put_contents($this->dir . '/secret.png', 'outside');
@symlink($this->dir . '/secret.png', $this->dir . '/docs/images/link.png');
}
protected function tearDown(): void
{
exec('rm -rf ' . escapeshellarg($this->dir));
}
public function testTheIndexHasTheTableOfContentsAndEveryPageInEveryLanguage(): void
{
$index = (new DocsLibrary($this->dir . '/docs'))->index();
self::assertSame('index', $index['toc']['groups'][0]['pages'][0]);
$pages = array_map(fn ($p) => $p['lang'] . ':' . $p['id'] . ':' . $p['title'], $index['pages']);
sort($pages);
self::assertSame(['en:index:Overview', 'fr:index:Aperçu'], $pages);
self::assertSame(DocsLibrary::WEB, $index['web']);
}
public function testPicturesAreReadOnlyInsideTheDocumentation(): void
{
$docs = new DocsLibrary($this->dir . '/docs');
self::assertSame(['mime' => 'image/png', 'data' => base64_encode("\x89PNG\r\n\x1a\nfake")], $docs->image('images/shot.png'));
foreach (['../secret.png', 'images/../../secret.png', '/etc/passwd', $this->dir . '/secret.png', 'images/link.png', 'toc.json', 'index.md', 'images/missing.png', 'images\\..\\..\\secret.png'] as $path) {
self::assertNull($docs->image($path), $path);
}
}
public function testWithoutDocumentationTheIndexIsEmpty(): void
{
self::assertSame([], (new DocsLibrary($this->dir . '/nothing'))->index()['pages']);
self::assertNull((new DocsLibrary($this->dir . '/nothing'))->image('images/shot.png'));
}
public function testTheRepositorysDocumentationIsFoundAndComplete(): void
{
$root = DocsLibrary::find();
self::assertNotNull($root, 'docs/ide/ (or the packaged ui/docs) is found');
$index = (new DocsLibrary($root))->index();
$english = array_column(array_filter($index['pages'], fn ($p) => $p['lang'] === 'en'), 'title', 'id');
foreach ($index['toc']['groups'] as $group) {
foreach ($group['pages'] as $id) {
self::assertArrayHasKey($id, $english, "toc.json names $id");
}
}
self::assertGreaterThan(30, count($english));
}
public function testTheIdeAnswersTheDocsCommands(): void
{
$ide = new Ide(new MemoryWorkspace([]));
self::assertTrue($ide->has('docs.index'));
self::assertNull($ide->handle('docs.image', ['path' => '../../../../etc/passwd']));
}
}