|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace RZ\Roadiz\Markdown\Tests; |
| 6 | + |
| 7 | +use League\CommonMark\Environment\Environment; |
| 8 | +use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; |
| 9 | +use League\CommonMark\MarkdownConverter; |
| 10 | +use PHPUnit\Framework\TestCase; |
| 11 | +use RZ\Roadiz\Markdown\CommonMark; |
| 12 | + |
| 13 | +final class CommonMarkTest extends TestCase |
| 14 | +{ |
| 15 | + private function createCommonMark(): CommonMark |
| 16 | + { |
| 17 | + $environment = new Environment(['html_input' => 'strip']); |
| 18 | + $environment->addExtension(new CommonMarkCoreExtension()); |
| 19 | + $converter = new MarkdownConverter($environment); |
| 20 | + |
| 21 | + // strip() only relies on the textExtra converter, but the constructor |
| 22 | + // requires all five converters. |
| 23 | + return new CommonMark($converter, $converter, $converter, $converter, $converter); |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * @dataProvider stripProvider |
| 28 | + */ |
| 29 | + public function testStrip(?string $input, ?string $expected): void |
| 30 | + { |
| 31 | + $this->assertSame($expected, $this->createCommonMark()->strip($input)); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @return array<array{0: ?string, 1: ?string}> |
| 36 | + */ |
| 37 | + public static function stripProvider(): array |
| 38 | + { |
| 39 | + return [ |
| 40 | + 'null returns null' => [null, null], |
| 41 | + 'plain text is untouched' => ['Hello world', 'Hello world'], |
| 42 | + 'markdown emphasis is stripped' => ['**Hello** _world_', 'Hello world'], |
| 43 | + 'headings are stripped' => ['# Title', 'Title'], |
| 44 | + 'links keep their label only' => ['[Roadiz](https://roadiz.io)', 'Roadiz'], |
| 45 | + 'hard line break becomes a space' => ["Line1 \nLine2", 'Line1 Line2'], |
| 46 | + 'control characters are removed' => ["a\x07b", 'ab'], |
| 47 | + 'DEL character is removed' => ["a\x7Fb", 'ab'], |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + public function testStripRemovesHtmlTags(): void |
| 52 | + { |
| 53 | + $result = $this->createCommonMark()->strip('Some **bold** text with a [link](https://roadiz.io).'); |
| 54 | + |
| 55 | + $this->assertStringNotContainsString('<', (string) $result); |
| 56 | + $this->assertStringNotContainsString('>', (string) $result); |
| 57 | + } |
| 58 | +} |
0 commit comments