| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of the league/commonmark package.
- *
- * (c) Colin O'Dell <colinodell@gmail.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace League\CommonMark\Tests\Functional\Extension\Mention;
- use League\CommonMark\Environment\Environment;
- use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
- use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
- use League\CommonMark\Extension\Mention\Mention;
- use League\CommonMark\Extension\Mention\MentionExtension;
- use League\CommonMark\MarkdownConverter;
- use League\CommonMark\Node\Inline\AbstractInline;
- use League\CommonMark\Parser\MarkdownParser;
- use League\CommonMark\Xml\XmlRenderer;
- use League\Config\Exception\InvalidConfigurationException;
- use PHPUnit\Framework\TestCase;
- final class MentionExtensionTest extends TestCase
- {
- public function testNoConfig(): void
- {
- $input = <<<'EOT'
- You can follow the author of this library on GitHub - he's @colinodell!
- EOT;
- $expected = <<<'EOT'
- <p>You can follow the author of this library on GitHub - he's @colinodell!</p>
- EOT;
- $environment = new Environment();
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $converter = new MarkdownConverter($environment);
- $this->assertEquals($expected, $converter->convert($input));
- }
- public function testConfigStringGenerator(): void
- {
- $input = <<<'EOT'
- You can follow the author of this library on GitHub - he's @colinodell!
- EOT;
- $expected = <<<'EOT'
- <p>You can follow the author of this library on GitHub - he's <a href="https://github.com/colinodell">@colinodell</a>!</p>
- EOT;
- $environment = new Environment([
- 'mentions' => [
- 'github_handle' => [
- 'prefix' => '@',
- 'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
- 'generator' => 'https://github.com/%s',
- ],
- ],
- ]);
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $converter = new MarkdownConverter($environment);
- $this->assertEquals($expected, $converter->convert($input));
- }
- public function testConfigCallableGenerator(): void
- {
- $input = <<<'EOT'
- You can follow the author of this library on GitHub - he's @colinodell!
- EOT;
- $expected = <<<'EOT'
- <p>You can follow the author of this library on GitHub - he's <a href="https://github.com/colinodell">@colinodell</a>!</p>
- EOT;
- $environment = new Environment([
- 'mentions' => [
- 'github_handle' => [
- 'prefix' => '@',
- 'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
- 'generator' => static function (Mention $mention) {
- $mention->setUrl(\sprintf('https://github.com/%s', $mention->getIdentifier()));
- return $mention;
- },
- ],
- ],
- ]);
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $converter = new MarkdownConverter($environment);
- $this->assertEquals($expected, $converter->convert($input));
- }
- public function testConfigObjectImplementingMentionGeneratorInterface(): void
- {
- $input = <<<'EOT'
- You can follow the author of this library on GitHub - he's @colinodell!
- EOT;
- $expected = <<<'EOT'
- <p>You can follow the author of this library on GitHub - he's <a href="https://github.com/colinodell">@colinodell</a>!</p>
- EOT;
- $environment = new Environment([
- 'mentions' => [
- 'github_handle' => [
- 'prefix' => '@',
- 'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
- 'generator' => new class () implements MentionGeneratorInterface {
- public function generateMention(Mention $mention): ?AbstractInline
- {
- $mention->setUrl(\sprintf('https://github.com/%s', $mention->getIdentifier()));
- return $mention;
- }
- },
- ],
- ],
- ]);
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $converter = new MarkdownConverter($environment);
- $this->assertEquals($expected, $converter->convert($input));
- }
- public function testConfigUnknownGenerator(): void
- {
- $this->expectException(InvalidConfigurationException::class);
- $environment = new Environment([
- 'mentions' => [
- 'github_handle' => [
- 'prefix' => '@',
- 'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
- 'generator' => new \stdClass(),
- ],
- ],
- ]);
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $converter = new MarkdownConverter($environment);
- $converter->convert('');
- }
- public function testLegacySymbolOption(): void
- {
- $this->expectException(InvalidConfigurationException::class);
- $environment = new Environment([
- 'mentions' => [
- 'github_handle' => [
- 'symbol' => '@',
- 'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
- 'generator' => 'https://github.com/%s',
- ],
- ],
- ]);
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $converter = new MarkdownConverter($environment);
- $converter->convert('foo');
- }
- public function testWithFullRegexOption(): void
- {
- $this->expectException(InvalidConfigurationException::class);
- $environment = new Environment([
- 'mentions' => [
- 'github_handle' => [
- 'prefix' => '@',
- 'pattern' => '/[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)/i',
- 'generator' => 'https://github.com/%s',
- ],
- ],
- ]);
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $converter = new MarkdownConverter($environment);
- $converter->convert('foo');
- }
- public function testXmlRendering(): void
- {
- $input = <<<'EOT'
- You can follow the author of this library on GitHub - he's @colinodell!
- EOT;
- $expected = <<<'EOT'
- <?xml version="1.0" encoding="UTF-8"?>
- <document xmlns="http://commonmark.org/xml/1.0">
- <paragraph>
- <text>You can follow the author of this library on GitHub - he's </text>
- <link destination="https://github.com/colinodell" title="">
- <text>@colinodell</text>
- </link>
- <text>!</text>
- </paragraph>
- </document>
- EOT;
- $environment = new Environment([
- 'mentions' => [
- 'github_handle' => [
- 'prefix' => '@',
- 'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
- 'generator' => new class () implements MentionGeneratorInterface {
- public function generateMention(Mention $mention): ?AbstractInline
- {
- $mention->setUrl(\sprintf('https://github.com/%s', $mention->getIdentifier()));
- return $mention;
- }
- },
- ],
- ],
- ]);
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $document = (new MarkdownParser($environment))->parse($input);
- $xml = (new XmlRenderer($environment))->renderDocument($document)->getContent();
- $this->assertSame($expected, $xml);
- }
- public function testMentionLikeLabelInExistingLinks(): void
- {
- $input = <<<'EOT'
- Try [asking **@driesvints**](https://github.com/driesvints).
- EOT;
- $expected = <<<'EOT'
- <p>Try <a href="https://github.com/driesvints">asking <strong>@driesvints</strong></a>.</p>
- EOT;
- $environment = new Environment([
- 'mentions' => [
- 'username' => [
- 'prefix' => '@',
- 'pattern' => '[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}(?!\w)',
- 'generator' => 'https://github.com/user/%s',
- ],
- ],
- ]);
- $environment->addExtension(new CommonMarkCoreExtension());
- $environment->addExtension(new MentionExtension());
- $converter = new MarkdownConverter($environment);
- $this->assertSame($expected, $converter->convert($input)->getContent());
- }
- }
|