SearchTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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->getRawInput());
  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:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  62. ['intitle:word1+word2', ['word1+word2'], null],
  63. ];
  64. }
  65. /**
  66. * @param array<string>|null $intext_value
  67. * @param array<string>|null $search_value
  68. */
  69. #[DataProvider('provideIntextSearch')]
  70. public static function test__construct_whenInputContainsIntext(string $input, ?array $intext_value, ?array $search_value): void {
  71. $search = new FreshRSS_Search($input);
  72. self::assertSame($intext_value, $search->getIntext());
  73. self::assertSame($search_value, $search->getSearch());
  74. }
  75. /**
  76. * @return list<list<mixed>>
  77. */
  78. public static function provideIntextSearch(): array {
  79. return [
  80. ['intext:word1', ['word1'], null],
  81. ['intext:"word1 word2"', ['word1 word2'], null],
  82. ];
  83. }
  84. /**
  85. * @param array<string>|null $author_value
  86. * @param array<string>|null $search_value
  87. */
  88. #[DataProvider('provideAuthorSearch')]
  89. public static function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void {
  90. $search = new FreshRSS_Search($input);
  91. self::assertSame($author_value, $search->getAuthor());
  92. self::assertSame($search_value, $search->getSearch());
  93. }
  94. /**
  95. * @return list<list<mixed>>
  96. */
  97. public static function provideAuthorSearch(): array {
  98. return [
  99. ['author:word1', ['word1'], null],
  100. ['author:word1-word2', ['word1-word2'], null],
  101. ['author:word1 word2', ['word1'], ['word2']],
  102. ['author:"word1 word2"', ['word1 word2'], null],
  103. ["author:'word1 word2'", ['word1 word2'], null],
  104. ['word1 author:word2', ['word2'], ['word1']],
  105. ['word1 author:word2 word3', ['word2'], ['word1', 'word3']],
  106. ['word1 author:"word2 word3"', ['word2 word3'], ['word1']],
  107. ["word1 author:'word2 word3'", ['word2 word3'], ['word1']],
  108. ['author:word1 author:word2', ['word1', 'word2'], null],
  109. ['author: word1 word2', null, ['word1', 'word2']],
  110. ['author:123', ['123'], null],
  111. ['author:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  112. ["author:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  113. ['author:"word1 word2\' word3"', ["word1 word2' word3"], null],
  114. ["author:'word1 word2\" word3'", ['word1 word2" word3'], null],
  115. ["author:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  116. ['author:word1+word2', ['word1+word2'], null],
  117. ];
  118. }
  119. /**
  120. * @param array<string>|null $inurl_value
  121. * @param array<string>|null $search_value
  122. */
  123. #[DataProvider('provideInurlSearch')]
  124. public static function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void {
  125. $search = new FreshRSS_Search($input);
  126. self::assertSame($inurl_value, $search->getInurl());
  127. self::assertSame($search_value, $search->getSearch());
  128. }
  129. /**
  130. * @return list<list<mixed>>
  131. */
  132. public static function provideInurlSearch(): array {
  133. return [
  134. ['inurl:word1', ['word1'], null],
  135. ['inurl: word1', null, ['word1']],
  136. ['inurl:123', ['123'], null],
  137. ['inurl:word1 word2', ['word1'], ['word2']],
  138. ['inurl:"word1 word2"', ['word1 word2'], null],
  139. ['inurl:word1 word2 inurl:word3', ['word1', 'word3'], ['word2']],
  140. ["inurl:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  141. ['inurl:word1+word2', ['word1+word2'], null],
  142. ];
  143. }
  144. #[DataProvider('provideDateSearch')]
  145. public static function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void {
  146. $search = new FreshRSS_Search($input);
  147. self::assertSame($min_date_value, $search->getMinDate());
  148. self::assertSame($max_date_value, $search->getMaxDate());
  149. }
  150. /**
  151. * @return list<list<mixed>>
  152. */
  153. public static function provideDateSearch(): array {
  154. return [
  155. ['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  156. ['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  157. ['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  158. ['date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  159. ['date:2007-03-01/', strtotime('2007-03-01'), null],
  160. ['date:/2008-05-11', null, strtotime('2008-05-12') - 1],
  161. ];
  162. }
  163. #[DataProvider('providePubdateSearch')]
  164. public static function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void {
  165. $search = new FreshRSS_Search($input);
  166. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  167. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  168. }
  169. /**
  170. * @return list<list<mixed>>
  171. */
  172. public static function providePubdateSearch(): array {
  173. return [
  174. ['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  175. ['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  176. ['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  177. ['pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  178. ['pubdate:2007-03-01/', strtotime('2007-03-01'), null],
  179. ['pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  180. ];
  181. }
  182. #[DataProvider('provideUserdateSearch')]
  183. public static function test__construct_whenInputContainsUserdate(string $input, ?int $min_userdate_value, ?int $max_userdate_value): void {
  184. $search = new FreshRSS_Search($input);
  185. self::assertSame($min_userdate_value, $search->getMinUserdate());
  186. self::assertSame($max_userdate_value, $search->getMaxUserdate());
  187. }
  188. /**
  189. * @return list<list<mixed>>
  190. */
  191. public static function provideUserdateSearch(): array {
  192. return [
  193. ['userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  194. ['userdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  195. ];
  196. }
  197. /**
  198. * @param array<string>|null $tags_value
  199. * @param array<string>|null $search_value
  200. */
  201. #[DataProvider('provideTagsSearch')]
  202. public static function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  203. $search = new FreshRSS_Search($input);
  204. self::assertSame($tags_value, $search->getTags());
  205. self::assertSame($search_value, $search->getSearch());
  206. }
  207. /**
  208. * @return list<list<string|list<string>|null>>
  209. */
  210. public static function provideTagsSearch(): array {
  211. return [
  212. ['#word1', ['word1'], null],
  213. ['# word1', null, ['#', 'word1']],
  214. ['#123', ['123'], null],
  215. ['#word1 word2', ['word1'], ['word2']],
  216. ['#"word1 word2"', ['word1 word2'], null],
  217. ['#word1 #word2', ['word1', 'word2'], null],
  218. ["#word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  219. ['#word1+word2', ['word1 word2'], null]
  220. ];
  221. }
  222. /**
  223. * @param list<array{search:string}> $queries
  224. * @param array{0:string,1:list<string|int>} $expectedResult
  225. */
  226. #[DataProvider('provideSavedQueryIdExpansion')]
  227. public static function test__construct_whenInputContainsSavedQueryIds_expandsSavedSearches(array $queries, string $input, array $expectedResult): void {
  228. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  229. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  230. $newUserConf->queries = $queries;
  231. FreshRSS_Context::$user_conf = $newUserConf;
  232. try {
  233. $search = new FreshRSS_BooleanSearch($input);
  234. [$actualValues, $actualSql] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $search);
  235. self::assertSame($expectedResult[0], trim($actualSql));
  236. self::assertSame($expectedResult[1], $actualValues);
  237. } finally {
  238. FreshRSS_Context::$user_conf = $previousUserConf;
  239. }
  240. }
  241. /**
  242. * @return array<string,array{0:list<array{search:string}>,1:string,2:array{0:string,1:list<string|int>}}>
  243. */
  244. public static function provideSavedQueryIdExpansion(): array {
  245. return [
  246. 'expanded single group' => [
  247. [
  248. ['search' => 'author:Alice'],
  249. ['search' => 'intitle:World'],
  250. ],
  251. 'S:0,1',
  252. [
  253. '((e.author LIKE ? )) OR ((e.title LIKE ? ))',
  254. ['%Alice%', '%World%'],
  255. ],
  256. ],
  257. 'separate groups with OR' => [
  258. [
  259. ['search' => 'author:Alice'],
  260. ['search' => 'intitle:World'],
  261. ['search' => 'inurl:Example'],
  262. ['search' => 'author:Bob'],
  263. ],
  264. 'S:0,1 OR S:2,3',
  265. [
  266. '((e.author LIKE ? )) OR ((e.title LIKE ? )) OR ((e.link LIKE ? )) OR ((e.author LIKE ? ))',
  267. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  268. ],
  269. ],
  270. 'mixed with other clauses' => [
  271. [
  272. ['search' => 'author:Alice'],
  273. ['search' => 'intitle:World'],
  274. ],
  275. 'intitle:Hello S:0,1 date:2025-10',
  276. [
  277. '((e.title LIKE ? )) AND ((e.author LIKE ? )) OR ((e.title LIKE ? )) AND ((e.id >= ? AND e.id <= ? ))',
  278. ['%Hello%', '%Alice%', '%World%', strtotime('2025-10-01') . '000000', (strtotime('2025-11-01') - 1) . '000000'],
  279. ],
  280. ],
  281. ];
  282. }
  283. /**
  284. * @param array<string>|null $author_value
  285. * @param array<string> $intitle_value
  286. * @param array<string>|null $inurl_value
  287. * @param array<string>|null $tags_value
  288. * @param array<string>|null $search_value
  289. */
  290. #[DataProvider('provideMultipleSearch')]
  291. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  292. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  293. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  294. $search = new FreshRSS_Search($input);
  295. self::assertSame($author_value, $search->getAuthor());
  296. self::assertSame($min_date_value, $search->getMinDate());
  297. self::assertSame($max_date_value, $search->getMaxDate());
  298. self::assertSame($intitle_value, $search->getIntitle());
  299. self::assertSame($inurl_value, $search->getInurl());
  300. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  301. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  302. self::assertSame($tags_value, $search->getTags());
  303. self::assertSame($search_value, $search->getSearch());
  304. self::assertSame($input, $search->getRawInput());
  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. '&quot;ab&quot; &quot;cd&quot; (&quot;ef&quot;) intitle:&quot;gh&quot; !&quot;ij&quot; -&quot;kl&quot;',
  533. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  534. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  535. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  536. ],
  537. [
  538. '/^(ab|cd) [(] \\) (ef|gh)/',
  539. '((e.title ~ ? OR e.content ~ ?) )',
  540. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  541. ],
  542. [
  543. '!/^(ab|cd)/',
  544. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  545. ['^(ab|cd)', '^(ab|cd)']
  546. ],
  547. [
  548. 'intitle:/^(ab|cd)/',
  549. '(e.title ~ ? )',
  550. ['^(ab|cd)']
  551. ],
  552. [
  553. 'intext:/^(ab|cd)/',
  554. '(e.content ~ ? )',
  555. ['^(ab|cd)']
  556. ],
  557. [
  558. 'L:1 L:2',
  559. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) AND ' .
  560. 'e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )',
  561. [1, 2]
  562. ],
  563. [
  564. 'L:1,2',
  565. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?,?)) )',
  566. [1, 2]
  567. ],
  568. ];
  569. }
  570. /**
  571. * @param array<string> $values
  572. */
  573. #[DataProvider('provideDateOperators')]
  574. public function test__date_operators(string $input, string $sql, array $values): void {
  575. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  576. self::assertSame(trim($sql), trim($filterSearch));
  577. self::assertSame($values, $filterValues);
  578. }
  579. /** @return list<list<mixed>> */
  580. public static function provideDateOperators(): array {
  581. return [
  582. // Basic date operator tests
  583. [
  584. 'date:2007-03-01/2008-05-11',
  585. '(e.id >= ? AND e.id <= ? )',
  586. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  587. ],
  588. [
  589. 'date:2007-03-01/',
  590. '(e.id >= ? )',
  591. [strtotime('2007-03-01T00:00:00Z') . '000000'],
  592. ],
  593. [
  594. 'date:/2008-05-11',
  595. '(e.id <= ? )',
  596. [strtotime('2008-05-11T23:59:59Z') . '000000'],
  597. ],
  598. // Basic pubdate operator tests
  599. [
  600. 'pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  601. '(e.date >= ? AND e.date <= ? )',
  602. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  603. ],
  604. [
  605. 'pubdate:2007-03-01/',
  606. '(e.date >= ? )',
  607. [strtotime('2007-03-01T00:00:00Z')],
  608. ],
  609. [
  610. 'pubdate:/2008-05-11',
  611. '(e.date <= ? )',
  612. [strtotime('2008-05-11T23:59:59Z')],
  613. ],
  614. // Basic userdate operator tests
  615. [
  616. 'userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  617. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? )',
  618. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  619. ],
  620. [
  621. 'userdate:2007-03-01/',
  622. '(e.`lastUserModified` >= ? )',
  623. [strtotime('2007-03-01T00:00:00Z')],
  624. ],
  625. [
  626. 'userdate:/2008-05-11',
  627. '(e.`lastUserModified` <= ? )',
  628. [strtotime('2008-05-11T23:59:59Z')],
  629. ],
  630. // Negative date operator tests
  631. [
  632. '-date:2007-03-01/2008-05-11',
  633. '((e.id < ? OR e.id > ?) )',
  634. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  635. ],
  636. [
  637. '!pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  638. '((e.date < ? OR e.date > ?) )',
  639. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  640. ],
  641. [
  642. '!userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  643. '((e.`lastUserModified` < ? OR e.`lastUserModified` > ?) )',
  644. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  645. ],
  646. // Combined date operators
  647. [
  648. 'date:2007-03-01/ pubdate:/2008-05-11',
  649. '(e.id >= ? AND e.date <= ? )',
  650. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z')],
  651. ],
  652. [
  653. 'pubdate:2007-03-01/ userdate:/2008-05-11',
  654. '(e.date >= ? AND e.`lastUserModified` <= ? )',
  655. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  656. ],
  657. [
  658. 'date:2007-03-01/ userdate:2007-06-01/',
  659. '(e.id >= ? AND e.`lastUserModified` >= ? )',
  660. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2007-06-01T00:00:00Z')],
  661. ],
  662. // Complex combinations with other operators
  663. [
  664. 'intitle:test date:2007-03-01/ pubdate:/2008-05-11',
  665. '(e.id >= ? AND e.date <= ? AND e.title LIKE ? )',
  666. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z'), '%test%'],
  667. ],
  668. [
  669. 'author:john userdate:2007-03-01/2008-05-11',
  670. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? AND e.author LIKE ? )',
  671. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z'), '%john%'],
  672. ],
  673. // Mixed positive and negative date operators
  674. [
  675. 'date:2007-03-01/ !pubdate:2008-01-01/2008-05-11',
  676. '(e.id >= ? AND (e.date < ? OR e.date > ?) )',
  677. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-01-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  678. ],
  679. ];
  680. }
  681. /**
  682. * @dataProvider provideRegexPostreSQL
  683. * @param array<string> $values
  684. */
  685. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  686. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  687. self::assertSame(trim($sql), trim($filterSearch));
  688. self::assertSame($values, $filterValues);
  689. }
  690. /** @return list<list<mixed>> */
  691. public static function provideRegexPostreSQL(): array {
  692. return [
  693. [
  694. 'intitle:/^ab$/',
  695. '(e.title ~ ? )',
  696. ['^ab$']
  697. ],
  698. [
  699. 'intitle:/^ab$/i',
  700. '(e.title ~* ? )',
  701. ['^ab$']
  702. ],
  703. [
  704. 'intitle:/^ab$/m',
  705. '(e.title ~ ? )',
  706. ['(?m)^ab$']
  707. ],
  708. [
  709. 'intitle:/^ab\\M/',
  710. '(e.title ~ ? )',
  711. ['^ab\\M']
  712. ],
  713. [
  714. 'intext:/^ab\\M/',
  715. '(e.content ~ ? )',
  716. ['^ab\\M']
  717. ],
  718. [
  719. 'author:/^ab$/',
  720. "(REPLACE(e.author, ';', '\n') ~ ? )",
  721. ['^ab$']
  722. ],
  723. [
  724. 'inurl:/^ab$/',
  725. '(e.link ~ ? )',
  726. ['^ab$']
  727. ],
  728. [
  729. '/^ab$/',
  730. '((e.title ~ ? OR e.content ~ ?) )',
  731. ['^ab$', '^ab$']
  732. ],
  733. [
  734. '!/^ab$/',
  735. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  736. ['^ab$', '^ab$']
  737. ],
  738. [
  739. '#/^a(b|c)$/im',
  740. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ? )",
  741. ['(?m)^a(b|c)$']
  742. ],
  743. [ // Not a regex
  744. 'inurl:https://example.net/test/',
  745. '(e.link LIKE ? )',
  746. ['%https://example.net/test/%']
  747. ],
  748. [ // Not a regex
  749. 'https://example.net/test/',
  750. '((e.title LIKE ? OR e.content LIKE ?) )',
  751. ['%https://example.net/test/%', '%https://example.net/test/%']
  752. ],
  753. ];
  754. }
  755. /**
  756. * @dataProvider provideRegexMariaDB
  757. * @param array<string> $values
  758. */
  759. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  760. FreshRSS_DatabaseDAO::$dummyConnection = true;
  761. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  762. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  763. self::assertSame(trim($sql), trim($filterSearch));
  764. self::assertSame($values, $filterValues);
  765. }
  766. /** @return list<list<mixed>> */
  767. public static function provideRegexMariaDB(): array {
  768. return [
  769. [
  770. 'intitle:/^ab$/',
  771. "(e.title REGEXP ? )",
  772. ['(?-i)^ab$']
  773. ],
  774. [
  775. 'intitle:/^ab$/i',
  776. "(e.title REGEXP ? )",
  777. ['(?i)^ab$']
  778. ],
  779. [
  780. 'intitle:/^ab$/m',
  781. "(e.title REGEXP ? )",
  782. ['(?-i)(?m)^ab$']
  783. ],
  784. [
  785. 'intext:/^ab$/m',
  786. '(UNCOMPRESS(e.content_bin) REGEXP ?) )',
  787. ['(?-i)(?m)^ab$']
  788. ],
  789. ];
  790. }
  791. /**
  792. * @dataProvider provideRegexMySQL
  793. * @param array<string> $values
  794. */
  795. public function test__regex_mysql(string $input, string $sql, array $values): void {
  796. FreshRSS_DatabaseDAO::$dummyConnection = true;
  797. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  798. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  799. self::assertSame(trim($sql), trim($filterSearch));
  800. self::assertSame($values, $filterValues);
  801. }
  802. /** @return list<list<mixed>> */
  803. public static function provideRegexMySQL(): array {
  804. return [
  805. [
  806. 'intitle:/^ab$/',
  807. "(REGEXP_LIKE(e.title,?,'c') )",
  808. ['^ab$']
  809. ],
  810. [
  811. 'intitle:/^ab$/i',
  812. "(REGEXP_LIKE(e.title,?,'i') )",
  813. ['^ab$']
  814. ],
  815. [
  816. 'intitle:/^ab$/m',
  817. "(REGEXP_LIKE(e.title,?,'mc') )",
  818. ['^ab$']
  819. ],
  820. [
  821. 'intext:/^ab$/m',
  822. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc')) )",
  823. ['^ab$']
  824. ],
  825. ];
  826. }
  827. /**
  828. * @dataProvider provideRegexSQLite
  829. * @param array<string> $values
  830. */
  831. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  832. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  833. self::assertSame(trim($sql), trim($filterSearch));
  834. self::assertSame($values, $filterValues);
  835. }
  836. /** @return list<list<mixed>> */
  837. public static function provideRegexSQLite(): array {
  838. return [
  839. [
  840. 'intitle:/^ab$/',
  841. "(e.title REGEXP ? )",
  842. ['/^ab$/']
  843. ],
  844. [
  845. 'intitle:/^ab$/i',
  846. "(e.title REGEXP ? )",
  847. ['/^ab$/i']
  848. ],
  849. [
  850. 'intitle:/^ab$/m',
  851. "(e.title REGEXP ? )",
  852. ['/^ab$/m']
  853. ],
  854. [
  855. 'intitle:/^ab\\b/',
  856. '(e.title REGEXP ? )',
  857. ['/^ab\\b/']
  858. ],
  859. [
  860. 'intext:/^ab\\b/',
  861. '(e.content REGEXP ? )',
  862. ['/^ab\\b/']
  863. ],
  864. ];
  865. }
  866. }