ExcerptsTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. use Codeception\Util\Fixtures;
  3. use Grav\Common\Helpers\Excerpts;
  4. use Grav\Common\Grav;
  5. use Grav\Common\Page\Interfaces\PageInterface;
  6. use Grav\Common\Uri;
  7. use Grav\Common\Config\Config;
  8. use Grav\Common\Page\Pages;
  9. use Grav\Common\Language\Language;
  10. use RocketTheme\Toolbox\ResourceLocator\UniformResourceLocator;
  11. /**
  12. * Class ExcerptsTest
  13. */
  14. class ExcerptsTest extends \Codeception\TestCase\Test
  15. {
  16. /** @var Parsedown $parsedown */
  17. protected $parsedown;
  18. /** @var Grav $grav */
  19. protected $grav;
  20. /** @var PageInterface $page */
  21. protected $page;
  22. /** @var Pages $pages */
  23. protected $pages;
  24. /** @var Config $config */
  25. protected $config;
  26. /** @var Uri $uri */
  27. protected $uri;
  28. /** @var Language $language */
  29. protected $language;
  30. protected $old_home;
  31. protected function _before(): void
  32. {
  33. $grav = Fixtures::get('grav');
  34. $this->grav = $grav();
  35. $this->pages = $this->grav['pages'];
  36. $this->config = $this->grav['config'];
  37. $this->uri = $this->grav['uri'];
  38. $this->language = $this->grav['language'];
  39. $this->old_home = $this->config->get('system.home.alias');
  40. $this->config->set('system.home.alias', '/item1');
  41. $this->config->set('system.absolute_urls', false);
  42. $this->config->set('system.languages.supported', []);
  43. unset($this->grav['language']);
  44. $this->grav['language'] = new Language($this->grav);
  45. /** @var UniformResourceLocator $locator */
  46. $locator = $this->grav['locator'];
  47. $locator->addPath('page', '', 'tests/fake/nested-site/user/pages', false);
  48. $this->pages->init();
  49. $defaults = [
  50. 'extra' => false,
  51. 'auto_line_breaks' => false,
  52. 'auto_url_links' => false,
  53. 'escape_markup' => false,
  54. 'special_chars' => ['>' => 'gt', '<' => 'lt'],
  55. ];
  56. $this->page = $this->pages->find('/item2/item2-2');
  57. $this->uri->initializeWithURL('http://testing.dev/item2/item2-2')->init();
  58. }
  59. protected function _after(): void
  60. {
  61. $this->config->set('system.home.alias', $this->old_home);
  62. }
  63. public function testProcessImageHtml(): void
  64. {
  65. self::assertRegexp(
  66. '|<img alt="Sample Image" src="\/images\/.*-sample-image.jpe?g\" data-src="sample-image\.jpg\?cropZoom=300,300" \/>|',
  67. Excerpts::processImageHtml('<img src="sample-image.jpg?cropZoom=300,300" alt="Sample Image" />', $this->page)
  68. );
  69. self::assertRegexp(
  70. '|<img alt="Sample Image" class="foo" src="\/images\/.*-sample-image.jpe?g\" data-src="sample-image\.jpg\?classes=foo" \/>|',
  71. Excerpts::processImageHtml('<img src="sample-image.jpg?classes=foo" alt="Sample Image" />', $this->page)
  72. );
  73. }
  74. public function testNoProcess(): void
  75. {
  76. self::assertStringStartsWith(
  77. '<a href="https://play.google.com/store/apps/details?hl=de" id="org.jitsi.meet" target="_blank"',
  78. Excerpts::processLinkHtml('<a href="https://play.google.com/store/apps/details?id=org.jitsi.meet&hl=de&target=_blank">regular process</a>')
  79. );
  80. self::assertStringStartsWith(
  81. '<a href="https://play.google.com/store/apps/details?id=org.jitsi.meet&hl=de&target=_blank"',
  82. Excerpts::processLinkHtml('<a href="https://play.google.com/store/apps/details?id=org.jitsi.meet&hl=de&target=_blank&noprocess">noprocess</a>')
  83. );
  84. self::assertStringStartsWith(
  85. '<a href="https://play.google.com/store/apps/details?id=org.jitsi.meet&hl=de" target="_blank"',
  86. Excerpts::processLinkHtml('<a href="https://play.google.com/store/apps/details?id=org.jitsi.meet&hl=de&target=_blank&noprocess=id">noprocess=id</a>')
  87. );
  88. }
  89. public function testTarget(): void
  90. {
  91. self::assertStringStartsWith(
  92. '<a href="https://play.google.com/store/apps/details" target="_blank"',
  93. Excerpts::processLinkHtml('<a href="https://play.google.com/store/apps/details?target=_blank">only target</a>')
  94. );
  95. self::assertStringStartsWith(
  96. '<a href="https://meet.weikamp.biz/Support" rel="nofollow" target="_blank"',
  97. Excerpts::processLinkHtml('<a href="https://meet.weikamp.biz/Support?rel=nofollow&target=_blank">target and rel</a>')
  98. );
  99. }
  100. }