4
0

SearchTest.php 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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. /**
  715. * @param array<string> $values
  716. */
  717. #[DataProvider('provideDateOperators')]
  718. public function test__date_operators(string $input, string $sql, array $values): void {
  719. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  720. self::assertSame(trim($sql), trim($filterSearch));
  721. self::assertSame($values, $filterValues);
  722. }
  723. /** @return list<list<mixed>> */
  724. public static function provideDateOperators(): array {
  725. return [
  726. // Basic date operator tests
  727. [
  728. 'date:2007-03-01/2008-05-11',
  729. '(e.id >= ? AND e.id <= ?)',
  730. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  731. ],
  732. [
  733. 'date:2007-03-01/',
  734. '(e.id >= ?)',
  735. [strtotime('2007-03-01T00:00:00Z') . '000000'],
  736. ],
  737. [
  738. 'date:/2008-05-11',
  739. '(e.id <= ?)',
  740. [strtotime('2008-05-11T23:59:59Z') . '000000'],
  741. ],
  742. // Basic pubdate operator tests
  743. [
  744. 'pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  745. '(e.date >= ? AND e.date <= ?)',
  746. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  747. ],
  748. [
  749. 'pubdate:2007-03-01/',
  750. '(e.date >= ?)',
  751. [strtotime('2007-03-01T00:00:00Z')],
  752. ],
  753. [
  754. 'pubdate:/2008-05-11',
  755. '(e.date <= ?)',
  756. [strtotime('2008-05-11T23:59:59Z')],
  757. ],
  758. // Basic modified date operator tests
  759. [
  760. 'mdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  761. '(e.`lastModified` >= ? AND COALESCE(e.`lastModified`, 0) <= ?)',
  762. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  763. ],
  764. [
  765. 'mdate:2007-03-01/',
  766. '(e.`lastModified` >= ?)',
  767. [strtotime('2007-03-01T00:00:00Z')],
  768. ],
  769. [
  770. 'mdate:/2008-05-11',
  771. '(COALESCE(e.`lastModified`, 0) <= ?)',
  772. [strtotime('2008-05-11T23:59:59Z')],
  773. ],
  774. // Basic userdate operator tests
  775. [
  776. 'userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  777. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ?)',
  778. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  779. ],
  780. [
  781. 'userdate:2007-03-01/',
  782. '(e.`lastUserModified` >= ?)',
  783. [strtotime('2007-03-01T00:00:00Z')],
  784. ],
  785. [
  786. 'userdate:/2008-05-11',
  787. '(e.`lastUserModified` <= ?)',
  788. [strtotime('2008-05-11T23:59:59Z')],
  789. ],
  790. // Negative date operator tests
  791. [
  792. '-date:2007-03-01/2008-05-11',
  793. '((e.id < ? OR e.id > ?))',
  794. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  795. ],
  796. [
  797. '!pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  798. '((e.date < ? OR e.date > ?))',
  799. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  800. ],
  801. [
  802. '!mdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  803. '((COALESCE(e.`lastModified`, 0) < ? OR e.`lastModified` > ?))',
  804. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  805. ],
  806. [
  807. '!userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  808. '((COALESCE(e.`lastUserModified`, 0) < ? OR e.`lastUserModified` > ?))',
  809. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  810. ],
  811. // Combined date operators
  812. [
  813. 'date:2007-03-01/ pubdate:/2008-05-11',
  814. '(e.id >= ? AND e.date <= ?)',
  815. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z')],
  816. ],
  817. [
  818. 'pubdate:2007-03-01/ userdate:/2008-05-11',
  819. '(e.date >= ? AND e.`lastUserModified` <= ?)',
  820. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  821. ],
  822. [
  823. 'userdate:2007-03-01/ mdate:/2008-05-11',
  824. '(COALESCE(e.`lastModified`, 0) <= ? AND e.`lastUserModified` >= ?)',
  825. [strtotime('2008-05-11T23:59:59Z'), strtotime('2007-03-01T00:00:00Z')],
  826. ],
  827. [
  828. 'date:2007-03-01/ userdate:2007-06-01/',
  829. '(e.id >= ? AND e.`lastUserModified` >= ?)',
  830. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2007-06-01T00:00:00Z')],
  831. ],
  832. // Complex combinations with other operators
  833. [
  834. 'intitle:test date:2007-03-01/ pubdate:/2008-05-11',
  835. '(e.id >= ? AND e.date <= ? AND e.title LIKE ?)',
  836. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z'), '%test%'],
  837. ],
  838. [
  839. 'author:john userdate:2007-03-01/2008-05-11',
  840. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? AND e.author LIKE ?)',
  841. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z'), '%john%'],
  842. ],
  843. // Mixed positive and negative date operators
  844. [
  845. 'date:2007-03-01/ !pubdate:2008-01-01/2008-05-11',
  846. '(e.id >= ? AND (e.date < ? OR e.date > ?))',
  847. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-01-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  848. ],
  849. ];
  850. }
  851. /** @param array<string> $values */
  852. #[DataProvider('provideRegexPostreSQL')]
  853. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  854. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  855. self::assertSame(trim($sql), trim($filterSearch));
  856. self::assertSame($values, $filterValues);
  857. }
  858. /** @return list<list<mixed>> */
  859. public static function provideRegexPostreSQL(): array {
  860. return [
  861. [
  862. 'intitle:/^ab$/',
  863. '(e.title ~ ?)',
  864. ['^ab$']
  865. ],
  866. [
  867. 'intitle:/^ab$/i',
  868. '(e.title ~* ?)',
  869. ['^ab$']
  870. ],
  871. [
  872. 'intitle:/^ab$/m',
  873. '(e.title ~ ?)',
  874. ['(?m)^ab$']
  875. ],
  876. [
  877. 'intitle:/^ab\\M/',
  878. '(e.title ~ ?)',
  879. ['^ab\\M']
  880. ],
  881. [
  882. 'intext:/^ab\\M/',
  883. '(e.content ~ ?)',
  884. ['^ab\\M']
  885. ],
  886. [
  887. 'intitle:/\\b\\d+/',
  888. '(e.title ~ ?)',
  889. ['\\y\\d+']
  890. ],
  891. [
  892. 'author:/^ab$/',
  893. "(REPLACE(e.author, ';', '\n') ~ ?)",
  894. ['^ab$']
  895. ],
  896. [
  897. 'inurl:/^ab$/',
  898. '(e.link ~ ?)',
  899. ['^ab$']
  900. ],
  901. [
  902. '/^ab$/',
  903. '((e.title ~ ? OR e.content ~ ?))',
  904. ['^ab$', '^ab$']
  905. ],
  906. [
  907. '!/^ab$/',
  908. '(NOT e.title ~ ? AND NOT e.content ~ ?)',
  909. ['^ab$', '^ab$']
  910. ],
  911. [
  912. '#/^a(b|c)$/im',
  913. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ?)",
  914. ['(?m)^a(b|c)$']
  915. ],
  916. [ // Not a regex
  917. 'inurl:https://example.net/test/',
  918. '(e.link LIKE ?)',
  919. ['%https://example.net/test/%']
  920. ],
  921. [ // Not a regex
  922. 'https://example.net/test/',
  923. '((e.title LIKE ? OR e.content LIKE ?))',
  924. ['%https://example.net/test/%', '%https://example.net/test/%']
  925. ],
  926. [ // Not a regex
  927. "author:'/u/Alice'",
  928. "(e.author LIKE ?)",
  929. ['%/u/Alice%'],
  930. ],
  931. [ // Not a regex
  932. "inurl:'/shorts/'",
  933. "(e.link LIKE ?)",
  934. ['%/shorts/%'],
  935. ],
  936. [ // Not a regex
  937. 'inurl:"/shorts/" OR inurl:"/spam/"',
  938. '(e.link LIKE ?) OR (e.link LIKE ?)',
  939. ['%/shorts/%', '%/spam/%'],
  940. ],
  941. [ // Regex with literal 'or'
  942. 'intitle:/^A or B/i',
  943. '(e.title ~* ?)',
  944. ['^A or B']
  945. ],
  946. [ // Regex with literal 'OR'
  947. 'intitle:/^A B OR C D/i OR intitle:/^A B OR C D/i',
  948. '(e.title ~* ?) OR (e.title ~* ?)',
  949. ['^A B OR C D', '^A B OR C D']
  950. ],
  951. [ // Quote with literal 'OR'
  952. 'intitle:"A B OR C D" OR intitle:"E or F"',
  953. '(e.title LIKE ?) OR (e.title LIKE ?)',
  954. ['%A B OR C D%', '%E or F%']
  955. ],
  956. ];
  957. }
  958. /** @param array<string> $values */
  959. #[DataProvider('provideRegexMariaDB')]
  960. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  961. FreshRSS_DatabaseDAO::$dummyConnection = true;
  962. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  963. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  964. self::assertSame(trim($sql), trim($filterSearch));
  965. self::assertSame($values, $filterValues);
  966. }
  967. /** @return list<list<mixed>> */
  968. public static function provideRegexMariaDB(): array {
  969. return [
  970. [
  971. 'intitle:/^ab$/',
  972. "(e.title REGEXP ?)",
  973. ['(?-i)^ab$']
  974. ],
  975. [
  976. 'intitle:/^ab$/i',
  977. "(e.title REGEXP ?)",
  978. ['(?i)^ab$']
  979. ],
  980. [
  981. 'intitle:/^ab$/m',
  982. "(e.title REGEXP ?)",
  983. ['(?-i)(?m)^ab$']
  984. ],
  985. [
  986. 'intitle:/\\b\\d+/',
  987. "(e.title REGEXP ?)",
  988. ['(?-i)\\b\\d+']
  989. ],
  990. [
  991. 'intext:/^ab$/m',
  992. '(UNCOMPRESS(e.content_bin) REGEXP ?)',
  993. ['(?-i)(?m)^ab$']
  994. ],
  995. [
  996. '-intext:/^ab$/m',
  997. '(NOT UNCOMPRESS(e.content_bin) REGEXP ?)',
  998. ['(?-i)(?m)^ab$']
  999. ],
  1000. [
  1001. '-/^ab$/',
  1002. '(NOT e.title REGEXP ? AND NOT UNCOMPRESS(e.content_bin) REGEXP ?)',
  1003. ['(?-i)^ab$', '(?-i)^ab$']
  1004. ],
  1005. ];
  1006. }
  1007. /** @param array<string> $values */
  1008. #[DataProvider('provideRegexMySQL')]
  1009. public function test__regex_mysql(string $input, string $sql, array $values): void {
  1010. FreshRSS_DatabaseDAO::$dummyConnection = true;
  1011. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  1012. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  1013. self::assertSame(trim($sql), trim($filterSearch));
  1014. self::assertSame($values, $filterValues);
  1015. }
  1016. /** @return list<list<mixed>> */
  1017. public static function provideRegexMySQL(): array {
  1018. return [
  1019. [
  1020. 'intitle:/^ab$/',
  1021. "(REGEXP_LIKE(e.title,?,'c'))",
  1022. ['^ab$']
  1023. ],
  1024. [
  1025. 'intitle:/^ab$/i',
  1026. "(REGEXP_LIKE(e.title,?,'i'))",
  1027. ['^ab$']
  1028. ],
  1029. [
  1030. 'intitle:/^ab$/m',
  1031. "(REGEXP_LIKE(e.title,?,'mc'))",
  1032. ['^ab$']
  1033. ],
  1034. [
  1035. 'intext:/^ab$/m',
  1036. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc'))",
  1037. ['^ab$']
  1038. ],
  1039. [
  1040. '-intext:/^ab$/m',
  1041. "(NOT REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc'))",
  1042. ['^ab$']
  1043. ],
  1044. [
  1045. '-/^ab$/',
  1046. "(NOT REGEXP_LIKE(e.title,?,'c') AND NOT REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'c'))",
  1047. ['^ab$', '^ab$']
  1048. ],
  1049. ];
  1050. }
  1051. /** @param array<string> $values */
  1052. #[DataProvider('provideRegexSQLite')]
  1053. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  1054. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  1055. self::assertSame(trim($sql), trim($filterSearch));
  1056. self::assertSame($values, $filterValues);
  1057. }
  1058. /** @return list<list<mixed>> */
  1059. public static function provideRegexSQLite(): array {
  1060. return [
  1061. [
  1062. 'intitle:/^ab$/',
  1063. "(e.title REGEXP ?)",
  1064. ['/^ab$/']
  1065. ],
  1066. [
  1067. 'intitle:/^ab$/i',
  1068. "(e.title REGEXP ?)",
  1069. ['/^ab$/i']
  1070. ],
  1071. [
  1072. 'intitle:/^ab$/m',
  1073. "(e.title REGEXP ?)",
  1074. ['/^ab$/m']
  1075. ],
  1076. [
  1077. 'intitle:/^ab\\b/',
  1078. '(e.title REGEXP ?)',
  1079. ['/^ab\\b/']
  1080. ],
  1081. [
  1082. 'intext:/^ab\\b/',
  1083. '(e.content REGEXP ?)',
  1084. ['/^ab\\b/']
  1085. ],
  1086. ];
  1087. }
  1088. #[DataProvider('provideToString')]
  1089. public static function test__toString(string $input): void {
  1090. $search = new FreshRSS_Search($input);
  1091. $expected = str_replace("\n", ' ', $input);
  1092. self::assertSame($expected, $search->__toString());
  1093. }
  1094. /**
  1095. * @return array<array<string>>
  1096. */
  1097. public static function provideToString(): array {
  1098. return [
  1099. [
  1100. <<<'EOD'
  1101. e:1,2 f:10,11 c:20,21 L:30,31 labels:"My label,My other label"
  1102. userdate:2025-01-01T00:00:00/2026-01-01T00:00:00
  1103. mdate:2025-12
  1104. pubdate:2025-02-01T00:00:00/2026-01-01T00:00:00
  1105. date:2025-03-01T00:00:00/2026-01-01T00:00:00
  1106. intitle:/<Inter&sting>/i intitle:"g ' & d\\:"
  1107. intext:/<Inter&sting>/i intext:g&d
  1108. author:/Bob/ author:"/u/Alice" author:Alice
  1109. inurl:/https/ inurl:"/shorts/" inurl:example.net
  1110. #/tag2/ #tag1
  1111. /search_regex/i "quoted search" search search:"A user search" search:U1
  1112. -e:3,4 -f:12,13 -c:22,23 -L:32,33 -labels:"Not label,Not other label"
  1113. -userdate:2025-06-01T00:00:00/2025-09-01T00:00:00
  1114. -mdate:2025-12-27
  1115. -pubdate:2025
  1116. -date:P30D
  1117. -intitle:/Spam/i -intitle:"'bad"
  1118. -intext:/Spam/i -intext:"'bad"
  1119. -author:/Dave/i -author:"/u/Charlie" -author:Charlie
  1120. -inurl:/ftp/ -inurl:"/spam/" -inurl:example.com
  1121. -#/tag4/ -#tag3
  1122. -/not_regex/i -"not quoted" -not_search -search:"Negative user search" -search:U2
  1123. EOD
  1124. ],
  1125. ];
  1126. }
  1127. #[DataProvider('provideBooleanSearchToString')]
  1128. public static function testBooleanSearchToString(string $input, string $expected): void {
  1129. $search = new FreshRSS_BooleanSearch($input);
  1130. self::assertSame($expected, $search->toString());
  1131. }
  1132. /**
  1133. * @return array<array<string>>
  1134. */
  1135. public static function provideBooleanSearchToString(): array {
  1136. return [
  1137. [
  1138. '((a OR b) (c OR d) -e) OR -(f g)',
  1139. '((a OR b) (c OR d) (-e)) OR -(f g)',
  1140. ],
  1141. [
  1142. '((a OR b) ((c) OR ((d))) (-e)) OR -(((f g)))',
  1143. '((a OR b) (c OR d) (-e)) OR -(f g)',
  1144. ],
  1145. [
  1146. '!((b c))',
  1147. '-(b c)',
  1148. ],
  1149. [
  1150. '(a) OR !((b c))',
  1151. 'a OR -(b c)',
  1152. ],
  1153. [
  1154. '((a) (b))',
  1155. 'a b',
  1156. ],
  1157. [
  1158. '((a) OR (b))',
  1159. 'a OR b',
  1160. ],
  1161. [
  1162. ' ( !( !( ( a ) ) ) ) ( ) ',
  1163. '-(-a)',
  1164. ],
  1165. [
  1166. '-intitle:a -inurl:b',
  1167. '-intitle:a -inurl:b',
  1168. ],
  1169. [
  1170. 'intitle:/^A or B/i',
  1171. 'intitle:/^A or B/i',
  1172. ],
  1173. [
  1174. 'intitle:/^A B OR C D/i',
  1175. 'intitle:/^A B OR C D/i',
  1176. ],
  1177. ];
  1178. }
  1179. /**
  1180. * @param list<array{search:string,name?:string}> $queries
  1181. */
  1182. #[DataProvider('provideBooleanSearchToStringExpansion')]
  1183. public static function testBooleanSearchToStringExpansion(array $queries, string $input,
  1184. string $expectedNotExpanded, string $expectedExpanded): void {
  1185. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  1186. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  1187. $newUserConf->queries = $queries;
  1188. FreshRSS_Context::setUserConf($newUserConf);
  1189. try {
  1190. $booleanSearch = new FreshRSS_BooleanSearch($input);
  1191. self::assertSame($expectedNotExpanded, $booleanSearch->toString(expandUserQueries: false));
  1192. self::assertSame($expectedExpanded, $booleanSearch->toString());
  1193. } finally {
  1194. FreshRSS_Context::setUserConf($previousUserConf);
  1195. }
  1196. }
  1197. /**
  1198. * @return array<string,array{0:list<array{search:string,name?:string}>,1:string,2:string,3:string}>
  1199. */
  1200. public static function provideBooleanSearchToStringExpansion(): array {
  1201. $overlappingQueries = array_fill(0, 11, ['search' => 'author:Unused']);
  1202. $overlappingQueries[1] = ['name' => 'News', 'search' => 'author:Alice'];
  1203. $overlappingQueries[10] = ['name' => 'Newsletter', 'search' => 'author:Bob'];
  1204. return [
  1205. 'Not found ID' => [
  1206. [
  1207. ['search' => 'author:Alice'],
  1208. ['search' => 'intitle:World'],
  1209. ],
  1210. 'S:3 S:4,5 ',
  1211. 'S:3 S:4,5',
  1212. '',
  1213. ],
  1214. 'Not found name' => [
  1215. [
  1216. ['search' => 'author:Alice', 'name' => 'First'],
  1217. ['search' => 'intitle:World', 'name' => 'Second'],
  1218. ],
  1219. 'search:Third ',
  1220. 'search:Third',
  1221. '',
  1222. ],
  1223. 'Found IDs' => [
  1224. [
  1225. ['search' => 'author:Alice', 'name' => 'First'],
  1226. ['search' => 'intitle:World', 'name' => 'Second'],
  1227. ],
  1228. 'S:0,1 ',
  1229. 'S:0,1',
  1230. 'author:Alice OR intitle:World',
  1231. ],
  1232. 'Found names' => [
  1233. [
  1234. ['search' => 'author:Alice', 'name' => 'First'],
  1235. ['search' => 'intitle:World', 'name' => 'Second'],
  1236. ],
  1237. 'search:First search:Second ',
  1238. 'search:First search:Second',
  1239. 'author:Alice intitle:World',
  1240. ],
  1241. 'Longer ID first' => [
  1242. $overlappingQueries,
  1243. 'S:10 OR S:1',
  1244. 'S:10 OR S:1',
  1245. 'author:Bob OR author:Alice',
  1246. ],
  1247. 'Shorter ID first' => [
  1248. $overlappingQueries,
  1249. 'S:1 OR S:10',
  1250. 'S:1 OR S:10',
  1251. 'author:Alice OR author:Bob',
  1252. ],
  1253. 'Overlapping IDs with AND' => [
  1254. $overlappingQueries,
  1255. 'S:1 S:10',
  1256. 'S:1 S:10',
  1257. 'author:Alice author:Bob',
  1258. ],
  1259. 'Overlapping ID groups' => [
  1260. $overlappingQueries,
  1261. 'S:1,10 S:1',
  1262. 'S:1,10 S:1',
  1263. '(author:Alice OR author:Bob) author:Alice',
  1264. ],
  1265. 'Longer name first' => [
  1266. $overlappingQueries,
  1267. 'search:Newsletter OR search:News',
  1268. 'search:Newsletter OR search:News',
  1269. 'author:Bob OR author:Alice',
  1270. ],
  1271. 'Shorter name first' => [
  1272. $overlappingQueries,
  1273. 'search:News OR search:Newsletter',
  1274. 'search:News OR search:Newsletter',
  1275. 'author:Alice OR author:Bob',
  1276. ],
  1277. 'Overlapping names with AND' => [
  1278. $overlappingQueries,
  1279. 'search:News search:Newsletter',
  1280. 'search:News search:Newsletter',
  1281. 'author:Alice author:Bob',
  1282. ],
  1283. 'Negated overlapping IDs' => [
  1284. $overlappingQueries,
  1285. '-S:1 -S:10',
  1286. '-S:1 -S:10',
  1287. '-author:Alice -author:Bob',
  1288. ],
  1289. 'Negated overlapping names' => [
  1290. $overlappingQueries,
  1291. '-search:News -search:Newsletter',
  1292. '-search:News -search:Newsletter',
  1293. '-author:Alice -author:Bob',
  1294. ],
  1295. ];
  1296. }
  1297. #[DataProvider('provideHasSameOperators')]
  1298. public function testHasSameOperators(string $input1, string $input2, bool $expected): void {
  1299. $search1 = new FreshRSS_Search($input1);
  1300. $search2 = new FreshRSS_Search($input2);
  1301. self::assertSame($expected, $search1->hasSameOperators($search2));
  1302. }
  1303. /**
  1304. * @return array<array{string,string,bool}>
  1305. */
  1306. public static function provideHasSameOperators(): array {
  1307. return [
  1308. ['', '', true],
  1309. ['intitle:a intext:b', 'intitle:c intext:d', true],
  1310. ['intitle:a intext:b', 'intitle:c inurl:d', false],
  1311. ];
  1312. }
  1313. #[DataProvider('provideBooleanSearchEnforce')]
  1314. public function testBooleanSearchEnforce(string $initialInput, string $enforceInput, string $expectedOutput): void {
  1315. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1316. $searchToEnforce = new FreshRSS_Search($enforceInput);
  1317. $newBooleanSearch = $booleanSearch->enforce($searchToEnforce);
  1318. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1319. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1320. }
  1321. /**
  1322. * @return array<array{string,string,string}>
  1323. */
  1324. public static function provideBooleanSearchEnforce(): array {
  1325. return [
  1326. ['', '', ''],
  1327. ['', 'intitle:b', 'intitle:b'],
  1328. ['intitle:a', '', 'intitle:a'],
  1329. ['intitle:a', 'intitle:b', 'intitle:b'],
  1330. ['a', 'intitle:b', 'intitle:b a'],
  1331. ['intitle:a intext:a', 'intitle:b', 'intitle:b intext:a'],
  1332. ['intitle:a inurl:a', 'intitle:b', 'intitle:b inurl:a'],
  1333. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:b (intitle:a OR inurl:a)'],
  1334. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', 'intitle:b (inurl:a intitle:c)'],
  1335. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', 'intitle:b (inurl:a OR intitle:c)'],
  1336. ['(intitle:a) (inurl:a)', 'intitle:b', 'intitle:b inurl:a'],
  1337. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a intitle:b'],
  1338. ['(a b) OR (c d)', 'e', 'e ((a b) OR (c d))'],
  1339. ['(a b) (c d)', 'e', 'e ((a b) (c d))'],
  1340. ['(a b)', 'e', 'e (a b)'],
  1341. ['date:2024/', 'date:/2025', 'date:/2025'],
  1342. ['a', 'date:/2025', 'date:/2025 a'],
  1343. ];
  1344. }
  1345. #[DataProvider('provideBooleanSearchRemove')]
  1346. public function testBooleanSearchRemove(string $initialInput, string $removeInput, string $expectedOutput): void {
  1347. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1348. $searchToRemove = new FreshRSS_Search($removeInput);
  1349. $newBooleanSearch = $booleanSearch->remove($searchToRemove);
  1350. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1351. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1352. }
  1353. /**
  1354. * @return array<array{string,string,string}>
  1355. */
  1356. public static function provideBooleanSearchRemove(): array {
  1357. return [
  1358. ['', 'intitle:b', ''],
  1359. ['intitle:a', 'intitle:b', ''],
  1360. ['intitle:a intext:a', 'intitle:b', 'intext:a'],
  1361. ['intitle:a inurl:a', 'intitle:b', 'inurl:a'],
  1362. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:a OR inurl:a'],
  1363. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', '(inurl:a intitle:c)'],
  1364. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', '(inurl:a OR intitle:c)'],
  1365. ['(intitle:a) (inurl:a)', 'intitle:b', 'inurl:a'],
  1366. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a'],
  1367. ['e ((a b) OR (c d))', 'e', '((a b) OR (c d))'],
  1368. ['e ((a b) (c d))', 'e', '((a b) (c d))'],
  1369. ['date:2024/', 'date:/2025', ''],
  1370. ['date:2024/ a', 'date:/2025', 'a'],
  1371. ];
  1372. }
  1373. #[DataProvider('provideNeedVisibility')]
  1374. public function testNeedVisibility(string $input, ?int $expected): void {
  1375. $search = new FreshRSS_Search($input);
  1376. self::assertSame($expected, $search->needVisibility());
  1377. }
  1378. /** @return list<list<string|int|null>> */
  1379. public static function provideNeedVisibility(): array {
  1380. return [
  1381. ['', null],
  1382. ['f:1', FreshRSS_Feed::PRIORITY_HIDDEN],
  1383. ['c:2', FreshRSS_Feed::PRIORITY_CATEGORY],
  1384. ['f:1 c:2', FreshRSS_Feed::PRIORITY_HIDDEN],
  1385. ['-f:1', null],
  1386. ['-c:2', null],
  1387. ];
  1388. }
  1389. }