diff options
| author | 2025-12-10 22:41:45 +0100 | |
|---|---|---|
| committer | 2025-12-10 22:41:45 +0100 | |
| commit | 394411677ea9b3e5bb520c39db6e39f751c35e28 (patch) | |
| tree | db95d211685ad295b147203390f57b1877dd45f6 /tests/app/Models | |
| parent | e85d8053511b12de9296b6acd3e963b3a6e82242 (diff) | |
Add functions to modify a search expression (#8293)
* Allows easier modifications of the search expression.
* Add proper `__toString()` instead of just returning the raw input string. Allows in particular showing the result of the actual parsing of the raw input string in the UI.
Needed for https://github.com/FreshRSS/FreshRSS/pull/8294
Diffstat (limited to 'tests/app/Models')
| -rw-r--r-- | tests/app/Models/SearchTest.php | 165 |
1 files changed, 165 insertions, 0 deletions
diff --git a/tests/app/Models/SearchTest.php b/tests/app/Models/SearchTest.php index b7cb43f8f..92024c7b6 100644 --- a/tests/app/Models/SearchTest.php +++ b/tests/app/Models/SearchTest.php @@ -915,4 +915,169 @@ final class SearchTest extends \PHPUnit\Framework\TestCase { ], ]; } + + #[DataProvider('provideToString')] + public static function test__toString(string $input): void { + $search = new FreshRSS_Search($input); + $expected = str_replace("\n", ' ', $input); + self::assertSame($expected, $search->__toString()); + } + + /** + * @return array<array<string>> + */ + public static function provideToString(): array { + return [ + [ + <<<'EOD' + e:1,2 f:10,11 c:20,21 L:30,31 labels:"My label,My other label" + userdate:2025-01-01T00:00:00/2026-01-01T00:00:00 + pubdate:2025-02-01T00:00:00/2026-01-01T00:00:00 + date:2025-03-01T00:00:00/2026-01-01T00:00:00 + intitle:/Interesting/i intitle:good + intext:/Interesting/i intext:good + author:/Bob/ author:Alice + inurl:/https/ inurl:example.net + #/tag2/ #tag1 + /search_regex/i "quoted search" search + -e:3,4 -f:12,13 -c:22,23 -L:32,33 -labels:"Not label,Not other label" + -userdate:2025-06-01T00:00:00/2025-09-01T00:00:00 + -pubdate:2025-06-01T00:00:00/2025-09-01T00:00:00 + -date:2025-06-01T00:00:00/2025-09-01T00:00:00 + -intitle:/Spam/i -intitle:bad + -intext:/Spam/i -intext:bad + -author:/Dave/i -author:Charlie + -inurl:/ftp/ -inurl:example.com + -#/tag4/ -#tag3 + -/not_regex/i -"not quoted" -not_search + EOD + ], + ]; + } + + #[DataProvider('provideBooleanSearchToString')] + public static function testBooleanSearch__toString(string $input, string $expected): void { + $search = new FreshRSS_BooleanSearch($input); + self::assertSame($expected, $search->__toString()); + } + + /** + * @return array<array<string>> + */ + public static function provideBooleanSearchToString(): array { + return [ + [ + '((a OR b) (c OR d) -e) OR -(f g)', + '((a OR b) (c OR d) (-e)) OR -(f g)', + ], + [ + '((a OR b) ((c) OR ((d))) (-e)) OR -(((f g)))', + '((a OR b) (c OR d) (-e)) OR -(f g)', + ], + [ + '!((b c))', + '-(b c)', + ], + [ + '(a) OR !((b c))', + 'a OR -(b c)', + ], + [ + '((a) (b))', + 'a b', + ], + [ + '((a) OR (b))', + 'a OR b', + ], + [ + ' ( !( !( ( a ) ) ) ) ( ) ', + '-(-a)', + ], + [ + '-intitle:a -inurl:b', + '-intitle:a -inurl:b', + ], + ]; + } + + #[DataProvider('provideHasSameOperators')] + public function testHasSameOperators(string $input1, string $input2, bool $expected): void { + $search1 = new FreshRSS_Search($input1); + $search2 = new FreshRSS_Search($input2); + self::assertSame($expected, $search1->hasSameOperators($search2)); + } + + /** + * @return array<array{string,string,bool}> + */ + public static function provideHasSameOperators(): array { + return [ + ['', '', true], + ['intitle:a intext:b', 'intitle:c intext:d', true], + ['intitle:a intext:b', 'intitle:c inurl:d', false], + ]; + } + + #[DataProvider('provideBooleanSearchEnforce')] + public function testBooleanSearchEnforce(string $initialInput, string $enforceInput, string $expectedOutput): void { + $booleanSearch = new FreshRSS_BooleanSearch($initialInput); + $searchToEnforce = new FreshRSS_Search($enforceInput); + $newBooleanSearch = $booleanSearch->enforce($searchToEnforce); + self::assertNotSame($booleanSearch, $newBooleanSearch); + self::assertSame($expectedOutput, $newBooleanSearch->__toString()); + } + + /** + * @return array<array{string,string,string}> + */ + public static function provideBooleanSearchEnforce(): array { + return [ + ['', 'intitle:b', 'intitle:b'], + ['intitle:a', 'intitle:b', 'intitle:b'], + ['a', 'intitle:b', 'intitle:b a'], + ['intitle:a intext:a', 'intitle:b', 'intitle:b intext:a'], + ['intitle:a inurl:a', 'intitle:b', 'intitle:b inurl:a'], + ['intitle:a OR inurl:a', 'intitle:b', 'intitle:b (intitle:a OR inurl:a)'], + ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', 'intitle:b (inurl:a intitle:c)'], + ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', 'intitle:b (inurl:a OR intitle:c)'], + ['(intitle:a) (inurl:a)', 'intitle:b', 'intitle:b inurl:a'], + ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a intitle:b'], + ['(a b) OR (c d)', 'e', 'e ((a b) OR (c d))'], + ['(a b) (c d)', 'e', 'e ((a b) (c d))'], + ['(a b)', 'e', 'e (a b)'], + ['date:2024/', 'date:/2025', 'date:/2025-12-31T23:59:59'], + ['a', 'date:/2025', 'date:/2025-12-31T23:59:59 a'], + ]; + } + + #[DataProvider('provideBooleanSearchRemove')] + public function testBooleanSearchRemove(string $initialInput, string $removeInput, string $expectedOutput): void { + $booleanSearch = new FreshRSS_BooleanSearch($initialInput); + $searchToRemove = new FreshRSS_Search($removeInput); + $newBooleanSearch = $booleanSearch->remove($searchToRemove); + self::assertNotSame($booleanSearch, $newBooleanSearch); + self::assertSame($expectedOutput, $newBooleanSearch->__toString()); + } + + /** + * @return array<array{string,string,string}> + */ + public static function provideBooleanSearchRemove(): array { + return [ + ['', 'intitle:b', ''], + ['intitle:a', 'intitle:b', ''], + ['intitle:a intext:a', 'intitle:b', 'intext:a'], + ['intitle:a inurl:a', 'intitle:b', 'inurl:a'], + ['intitle:a OR inurl:a', 'intitle:b', 'intitle:a OR inurl:a'], + ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', '(inurl:a intitle:c)'], + ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', '(inurl:a OR intitle:c)'], + ['(intitle:a) (inurl:a)', 'intitle:b', 'inurl:a'], + ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a'], + ['e ((a b) OR (c d))', 'e', '((a b) OR (c d))'], + ['e ((a b) (c d))', 'e', '((a b) (c d))'], + ['date:2024/', 'date:/2025', ''], + ['date:2024/ a', 'date:/2025', 'a'], + ]; + } } |
