4
0

SearchTest.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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. #[DataProvider('provideEmptyInput')]
  7. public static function test__construct_whenInputIsEmpty_getsOnlyNullValues(string $input): void {
  8. $search = new FreshRSS_Search($input);
  9. self::assertSame('', $search->__toString());
  10. self::assertNull($search->getIntitle());
  11. self::assertNull($search->getMinDate());
  12. self::assertNull($search->getMaxDate());
  13. self::assertNull($search->getMinPubdate());
  14. self::assertNull($search->getMaxPubdate());
  15. self::assertNull($search->getAuthor());
  16. self::assertNull($search->getTags());
  17. self::assertNull($search->getSearch());
  18. }
  19. /**
  20. * Return an array of values for the search object.
  21. * Here is the description of the values
  22. * @return array{array{''},array{' '}}
  23. */
  24. public static function provideEmptyInput(): array {
  25. return [
  26. [''],
  27. [' '],
  28. ];
  29. }
  30. /**
  31. * @param array<string>|null $intitle_value
  32. * @param array<string>|null $search_value
  33. */
  34. #[DataProvider('provideIntitleSearch')]
  35. public static function test__construct_whenInputContainsIntitle_setsIntitleProperty(string $input, ?array $intitle_value, ?array $search_value): void {
  36. $search = new FreshRSS_Search($input);
  37. self::assertSame($intitle_value, $search->getIntitle());
  38. self::assertSame($search_value, $search->getSearch());
  39. }
  40. /**
  41. * @return list<list<mixed>>
  42. */
  43. public static function provideIntitleSearch(): array {
  44. return [
  45. ['intitle:word1', ['word1'], null],
  46. ['intitle:word1-word2', ['word1-word2'], null],
  47. ['intitle:word1 word2', ['word1'], ['word2']],
  48. ['intitle:"word1 word2"', ['word1 word2'], null],
  49. ["intitle:'word1 word2'", ['word1 word2'], null],
  50. ['word1 intitle:word2', ['word2'], ['word1']],
  51. ['word1 intitle:word2 word3', ['word2'], ['word1', 'word3']],
  52. ['word1 intitle:"word2 word3"', ['word2 word3'], ['word1']],
  53. ["word1 intitle:'word2 word3'", ['word2 word3'], ['word1']],
  54. ['intitle:word1 intitle:word2', ['word1', 'word2'], null],
  55. ['intitle: word1 word2', null, ['word1', 'word2']],
  56. ['intitle:123', ['123'], null],
  57. ['intitle:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  58. ["intitle:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  59. ['intitle:"word1 word2\' word3"', ["word1 word2' word3"], null],
  60. ["intitle:'word1 word2\" word3'", ['word1 word2" word3'], null],
  61. ['intitle:"< & >"', ['&lt; &amp; &gt;'], null],
  62. ["intitle:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  63. ['intitle:word1+word2', ['word1+word2'], null],
  64. ];
  65. }
  66. /**
  67. * @param array<string>|null $intext_value
  68. * @param array<string>|null $search_value
  69. */
  70. #[DataProvider('provideIntextSearch')]
  71. public static function test__construct_whenInputContainsIntext(string $input, ?array $intext_value, ?array $search_value): void {
  72. $search = new FreshRSS_Search($input);
  73. self::assertSame($intext_value, $search->getIntext());
  74. self::assertSame($search_value, $search->getSearch());
  75. }
  76. /**
  77. * @return list<list<mixed>>
  78. */
  79. public static function provideIntextSearch(): array {
  80. return [
  81. ['intext:word1', ['word1'], null],
  82. ['intext:"word1 word2"', ['word1 word2'], null],
  83. ];
  84. }
  85. /**
  86. * @param array<string>|null $author_value
  87. * @param array<string>|null $search_value
  88. */
  89. #[DataProvider('provideAuthorSearch')]
  90. public static function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void {
  91. $search = new FreshRSS_Search($input);
  92. self::assertSame($author_value, $search->getAuthor());
  93. self::assertSame($search_value, $search->getSearch());
  94. }
  95. /**
  96. * @return list<list<mixed>>
  97. */
  98. public static function provideAuthorSearch(): array {
  99. return [
  100. ['author:word1', ['word1'], null],
  101. ['author:word1-word2', ['word1-word2'], null],
  102. ['author:word1 word2', ['word1'], ['word2']],
  103. ['author:"word1 word2"', ['word1 word2'], null],
  104. ["author:'word1 word2'", ['word1 word2'], null],
  105. ['word1 author:word2', ['word2'], ['word1']],
  106. ['word1 author:word2 word3', ['word2'], ['word1', 'word3']],
  107. ['word1 author:"word2 word3"', ['word2 word3'], ['word1']],
  108. ["word1 author:'word2 word3'", ['word2 word3'], ['word1']],
  109. ['author:word1 author:word2', ['word1', 'word2'], null],
  110. ['author: word1 word2', null, ['word1', 'word2']],
  111. ['author:123', ['123'], null],
  112. ['author:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  113. ["author:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  114. ['author:"word1 word2\' word3"', ["word1 word2' word3"], null],
  115. ["author:'word1 word2\" word3'", ['word1 word2" word3'], null],
  116. ["author:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  117. ['author:word1+word2', ['word1+word2'], null],
  118. ];
  119. }
  120. /**
  121. * @param array<string>|null $inurl_value
  122. * @param array<string>|null $search_value
  123. */
  124. #[DataProvider('provideInurlSearch')]
  125. public static function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void {
  126. $search = new FreshRSS_Search($input);
  127. self::assertSame($inurl_value, $search->getInurl());
  128. self::assertSame($search_value, $search->getSearch());
  129. }
  130. /**
  131. * @return list<list<mixed>>
  132. */
  133. public static function provideInurlSearch(): array {
  134. return [
  135. ['inurl:word1', ['word1'], null],
  136. ['inurl: word1', null, ['word1']],
  137. ['inurl:123', ['123'], null],
  138. ['inurl:word1 word2', ['word1'], ['word2']],
  139. ['inurl:"word1 word2"', ['word1 word2'], null],
  140. ['inurl:word1 word2 inurl:word3', ['word1', 'word3'], ['word2']],
  141. ["inurl:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  142. ['inurl:word1+word2', ['word1+word2'], null],
  143. ];
  144. }
  145. #[DataProvider('provideDateSearch')]
  146. public static function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void {
  147. $search = new FreshRSS_Search($input);
  148. self::assertSame($min_date_value, $search->getMinDate());
  149. self::assertSame($max_date_value, $search->getMaxDate());
  150. }
  151. /**
  152. * @return list<list<mixed>>
  153. */
  154. public static function provideDateSearch(): array {
  155. return [
  156. ['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  157. ['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  158. ['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  159. ['date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  160. ['date:2007-03-01/', strtotime('2007-03-01'), null],
  161. ['date:/2008-05-11', null, strtotime('2008-05-12') - 1],
  162. ];
  163. }
  164. #[DataProvider('providePubdateSearch')]
  165. public static function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void {
  166. $search = new FreshRSS_Search($input);
  167. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  168. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  169. }
  170. /**
  171. * @return list<list<mixed>>
  172. */
  173. public static function providePubdateSearch(): array {
  174. return [
  175. ['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  176. ['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  177. ['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  178. ['pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  179. ['pubdate:2007-03-01/', strtotime('2007-03-01'), null],
  180. ['pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  181. ];
  182. }
  183. #[DataProvider('provideUserdateSearch')]
  184. public static function test__construct_whenInputContainsUserdate(string $input, ?int $min_userdate_value, ?int $max_userdate_value): void {
  185. $search = new FreshRSS_Search($input);
  186. self::assertSame($min_userdate_value, $search->getMinUserdate());
  187. self::assertSame($max_userdate_value, $search->getMaxUserdate());
  188. }
  189. /**
  190. * @return list<list<mixed>>
  191. */
  192. public static function provideUserdateSearch(): array {
  193. return [
  194. ['userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  195. ['userdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  196. ];
  197. }
  198. /**
  199. * @param array<string>|null $tags_value
  200. * @param array<string>|null $search_value
  201. */
  202. #[DataProvider('provideTagsSearch')]
  203. public static function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  204. $search = new FreshRSS_Search($input);
  205. self::assertSame($tags_value, $search->getTags());
  206. self::assertSame($search_value, $search->getSearch());
  207. }
  208. /**
  209. * @return list<list<string|list<string>|null>>
  210. */
  211. public static function provideTagsSearch(): array {
  212. return [
  213. ['#word1', ['word1'], null],
  214. ['# word1', null, ['#', 'word1']],
  215. ['#123', ['123'], null],
  216. ['#word1 word2', ['word1'], ['word2']],
  217. ['#"word1 word2"', ['word1 word2'], null],
  218. ['#word1 #word2', ['word1', 'word2'], null],
  219. ["#word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  220. ['#word1+word2', ['word1 word2'], null]
  221. ];
  222. }
  223. /**
  224. * @param list<array{search:string}> $queries
  225. * @param array{0:string,1:list<string|int>} $expectedResult
  226. */
  227. #[DataProvider('provideSavedQueryIdExpansion')]
  228. public static function test__construct_whenInputContainsSavedQueryIds_expandsSavedSearches(array $queries, string $input, array $expectedResult): void {
  229. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  230. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  231. $newUserConf->queries = $queries;
  232. FreshRSS_Context::setUserConf($newUserConf);
  233. try {
  234. $search = new FreshRSS_BooleanSearch($input);
  235. [$actualValues, $actualSql] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $search);
  236. self::assertSame($expectedResult[0], trim($actualSql));
  237. self::assertSame($expectedResult[1], $actualValues);
  238. } finally {
  239. FreshRSS_Context::setUserConf($previousUserConf);
  240. }
  241. }
  242. /**
  243. * @return array<string,array{0:list<array{search:string}>,1:string,2:array{0:string,1:list<string|int>}}>
  244. */
  245. public static function provideSavedQueryIdExpansion(): array {
  246. return [
  247. 'expanded single group' => [
  248. [
  249. ['search' => 'author:Alice'],
  250. ['search' => 'intitle:World'],
  251. ],
  252. 'S:0,1',
  253. [
  254. '((e.author LIKE ? )) OR ((e.title LIKE ? ))',
  255. ['%Alice%', '%World%'],
  256. ],
  257. ],
  258. 'separate groups with OR' => [
  259. [
  260. ['search' => 'author:Alice'],
  261. ['search' => 'intitle:World'],
  262. ['search' => 'inurl:Example'],
  263. ['search' => 'author:Bob'],
  264. ],
  265. 'S:0,1 OR S:2,3',
  266. [
  267. '((e.author LIKE ? )) OR ((e.title LIKE ? )) OR ((e.link LIKE ? )) OR ((e.author LIKE ? ))',
  268. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  269. ],
  270. ],
  271. 'mixed with other clauses' => [
  272. [
  273. ['search' => 'author:Alice'],
  274. ['search' => 'intitle:World'],
  275. ],
  276. 'intitle:Hello S:0,1 date:2025-10',
  277. [
  278. '((e.title LIKE ? )) AND ((e.author LIKE ? )) OR ((e.title LIKE ? )) AND ((e.id >= ? AND e.id <= ? ))',
  279. ['%Hello%', '%Alice%', '%World%', strtotime('2025-10-01') . '000000', (strtotime('2025-11-01') - 1) . '000000'],
  280. ],
  281. ],
  282. ];
  283. }
  284. /**
  285. * @param array<string>|null $author_value
  286. * @param array<string> $intitle_value
  287. * @param array<string>|null $inurl_value
  288. * @param array<string>|null $tags_value
  289. * @param array<string>|null $search_value
  290. */
  291. #[DataProvider('provideMultipleSearch')]
  292. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  293. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  294. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  295. $search = new FreshRSS_Search($input);
  296. self::assertSame($author_value, $search->getAuthor());
  297. self::assertSame($min_date_value, $search->getMinDate());
  298. self::assertSame($max_date_value, $search->getMaxDate());
  299. self::assertSame($intitle_value, $search->getIntitle());
  300. self::assertSame($inurl_value, $search->getInurl());
  301. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  302. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  303. self::assertSame($tags_value, $search->getTags());
  304. self::assertSame($search_value, $search->getSearch());
  305. }
  306. /** @return list<list<mixed>> */
  307. public static function provideMultipleSearch(): array {
  308. return [
  309. [
  310. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  311. ['word1'],
  312. strtotime('2007-03-01'),
  313. strtotime('2008-05-12') - 1,
  314. ['word2'],
  315. ['word3'],
  316. strtotime('2007-03-01'),
  317. strtotime('2008-05-12') - 1,
  318. ['word4', 'word5'],
  319. null
  320. ],
  321. [
  322. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  323. ['word1'],
  324. strtotime('2007-03-01'),
  325. strtotime('2008-05-12') - 1,
  326. ['word2'],
  327. ['word3'],
  328. strtotime('2007-03-01'),
  329. strtotime('2008-05-12') - 1,
  330. ['word4', 'word5'],
  331. ['word6']
  332. ],
  333. [
  334. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  335. ['word1'],
  336. strtotime('2007-03-01'),
  337. strtotime('2008-05-12') - 1,
  338. ['word2'],
  339. ['word3'],
  340. strtotime('2007-03-01'),
  341. strtotime('2008-05-12') - 1,
  342. ['word4', 'word5'],
  343. ['word6', 'word7']
  344. ],
  345. [
  346. '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',
  347. ['word1'],
  348. strtotime('2007-03-01'),
  349. strtotime('2008-05-12') - 1,
  350. ['word2'],
  351. ['word3'],
  352. strtotime('2007-03-01'),
  353. strtotime('2008-05-12') - 1,
  354. ['word4', 'word5'],
  355. ['word7 word8', 'word6']
  356. ]
  357. ];
  358. }
  359. #[DataProvider('provideAddOrParentheses')]
  360. public static function test__addOrParentheses(string $input, string $output): void {
  361. self::assertSame($output, FreshRSS_BooleanSearch::addOrParentheses($input));
  362. }
  363. /** @return list<list{string,string}> */
  364. public static function provideAddOrParentheses(): array {
  365. return [
  366. ['ab', 'ab'],
  367. ['ab cd', 'ab cd'],
  368. ['!ab -cd', '!ab -cd'],
  369. ['ab OR cd', '(ab) OR (cd)'],
  370. ['!ab OR -cd', '(!ab) OR (-cd)'],
  371. ['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],
  372. ['ab (!cd)', 'ab (!cd)'],
  373. ['"ab" (!"cd")', '"ab" (!"cd")'],
  374. ];
  375. }
  376. #[DataProvider('provideconsistentOrParentheses')]
  377. public static function test__consistentOrParentheses(string $input, string $output): void {
  378. self::assertSame($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  379. }
  380. /** @return list<list{string,string}> */
  381. public static function provideconsistentOrParentheses(): array {
  382. return [
  383. ['ab cd ef', 'ab cd ef'],
  384. ['(ab cd ef)', '(ab cd ef)'],
  385. ['("ab cd" ef)', '("ab cd" ef)'],
  386. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  387. ['ab (!cd)', 'ab (!cd)'],
  388. ['ab !(cd)', 'ab !(cd)'],
  389. ['(ab) -(cd)', '(ab) -(cd)'],
  390. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  391. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  392. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  393. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  394. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  395. ['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],
  396. ['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],
  397. ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],
  398. ];
  399. }
  400. /**
  401. * @param array<string> $values
  402. */
  403. #[DataProvider('provideParentheses')]
  404. public function test__parentheses(string $input, string $sql, array $values): void {
  405. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  406. self::assertSame(trim($sql), trim($filterSearch));
  407. self::assertSame($values, $filterValues);
  408. }
  409. /** @return list<list<mixed>> */
  410. public static function provideParentheses(): array {
  411. return [
  412. [
  413. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  414. ' ((e.id_feed IN (?) )) AND ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ) OR (e.id_feed IN (?) )) AND' .
  415. ' (((e.id_feed IN (?) )) OR ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ))) ',
  416. [1, 2, 3, 4, 5, 6, 7]
  417. ],
  418. [
  419. 'c:1 OR c:2,3',
  420. ' (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 (?,?)) ) ',
  421. [1, 2, 3]
  422. ],
  423. [
  424. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  425. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  426. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  427. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 12]
  428. ],
  429. [
  430. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  431. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  432. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  433. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  434. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  435. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 'Bleu']
  436. ],
  437. [
  438. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  439. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  440. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  441. ],
  442. [
  443. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  444. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  445. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  446. ],
  447. [
  448. 'intitle:"(test)"',
  449. '(e.title LIKE ? )',
  450. ['%(test)%'],
  451. ],
  452. [
  453. 'intitle:\'"hello world"\'',
  454. '(e.title LIKE ? )',
  455. ['%"hello world"%'],
  456. ],
  457. [
  458. 'intext:\'"hello world"\'',
  459. '(e.content LIKE ? )',
  460. ['%"hello world"%'],
  461. ],
  462. [
  463. '(ab) OR (cd) OR (ef)',
  464. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  465. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  466. ],
  467. [
  468. '("plain or text") OR (cd)',
  469. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  470. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  471. ],
  472. [
  473. '"plain or text" OR cd',
  474. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  475. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  476. ],
  477. [
  478. '"plain OR text" OR cd',
  479. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) ',
  480. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  481. ],
  482. [
  483. 'ab OR cd OR (ef)',
  484. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ',
  485. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  486. ],
  487. [
  488. 'ab OR cd OR ef',
  489. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  490. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  491. ],
  492. [
  493. '(ab) cd OR ef OR (gh)',
  494. '(((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  495. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  496. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  497. ],
  498. [
  499. '(ab) OR cd OR ef OR (gh)',
  500. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  501. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  502. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  503. ],
  504. [
  505. 'ab OR (!(cd OR ef))',
  506. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )))',
  507. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  508. ],
  509. [
  510. 'ab !(cd OR ef)',
  511. '(((e.title LIKE ? OR e.content LIKE ?) )) AND NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  512. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  513. ],
  514. [
  515. 'ab OR !(cd OR ef)',
  516. '(((e.title LIKE ? OR e.content LIKE ?) )) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  517. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  518. ],
  519. [
  520. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  521. '((((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 ?) )) ' .
  522. '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 ?) ))',
  523. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  524. ],
  525. [
  526. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  527. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  528. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  529. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  530. ],
  531. [
  532. 'intitle:"é & \' è" intext:/<&>/ \'< & " >\'',
  533. '(e.title LIKE ? AND e.content ~ ? AND (e.title LIKE ? OR e.content LIKE ?) )',
  534. ['%é &amp; \' è%', '<&>', '%&lt; &amp; " &gt;%', '%&lt; &amp; " &gt;%']
  535. ],
  536. [
  537. '/^(ab|cd) [(] \\) (ef|gh)/',
  538. '((e.title ~ ? OR e.content ~ ?) )',
  539. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  540. ],
  541. [
  542. '!/^(ab|cd)/',
  543. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  544. ['^(ab|cd)', '^(ab|cd)']
  545. ],
  546. [
  547. 'intitle:/^(ab|cd)/',
  548. '(e.title ~ ? )',
  549. ['^(ab|cd)']
  550. ],
  551. [
  552. 'intext:/^(ab|cd)/',
  553. '(e.content ~ ? )',
  554. ['^(ab|cd)']
  555. ],
  556. [
  557. 'L:1 L:2',
  558. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) AND ' .
  559. 'e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )',
  560. [1, 2]
  561. ],
  562. [
  563. 'L:1,2',
  564. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?,?)) )',
  565. [1, 2]
  566. ],
  567. ];
  568. }
  569. /**
  570. * @param array<string> $values
  571. */
  572. #[DataProvider('provideDateOperators')]
  573. public function test__date_operators(string $input, string $sql, array $values): void {
  574. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  575. self::assertSame(trim($sql), trim($filterSearch));
  576. self::assertSame($values, $filterValues);
  577. }
  578. /** @return list<list<mixed>> */
  579. public static function provideDateOperators(): array {
  580. return [
  581. // Basic date operator tests
  582. [
  583. 'date:2007-03-01/2008-05-11',
  584. '(e.id >= ? AND e.id <= ? )',
  585. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  586. ],
  587. [
  588. 'date:2007-03-01/',
  589. '(e.id >= ? )',
  590. [strtotime('2007-03-01T00:00:00Z') . '000000'],
  591. ],
  592. [
  593. 'date:/2008-05-11',
  594. '(e.id <= ? )',
  595. [strtotime('2008-05-11T23:59:59Z') . '000000'],
  596. ],
  597. // Basic pubdate operator tests
  598. [
  599. 'pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  600. '(e.date >= ? AND e.date <= ? )',
  601. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  602. ],
  603. [
  604. 'pubdate:2007-03-01/',
  605. '(e.date >= ? )',
  606. [strtotime('2007-03-01T00:00:00Z')],
  607. ],
  608. [
  609. 'pubdate:/2008-05-11',
  610. '(e.date <= ? )',
  611. [strtotime('2008-05-11T23:59:59Z')],
  612. ],
  613. // Basic userdate operator tests
  614. [
  615. 'userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  616. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? )',
  617. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  618. ],
  619. [
  620. 'userdate:2007-03-01/',
  621. '(e.`lastUserModified` >= ? )',
  622. [strtotime('2007-03-01T00:00:00Z')],
  623. ],
  624. [
  625. 'userdate:/2008-05-11',
  626. '(e.`lastUserModified` <= ? )',
  627. [strtotime('2008-05-11T23:59:59Z')],
  628. ],
  629. // Negative date operator tests
  630. [
  631. '-date:2007-03-01/2008-05-11',
  632. '((e.id < ? OR e.id > ?) )',
  633. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  634. ],
  635. [
  636. '!pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  637. '((e.date < ? OR e.date > ?) )',
  638. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  639. ],
  640. [
  641. '!userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  642. '((e.`lastUserModified` < ? OR e.`lastUserModified` > ?) )',
  643. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  644. ],
  645. // Combined date operators
  646. [
  647. 'date:2007-03-01/ pubdate:/2008-05-11',
  648. '(e.id >= ? AND e.date <= ? )',
  649. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z')],
  650. ],
  651. [
  652. 'pubdate:2007-03-01/ userdate:/2008-05-11',
  653. '(e.date >= ? AND e.`lastUserModified` <= ? )',
  654. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  655. ],
  656. [
  657. 'date:2007-03-01/ userdate:2007-06-01/',
  658. '(e.id >= ? AND e.`lastUserModified` >= ? )',
  659. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2007-06-01T00:00:00Z')],
  660. ],
  661. // Complex combinations with other operators
  662. [
  663. 'intitle:test date:2007-03-01/ pubdate:/2008-05-11',
  664. '(e.id >= ? AND e.date <= ? AND e.title LIKE ? )',
  665. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z'), '%test%'],
  666. ],
  667. [
  668. 'author:john userdate:2007-03-01/2008-05-11',
  669. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? AND e.author LIKE ? )',
  670. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z'), '%john%'],
  671. ],
  672. // Mixed positive and negative date operators
  673. [
  674. 'date:2007-03-01/ !pubdate:2008-01-01/2008-05-11',
  675. '(e.id >= ? AND (e.date < ? OR e.date > ?) )',
  676. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-01-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  677. ],
  678. ];
  679. }
  680. /**
  681. * @dataProvider provideRegexPostreSQL
  682. * @param array<string> $values
  683. */
  684. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  685. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  686. self::assertSame(trim($sql), trim($filterSearch));
  687. self::assertSame($values, $filterValues);
  688. }
  689. /** @return list<list<mixed>> */
  690. public static function provideRegexPostreSQL(): array {
  691. return [
  692. [
  693. 'intitle:/^ab$/',
  694. '(e.title ~ ? )',
  695. ['^ab$']
  696. ],
  697. [
  698. 'intitle:/^ab$/i',
  699. '(e.title ~* ? )',
  700. ['^ab$']
  701. ],
  702. [
  703. 'intitle:/^ab$/m',
  704. '(e.title ~ ? )',
  705. ['(?m)^ab$']
  706. ],
  707. [
  708. 'intitle:/^ab\\M/',
  709. '(e.title ~ ? )',
  710. ['^ab\\M']
  711. ],
  712. [
  713. 'intext:/^ab\\M/',
  714. '(e.content ~ ? )',
  715. ['^ab\\M']
  716. ],
  717. [
  718. 'intitle:/\\b\\d+/',
  719. '(e.title ~ ? )',
  720. ['\\y\\d+']
  721. ],
  722. [
  723. 'author:/^ab$/',
  724. "(REPLACE(e.author, ';', '\n') ~ ? )",
  725. ['^ab$']
  726. ],
  727. [
  728. 'inurl:/^ab$/',
  729. '(e.link ~ ? )',
  730. ['^ab$']
  731. ],
  732. [
  733. '/^ab$/',
  734. '((e.title ~ ? OR e.content ~ ?) )',
  735. ['^ab$', '^ab$']
  736. ],
  737. [
  738. '!/^ab$/',
  739. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  740. ['^ab$', '^ab$']
  741. ],
  742. [
  743. '#/^a(b|c)$/im',
  744. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ? )",
  745. ['(?m)^a(b|c)$']
  746. ],
  747. [ // Not a regex
  748. 'inurl:https://example.net/test/',
  749. '(e.link LIKE ? )',
  750. ['%https://example.net/test/%']
  751. ],
  752. [ // Not a regex
  753. 'https://example.net/test/',
  754. '((e.title LIKE ? OR e.content LIKE ?) )',
  755. ['%https://example.net/test/%', '%https://example.net/test/%']
  756. ],
  757. [ // Regex with literal 'or'
  758. 'intitle:/^A or B/i',
  759. '(e.title ~* ? )',
  760. ['^A or B']
  761. ],
  762. [ // Regex with literal 'OR'
  763. 'intitle:/^A B OR C D/i OR intitle:/^A B OR C D/i',
  764. '(e.title ~* ? ) OR (e.title ~* ? )',
  765. ['^A B OR C D', '^A B OR C D']
  766. ],
  767. [ // Quote with literal 'OR'
  768. 'intitle:"A B OR C D" OR intitle:"E or F"',
  769. '(e.title LIKE ? ) OR (e.title LIKE ? )',
  770. ['%A B OR C D%', '%E or F%']
  771. ],
  772. ];
  773. }
  774. /**
  775. * @dataProvider provideRegexMariaDB
  776. * @param array<string> $values
  777. */
  778. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  779. FreshRSS_DatabaseDAO::$dummyConnection = true;
  780. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  781. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  782. self::assertSame(trim($sql), trim($filterSearch));
  783. self::assertSame($values, $filterValues);
  784. }
  785. /** @return list<list<mixed>> */
  786. public static function provideRegexMariaDB(): array {
  787. return [
  788. [
  789. 'intitle:/^ab$/',
  790. "(e.title REGEXP ? )",
  791. ['(?-i)^ab$']
  792. ],
  793. [
  794. 'intitle:/^ab$/i',
  795. "(e.title REGEXP ? )",
  796. ['(?i)^ab$']
  797. ],
  798. [
  799. 'intitle:/^ab$/m',
  800. "(e.title REGEXP ? )",
  801. ['(?-i)(?m)^ab$']
  802. ],
  803. [
  804. 'intitle:/\\b\\d+/',
  805. "(e.title REGEXP ? )",
  806. ['(?-i)\\b\\d+']
  807. ],
  808. [
  809. 'intext:/^ab$/m',
  810. '(UNCOMPRESS(e.content_bin) REGEXP ?) )',
  811. ['(?-i)(?m)^ab$']
  812. ],
  813. ];
  814. }
  815. /**
  816. * @dataProvider provideRegexMySQL
  817. * @param array<string> $values
  818. */
  819. public function test__regex_mysql(string $input, string $sql, array $values): void {
  820. FreshRSS_DatabaseDAO::$dummyConnection = true;
  821. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  822. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  823. self::assertSame(trim($sql), trim($filterSearch));
  824. self::assertSame($values, $filterValues);
  825. }
  826. /** @return list<list<mixed>> */
  827. public static function provideRegexMySQL(): array {
  828. return [
  829. [
  830. 'intitle:/^ab$/',
  831. "(REGEXP_LIKE(e.title,?,'c') )",
  832. ['^ab$']
  833. ],
  834. [
  835. 'intitle:/^ab$/i',
  836. "(REGEXP_LIKE(e.title,?,'i') )",
  837. ['^ab$']
  838. ],
  839. [
  840. 'intitle:/^ab$/m',
  841. "(REGEXP_LIKE(e.title,?,'mc') )",
  842. ['^ab$']
  843. ],
  844. [
  845. 'intext:/^ab$/m',
  846. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc')) )",
  847. ['^ab$']
  848. ],
  849. ];
  850. }
  851. /**
  852. * @dataProvider provideRegexSQLite
  853. * @param array<string> $values
  854. */
  855. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  856. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  857. self::assertSame(trim($sql), trim($filterSearch));
  858. self::assertSame($values, $filterValues);
  859. }
  860. /** @return list<list<mixed>> */
  861. public static function provideRegexSQLite(): array {
  862. return [
  863. [
  864. 'intitle:/^ab$/',
  865. "(e.title REGEXP ? )",
  866. ['/^ab$/']
  867. ],
  868. [
  869. 'intitle:/^ab$/i',
  870. "(e.title REGEXP ? )",
  871. ['/^ab$/i']
  872. ],
  873. [
  874. 'intitle:/^ab$/m',
  875. "(e.title REGEXP ? )",
  876. ['/^ab$/m']
  877. ],
  878. [
  879. 'intitle:/^ab\\b/',
  880. '(e.title REGEXP ? )',
  881. ['/^ab\\b/']
  882. ],
  883. [
  884. 'intext:/^ab\\b/',
  885. '(e.content REGEXP ? )',
  886. ['/^ab\\b/']
  887. ],
  888. ];
  889. }
  890. #[DataProvider('provideToString')]
  891. public static function test__toString(string $input): void {
  892. $search = new FreshRSS_Search($input);
  893. $expected = str_replace("\n", ' ', $input);
  894. self::assertSame($expected, $search->__toString());
  895. }
  896. /**
  897. * @return array<array<string>>
  898. */
  899. public static function provideToString(): array {
  900. return [
  901. [
  902. <<<'EOD'
  903. e:1,2 f:10,11 c:20,21 L:30,31 labels:"My label,My other label"
  904. userdate:2025-01-01T00:00:00/2026-01-01T00:00:00
  905. pubdate:2025-02-01T00:00:00/2026-01-01T00:00:00
  906. date:2025-03-01T00:00:00/2026-01-01T00:00:00
  907. intitle:/<Inter&sting>/i intitle:"g ' & d"
  908. intext:/<Inter&sting>/i intext:g&d
  909. author:/Bob/ author:Alice
  910. inurl:/https/ inurl:example.net
  911. #/tag2/ #tag1
  912. /search_regex/i "quoted search" search
  913. -e:3,4 -f:12,13 -c:22,23 -L:32,33 -labels:"Not label,Not other label"
  914. -userdate:2025-06-01T00:00:00/2025-09-01T00:00:00
  915. -pubdate:2025
  916. -date:P30D
  917. -intitle:/Spam/i -intitle:"'bad"
  918. -intext:/Spam/i -intext:"'bad"
  919. -author:/Dave/i -author:Charlie
  920. -inurl:/ftp/ -inurl:example.com
  921. -#/tag4/ -#tag3
  922. -/not_regex/i -"not quoted" -not_search
  923. EOD
  924. ],
  925. ];
  926. }
  927. #[DataProvider('provideBooleanSearchToString')]
  928. public static function testBooleanSearch__toString(string $input, string $expected): void {
  929. $search = new FreshRSS_BooleanSearch($input);
  930. self::assertSame($expected, $search->__toString());
  931. }
  932. /**
  933. * @return array<array<string>>
  934. */
  935. public static function provideBooleanSearchToString(): array {
  936. return [
  937. [
  938. '((a OR b) (c OR d) -e) OR -(f g)',
  939. '((a OR b) (c OR d) (-e)) OR -(f g)',
  940. ],
  941. [
  942. '((a OR b) ((c) OR ((d))) (-e)) OR -(((f g)))',
  943. '((a OR b) (c OR d) (-e)) OR -(f g)',
  944. ],
  945. [
  946. '!((b c))',
  947. '-(b c)',
  948. ],
  949. [
  950. '(a) OR !((b c))',
  951. 'a OR -(b c)',
  952. ],
  953. [
  954. '((a) (b))',
  955. 'a b',
  956. ],
  957. [
  958. '((a) OR (b))',
  959. 'a OR b',
  960. ],
  961. [
  962. ' ( !( !( ( a ) ) ) ) ( ) ',
  963. '-(-a)',
  964. ],
  965. [
  966. '-intitle:a -inurl:b',
  967. '-intitle:a -inurl:b',
  968. ],
  969. [
  970. 'intitle:/^A or B/i',
  971. 'intitle:/^A or B/i',
  972. ],
  973. [
  974. 'intitle:/^A B OR C D/i',
  975. 'intitle:/^A B OR C D/i',
  976. ],
  977. ];
  978. }
  979. #[DataProvider('provideHasSameOperators')]
  980. public function testHasSameOperators(string $input1, string $input2, bool $expected): void {
  981. $search1 = new FreshRSS_Search($input1);
  982. $search2 = new FreshRSS_Search($input2);
  983. self::assertSame($expected, $search1->hasSameOperators($search2));
  984. }
  985. /**
  986. * @return array<array{string,string,bool}>
  987. */
  988. public static function provideHasSameOperators(): array {
  989. return [
  990. ['', '', true],
  991. ['intitle:a intext:b', 'intitle:c intext:d', true],
  992. ['intitle:a intext:b', 'intitle:c inurl:d', false],
  993. ];
  994. }
  995. #[DataProvider('provideBooleanSearchEnforce')]
  996. public function testBooleanSearchEnforce(string $initialInput, string $enforceInput, string $expectedOutput): void {
  997. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  998. $searchToEnforce = new FreshRSS_Search($enforceInput);
  999. $newBooleanSearch = $booleanSearch->enforce($searchToEnforce);
  1000. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1001. self::assertSame($expectedOutput, $newBooleanSearch->__toString());
  1002. }
  1003. /**
  1004. * @return array<array{string,string,string}>
  1005. */
  1006. public static function provideBooleanSearchEnforce(): array {
  1007. return [
  1008. ['', '', ''],
  1009. ['', 'intitle:b', 'intitle:b'],
  1010. ['intitle:a', '', 'intitle:a'],
  1011. ['intitle:a', 'intitle:b', 'intitle:b'],
  1012. ['a', 'intitle:b', 'intitle:b a'],
  1013. ['intitle:a intext:a', 'intitle:b', 'intitle:b intext:a'],
  1014. ['intitle:a inurl:a', 'intitle:b', 'intitle:b inurl:a'],
  1015. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:b (intitle:a OR inurl:a)'],
  1016. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', 'intitle:b (inurl:a intitle:c)'],
  1017. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', 'intitle:b (inurl:a OR intitle:c)'],
  1018. ['(intitle:a) (inurl:a)', 'intitle:b', 'intitle:b inurl:a'],
  1019. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a intitle:b'],
  1020. ['(a b) OR (c d)', 'e', 'e ((a b) OR (c d))'],
  1021. ['(a b) (c d)', 'e', 'e ((a b) (c d))'],
  1022. ['(a b)', 'e', 'e (a b)'],
  1023. ['date:2024/', 'date:/2025', 'date:/2025'],
  1024. ['a', 'date:/2025', 'date:/2025 a'],
  1025. ];
  1026. }
  1027. #[DataProvider('provideBooleanSearchRemove')]
  1028. public function testBooleanSearchRemove(string $initialInput, string $removeInput, string $expectedOutput): void {
  1029. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1030. $searchToRemove = new FreshRSS_Search($removeInput);
  1031. $newBooleanSearch = $booleanSearch->remove($searchToRemove);
  1032. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1033. self::assertSame($expectedOutput, $newBooleanSearch->__toString());
  1034. }
  1035. /**
  1036. * @return array<array{string,string,string}>
  1037. */
  1038. public static function provideBooleanSearchRemove(): array {
  1039. return [
  1040. ['', 'intitle:b', ''],
  1041. ['intitle:a', 'intitle:b', ''],
  1042. ['intitle:a intext:a', 'intitle:b', 'intext:a'],
  1043. ['intitle:a inurl:a', 'intitle:b', 'inurl:a'],
  1044. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:a OR inurl:a'],
  1045. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', '(inurl:a intitle:c)'],
  1046. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', '(inurl:a OR intitle:c)'],
  1047. ['(intitle:a) (inurl:a)', 'intitle:b', 'inurl:a'],
  1048. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a'],
  1049. ['e ((a b) OR (c d))', 'e', '((a b) OR (c d))'],
  1050. ['e ((a b) (c d))', 'e', '((a b) (c d))'],
  1051. ['date:2024/', 'date:/2025', ''],
  1052. ['date:2024/ a', 'date:/2025', 'a'],
  1053. ];
  1054. }
  1055. }