mirror of
https://github.com/FreshRSS/FreshRSS.git
synced 2026-10-28 14:08:45 -04:00
* Preserve explicit sort overrides when searching Closes #9466 Changes proposed in this pull request: - Keep an explicit sort override when submitting the header search form, including `c.name`, `f.name` and `lastUserModified`. - Continue using the already validated `FreshRSS_Context::$sort` value; omit the hidden field when no non-empty override was requested. - Add header-rendering regression tests for all nine supported sorts, absent/empty overrides and invalid input. * A bit more fixing * Fix line mistake * Forgotten plaintext * Fix test --------- Co-authored-by: Alexandre Alapetite <alexandre@alapetite.fr>
53 lines
2.2 KiB
PHP
53 lines
2.2 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
use PHPUnit\Framework\Attributes\DataProvider;
|
|
use PHPUnit\Framework\Attributes\PreserveGlobalState;
|
|
use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
|
|
|
|
#[RunTestsInSeparateProcesses]
|
|
#[PreserveGlobalState(false)]
|
|
final class headerTest extends \PHPUnit\Framework\TestCase {
|
|
|
|
/** @return Traversable<string,array{?string,?string}> */
|
|
public static function provideSearchSorts(): Traversable {
|
|
foreach (['id', 'c.name', 'date', 'f.name', 'lastUserModified', 'length', 'link', 'title', 'rand'] as $sort) {
|
|
yield $sort => [$sort, $sort];
|
|
}
|
|
yield 'no override' => [null, null];
|
|
yield 'empty override' => ['', null];
|
|
yield 'invalid override' => ['" onfocus="alert(1)', null];
|
|
}
|
|
|
|
#[DataProvider('provideSearchSorts')]
|
|
public function testSearchPreservesSort(?string $requestedSort, ?string $expectedSort): void {
|
|
FreshRSS_Context::initSystem();
|
|
FreshRSS_Context::systemConf()->allow_anonymous = true;
|
|
Minz_User::change(FreshRSS_Context::systemConf()->default_user);
|
|
FreshRSS_Context::setUserConf(clone FreshRSS_UserConfiguration::default());
|
|
Minz_Translate::init('en');
|
|
Minz_Request::_params($requestedSort === null ? [] : ['sort' => $requestedSort]);
|
|
// The context has already validated the requested sort before rendering the header.
|
|
FreshRSS_Context::$sort = match ($expectedSort) {
|
|
'c.name', 'date', 'f.name', 'lastUserModified', 'length', 'link', 'title', 'rand' => $expectedSort,
|
|
default => 'id',
|
|
};
|
|
FreshRSS_Context::$search = new FreshRSS_BooleanSearch('needle');
|
|
|
|
$view = new FreshRSS_View();
|
|
$view->_path('../layout/header.phtml');
|
|
$view->html_url = 'https://example.net/';
|
|
$html = new DOMDocument();
|
|
self::assertTrue($html->loadHTML($view->renderToString(), LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING));
|
|
$inputs = (new DOMXPath($html))->query('//form[.//input[@name="search"]]/input[@name="sort"]');
|
|
self::assertNotFalse($inputs);
|
|
self::assertSame($expectedSort === null ? 0 : 1, $inputs->length);
|
|
if ($expectedSort !== null) {
|
|
$input = $inputs->item(0);
|
|
self::assertInstanceOf(DOMElement::class, $input);
|
|
self::assertSame('hidden', $input->getAttribute('type'));
|
|
self::assertSame($expectedSort, $input->getAttribute('value'));
|
|
}
|
|
}
|
|
}
|