SearchTest.php 24 KB

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