test: add feed item test (#3709)

* test: add feed item test

also some refactor

* yup

* yup
This commit is contained in:
Dag 2023-09-25 22:32:15 +02:00 committed by GitHub
parent cd30c25b08
commit f421c45b21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 98 additions and 81 deletions

View File

@ -73,6 +73,15 @@ class FilterBridge extends FeedExpander
],
]];
public function collectData()
{
$url = $this->getInput('url');
if (!Url::validate($url)) {
returnClientError('The url parameter must either refer to http or https protocol.');
}
$this->collectExpandableDatas($this->getURI());
}
protected function parseItem($newItem)
{
$item = parent::parseItem($newItem);
@ -158,13 +167,4 @@ class FilterBridge extends FeedExpander
return $url;
}
public function collectData()
{
if ($this->getInput('url') && substr($this->getInput('url'), 0, 4) !== 'http') {
// just in case someone finds a way to access local files by playing with the url
returnClientError('The url parameter must either refer to http or https protocol.');
}
$this->collectExpandableDatas($this->getURI());
}
}

View File

@ -14,8 +14,6 @@ class AtomFormat extends FormatAbstract
protected const ATOM_NS = 'http://www.w3.org/2005/Atom';
protected const MRSS_NS = 'http://search.yahoo.com/mrss/';
const LIMIT_TITLE = 140;
public function stringify()
{
$feedUrl = get_current_url();
@ -109,8 +107,8 @@ class AtomFormat extends FormatAbstract
if (empty($entryTitle)) {
$entryTitle = str_replace("\n", ' ', strip_tags($entryContent));
if (strlen($entryTitle) > self::LIMIT_TITLE) {
$wrapPos = strpos(wordwrap($entryTitle, self::LIMIT_TITLE), "\n");
if (strlen($entryTitle) > 140) {
$wrapPos = strpos(wordwrap($entryTitle, 140), "\n");
$entryTitle = substr($entryTitle, 0, $wrapPos) . '...';
}
}
@ -182,11 +180,11 @@ class AtomFormat extends FormatAbstract
}
}
$toReturn = $document->saveXML();
$xml = $document->saveXML();
// Remove invalid characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
$xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8');
return $xml;
}
}

View File

@ -6,6 +6,8 @@ class HtmlFormat extends FormatAbstract
public function stringify()
{
$queryString = $_SERVER['QUERY_STRING'];
$extraInfos = $this->getExtraInfos();
$formatFactory = new FormatFactory();
$buttons = [];
@ -15,7 +17,7 @@ class HtmlFormat extends FormatAbstract
if ($format === 'Html') {
continue;
}
$formatUrl = '?' . str_ireplace('format=Html', 'format=' . $format, htmlentities($_SERVER['QUERY_STRING']));
$formatUrl = '?' . str_ireplace('format=Html', 'format=' . $format, htmlentities($queryString));
$buttons[] = [
'href' => $formatUrl,
'value' => $format,
@ -57,6 +59,7 @@ class HtmlFormat extends FormatAbstract
]);
// Remove invalid characters
ini_set('mbstring.substitute_character', 'none');
return mb_convert_encoding($html, $this->getCharset(), 'UTF-8');
$html = mb_convert_encoding($html, $this->getCharset(), 'UTF-8');
return $html;
}
}

View File

@ -110,13 +110,8 @@ class JsonFormat extends FormatAbstract
}
$data['items'] = $items;
/**
* The intention here is to discard non-utf8 byte sequences.
* But the JSON_PARTIAL_OUTPUT_ON_ERROR also discards lots of other errors.
* So consider this a hack.
* Switch to JSON_INVALID_UTF8_IGNORE when PHP 7.2 is the latest platform requirement.
*/
$json = json_encode($data, \JSON_PRETTY_PRINT | \JSON_PARTIAL_OUTPUT_ON_ERROR);
// Ignoring invalid json
$json = json_encode($data, \JSON_PRETTY_PRINT | \JSON_INVALID_UTF8_IGNORE);
return $json;
}

View File

