SearchTest.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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', 1172754000, 1210519800],
  156. ['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799],
  157. ['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800],
  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', 1172754000, 1210519800],
  175. ['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799],
  176. ['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800],
  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. /**
  183. * @param array<string>|null $tags_value
  184. * @param array<string>|null $search_value
  185. */
  186. #[DataProvider('provideTagsSearch')]
  187. public static function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  188. $search = new FreshRSS_Search($input);
  189. self::assertSame($tags_value, $search->getTags());
  190. self::assertSame($search_value, $search->getSearch());
  191. }
  192. /**
  193. * @return list<list<string|list<string>|null>>
  194. */
  195. public static function provideTagsSearch(): array {
  196. return [
  197. ['#word1', ['word1'], null],
  198. ['# word1', null, ['#', 'word1']],
  199. ['#123', ['123'], null],
  200. ['#word1 word2', ['word1'], ['word2']],
  201. ['#"word1 word2"', ['word1 word2'], null],
  202. ['#word1 #word2', ['word1', 'word2'], null],
  203. ["#word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  204. ['#word1+word2', ['word1 word2'], null]
  205. ];
  206. }
  207. /**
  208. * @param list<array{search:string}> $queries
  209. * @param array{0:string,1:list<string|int>} $expectedResult
  210. */
  211. #[DataProvider('provideSavedQueryIdExpansion')]
  212. public static function test__construct_whenInputContainsSavedQueryIds_expandsSavedSearches(array $queries, string $input, array $expectedResult): void {
  213. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  214. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  215. $newUserConf->queries = $queries;
  216. FreshRSS_Context::$user_conf = $newUserConf;
  217. try {
  218. $search = new FreshRSS_BooleanSearch($input);
  219. [$actualValues, $actualSql] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $search);
  220. self::assertSame($expectedResult[0], trim($actualSql));
  221. self::assertSame($expectedResult[1], $actualValues);
  222. } finally {
  223. FreshRSS_Context::$user_conf = $previousUserConf;
  224. }
  225. }
  226. /**
  227. * @return array<string,array{0:list<array{search:string}>,1:string,2:array{0:string,1:list<string|int>}}>
  228. */
  229. public static function provideSavedQueryIdExpansion(): array {
  230. return [
  231. 'expanded single group' => [
  232. [
  233. ['search' => 'author:Alice'],
  234. ['search' => 'intitle:World'],
  235. ],
  236. 'S:0,1',
  237. [
  238. '((e.author LIKE ? )) OR ((e.title LIKE ? ))',
  239. ['%Alice%', '%World%'],
  240. ],
  241. ],
  242. 'separate groups with OR' => [
  243. [
  244. ['search' => 'author:Alice'],
  245. ['search' => 'intitle:World'],
  246. ['search' => 'inurl:Example'],
  247. ['search' => 'author:Bob'],
  248. ],
  249. 'S:0,1 OR S:2,3',
  250. [
  251. '((e.author LIKE ? )) OR ((e.title LIKE ? )) OR ((e.link LIKE ? )) OR ((e.author LIKE ? ))',
  252. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  253. ],
  254. ],
  255. 'mixed with other clauses' => [
  256. [
  257. ['search' => 'author:Alice'],
  258. ['search' => 'intitle:World'],
  259. ],
  260. 'intitle:Hello S:0,1 date:2025-10',
  261. [
  262. '((e.title LIKE ? )) AND ((e.author LIKE ? )) OR ((e.title LIKE ? )) AND ((e.id >= ? AND e.id <= ? ))',
  263. ['%Hello%', '%Alice%', '%World%', strtotime('2025-10-01') . '000000', (strtotime('2025-11-01') - 1) . '000000'],
  264. ],
  265. ],
  266. ];
  267. }
  268. /**
  269. * @param array<string>|null $author_value
  270. * @param array<string> $intitle_value
  271. * @param array<string>|null $inurl_value
  272. * @param array<string>|null $tags_value
  273. * @param array<string>|null $search_value
  274. */
  275. #[DataProvider('provideMultipleSearch')]
  276. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  277. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  278. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  279. $search = new FreshRSS_Search($input);
  280. self::assertSame($author_value, $search->getAuthor());
  281. self::assertSame($min_date_value, $search->getMinDate());
  282. self::assertSame($max_date_value, $search->getMaxDate());
  283. self::assertSame($intitle_value, $search->getIntitle());
  284. self::assertSame($inurl_value, $search->getInurl());
  285. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  286. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  287. self::assertSame($tags_value, $search->getTags());
  288. self::assertSame($search_value, $search->getSearch());
  289. self::assertSame($input, $search->getRawInput());
  290. }
  291. /** @return list<list<mixed>> */
  292. public static function provideMultipleSearch(): array {
  293. return [
  294. [
  295. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  296. ['word1'],
  297. strtotime('2007-03-01'),
  298. strtotime('2008-05-12') - 1,
  299. ['word2'],
  300. ['word3'],
  301. strtotime('2007-03-01'),
  302. strtotime('2008-05-12') - 1,
  303. ['word4', 'word5'],
  304. null
  305. ],
  306. [
  307. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  308. ['word1'],
  309. strtotime('2007-03-01'),
  310. strtotime('2008-05-12') - 1,
  311. ['word2'],
  312. ['word3'],
  313. strtotime('2007-03-01'),
  314. strtotime('2008-05-12') - 1,
  315. ['word4', 'word5'],
  316. ['word6']
  317. ],
  318. [
  319. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  320. ['word1'],
  321. strtotime('2007-03-01'),
  322. strtotime('2008-05-12') - 1,
  323. ['word2'],
  324. ['word3'],
  325. strtotime('2007-03-01'),
  326. strtotime('2008-05-12') - 1,
  327. ['word4', 'word5'],
  328. ['word6', 'word7']
  329. ],
  330. [
  331. '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',
  332. ['word1'],
  333. strtotime('2007-03-01'),
  334. strtotime('2008-05-12') - 1,
  335. ['word2'],
  336. ['word3'],
  337. strtotime('2007-03-01'),
  338. strtotime('2008-05-12') - 1,
  339. ['word4', 'word5'],
  340. ['word7 word8', 'word6']
  341. ]
  342. ];
  343. }
  344. #[DataProvider('provideAddOrParentheses')]
  345. public static function test__addOrParentheses(string $input, string $output): void {
  346. self::assertSame($output, FreshRSS_BooleanSearch::addOrParentheses($input));
  347. }
  348. /** @return list<list{string,string}> */
  349. public static function provideAddOrParentheses(): array {
  350. return [
  351. ['ab', 'ab'],
  352. ['ab cd', 'ab cd'],
  353. ['!ab -cd', '!ab -cd'],
  354. ['ab OR cd', '(ab) OR (cd)'],
  355. ['!ab OR -cd', '(!ab) OR (-cd)'],
  356. ['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],
  357. ['ab (!cd)', 'ab (!cd)'],
  358. ['"ab" (!"cd")', '"ab" (!"cd")'],
  359. ];
  360. }
  361. #[DataProvider('provideconsistentOrParentheses')]
  362. public static function test__consistentOrParentheses(string $input, string $output): void {
  363. self::assertSame($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  364. }
  365. /** @return list<list{string,string}> */
  366. public static function provideconsistentOrParentheses(): array {
  367. return [
  368. ['ab cd ef', 'ab cd ef'],
  369. ['(ab cd ef)', '(ab cd ef)'],
  370. ['("ab cd" ef)', '("ab cd" ef)'],
  371. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  372. ['ab (!cd)', 'ab (!cd)'],
  373. ['ab !(cd)', 'ab !(cd)'],
  374. ['(ab) -(cd)', '(ab) -(cd)'],
  375. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  376. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  377. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  378. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  379. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  380. ['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],
  381. ['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],
  382. ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],
  383. ];
  384. }
  385. /**
  386. * @param array<string> $values
  387. */
  388. #[DataProvider('provideParentheses')]
  389. public function test__parentheses(string $input, string $sql, array $values): void {
  390. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  391. self::assertSame(trim($sql), trim($filterSearch));
  392. self::assertSame($values, $filterValues);
  393. }
  394. /** @return list<list<mixed>> */
  395. public static function provideParentheses(): array {
  396. return [
  397. [
  398. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  399. ' ((e.id_feed IN (?) )) AND ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ) OR (e.id_feed IN (?) )) AND' .
  400. ' (((e.id_feed IN (?) )) OR ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ))) ',
  401. [1, 2, 3, 4, 5, 6, 7]
  402. ],
  403. [
  404. 'c:1 OR c:2,3',
  405. ' (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 (?,?)) ) ',
  406. [1, 2, 3]
  407. ],
  408. [
  409. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  410. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  411. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  412. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 12]
  413. ],
  414. [
  415. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  416. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  417. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  418. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  419. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  420. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 'Bleu']
  421. ],
  422. [
  423. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  424. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  425. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  426. ],
  427. [
  428. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  429. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  430. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  431. ],
  432. [
  433. 'intitle:"(test)"',
  434. '(e.title LIKE ? )',
  435. ['%(test)%'],
  436. ],
  437. [
  438. 'intitle:\'"hello world"\'',
  439. '(e.title LIKE ? )',
  440. ['%"hello world"%'],
  441. ],
  442. [
  443. 'intext:\'"hello world"\'',
  444. '(e.content LIKE ? )',
  445. ['%"hello world"%'],
  446. ],
  447. [
  448. '(ab) OR (cd) OR (ef)',
  449. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  450. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  451. ],
  452. [
  453. '("plain or text") OR (cd)',
  454. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  455. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  456. ],
  457. [
  458. '"plain or text" OR cd',
  459. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  460. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  461. ],
  462. [
  463. '"plain OR text" OR cd',
  464. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) ',
  465. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  466. ],
  467. [
  468. 'ab OR cd OR (ef)',
  469. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ',
  470. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  471. ],
  472. [
  473. 'ab OR cd OR ef',
  474. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  475. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  476. ],
  477. [
  478. '(ab) cd OR ef OR (gh)',
  479. '(((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  480. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  481. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  482. ],
  483. [
  484. '(ab) OR cd OR ef OR (gh)',
  485. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  486. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  487. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  488. ],
  489. [
  490. 'ab OR (!(cd OR ef))',
  491. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )))',
  492. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  493. ],
  494. [
  495. 'ab !(cd OR ef)',
  496. '(((e.title LIKE ? OR e.content LIKE ?) )) AND NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  497. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  498. ],
  499. [
  500. 'ab OR !(cd OR ef)',
  501. '(((e.title LIKE ? OR e.content LIKE ?) )) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  502. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  503. ],
  504. [
  505. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  506. '((((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 ?) )) ' .
  507. '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 ?) ))',
  508. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  509. ],
  510. [
  511. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  512. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  513. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  514. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  515. ],
  516. [
  517. '&quot;ab&quot; &quot;cd&quot; (&quot;ef&quot;) intitle:&quot;gh&quot; !&quot;ij&quot; -&quot;kl&quot;',
  518. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  519. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  520. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  521. ],
  522. [
  523. '/^(ab|cd) [(] \\) (ef|gh)/',
  524. '((e.title ~ ? OR e.content ~ ?) )',
  525. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  526. ],
  527. [
  528. '!/^(ab|cd)/',
  529. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  530. ['^(ab|cd)', '^(ab|cd)']
  531. ],
  532. [
  533. 'intitle:/^(ab|cd)/',
  534. '(e.title ~ ? )',
  535. ['^(ab|cd)']
  536. ],
  537. [
  538. 'intext:/^(ab|cd)/',
  539. '(e.content ~ ? )',
  540. ['^(ab|cd)']
  541. ],
  542. [
  543. 'L:1 L:2',
  544. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) AND ' .
  545. 'e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )',
  546. [1, 2]
  547. ],
  548. [
  549. 'L:1,2',
  550. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?,?)) )',
  551. [1, 2]
  552. ],
  553. ];
  554. }
  555. /**
  556. * @dataProvider provideRegexPostreSQL
  557. * @param array<string> $values
  558. */
  559. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  560. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  561. self::assertSame(trim($sql), trim($filterSearch));
  562. self::assertSame($values, $filterValues);
  563. }
  564. /** @return list<list<mixed>> */
  565. public static function provideRegexPostreSQL(): array {
  566. return [
  567. [
  568. 'intitle:/^ab$/',
  569. '(e.title ~ ? )',
  570. ['^ab$']
  571. ],
  572. [
  573. 'intitle:/^ab$/i',
  574. '(e.title ~* ? )',
  575. ['^ab$']
  576. ],
  577. [
  578. 'intitle:/^ab$/m',
  579. '(e.title ~ ? )',
  580. ['(?m)^ab$']
  581. ],
  582. [
  583. 'intitle:/^ab\\M/',
  584. '(e.title ~ ? )',
  585. ['^ab\\M']
  586. ],
  587. [
  588. 'intext:/^ab\\M/',
  589. '(e.content ~ ? )',
  590. ['^ab\\M']
  591. ],
  592. [
  593. 'author:/^ab$/',
  594. "(REPLACE(e.author, ';', '\n') ~ ? )",
  595. ['^ab$']
  596. ],
  597. [
  598. 'inurl:/^ab$/',
  599. '(e.link ~ ? )',
  600. ['^ab$']
  601. ],
  602. [
  603. '/^ab$/',
  604. '((e.title ~ ? OR e.content ~ ?) )',
  605. ['^ab$', '^ab$']
  606. ],
  607. [
  608. '!/^ab$/',
  609. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  610. ['^ab$', '^ab$']
  611. ],
  612. [
  613. '#/^a(b|c)$/im',
  614. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ? )",
  615. ['(?m)^a(b|c)$']
  616. ],
  617. [ // Not a regex
  618. 'inurl:https://example.net/test/',
  619. '(e.link LIKE ? )',
  620. ['%https://example.net/test/%']
  621. ],
  622. [ // Not a regex
  623. 'https://example.net/test/',
  624. '((e.title LIKE ? OR e.content LIKE ?) )',
  625. ['%https://example.net/test/%', '%https://example.net/test/%']
  626. ],
  627. ];
  628. }
  629. /**
  630. * @dataProvider provideRegexMariaDB
  631. * @param array<string> $values
  632. */
  633. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  634. FreshRSS_DatabaseDAO::$dummyConnection = true;
  635. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  636. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  637. self::assertSame(trim($sql), trim($filterSearch));
  638. self::assertSame($values, $filterValues);
  639. }
  640. /** @return list<list<mixed>> */
  641. public static function provideRegexMariaDB(): array {
  642. return [
  643. [
  644. 'intitle:/^ab$/',
  645. "(e.title REGEXP ? )",
  646. ['(?-i)^ab$']
  647. ],
  648. [
  649. 'intitle:/^ab$/i',
  650. "(e.title REGEXP ? )",
  651. ['(?i)^ab$']
  652. ],
  653. [
  654. 'intitle:/^ab$/m',
  655. "(e.title REGEXP ? )",
  656. ['(?-i)(?m)^ab$']
  657. ],
  658. [
  659. 'intext:/^ab$/m',
  660. '(UNCOMPRESS(e.content_bin) REGEXP ?) )',
  661. ['(?-i)(?m)^ab$']
  662. ],
  663. ];
  664. }
  665. /**
  666. * @dataProvider provideRegexMySQL
  667. * @param array<string> $values
  668. */
  669. public function test__regex_mysql(string $input, string $sql, array $values): void {
  670. FreshRSS_DatabaseDAO::$dummyConnection = true;
  671. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  672. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  673. self::assertSame(trim($sql), trim($filterSearch));
  674. self::assertSame($values, $filterValues);
  675. }
  676. /** @return list<list<mixed>> */
  677. public static function provideRegexMySQL(): array {
  678. return [
  679. [
  680. 'intitle:/^ab$/',
  681. "(REGEXP_LIKE(e.title,?,'c') )",
  682. ['^ab$']
  683. ],
  684. [
  685. 'intitle:/^ab$/i',
  686. "(REGEXP_LIKE(e.title,?,'i') )",
  687. ['^ab$']
  688. ],
  689. [
  690. 'intitle:/^ab$/m',
  691. "(REGEXP_LIKE(e.title,?,'mc') )",
  692. ['^ab$']
  693. ],
  694. [
  695. 'intext:/^ab$/m',
  696. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc')) )",
  697. ['^ab$']
  698. ],
  699. ];
  700. }
  701. /**
  702. * @dataProvider provideRegexSQLite
  703. * @param array<string> $values
  704. */
  705. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  706. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  707. self::assertSame(trim($sql), trim($filterSearch));
  708. self::assertSame($values, $filterValues);
  709. }
  710. /** @return list<list<mixed>> */
  711. public static function provideRegexSQLite(): array {
  712. return [
  713. [
  714. 'intitle:/^ab$/',
  715. "(e.title REGEXP ? )",
  716. ['/^ab$/']
  717. ],
  718. [
  719. 'intitle:/^ab$/i',
  720. "(e.title REGEXP ? )",
  721. ['/^ab$/i']
  722. ],
  723. [
  724. 'intitle:/^ab$/m',
  725. "(e.title REGEXP ? )",
  726. ['/^ab$/m']
  727. ],
  728. [
  729. 'intitle:/^ab\\b/',
  730. '(e.title REGEXP ? )',
  731. ['/^ab\\b/']
  732. ],
  733. [
  734. 'intext:/^ab\\b/',
  735. '(e.content REGEXP ? )',
  736. ['/^ab\\b/']
  737. ],
  738. ];
  739. }
  740. }