MarkdownConverterTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Tests\Utils;
  3. use App\CommonMark\MarkdownConverter;
  4. use PHPUnit\Framework\TestCase;
  5. use Symfony\Component\DomCrawler\Crawler;
  6. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  7. class MarkdownConverterTest extends TestCase {
  8. public function testLinksHaveNoTargetByDefault() {
  9. $converter = new MarkdownConverter(
  10. $this->createMock(UrlGeneratorInterface::class)
  11. );
  12. $output = $converter->convertToHtml('[link](http://example.com)');
  13. $crawler = new Crawler($output);
  14. $crawler = $crawler->filterXPath('//p/a[not(@target)]');
  15. $this->assertEquals('link', $crawler->html());
  16. }
  17. public function testLinksHaveTargetWithOpenExternalLinksInNewTabOption() {
  18. $converter = new MarkdownConverter(
  19. $this->createMock(UrlGeneratorInterface::class)
  20. );
  21. $output = $converter->convertToHtml('[link](http://example.com)', [
  22. 'open_external_links_in_new_tab' => true,
  23. ]);
  24. $crawler = new Crawler($output);
  25. $crawler = $crawler->filterXPath('//p/a[contains(@target,"_blank")]');
  26. $this->assertEquals('link', $crawler->html());
  27. }
  28. /**
  29. * @expectedException \InvalidArgumentException
  30. */
  31. public function testThrowsExceptionOnInvalidOptions() {
  32. MarkdownConverter::resolveOptions([
  33. 'barf' => 'poop',
  34. ]);
  35. }
  36. /**
  37. * @dataProvider optionsProvider
  38. *
  39. * @param array $options
  40. */
  41. public function testCanResolveOptions(array $expected, array $options) {
  42. $this->assertSame($expected, MarkdownConverter::resolveOptions($options));
  43. }
  44. public function optionsProvider() {
  45. yield [[
  46. 'base_path' => '',
  47. 'open_external_links_in_new_tab' => false,
  48. ], []];
  49. yield [[
  50. 'base_path' => '/foo',
  51. 'open_external_links_in_new_tab' => false,
  52. ], [
  53. 'base_path' => '/foo',
  54. ]];
  55. yield [[
  56. 'base_path' => '/foo',
  57. 'open_external_links_in_new_tab' => true,
  58. ], [
  59. 'open_external_links_in_new_tab' => true,
  60. 'base_path' => '/foo',
  61. ]];
  62. }
  63. }