@ -32,12 +32,6 @@ class MrssFormat extends FormatAbstract
protected const ATOM_NS = 'http://www.w3.org/2005/Atom';
protected const MRSS_NS = 'http://search.yahoo.com/mrss/';
const ALLOWED_IMAGE_EXT = [
'.gif',
'.jpg',
'.png',
];
public function stringify()
{
$feedUrl = get_current_url();
@ -72,8 +66,13 @@ class MrssFormat extends FormatAbstract
$channel->appendChild($description);
$description->appendChild($document->createTextNode($extraInfos['name']));
$allowedIconExtensions = [
'.gif',
'.jpg',
'.png',
];
$icon = $extraInfos['icon'];
if (!empty($icon) && in_array(substr($icon, -4), self::ALLOWED_IMAGE_EXT)) {
if (!empty($icon) && in_array(substr($icon, -4), $allowedIconExtensions)) {
$feedImage = $document->createElement('image');
$channel->appendChild($feedImage);
$iconUrl = $document->createElement('url');
@ -164,11 +163,10 @@ class MrssFormat extends FormatAbstract
}
}
$toReturn = $document->saveXML();
$xml = $document->saveXML();
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
$xml = mb_convert_encoding($xml, $this->getCharset(), 'UTF-8');
return $xml;
}
}

View File

@ -6,18 +6,14 @@ class PlaintextFormat extends FormatAbstract
public function stringify()
{
$items = $this->getItems();
$data = [];
foreach ($items as $item) {
foreach ($this->getItems() as $item) {
$data[] = $item->toArray();
}
$toReturn = print_r($data, true);
$text = print_r($data, true);
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
$text = mb_convert_encoding($text, $this->getCharset(), 'UTF-8');
return $text;
}
}

View File

@ -4,6 +4,38 @@ class SfeedFormat extends FormatAbstract
{
const MIME_TYPE = 'text/plain';
public function stringify()
{
$text = '';
foreach ($this->getItems() as $item) {
$text .= sprintf(
"%s\t%s\t%s\t%s\thtml\t\t%s\t%s\t%s\n",
$item->toArray()['timestamp'],
preg_replace('/\s/', ' ', $item->toArray()['title']),
$item->toArray()['uri'],
$this->escape($item->toArray()['content']),
$item->toArray()['author'],
$this->getFirstEnclosure(
$item->toArray()['enclosures']
),
$this->escape(
$this->getCategories(
$item->toArray()['categories']
)
)
);
}
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$text = mb_convert_encoding(
$text,
$this->getCharset(),
'UTF-8'
);
return $text;
}
private function escape(string $str)
{
$str = str_replace('\\', '\\\\', $str);
@ -31,39 +63,4 @@ class SfeedFormat extends FormatAbstract
}
return $toReturn;
}
public function stringify()
{
$items = $this->getItems();
$toReturn = '';
foreach ($items as $item) {
$toReturn .= sprintf(
"%s\t%s\t%s\t%s\thtml\t\t%s\t%s\t%s\n",
$item->toArray()['timestamp'],
preg_replace('/\s/', ' ', $item->toArray()['title']),
$item->toArray()['uri'],
$this->escape($item->toArray()['content']),
$item->toArray()['author'],
$this->getFirstEnclosure(
$item->toArray()['enclosures']
),
$this->escape(
$this->getCategories(
$item->toArray()['categories']
)
)
);
}
// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding(
$toReturn,
$this->getCharset(),
'UTF-8'
);
return $toReturn;
}
}
// vi: expandtab

View File

@ -15,6 +15,7 @@ function getContents(
bool $returnFull = false
) {
$httpClient = RssBridge::getHttpClient();
$cache = RssBridge::getCache();
$httpHeadersNormalized = [];
foreach ($httpHeaders as $httpHeader) {
@ -51,7 +52,6 @@ function getContents(
$config['proxy'] = Configuration::getConfig('proxy', 'url');
}
$cache = RssBridge::getCache();
$cacheKey = 'server_' . $url;
/** @var Response $cachedResponse */

View File

@ -148,7 +148,7 @@ final class StreamHandler
$context
);
error_log($text);
if (Debug::isEnabled()) {
if ($record['level'] < Logger::ERROR && Debug::isEnabled()) {
print sprintf("<pre>%s</pre>\n", e($text));
}
//$bytes = file_put_contents('/tmp/rss-bridge.log', $text, FILE_APPEND | LOCK_EX);

30
tests/FeedItemTest.php Normal file
View File

@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace RssBridge\Tests;
use FeedItem;
use PHPUnit\Framework\TestCase;
class FeedItemTest extends TestCase
{
public function test()
{
$item = new FeedItem();
$item->setTitle('hello');
$this->assertSame('hello', $item->getTitle());
$item = FeedItem::fromArray(['title' => 'hello2']);
$this->assertSame('hello2', $item->getTitle());
$item = new FeedItem();
$item->setAuthor('123');
$this->assertSame('123', $item->getAuthor());
$item = new FeedItem();
$item->title = 'aa';
$this->assertSame('aa', $item->getTitle());
$this->assertSame('aa', $item->title);
}
}