4
0

SearchTest.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. require_once LIB_PATH . '/lib_date.php';
  5. final class SearchTest extends \PHPUnit\Framework\TestCase {
  6. #[\Override]
  7. protected function setUp(): void {
  8. if (!FreshRSS_Context::hasSystemConf()) {
  9. FreshRSS_Context::initSystem();
  10. }
  11. }
  12. #[DataProvider('provideEmptyInput')]
  13. public static function test__construct_whenInputIsEmpty_getsOnlyNullValues(string $input): void {
  14. $search = new FreshRSS_Search($input);
  15. self::assertSame('', $search->__toString());
  16. self::assertNull($search->getIntitle());
  17. self::assertNull($search->getMinDate());
  18. self::assertNull($search->getMaxDate());
  19. self::assertNull($search->getMinPubdate());
  20. self::assertNull($search->getMaxPubdate());
  21. self::assertNull($search->getAuthor());
  22. self::assertNull($search->getTags());
  23. self::assertNull($search->getSearch());
  24. }
  25. /**
  26. * Return an array of values for the search object.
  27. * Here is the description of the values
  28. * @return array{array{''},array{' '}}
  29. */
  30. public static function provideEmptyInput(): array {
  31. return [
  32. [''],
  33. [' '],
  34. ];
  35. }
  36. /**
  37. * @param array<string>|null $intitle_value
  38. * @param array<string>|null $search_value
  39. */
  40. #[DataProvider('provideIntitleSearch')]
  41. public static function test__construct_whenInputContainsIntitle_setsIntitleProperty(string $input, ?array $intitle_value, ?array $search_value): void {
  42. $search = new FreshRSS_Search($input);
  43. self::assertSame($intitle_value, $search->getIntitle());
  44. self::assertSame($search_value, $search->getSearch());
  45. }
  46. /**
  47. * @return list<list<mixed>>
  48. */
  49. public static function provideIntitleSearch(): array {
  50. return [
  51. ['intitle:word1', ['word1'], null],
  52. ['intitle:word1-word2', ['word1-word2'], null],
  53. ['intitle:word1 word2', ['word1'], ['word2']],
  54. ['intitle:"word1 word2"', ['word1 word2'], null],
  55. ["intitle:'word1 word2'", ['word1 word2'], null],
  56. ['word1 intitle:word2', ['word2'], ['word1']],
  57. ['word1 intitle:word2 word3', ['word2'], ['word1', 'word3']],
  58. ['word1 intitle:"word2 word3"', ['word2 word3'], ['word1']],
  59. ["word1 intitle:'word2 word3'", ['word2 word3'], ['word1']],
  60. ['intitle:word1 intitle:word2', ['word1', 'word2'], null],
  61. ['intitle:word1 intitle:word10', ['word1', 'word10'], null],
  62. ['intitle: word1 word2', null, ['word1', 'word2']],
  63. ['intitle:123', ['123'], null],
  64. ['intitle:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  65. ["intitle:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  66. ['intitle:"word1 word2\' word3"', ["word1 word2' word3"], null],
  67. ["intitle:'word1 word2\" word3'", ['word1 word2" word3'], null],
  68. ['intitle:"< & >"', ['&lt; &amp; &gt;'], null],
  69. ["intitle:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  70. ['intitle:word1+word2', ['word1+word2'], null],
  71. ];
  72. }
  73. /**
  74. * @param array<string>|null $intext_value
  75. * @param array<string>|null $search_value
  76. */
  77. #[DataProvider('provideIntextSearch')]
  78. public static function test__construct_whenInputContainsIntext(string $input, ?array $intext_value, ?array $search_value): void {
  79. $search = new FreshRSS_Search($input);
  80. self::assertSame($intext_value, $search->getIntext());
  81. self::assertSame($search_value, $search->getSearch());
  82. }
  83. /**
  84. * @return list<list<mixed>>
  85. */
  86. public static function provideIntextSearch(): array {
  87. return [
  88. ['intext:word1', ['word1'], null],
  89. ['intext:"word1 word2"', ['word1 word2'], null],
  90. ];
  91. }
  92. /**
  93. * @param array<string>|null $author_value
  94. * @param array<string>|null $search_value
  95. */
  96. #[DataProvider('provideAuthorSearch')]
  97. public static function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void {
  98. $search = new FreshRSS_Search($input);
  99. self::assertSame($author_value, $search->getAuthor());
  100. self::assertSame($search_value, $search->getSearch());
  101. }
  102. /**
  103. * @return list<list<mixed>>
  104. */
  105. public static function provideAuthorSearch(): array {
  106. return [
  107. ['author:word1', ['word1'], null],
  108. ['author:word1-word2', ['word1-word2'], null],
  109. ['author:word1 word2', ['word1'], ['word2']],
  110. ['author:"word1 word2"', ['word1 word2'], null],
  111. ["author:'word1 word2'", ['word1 word2'], null],
  112. ['word1 author:word2', ['word2'], ['word1']],
  113. ['word1 author:word2 word3', ['word2'], ['word1', 'word3']],
  114. ['word1 author:"word2 word3"', ['word2 word3'], ['word1']],
  115. ["word1 author:'word2 word3'", ['word2 word3'], ['word1']],
  116. ['author:word1 author:word2', ['word1', 'word2'], null],
  117. ['author:word1 author:word10', ['word1', 'word10'], null],
  118. ['author: word1 word2', null, ['word1', 'word2']],
  119. ['author:123', ['123'], null],
  120. ['author:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  121. ["author:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  122. ['author:"word1 word2\' word3"', ["word1 word2' word3"], null],
  123. ["author:'word1 word2\" word3'", ['word1 word2" word3'], null],
  124. ["author:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  125. ['author:word1+word2', ['word1+word2'], null],
  126. ];
  127. }
  128. /**
  129. * @param array<string>|null $inurl_value
  130. * @param array<string>|null $search_value
  131. */
  132. #[DataProvider('provideInurlSearch')]
  133. public static function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void {
  134. $search = new FreshRSS_Search($input);
  135. self::assertSame($inurl_value, $search->getInurl());
  136. self::assertSame($search_value, $search->getSearch());
  137. }
  138. /**
  139. * @return list<list<mixed>>
  140. */
  141. public static function provideInurlSearch(): array {
  142. return [
  143. ['inurl:word1', ['word1'], null],
  144. ['inurl: word1', null, ['word1']],
  145. ['inurl:123', ['123'], null],
  146. ['inurl:word1 word2', ['word1'], ['word2']],
  147. ['inurl:"word1 word2"', ['word1 word2'], null],
  148. ['inurl:word1 word2 inurl:word3', ['word1', 'word3'], ['word2']],
  149. ['inurl:word1 inurl:word10', ['word1', 'word10'], null],
  150. ["inurl:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  151. ['inurl:word1+word2', ['word1+word2'], null],
  152. ];
  153. }
  154. #[DataProvider('provideDateSearch')]
  155. public static function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void {
  156. $search = new FreshRSS_Search($input);
  157. self::assertSame($min_date_value, $search->getMinDate());
  158. self::assertSame($max_date_value, $search->getMaxDate());
  159. }
  160. /**
  161. * @return list<list<mixed>>
  162. */
  163. public static function provideDateSearch(): array {
  164. return [
  165. ['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  166. ['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  167. ['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  168. ['date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  169. ['date:2007-03-01/', strtotime('2007-03-01'), null],
  170. ['date:/2008-05-11', null, strtotime('2008-05-12') - 1],
  171. ];
  172. }
  173. #[DataProvider('providePubdateSearch')]
  174. public static function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void {
  175. $search = new FreshRSS_Search($input);
  176. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  177. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  178. }
  179. /**
  180. * @return list<list<mixed>>
  181. */
  182. public static function providePubdateSearch(): array {
  183. return [
  184. ['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  185. ['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  186. ['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  187. ['pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  188. ['pubdate:2007-03-01/', strtotime('2007-03-01'), null],
  189. ['pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  190. ];
  191. }
  192. #[DataProvider('provideModifiedDateSearch')]
  193. public static function test__construct_whenInputContainsModifiedDate(string $input, ?int $min_modified_value, ?int $max_modified_value): void {
  194. $search = new FreshRSS_Search($input);
  195. self::assertSame($min_modified_value, $search->getMinModifiedDate());
  196. self::assertSame($max_modified_value, $search->getMaxModifiedDate());
  197. }
  198. /**
  199. * @return list<list<mixed>>
  200. */
  201. public static function provideModifiedDateSearch(): array {
  202. return [
  203. ['mdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  204. ['mdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  205. ];
  206. }
  207. #[DataProvider('provideUserdateSearch')]
  208. public static function test__construct_whenInputContainsUserdate(string $input, ?int $min_userdate_value, ?int $max_userdate_value): void {
  209. $search = new FreshRSS_Search($input);
  210. self::assertSame($min_userdate_value, $search->getMinUserdate());
  211. self::assertSame($max_userdate_value, $search->getMaxUserdate());
  212. }
  213. /**
  214. * @return list<list<mixed>>
  215. */
  216. public static function provideUserdateSearch(): array {
  217. return [
  218. ['userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  219. ['userdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  220. ];
  221. }
  222. /**
  223. * @param array<string>|null $tags_value
  224. * @param array<string>|null $search_value
  225. */
  226. #[DataProvider('provideTagsSearch')]
  227. public static function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  228. $search = new FreshRSS_Search($input);
  229. self::assertSame($tags_value, $search->getTags());
  230. self::assertSame($search_value, $search->getSearch());
  231. }
  232. /**
  233. * @return list<list<string|list<string>|null>>
  234. */
  235. public static function provideTagsSearch(): array {
  236. return [
  237. ['#word1', ['word1'], null],
  238. ['# word1', null, ['#', 'word1']],
  239. ['#123', ['123'], null],
  240. ['#word1 word2', ['word1'], ['word2']],
  241. ['#"word1 word2"', ['word1 word2'], null],
  242. ['#word1 #word2', ['word1', 'word2'], null],
  243. ['#word1 #word10', ['word1', 'word10'], null],
  244. ['#word10 #word1', ['word10', 'word1'], null],
  245. ["#word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  246. ['#word1+word2', ['word1 word2'], null]
  247. ];
  248. }
  249. /**
  250. * @param list<array{search:string,name?:string}> $queries
  251. * @param array{0:string,1:list<string|int>} $expectedResult
  252. */
  253. #[DataProvider('provideSavedQueriesExpansion')]
  254. public static function test__construct_whenInputContainsSavedQueries_expandsSavedSearches(array $queries, string $input,
  255. array $expectedResult, string $expectedToString): void {
  256. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  257. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  258. $newUserConf->queries = $queries;
  259. FreshRSS_Context::setUserConf($newUserConf);
  260. try {
  261. $search = new FreshRSS_BooleanSearch($input);
  262. [$actualValues, $actualSql] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $search);
  263. self::assertSame($expectedResult[0], trim($actualSql));
  264. self::assertSame($expectedResult[1], $actualValues);
  265. self::assertSame($expectedToString, $search->toString(expandUserQueries: false));
  266. } finally {
  267. FreshRSS_Context::setUserConf($previousUserConf);
  268. }
  269. }
  270. /**
  271. * @return array<string,array{0:list<array{search:string}>,1:string,2:array{0:string,1:list<string|int>},3:string}>
  272. */
  273. public static function provideSavedQueriesExpansion(): array {
  274. return [
  275. 'not found ID' => [
  276. [
  277. ['search' => 'author:Alice'],
  278. ['search' => 'intitle:World'],
  279. ],
  280. 'S:3',
  281. [
  282. '',
  283. [],
  284. ],
  285. 'S:3',
  286. ],
  287. 'not found name' => [
  288. [
  289. ['search' => 'author:Alice', 'name' => 'First'],
  290. ['search' => 'intitle:World', 'name' => 'Second'],
  291. ],
  292. 'search:Third',
  293. [
  294. '',
  295. [],
  296. ],
  297. 'search:Third',
  298. ],
  299. 'expanded single group name' => [
  300. [
  301. ['search' => 'author:Alice', 'name' => 'First'],
  302. ['search' => 'intitle:World', 'name' => 'Second'],
  303. ],
  304. 'search:First OR search:Second',
  305. [
  306. '((e.author LIKE ?)) OR ((e.title LIKE ?))',
  307. ['%Alice%', '%World%'],
  308. ],
  309. 'search:First OR search:Second',
  310. ],
  311. 'expanded single group name quotes' => [
  312. [
  313. ['search' => 'author:Alice', 'name' => 'A First'],
  314. ['search' => 'intitle:World', 'name' => 'A Second'],
  315. ],
  316. 'search:"A First" OR search:\'A Second\'',
  317. [
  318. '((e.author LIKE ?)) OR ((e.title LIKE ?))',
  319. ['%Alice%', '%World%'],
  320. ],
  321. '(search:"A First") OR (search:"A Second")',
  322. ],
  323. 'expanded single group name quotes special characters' => [
  324. [
  325. ['search' => 'author:Alice', 'name' => 'A or B'],
  326. ['search' => 'intitle:World', 'name' => '(C OR D)'],
  327. ],
  328. 'search:"A or B" OR search:\'(C OR D)\'',
  329. [
  330. '((e.author LIKE ?)) OR ((e.title LIKE ?))',
  331. ['%Alice%', '%World%'],
  332. ],
  333. '(search:"A or B") OR (search:"(C OR D)")',
  334. ],
  335. 'separate groups with AND' => [
  336. [
  337. ['search' => 'author:Alice'],
  338. ['search' => 'intitle:World'],
  339. ['search' => 'inurl:Example'],
  340. ['search' => 'author:Bob'],
  341. ],
  342. 'S:0,1 S:2,3,5',
  343. [
  344. '(((e.author LIKE ?)) OR ((e.title LIKE ?))) AND (((e.link LIKE ?)) OR ((e.author LIKE ?)))',
  345. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  346. ],
  347. 'S:0,1 S:2,3,5',
  348. ],
  349. 'separate groups with OR' => [
  350. [
  351. ['search' => 'author:Alice'],
  352. ['search' => 'intitle:World'],
  353. ['search' => 'inurl:Example'],
  354. ['search' => 'author:Bob'],
  355. ],
  356. 'S:0,1 OR S:2,3,5',
  357. [
  358. '(((e.author LIKE ?)) OR ((e.title LIKE ?))) OR (((e.link LIKE ?)) OR ((e.author LIKE ?)))',
  359. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  360. ],
  361. 'S:0,1 OR S:2,3,5',
  362. ],
  363. 'mixed with other clauses' => [
  364. [
  365. ['search' => 'author:Alice'],
  366. ['search' => 'intitle:World'],
  367. ],
  368. 'date:2025-10 intitle:Hello S:0,1',
  369. [
  370. '((e.id >= ? AND e.id <= ? AND e.title LIKE ?)) AND (((e.author LIKE ?)) OR ((e.title LIKE ?)))',
  371. [strtotime('2025-10-01') . '000000', (strtotime('2025-11-01') - 1) . '000000', '%Hello%', '%Alice%', '%World%'],
  372. ],
  373. 'date:2025-10 intitle:Hello S:0,1',
  374. ],
  375. ];
  376. }
  377. /**
  378. * @param array<string>|null $author_value
  379. * @param array<string> $intitle_value
  380. * @param array<string>|null $inurl_value
  381. * @param array<string>|null $tags_value
  382. * @param array<string>|null $search_value
  383. */
  384. #[DataProvider('provideMultipleSearch')]
  385. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  386. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  387. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  388. $search = new FreshRSS_Search($input);
  389. self::assertSame($author_value, $search->getAuthor());
  390. self::assertSame($min_date_value, $search->getMinDate());
  391. self::assertSame($max_date_value, $search->getMaxDate());
  392. self::assertSame($intitle_value, $search->getIntitle());
  393. self::assertSame($inurl_value, $search->getInurl());
  394. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  395. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  396. self::assertSame($tags_value, $search->getTags());
  397. self::assertSame($search_value, $search->getSearch());
  398. }
  399. /** @return list<list<mixed>> */
  400. public static function provideMultipleSearch(): array {
  401. return [
  402. [
  403. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  404. ['word1'],
  405. strtotime('2007-03-01'),
  406. strtotime('2008-05-12') - 1,
  407. ['word2'],
  408. ['word3'],
  409. strtotime('2007-03-01'),
  410. strtotime('2008-05-12') - 1,
  411. ['word4', 'word5'],
  412. null
  413. ],
  414. [
  415. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  416. ['word1'],
  417. strtotime('2007-03-01'),
  418. strtotime('2008-05-12') - 1,
  419. ['word2'],
  420. ['word3'],
  421. strtotime('2007-03-01'),
  422. strtotime('2008-05-12') - 1,
  423. ['word4', 'word5'],
  424. ['word6']
  425. ],
  426. [
  427. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  428. ['word1'],
  429. strtotime('2007-03-01'),
  430. strtotime('2008-05-12') - 1,
  431. ['word2'],
  432. ['word3'],
  433. strtotime('2007-03-01'),
  434. strtotime('2008-05-12') - 1,
  435. ['word4', 'word5'],
  436. ['word6', 'word7']
  437. ],
  438. [
  439. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 "word7 word8" date:2007-03-01/2008-05-11',
  440. ['word1'],
  441. strtotime('2007-03-01'),
  442. strtotime('2008-05-12') - 1,
  443. ['word2'],
  444. ['word3'],
  445. strtotime('2007-03-01'),
  446. strtotime('2008-05-12') - 1,
  447. ['word4', 'word5'],
  448. ['word7 word8', 'word6']
  449. ]
  450. ];
  451. }
  452. #[DataProvider('provideAddOrParentheses')]
  453. public static function test__addOrParentheses(string $input, string $output): void {
  454. self::assertSame($output, FreshRSS_BooleanSearch::addOrParentheses($input));
  455. }
  456. /** @return list<list{string,string}> */
  457. public static function provideAddOrParentheses(): array {
  458. return [
  459. ['ab', 'ab'],
  460. ['ab cd', 'ab cd'],
  461. ['!ab -cd', '!ab -cd'],
  462. ['ab OR cd', '(ab) OR (cd)'],
  463. ['!ab OR -cd', '(!ab) OR (-cd)'],
  464. ['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],
  465. ['ab (!cd)', 'ab (!cd)'],
  466. ['"ab" (!"cd")', '"ab" (!"cd")'],
  467. ];
  468. }
  469. #[DataProvider('provideconsistentOrParentheses')]
  470. public static function test__consistentOrParentheses(string $input, string $output): void {
  471. self::assertSame($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  472. }
  473. /** @return list<list{string,string}> */
  474. public static function provideconsistentOrParentheses(): array {
  475. return [
  476. ['ab cd ef', 'ab cd ef'],
  477. ['(ab cd ef)', '(ab cd ef)'],
  478. ['("ab cd" ef)', '("ab cd" ef)'],
  479. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  480. ['ab (!cd)', 'ab (!cd)'],
  481. ['ab !(cd)', 'ab !(cd)'],
  482. ['(ab) -(cd)', '(ab) -(cd)'],
  483. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  484. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  485. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  486. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  487. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  488. ['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],
  489. ['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],
  490. ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],
  491. ];
  492. }
  493. /**
  494. * @param array<string> $values
  495. */
  496. #[DataProvider('provideParentheses')]
  497. public function test__parentheses(string $input, string $sql, array $values): void {
  498. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  499. self::assertSame(trim($sql), trim($filterSearch));
  500. self::assertSame($values, $filterValues);
  501. }
  502. /** @return list<list<mixed>> */
  503. public static function provideParentheses(): array {
  504. return [
  505. [
  506. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  507. ' ((e.id_feed IN (?))) AND ((e.id_feed IN (?)) OR (e.id_feed IN (?)) OR (e.id_feed IN (?))) AND' .
  508. ' (((e.id_feed IN (?))) OR ((e.id_feed IN (?)) OR (e.id_feed IN (?)))) ',
  509. [1, 2, 3, 4, 5, 6, 7]
  510. ],
  511. [
  512. 'c:1 OR c:2,3',
  513. ' (e.id_feed IN (SELECT f.id FROM `_feed` f WHERE f.category IN (?))) OR (e.id_feed IN (SELECT f.id FROM `_feed` f WHERE f.category IN (?,?))) ',
  514. [1, 2, 3]
  515. ],
  516. [
  517. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  518. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?))) OR ((e.author LIKE ? AND e.link LIKE ?)) OR" .
  519. ' ((e.id_feed IN (?) AND e.title LIKE ?)) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)))) ',
  520. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 12]
  521. ],
  522. [
  523. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  524. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?))) AND" .
  525. ' ((e.author LIKE ? AND e.link LIKE ?)) AND' .
  526. ' ((e.id_feed IN (?) AND e.title LIKE ?)) AND' .
  527. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)))) ',
  528. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 'Bleu']
  529. ],
  530. [
  531. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  532. ' NOT (((e.author LIKE ? AND e.title LIKE ?)) OR ((e.author LIKE ? AND e.title LIKE ?))) ',
  533. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  534. ],
  535. [
  536. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  537. ' ((e.author LIKE ? AND e.title LIKE ?)) AND NOT ((e.author LIKE ? AND e.title LIKE ?)) ',
  538. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  539. ],
  540. [
  541. 'intitle:"(test)"',
  542. '(e.title LIKE ?)',
  543. ['%(test)%'],
  544. ],
  545. [
  546. 'intitle:\'"hello world"\'',
  547. '(e.title LIKE ?)',
  548. ['%"hello world"%'],
  549. ],
  550. [
  551. 'intext:\'"hello world"\'',
  552. '(e.content LIKE ?)',
  553. ['%"hello world"%'],
  554. ],
  555. [
  556. '(ab) OR (cd) OR (ef)',
  557. '(((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?)))',
  558. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  559. ],
  560. [
  561. '("plain or text") OR (cd)',
  562. '(((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?)))',
  563. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  564. ],
  565. [
  566. '"plain or text" OR cd',
  567. '((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?))',
  568. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  569. ],
  570. [
  571. '"plain OR text" OR cd',
  572. '((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)) ',
  573. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  574. ],
  575. [
  576. 'ab OR cd OR (ef)',
  577. '(((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?))) ',
  578. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  579. ],
  580. [
  581. 'ab OR cd OR ef',
  582. '((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?))',
  583. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  584. ],
  585. [
  586. '(ab) cd OR ef OR (gh)',
  587. '(((e.title LIKE ? OR e.content LIKE ?))) AND (((e.title LIKE ? OR e.content LIKE ?))) ' .
  588. 'OR (((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?)))',
  589. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  590. ],
  591. [
  592. '(ab) OR cd OR ef OR (gh)',
  593. '(((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?))) ' .
  594. 'OR (((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?)))',
  595. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  596. ],
  597. [
  598. 'ab OR (!(cd OR ef))',
  599. '(((e.title LIKE ? OR e.content LIKE ?))) OR (NOT (((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?))))',
  600. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  601. ],
  602. [
  603. 'ab !(cd OR ef)',
  604. '(((e.title LIKE ? OR e.content LIKE ?))) AND NOT (((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)))',
  605. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  606. ],
  607. [
  608. 'ab OR !(cd OR ef)',
  609. '(((e.title LIKE ? OR e.content LIKE ?))) OR NOT (((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)))',
  610. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  611. ],
  612. [
  613. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  614. '((((e.title LIKE ? OR e.content LIKE ?))) AND (((e.title NOT LIKE ? AND e.content NOT LIKE ?)) OR (((e.title LIKE ? OR e.content LIKE ?))) ' .
  615. 'OR (((e.title LIKE ? OR e.content LIKE ?))))) OR NOT (((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)))',
  616. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  617. ],
  618. [
  619. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  620. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?))) AND (((e.title LIKE ? OR e.content LIKE ?))) ' .
  621. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ?))',
  622. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  623. ],
  624. [
  625. 'intitle:"é & \' è" intext:/<&>/ \'< & " >\'',
  626. '(e.title LIKE ? AND e.content ~ ? AND (e.title LIKE ? OR e.content LIKE ?))',
  627. ['%é &amp; \' è%', '<&>', '%&lt; &amp; " &gt;%', '%&lt; &amp; " &gt;%']
  628. ],
  629. [
  630. '/^(ab|cd) [(] \\) (ef|gh)/',
  631. '((e.title ~ ? OR e.content ~ ?))',
  632. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  633. ],
  634. [
  635. '!/^(ab|cd)/',
  636. '(NOT e.title ~ ? AND NOT e.content ~ ?)',
  637. ['^(ab|cd)', '^(ab|cd)']
  638. ],
  639. [
  640. 'intitle:/^(ab|cd)/',
  641. '(e.title ~ ?)',
  642. ['^(ab|cd)']
  643. ],
  644. [
  645. 'intext:/^(ab|cd)/',
  646. '(e.content ~ ?)',
  647. ['^(ab|cd)']
  648. ],
  649. [
  650. '!e:9 !e:90 !f:1 !f:10 !c:2 !c:20',
  651. '(e.id NOT IN (?,?) AND e.id_feed NOT IN (?,?) AND e.id_feed NOT IN (SELECT f.id FROM `_feed` f WHERE f.category IN (?,?)))',
  652. ['9', '90', 1, 10, 2, 20]
  653. ],
  654. [
  655. '!intitle:word1 !intitle:word10',
  656. '(e.title NOT LIKE ? AND e.title NOT LIKE ?)',
  657. ['%word1%', '%word10%']
  658. ],
  659. [
  660. 'L:1 L:2',
  661. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) AND ' .
  662. 'e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)))',
  663. [1, 2]
  664. ],
  665. [
  666. 'e:1 e:10',
  667. '(e.id IN (?,?))',
  668. ['1', '10']
  669. ],
  670. [
  671. 'f:1 f:10 c:2 c:20',
  672. '(e.id_feed IN (?,?) AND e.id_feed IN (SELECT f.id FROM `_feed` f WHERE f.category IN (?,?)))',
  673. [1, 10, 2, 20]
  674. ],
  675. [
  676. 'L:1 L:10',
  677. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) AND ' .
  678. 'e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)))',
  679. [1, 10]
  680. ],
  681. [
  682. 'L:1,2',
  683. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?,?)))',
  684. [1, 2]
  685. ],
  686. ];
  687. }
  688. public function test__add_single_search_combines_conditions_with_and(): void {
  689. $startTime = strtotime('2026-02-21T12:00:00Z');
  690. $searches = new FreshRSS_BooleanSearch('');
  691. $search = new FreshRSS_Search('');
  692. $search->setMinDate($startTime);
  693. $search->setMinModifiedDate($startTime);
  694. $searches->add($search);
  695. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $searches);
  696. $filterSearch = preg_replace('/\s+/', ' ', trim($filterSearch)) ?? '';
  697. self::assertSame('(e.id >= ? AND e.`lastModified` >= ?)', $filterSearch);
  698. self::assertSame([$startTime . '000000', $startTime], $filterValues);
  699. }
  700. public function test__add_multiple_searches_combines_conditions_with_or(): void {
  701. $startTime = strtotime('2026-02-21T12:00:00Z');
  702. $searches = new FreshRSS_BooleanSearch('');
  703. $search = new FreshRSS_Search('');
  704. $search->setMinDate($startTime);
  705. $searches->add($search);
  706. $search = new FreshRSS_Search('');
  707. $search->setMinModifiedDate($startTime);
  708. $searches->add($search);
  709. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $searches);
  710. $filterSearch = preg_replace('/\s+/', ' ', trim($filterSearch)) ?? '';
  711. self::assertSame('(e.id >= ?) OR (e.`lastModified` >= ?)', $filterSearch);
  712. self::assertSame([$startTime . '000000', $startTime], $filterValues);
  713. }
  714. public function test__add_boolean_searches_combines_with_and(): void {
  715. $combined = new FreshRSS_BooleanSearch('');
  716. $combined->add(new FreshRSS_BooleanSearch('ab'));
  717. $combined->add(new FreshRSS_BooleanSearch('cd OR ef', 0, 'AND', allowUserQueries: false));
  718. self::assertSame('ab (cd OR ef)', $combined->toString());
  719. [$filterValues] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $combined);
  720. self::assertSame(['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'], $filterValues);
  721. }
  722. /**
  723. * @param array<string> $values
  724. */
  725. #[DataProvider('provideDateOperators')]
  726. public function test__date_operators(string $input, string $sql, array $values): void {
  727. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  728. self::assertSame(trim($sql), trim($filterSearch));
  729. self::assertSame($values, $filterValues);
  730. }
  731. /** @return list<list<mixed>> */
  732. public static function provideDateOperators(): array {
  733. return [
  734. // Basic date operator tests
  735. [
  736. 'date:2007-03-01/2008-05-11',
  737. '(e.id >= ? AND e.id <= ?)',
  738. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  739. ],
  740. [
  741. 'date:2007-03-01/',
  742. '(e.id >= ?)',
  743. [strtotime('2007-03-01T00:00:00Z') . '000000'],
  744. ],
  745. [
  746. 'date:/2008-05-11',
  747. '(e.id <= ?)',
  748. [strtotime('2008-05-11T23:59:59Z') . '000000'],
  749. ],
  750. // Basic pubdate operator tests
  751. [
  752. 'pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  753. '(e.date >= ? AND e.date <= ?)',
  754. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  755. ],
  756. [
  757. 'pubdate:2007-03-01/',
  758. '(e.date >= ?)',
  759. [strtotime('2007-03-01T00:00:00Z')],
  760. ],
  761. [
  762. 'pubdate:/2008-05-11',
  763. '(e.date <= ?)',
  764. [strtotime('2008-05-11T23:59:59Z')],
  765. ],
  766. // Basic modified date operator tests
  767. [
  768. 'mdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  769. '(e.`lastModified` >= ? AND COALESCE(e.`lastModified`, 0) <= ?)',
  770. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  771. ],
  772. [
  773. 'mdate:2007-03-01/',
  774. '(e.`lastModified` >= ?)',
  775. [strtotime('2007-03-01T00:00:00Z')],
  776. ],
  777. [
  778. 'mdate:/2008-05-11',
  779. '(COALESCE(e.`lastModified`, 0) <= ?)',
  780. [strtotime('2008-05-11T23:59:59Z')],
  781. ],
  782. // Basic userdate operator tests
  783. [
  784. 'userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  785. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ?)',
  786. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  787. ],
  788. [
  789. 'userdate:2007-03-01/',
  790. '(e.`lastUserModified` >= ?)',
  791. [strtotime('2007-03-01T00:00:00Z')],
  792. ],
  793. [
  794. 'userdate:/2008-05-11',
  795. '(e.`lastUserModified` <= ?)',
  796. [strtotime('2008-05-11T23:59:59Z')],
  797. ],
  798. // Negative date operator tests
  799. [
  800. '-date:2007-03-01/2008-05-11',
  801. '((e.id < ? OR e.id > ?))',
  802. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  803. ],
  804. [
  805. '!pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  806. '((e.date < ? OR e.date > ?))',
  807. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  808. ],
  809. [
  810. '!mdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  811. '((COALESCE(e.`lastModified`, 0) < ? OR e.`lastModified` > ?))',
  812. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  813. ],
  814. [
  815. '!userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  816. '((COALESCE(e.`lastUserModified`, 0) < ? OR e.`lastUserModified` > ?))',
  817. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  818. ],
  819. // Combined date operators
  820. [
  821. 'date:2007-03-01/ pubdate:/2008-05-11',
  822. '(e.id >= ? AND e.date <= ?)',
  823. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z')],
  824. ],
  825. [
  826. 'pubdate:2007-03-01/ userdate:/2008-05-11',
  827. '(e.date >= ? AND e.`lastUserModified` <= ?)',
  828. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  829. ],
  830. [
  831. 'userdate:2007-03-01/ mdate:/2008-05-11',
  832. '(COALESCE(e.`lastModified`, 0) <= ? AND e.`lastUserModified` >= ?)',
  833. [strtotime('2008-05-11T23:59:59Z'), strtotime('2007-03-01T00:00:00Z')],
  834. ],
  835. [
  836. 'date:2007-03-01/ userdate:2007-06-01/',
  837. '(e.id >= ? AND e.`lastUserModified` >= ?)',
  838. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2007-06-01T00:00:00Z')],
  839. ],
  840. // Complex combinations with other operators
  841. [
  842. 'intitle:test date:2007-03-01/ pubdate:/2008-05-11',
  843. '(e.id >= ? AND e.date <= ? AND e.title LIKE ?)',
  844. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z'), '%test%'],
  845. ],
  846. [
  847. 'author:john userdate:2007-03-01/2008-05-11',
  848. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? AND e.author LIKE ?)',
  849. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z'), '%john%'],
  850. ],
  851. // Mixed positive and negative date operators
  852. [
  853. 'date:2007-03-01/ !pubdate:2008-01-01/2008-05-11',
  854. '(e.id >= ? AND (e.date < ? OR e.date > ?))',
  855. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-01-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  856. ],
  857. ];
  858. }
  859. /** @param array<string> $values */
  860. #[DataProvider('provideRegexPostreSQL')]
  861. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  862. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  863. self::assertSame(trim($sql), trim($filterSearch));
  864. self::assertSame($values, $filterValues);
  865. }
  866. /** @return list<list<mixed>> */
  867. public static function provideRegexPostreSQL(): array {
  868. return [
  869. [
  870. 'intitle:/^ab$/',
  871. '(e.title ~ ?)',
  872. ['^ab$']
  873. ],
  874. [
  875. 'intitle:/^ab$/i',
  876. '(e.title ~* ?)',
  877. ['^ab$']
  878. ],
  879. [
  880. 'intitle:/^ab$/m',
  881. '(e.title ~ ?)',
  882. ['(?m)^ab$']
  883. ],
  884. [
  885. 'intitle:/^ab\\M/',
  886. '(e.title ~ ?)',
  887. ['^ab\\M']
  888. ],
  889. [
  890. 'intext:/^ab\\M/',
  891. '(e.content ~ ?)',
  892. ['^ab\\M']
  893. ],
  894. [
  895. 'intitle:/\\b\\d+/',
  896. '(e.title ~ ?)',
  897. ['\\y\\d+']
  898. ],
  899. [
  900. 'author:/^ab$/',
  901. "(REPLACE(e.author, ';', '\n') ~ ?)",
  902. ['^ab$']
  903. ],
  904. [
  905. 'inurl:/^ab$/',
  906. '(e.link ~ ?)',
  907. ['^ab$']
  908. ],
  909. [
  910. '/^ab$/',
  911. '((e.title ~ ? OR e.content ~ ?))',
  912. ['^ab$', '^ab$']
  913. ],
  914. [
  915. '!/^ab$/',
  916. '(NOT e.title ~ ? AND NOT e.content ~ ?)',
  917. ['^ab$', '^ab$']
  918. ],
  919. [
  920. '#/^a(b|c)$/im',
  921. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ?)",
  922. ['(?m)^a(b|c)$']
  923. ],
  924. [ // Not a regex
  925. 'inurl:https://example.net/test/',
  926. '(e.link LIKE ?)',
  927. ['%https://example.net/test/%']
  928. ],
  929. [ // Not a regex
  930. 'https://example.net/test/',
  931. '((e.title LIKE ? OR e.content LIKE ?))',
  932. ['%https://example.net/test/%', '%https://example.net/test/%']
  933. ],
  934. [ // Not a regex
  935. "author:'/u/Alice'",
  936. "(e.author LIKE ?)",
  937. ['%/u/Alice%'],
  938. ],
  939. [ // Not a regex
  940. "inurl:'/shorts/'",
  941. "(e.link LIKE ?)",
  942. ['%/shorts/%'],
  943. ],
  944. [ // Not a regex
  945. 'inurl:"/shorts/" OR inurl:"/spam/"',
  946. '(e.link LIKE ?) OR (e.link LIKE ?)',
  947. ['%/shorts/%', '%/spam/%'],
  948. ],
  949. [ // Regex with literal 'or'
  950. 'intitle:/^A or B/i',
  951. '(e.title ~* ?)',
  952. ['^A or B']
  953. ],
  954. [ // Regex with literal 'OR'
  955. 'intitle:/^A B OR C D/i OR intitle:/^A B OR C D/i',
  956. '(e.title ~* ?) OR (e.title ~* ?)',
  957. ['^A B OR C D', '^A B OR C D']
  958. ],
  959. [ // Quote with literal 'OR'
  960. 'intitle:"A B OR C D" OR intitle:"E or F"',
  961. '(e.title LIKE ?) OR (e.title LIKE ?)',
  962. ['%A B OR C D%', '%E or F%']
  963. ],
  964. ];
  965. }
  966. /** @param array<string> $values */
  967. #[DataProvider('provideRegexMariaDB')]
  968. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  969. FreshRSS_DatabaseDAO::$dummyConnection = true;
  970. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  971. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  972. self::assertSame(trim($sql), trim($filterSearch));
  973. self::assertSame($values, $filterValues);
  974. }
  975. /** @return list<list<mixed>> */
  976. public static function provideRegexMariaDB(): array {
  977. return [
  978. [
  979. 'intitle:/^ab$/',
  980. "(e.title REGEXP ?)",
  981. ['(?-i)^ab$']
  982. ],
  983. [
  984. 'intitle:/^ab$/i',
  985. "(e.title REGEXP ?)",
  986. ['(?i)^ab$']
  987. ],
  988. [
  989. 'intitle:/^ab$/m',
  990. "(e.title REGEXP ?)",
  991. ['(?-i)(?m)^ab$']
  992. ],
  993. [
  994. 'intitle:/\\b\\d+/',
  995. "(e.title REGEXP ?)",
  996. ['(?-i)\\b\\d+']
  997. ],
  998. [
  999. 'intext:/^ab$/m',
  1000. '(UNCOMPRESS(e.content_bin) REGEXP ?)',
  1001. ['(?-i)(?m)^ab$']
  1002. ],
  1003. [
  1004. '-intext:/^ab$/m',
  1005. '(NOT UNCOMPRESS(e.content_bin) REGEXP ?)',
  1006. ['(?-i)(?m)^ab$']
  1007. ],
  1008. [
  1009. '-/^ab$/',
  1010. '(NOT e.title REGEXP ? AND NOT UNCOMPRESS(e.content_bin) REGEXP ?)',
  1011. ['(?-i)^ab$', '(?-i)^ab$']
  1012. ],
  1013. ];
  1014. }
  1015. /** @param array<string> $values */
  1016. #[DataProvider('provideRegexMySQL')]
  1017. public function test__regex_mysql(string $input, string $sql, array $values): void {
  1018. FreshRSS_DatabaseDAO::$dummyConnection = true;
  1019. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  1020. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  1021. self::assertSame(trim($sql), trim($filterSearch));
  1022. self::assertSame($values, $filterValues);
  1023. }
  1024. /** @return list<list<mixed>> */
  1025. public static function provideRegexMySQL(): array {
  1026. return [
  1027. [
  1028. 'intitle:/^ab$/',
  1029. "(REGEXP_LIKE(e.title,?,'c'))",
  1030. ['^ab$']
  1031. ],
  1032. [
  1033. 'intitle:/^ab$/i',
  1034. "(REGEXP_LIKE(e.title,?,'i'))",
  1035. ['^ab$']
  1036. ],
  1037. [
  1038. 'intitle:/^ab$/m',
  1039. "(REGEXP_LIKE(e.title,?,'mc'))",
  1040. ['^ab$']
  1041. ],
  1042. [
  1043. 'intext:/^ab$/m',
  1044. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc'))",
  1045. ['^ab$']
  1046. ],
  1047. [
  1048. '-intext:/^ab$/m',
  1049. "(NOT REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc'))",
  1050. ['^ab$']
  1051. ],
  1052. [
  1053. '-/^ab$/',
  1054. "(NOT REGEXP_LIKE(e.title,?,'c') AND NOT REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'c'))",
  1055. ['^ab$', '^ab$']
  1056. ],
  1057. ];
  1058. }
  1059. /** @param array<string> $values */
  1060. #[DataProvider('provideRegexSQLite')]
  1061. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  1062. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  1063. self::assertSame(trim($sql), trim($filterSearch));
  1064. self::assertSame($values, $filterValues);
  1065. }
  1066. /** @return list<list<mixed>> */
  1067. public static function provideRegexSQLite(): array {
  1068. return [
  1069. [
  1070. 'intitle:/^ab$/',
  1071. "(e.title REGEXP ?)",
  1072. ['/^ab$/']
  1073. ],
  1074. [
  1075. 'intitle:/^ab$/i',
  1076. "(e.title REGEXP ?)",
  1077. ['/^ab$/i']
  1078. ],
  1079. [
  1080. 'intitle:/^ab$/m',
  1081. "(e.title REGEXP ?)",
  1082. ['/^ab$/m']
  1083. ],
  1084. [
  1085. 'intitle:/^ab\\b/',
  1086. '(e.title REGEXP ?)',
  1087. ['/^ab\\b/']
  1088. ],
  1089. [
  1090. 'intext:/^ab\\b/',
  1091. '(e.content REGEXP ?)',
  1092. ['/^ab\\b/']
  1093. ],
  1094. ];
  1095. }
  1096. #[DataProvider('provideToString')]
  1097. public static function test__toString(string $input): void {
  1098. $search = new FreshRSS_Search($input);
  1099. $expected = str_replace("\n", ' ', $input);
  1100. self::assertSame($expected, $search->__toString());
  1101. }
  1102. /**
  1103. * @return array<array<string>>
  1104. */
  1105. public static function provideToString(): array {
  1106. return [
  1107. [
  1108. <<<'EOD'
  1109. e:1,2 f:10,11 c:20,21 L:30,31 labels:"My label,My other label"
  1110. userdate:2025-01-01T00:00:00/2026-01-01T00:00:00
  1111. mdate:2025-12
  1112. pubdate:2025-02-01T00:00:00/2026-01-01T00:00:00
  1113. date:2025-03-01T00:00:00/2026-01-01T00:00:00
  1114. intitle:/<Inter&sting>/i intitle:"g ' & d\\:"
  1115. intext:/<Inter&sting>/i intext:g&d
  1116. author:/Bob/ author:"/u/Alice" author:Alice
  1117. inurl:/https/ inurl:"/shorts/" inurl:example.net
  1118. #/tag2/ #tag1
  1119. /search_regex/i "quoted search" search search:"A user search" search:U1
  1120. -e:3,4 -f:12,13 -c:22,23 -L:32,33 -labels:"Not label,Not other label"
  1121. -userdate:2025-06-01T00:00:00/2025-09-01T00:00:00
  1122. -mdate:2025-12-27
  1123. -pubdate:2025
  1124. -date:P30D
  1125. -intitle:/Spam/i -intitle:"'bad"
  1126. -intext:/Spam/i -intext:"'bad"
  1127. -author:/Dave/i -author:"/u/Charlie" -author:Charlie
  1128. -inurl:/ftp/ -inurl:"/spam/" -inurl:example.com
  1129. -#/tag4/ -#tag3
  1130. -/not_regex/i -"not quoted" -not_search -search:"Negative user search" -search:U2
  1131. EOD
  1132. ],
  1133. ];
  1134. }
  1135. #[DataProvider('provideBooleanSearchToString')]
  1136. public static function testBooleanSearchToString(string $input, string $expected): void {
  1137. $search = new FreshRSS_BooleanSearch($input);
  1138. self::assertSame($expected, $search->toString());
  1139. }
  1140. /**
  1141. * @return array<array<string>>
  1142. */
  1143. public static function provideBooleanSearchToString(): array {
  1144. return [
  1145. [
  1146. '((a OR b) (c OR d) -e) OR -(f g)',
  1147. '((a OR b) (c OR d) (-e)) OR -(f g)',
  1148. ],
  1149. [
  1150. '((a OR b) ((c) OR ((d))) (-e)) OR -(((f g)))',
  1151. '((a OR b) (c OR d) (-e)) OR -(f g)',
  1152. ],
  1153. [
  1154. '!((b c))',
  1155. '-(b c)',
  1156. ],
  1157. [
  1158. '(a) OR !((b c))',
  1159. 'a OR -(b c)',
  1160. ],
  1161. [
  1162. '((a) (b))',
  1163. 'a b',
  1164. ],
  1165. [
  1166. '((a) OR (b))',
  1167. 'a OR b',
  1168. ],
  1169. [
  1170. ' ( !( !( ( a ) ) ) ) ( ) ',
  1171. '-(-a)',
  1172. ],
  1173. [
  1174. '-intitle:a -inurl:b',
  1175. '-intitle:a -inurl:b',
  1176. ],
  1177. [
  1178. 'intitle:/^A or B/i',
  1179. 'intitle:/^A or B/i',
  1180. ],
  1181. [
  1182. 'intitle:/^A B OR C D/i',
  1183. 'intitle:/^A B OR C D/i',
  1184. ],
  1185. [
  1186. '"a(b"',
  1187. '"a(b"',
  1188. ],
  1189. [
  1190. 'intitle:"(test)"',
  1191. 'intitle:"(test)"',
  1192. ],
  1193. [
  1194. '"a)OR(b"',
  1195. '"a)OR(b"',
  1196. ],
  1197. [
  1198. '"x.OR.y"',
  1199. '"x.OR.y"',
  1200. ],
  1201. ];
  1202. }
  1203. #[DataProvider('provideBooleanSearchToStringRoundTrip')]
  1204. public static function testBooleanSearchToStringRoundTrip(string $input): void {
  1205. $search = new FreshRSS_BooleanSearch($input);
  1206. $reparsed = new FreshRSS_BooleanSearch($search->toString());
  1207. self::assertSame($search->toString(), $reparsed->toString());
  1208. }
  1209. /** @return list<list{string}> */
  1210. public static function provideBooleanSearchToStringRoundTrip(): array {
  1211. return [
  1212. ['"a(b"'],
  1213. ['"a)OR(b"'],
  1214. ['"a b" ("cd)OR(ef")'],
  1215. ['intitle:"(x)" author:"a OR b"'],
  1216. ['"x.OR.y" OR z'],
  1217. ['intitle:/^(ab|cd)/ "e)f"'],
  1218. ];
  1219. }
  1220. /**
  1221. * @param list<array{search:string,name?:string}> $queries
  1222. */
  1223. #[DataProvider('provideBooleanSearchToStringExpansion')]
  1224. public static function testBooleanSearchToStringExpansion(array $queries, string $input,
  1225. string $expectedNotExpanded, string $expectedExpanded): void {
  1226. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  1227. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  1228. $newUserConf->queries = $queries;
  1229. FreshRSS_Context::setUserConf($newUserConf);
  1230. try {
  1231. $booleanSearch = new FreshRSS_BooleanSearch($input);
  1232. self::assertSame($expectedNotExpanded, $booleanSearch->toString(expandUserQueries: false));
  1233. self::assertSame($expectedExpanded, $booleanSearch->toString());
  1234. } finally {
  1235. FreshRSS_Context::setUserConf($previousUserConf);
  1236. }
  1237. }
  1238. /**
  1239. * @return array<string,array{0:list<array{search:string,name?:string}>,1:string,2:string,3:string}>
  1240. */
  1241. public static function provideBooleanSearchToStringExpansion(): array {
  1242. $overlappingQueries = array_fill(0, 11, ['search' => 'author:Unused']);
  1243. $overlappingQueries[1] = ['name' => 'News', 'search' => 'author:Alice'];
  1244. $overlappingQueries[10] = ['name' => 'Newsletter', 'search' => 'author:Bob'];
  1245. return [
  1246. 'Not found ID' => [
  1247. [
  1248. ['search' => 'author:Alice'],
  1249. ['search' => 'intitle:World'],
  1250. ],
  1251. 'S:3 S:4,5 ',
  1252. 'S:3 S:4,5',
  1253. '',
  1254. ],
  1255. 'Not found name' => [
  1256. [
  1257. ['search' => 'author:Alice', 'name' => 'First'],
  1258. ['search' => 'intitle:World', 'name' => 'Second'],
  1259. ],
  1260. 'search:Third ',
  1261. 'search:Third',
  1262. '',
  1263. ],
  1264. 'Found IDs' => [
  1265. [
  1266. ['search' => 'author:Alice', 'name' => 'First'],
  1267. ['search' => 'intitle:World', 'name' => 'Second'],
  1268. ],
  1269. 'S:0,1 ',
  1270. 'S:0,1',
  1271. 'author:Alice OR intitle:World',
  1272. ],
  1273. 'Found names' => [
  1274. [
  1275. ['search' => 'author:Alice', 'name' => 'First'],
  1276. ['search' => 'intitle:World', 'name' => 'Second'],
  1277. ],
  1278. 'search:First search:Second ',
  1279. 'search:First search:Second',
  1280. 'author:Alice intitle:World',
  1281. ],
  1282. 'Longer ID first' => [
  1283. $overlappingQueries,
  1284. 'S:10 OR S:1',
  1285. 'S:10 OR S:1',
  1286. 'author:Bob OR author:Alice',
  1287. ],
  1288. 'Shorter ID first' => [
  1289. $overlappingQueries,
  1290. 'S:1 OR S:10',
  1291. 'S:1 OR S:10',
  1292. 'author:Alice OR author:Bob',
  1293. ],
  1294. 'Overlapping IDs with AND' => [
  1295. $overlappingQueries,
  1296. 'S:1 S:10',
  1297. 'S:1 S:10',
  1298. 'author:Alice author:Bob',
  1299. ],
  1300. 'Overlapping ID groups' => [
  1301. $overlappingQueries,
  1302. 'S:1,10 S:1',
  1303. 'S:1,10 S:1',
  1304. '(author:Alice OR author:Bob) author:Alice',
  1305. ],
  1306. 'Longer name first' => [
  1307. $overlappingQueries,
  1308. 'search:Newsletter OR search:News',
  1309. 'search:Newsletter OR search:News',
  1310. 'author:Bob OR author:Alice',
  1311. ],
  1312. 'Shorter name first' => [
  1313. $overlappingQueries,
  1314. 'search:News OR search:Newsletter',
  1315. 'search:News OR search:Newsletter',
  1316. 'author:Alice OR author:Bob',
  1317. ],
  1318. 'Overlapping names with AND' => [
  1319. $overlappingQueries,
  1320. 'search:News search:Newsletter',
  1321. 'search:News search:Newsletter',
  1322. 'author:Alice author:Bob',
  1323. ],
  1324. 'Negated overlapping IDs' => [
  1325. $overlappingQueries,
  1326. '-S:1 -S:10',
  1327. '-S:1 -S:10',
  1328. '-author:Alice -author:Bob',
  1329. ],
  1330. 'Negated overlapping names' => [
  1331. $overlappingQueries,
  1332. '-search:News -search:Newsletter',
  1333. '-search:News -search:Newsletter',
  1334. '-author:Alice -author:Bob',
  1335. ],
  1336. ];
  1337. }
  1338. #[DataProvider('provideHasSameOperators')]
  1339. public function testHasSameOperators(string $input1, string $input2, bool $expected): void {
  1340. $search1 = new FreshRSS_Search($input1);
  1341. $search2 = new FreshRSS_Search($input2);
  1342. self::assertSame($expected, $search1->hasSameOperators($search2));
  1343. }
  1344. /**
  1345. * @return array<array{string,string,bool}>
  1346. */
  1347. public static function provideHasSameOperators(): array {
  1348. return [
  1349. ['', '', true],
  1350. ['intitle:a intext:b', 'intitle:c intext:d', true],
  1351. ['intitle:a intext:b', 'intitle:c inurl:d', false],
  1352. ];
  1353. }
  1354. #[DataProvider('provideBooleanSearchEnforce')]
  1355. public function testBooleanSearchEnforce(string $initialInput, string $enforceInput, string $expectedOutput): void {
  1356. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1357. $searchToEnforce = new FreshRSS_Search($enforceInput);
  1358. $newBooleanSearch = $booleanSearch->enforce($searchToEnforce);
  1359. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1360. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1361. }
  1362. /**
  1363. * @return array<array{string,string,string}>
  1364. */
  1365. public static function provideBooleanSearchEnforce(): array {
  1366. return [
  1367. ['', '', ''],
  1368. ['', 'intitle:b', 'intitle:b'],
  1369. ['intitle:a', '', 'intitle:a'],
  1370. ['intitle:a', 'intitle:b', 'intitle:b'],
  1371. ['a', 'intitle:b', 'intitle:b a'],
  1372. ['intitle:a intext:a', 'intitle:b', 'intitle:b intext:a'],
  1373. ['intitle:a inurl:a', 'intitle:b', 'intitle:b inurl:a'],
  1374. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:b (intitle:a OR inurl:a)'],
  1375. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', 'intitle:b (inurl:a intitle:c)'],
  1376. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', 'intitle:b (inurl:a OR intitle:c)'],
  1377. ['(intitle:a) (inurl:a)', 'intitle:b', 'intitle:b inurl:a'],
  1378. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a intitle:b'],
  1379. ['(a b) OR (c d)', 'e', 'e ((a b) OR (c d))'],
  1380. ['(a b) (c d)', 'e', 'e ((a b) (c d))'],
  1381. ['(a b)', 'e', 'e (a b)'],
  1382. ['date:2024/', 'date:/2025', 'date:/2025'],
  1383. ['a', 'date:/2025', 'date:/2025 a'],
  1384. ];
  1385. }
  1386. #[DataProvider('provideBooleanSearchRemove')]
  1387. public function testBooleanSearchRemove(string $initialInput, string $removeInput, string $expectedOutput): void {
  1388. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1389. $searchToRemove = new FreshRSS_Search($removeInput);
  1390. $newBooleanSearch = $booleanSearch->remove($searchToRemove);
  1391. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1392. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1393. }
  1394. /**
  1395. * @return array<array{string,string,string}>
  1396. */
  1397. public static function provideBooleanSearchRemove(): array {
  1398. return [
  1399. ['', 'intitle:b', ''],
  1400. ['intitle:a', 'intitle:b', ''],
  1401. ['intitle:a intext:a', 'intitle:b', 'intext:a'],
  1402. ['intitle:a inurl:a', 'intitle:b', 'inurl:a'],
  1403. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:a OR inurl:a'],
  1404. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', '(inurl:a intitle:c)'],
  1405. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', '(inurl:a OR intitle:c)'],
  1406. ['(intitle:a) (inurl:a)', 'intitle:b', 'inurl:a'],
  1407. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a'],
  1408. ['e ((a b) OR (c d))', 'e', '((a b) OR (c d))'],
  1409. ['e ((a b) (c d))', 'e', '((a b) (c d))'],
  1410. ['date:2024/', 'date:/2025', ''],
  1411. ['date:2024/ a', 'date:/2025', 'a'],
  1412. ];
  1413. }
  1414. #[DataProvider('provideNeedVisibility')]
  1415. public function testNeedVisibility(string $input, ?int $expected): void {
  1416. $search = new FreshRSS_Search($input);
  1417. self::assertSame($expected, $search->needVisibility());
  1418. }
  1419. /** @return list<list<string|int|null>> */
  1420. public static function provideNeedVisibility(): array {
  1421. return [
  1422. ['', null],
  1423. ['f:1', FreshRSS_Feed::PRIORITY_HIDDEN],
  1424. ['c:2', FreshRSS_Feed::PRIORITY_CATEGORY],
  1425. ['f:1 c:2', FreshRSS_Feed::PRIORITY_HIDDEN],
  1426. ['-f:1', null],
  1427. ['-c:2', null],
  1428. ];
  1429. }
  1430. }