MentionParserTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of the league/commonmark package.
  5. *
  6. * (c) Colin O'Dell <colinodell@gmail.com>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace League\CommonMark\Tests\Functional\Extension\Mention;
  12. use League\CommonMark\Environment\Environment;
  13. use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension;
  14. use League\CommonMark\Extension\CommonMark\Node\Inline\Emphasis;
  15. use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface;
  16. use League\CommonMark\Extension\Mention\Generator\StringTemplateLinkGenerator;
  17. use League\CommonMark\Extension\Mention\Mention;
  18. use League\CommonMark\Extension\Mention\MentionParser;
  19. use League\CommonMark\MarkdownConverter;
  20. use League\CommonMark\Node\Inline\Text;
  21. use PHPUnit\Framework\TestCase;
  22. final class MentionParserTest extends TestCase
  23. {
  24. public function testMentionParser(): void
  25. {
  26. $input = 'See #123 for more information.';
  27. $expected = '<p>See <a href="https://www.example.com/123">#123</a> for more information.</p>';
  28. $mentionParser = new MentionParser('test', '#', '\d+', new StringTemplateLinkGenerator('https://www.example.com/%s'));
  29. $environment = new Environment();
  30. $environment->addExtension(new CommonMarkCoreExtension());
  31. $environment->addInlineParser($mentionParser);
  32. $converter = new MarkdownConverter($environment);
  33. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  34. }
  35. public function testMentionParserWithMultiCharacterPrefix(): void
  36. {
  37. $input = 'Try asking u:colinodell about that.';
  38. $expected = '<p>Try asking <a href="https://www.example.com/users/colinodell">u:colinodell</a> about that.</p>';
  39. $mentionParser = new MentionParser('test', 'u:', '\w+', new StringTemplateLinkGenerator('https://www.example.com/users/%s'));
  40. $environment = new Environment();
  41. $environment->addExtension(new CommonMarkCoreExtension());
  42. $environment->addInlineParser($mentionParser);
  43. $converter = new MarkdownConverter($environment);
  44. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  45. }
  46. public function testMentionParserWithMultiCharacterPrefixContainingSpecialRegexCharsThatShouldBeEscaped(): void
  47. {
  48. $input = 'I spend too much time on the /r/php subreddit.';
  49. $expected = '<p>I spend too much time on the <a href="https://www.reddit.com/r/php">/r/php</a> subreddit.</p>';
  50. $mentionParser = new MentionParser('test', '/r/', '\w+', new StringTemplateLinkGenerator('https://www.reddit.com/r/%s'));
  51. $environment = new Environment();
  52. $environment->addExtension(new CommonMarkCoreExtension());
  53. $environment->addInlineParser($mentionParser);
  54. $converter = new MarkdownConverter($environment);
  55. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  56. }
  57. public function testMentionParserWithoutSpaceInFront(): void
  58. {
  59. $input = 'See#123 for more information.';
  60. $expected = '<p>See#123 for more information.</p>';
  61. $mentionParser = new MentionParser('test', '#', '\d+', $this->createMock(MentionGeneratorInterface::class));
  62. $environment = new Environment();
  63. $environment->addExtension(new CommonMarkCoreExtension());
  64. $environment->addInlineParser($mentionParser);
  65. $converter = new MarkdownConverter($environment);
  66. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  67. }
  68. public function testMentionParserWithNonMatchingPrefix(): void
  69. {
  70. $input = 'See #123 for more information.';
  71. $expected = '<p>See #123 for more information.</p>';
  72. $mentionParser = new MentionParser('test', '@', '\d+', $this->createMock(MentionGeneratorInterface::class));
  73. $environment = new Environment();
  74. $environment->addExtension(new CommonMarkCoreExtension());
  75. $environment->addInlineParser($mentionParser);
  76. $converter = new MarkdownConverter($environment);
  77. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  78. }
  79. public function testMentionParserWithNonMatchingRegex(): void
  80. {
  81. $input = 'See #123 for more information.';
  82. $expected = '<p>See #123 for more information.</p>';
  83. $mentionParser = new MentionParser('test', '#', '[a-z]+', $this->createMock(MentionGeneratorInterface::class));
  84. $environment = new Environment();
  85. $environment->addExtension(new CommonMarkCoreExtension());
  86. $environment->addInlineParser($mentionParser);
  87. $converter = new MarkdownConverter($environment);
  88. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  89. }
  90. public function testMentionParserWithNullUrl(): void
  91. {
  92. $input = 'See #123 for more information.';
  93. $expected = '<p>See #123 for more information.</p>';
  94. $returnsNull = $this->createMock(MentionGeneratorInterface::class);
  95. $returnsNull->method('generateMention')->willReturn(null);
  96. $mentionParser = new MentionParser('test', '#', '\d+', $returnsNull);
  97. $environment = new Environment();
  98. $environment->addExtension(new CommonMarkCoreExtension());
  99. $environment->addInlineParser($mentionParser);
  100. $converter = new MarkdownConverter($environment);
  101. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  102. }
  103. public function testMentionParserUsingCallback(): void
  104. {
  105. $callable = static function (Mention $mention) {
  106. // Stuff the three params into the URL just to prove we received them all properly
  107. $mention->setUrl(\sprintf('https://www.example.com/%s/%s/%s', $mention->getIdentifier(), $mention->getLabel(), $mention->getPrefix()));
  108. // Change the label
  109. $mention->setLabel('Replaced Label');
  110. return $mention;
  111. };
  112. $input = 'This should parse #123.';
  113. $expected = '<p>This should parse <a href="https://www.example.com/123/#123/#">Replaced Label</a>.</p>';
  114. $mentionParser = MentionParser::createWithCallback('test', '#', '\d+', $callable);
  115. $environment = new Environment();
  116. $environment->addExtension(new CommonMarkCoreExtension());
  117. $environment->addInlineParser($mentionParser);
  118. $converter = new MarkdownConverter($environment);
  119. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  120. }
  121. public function testMentionParserUsingCallbackReturnsAbstractInline(): void
  122. {
  123. $callable = static function (Mention $mention) {
  124. // Pretend callback does some access logic to determine visibility.
  125. $emphasis = new Emphasis('*');
  126. $emphasis->appendChild(new Text('[members only]'));
  127. return $emphasis;
  128. };
  129. $input = 'This should parse #123.';
  130. $expected = '<p>This should parse <em>[members only]</em>.</p>';
  131. $mentionParser = MentionParser::createWithCallback('test', '#', '\d+', $callable);
  132. $environment = new Environment();
  133. $environment->addExtension(new CommonMarkCoreExtension());
  134. $environment->addInlineParser($mentionParser);
  135. $converter = new MarkdownConverter($environment);
  136. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  137. }
  138. public function testMentionParserWithNonWordCharacterBefore(): void
  139. {
  140. $input = "Test\n#123 for more information.";
  141. $expected = "<p>Test\n<a href=\"https://www.example.com/123\">#123</a> for more information.</p>";
  142. $mentionParser = new MentionParser(
  143. 'test',
  144. '#',
  145. '\d+',
  146. new StringTemplateLinkGenerator('https://www.example.com/%s')
  147. );
  148. $environment = new Environment();
  149. $environment->addExtension(new CommonMarkCoreExtension());
  150. $environment->addInlineParser($mentionParser);
  151. $converter = new MarkdownConverter($environment);
  152. $this->assertEquals($expected, \rtrim((string) $converter->convert($input)));
  153. }
  154. }