4
0

entryHeaderTest.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\PreserveGlobalState;
  4. use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
  5. require_once LIB_PATH . '/Minz/Url.php'; // For _url()
  6. #[RunTestsInSeparateProcesses]
  7. #[PreserveGlobalState(false)]
  8. final class entryHeaderTest extends \PHPUnit\Framework\TestCase {
  9. private const ARTICLE_URL = 'https://example.net/article';
  10. private const LINK_ITEM = '//li[contains(concat(" ", normalize-space(@class), " "), " link ")]';
  11. private static function renderEntryHeader(bool $titleOpensWebsite): Dom\XPath {
  12. FreshRSS_Context::initSystem();
  13. Minz_Session::init('FreshRSS', volatile: true);
  14. FreshRSS_Context::setUserConf(clone FreshRSS_UserConfiguration::default());
  15. FreshRSS_Context::userConf()->title_opens_website = $titleOpensWebsite;
  16. Minz_Translate::init('en');
  17. $view = new FreshRSS_View();
  18. $view->_path('helpers/index/normal/entry_header.phtml');
  19. $view->feed = new FreshRSS_Feed('https://example.net/feed.xml', false);
  20. $view->entry = new FreshRSS_Entry(1, 'guid', 'Title', '', 'Content', self::ARTICLE_URL, 1700000000);
  21. // Fragment of a page, so prefix with `<!DOCTYPE html>`
  22. $doc = Dom\HTMLDocument::createFromString('<!DOCTYPE html>' . $view->renderToString(), Dom\HTML_NO_DEFAULT_NS);
  23. return new Dom\XPath($doc);
  24. }
  25. private static function countNodes(Dom\XPath $xpath, string $query): int {
  26. return $xpath->query($query)->length;
  27. }
  28. public function testLinkIconOpensOriginalWebsiteByDefault(): void {
  29. $xpath = self::renderEntryHeader(titleOpensWebsite: false);
  30. self::assertSame(1, self::countNodes($xpath, self::LINK_ITEM . '/a[@href="' . self::ARTICLE_URL . '"]'));
  31. self::assertSame(0, self::countNodes($xpath, self::LINK_ITEM . '/button'));
  32. }
  33. public function testLinkIconOpensArticleInFreshRSSWhenTitleOpensWebsite(): void {
  34. $xpath = self::renderEntryHeader(titleOpensWebsite: true);
  35. self::assertSame(1, self::countNodes($xpath, self::LINK_ITEM . '/button[@type="button"]'));
  36. self::assertSame(0, self::countNodes($xpath, self::LINK_ITEM . '/a'));
  37. // The title is now the way to open the original website
  38. self::assertSame(1, self::countNodes($xpath,
  39. '//a[contains(concat(" ", normalize-space(@class), " "), " title ")][@href="' . self::ARTICLE_URL . '"][@target="_blank"]'));
  40. }
  41. }