4
0

entryHeaderTest.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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): DOMXPath {
  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. $html = new DOMDocument();
  22. self::assertTrue($html->loadHTML($view->renderToString(), LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING));
  23. return new DOMXPath($html);
  24. }
  25. private static function countNodes(DOMXPath $xpath, string $query): int {
  26. $nodes = $xpath->query($query);
  27. self::assertNotFalse($nodes);
  28. return $nodes->length;
  29. }
  30. public function testLinkIconOpensOriginalWebsiteByDefault(): void {
  31. $xpath = self::renderEntryHeader(titleOpensWebsite: false);
  32. self::assertSame(1, self::countNodes($xpath, self::LINK_ITEM . '/a[@href="' . self::ARTICLE_URL . '"]'));
  33. self::assertSame(0, self::countNodes($xpath, self::LINK_ITEM . '/button'));
  34. }
  35. public function testLinkIconOpensArticleInFreshRSSWhenTitleOpensWebsite(): void {
  36. $xpath = self::renderEntryHeader(titleOpensWebsite: true);
  37. self::assertSame(1, self::countNodes($xpath, self::LINK_ITEM . '/button[@type="button"]'));
  38. self::assertSame(0, self::countNodes($xpath, self::LINK_ITEM . '/a'));
  39. // The title is now the way to open the original website
  40. self::assertSame(1, self::countNodes($xpath,
  41. '//a[contains(concat(" ", normalize-space(@class), " "), " title ")][@href="' . self::ARTICLE_URL . '"][@target="_blank"]'));
  42. }
  43. }