4
0

SearchTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. <?php
  2. declare(strict_types=1);
  3. use PHPUnit\Framework\Attributes\DataProvider;
  4. require_once(LIB_PATH . '/lib_date.php');
  5. 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. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  344. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  345. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  346. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 12]
  347. ],
  348. [
  349. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  350. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  351. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  352. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  353. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  354. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 'Bleu']
  355. ],
  356. [
  357. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  358. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  359. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  360. ],
  361. [
  362. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  363. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  364. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  365. ],
  366. [
  367. 'intitle:"(test)"',
  368. '(e.title LIKE ? )',
  369. ['%(test)%'],
  370. ],
  371. [
  372. 'intitle:\'"hello world"\'',
  373. '(e.title LIKE ? )',
  374. ['%"hello world"%'],
  375. ],
  376. [
  377. 'intext:\'"hello world"\'',
  378. '(e.content LIKE ? )',
  379. ['%"hello world"%'],
  380. ],
  381. [
  382. '(ab) OR (cd) OR (ef)',
  383. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  384. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  385. ],
  386. [
  387. '("plain or text") OR (cd)',
  388. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  389. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  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. 'ab OR cd OR (ef)',
  403. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ',
  404. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  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) cd OR ef OR (gh)',
  413. '(((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  414. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  415. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  416. ],
  417. [
  418. '(ab) OR cd OR ef OR (gh)',
  419. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  420. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  421. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  422. ],
  423. [
  424. 'ab OR (!(cd OR ef))',
  425. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )))',
  426. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  427. ],
  428. [
  429. 'ab !(cd OR ef)',
  430. '(((e.title LIKE ? OR e.content LIKE ?) )) AND 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 OR !(cd OR ef)',
  435. '(((e.title LIKE ? OR e.content LIKE ?) )) OR 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 (!cd OR ef OR (gh))) OR !(ij OR kl)',
  440. '((((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 ?) )) ' .
  441. '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 ?) ))',
  442. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  443. ],
  444. [
  445. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  446. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  447. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  448. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  449. ],
  450. [
  451. '&quot;ab&quot; &quot;cd&quot; (&quot;ef&quot;) intitle:&quot;gh&quot; !&quot;ij&quot; -&quot;kl&quot;',
  452. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  453. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  454. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  455. ],
  456. [
  457. '/^(ab|cd) [(] \\) (ef|gh)/',
  458. '((e.title ~ ? OR e.content ~ ?) )',
  459. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  460. ],
  461. [
  462. '!/^(ab|cd)/',
  463. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  464. ['^(ab|cd)', '^(ab|cd)']
  465. ],
  466. [
  467. 'intitle:/^(ab|cd)/',
  468. '(e.title ~ ? )',
  469. ['^(ab|cd)']
  470. ],
  471. [
  472. 'intext:/^(ab|cd)/',
  473. '(e.content ~ ? )',
  474. ['^(ab|cd)']
  475. ],
  476. ];
  477. }
  478. /**
  479. * @dataProvider provideRegexPostreSQL
  480. * @param array<string> $values
  481. */
  482. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  483. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  484. self::assertSame(trim($sql), trim($filterSearch));
  485. self::assertSame($values, $filterValues);
  486. }
  487. /** @return list<list<mixed>> */
  488. public static function provideRegexPostreSQL(): array {
  489. return [
  490. [
  491. 'intitle:/^ab$/',
  492. '(e.title ~ ? )',
  493. ['^ab$']
  494. ],
  495. [
  496. 'intitle:/^ab$/i',
  497. '(e.title ~* ? )',
  498. ['^ab$']
  499. ],
  500. [
  501. 'intitle:/^ab$/m',
  502. '(e.title ~ ? )',
  503. ['(?m)^ab$']
  504. ],
  505. [
  506. 'intitle:/^ab\\M/',
  507. '(e.title ~ ? )',
  508. ['^ab\\M']
  509. ],
  510. [
  511. 'intext:/^ab\\M/',
  512. '(e.content ~ ? )',
  513. ['^ab\\M']
  514. ],
  515. [
  516. 'author:/^ab$/',
  517. "(REPLACE(e.author, ';', '\n') ~ ? )",
  518. ['^ab$']
  519. ],
  520. [
  521. 'inurl:/^ab$/',
  522. '(e.link ~ ? )',
  523. ['^ab$']
  524. ],
  525. [
  526. '/^ab$/',
  527. '((e.title ~ ? OR e.content ~ ?) )',
  528. ['^ab$', '^ab$']
  529. ],
  530. [
  531. '!/^ab$/',
  532. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  533. ['^ab$', '^ab$']
  534. ],
  535. [
  536. '#/^a(b|c)$/im',
  537. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ? )",
  538. ['(?m)^a(b|c)$']
  539. ],
  540. [ // Not a regex
  541. 'inurl:https://example.net/test/',
  542. '(e.link LIKE ? )',
  543. ['%https://example.net/test/%']
  544. ],
  545. [ // Not a regex
  546. 'https://example.net/test/',
  547. '((e.title LIKE ? OR e.content LIKE ?) )',
  548. ['%https://example.net/test/%', '%https://example.net/test/%']
  549. ],
  550. ];
  551. }
  552. /**
  553. * @dataProvider provideRegexMariaDB
  554. * @param array<string> $values
  555. */
  556. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  557. FreshRSS_DatabaseDAO::$dummyConnection = true;
  558. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  559. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  560. self::assertSame(trim($sql), trim($filterSearch));
  561. self::assertSame($values, $filterValues);
  562. }
  563. /** @return list<list<mixed>> */
  564. public static function provideRegexMariaDB(): array {
  565. return [
  566. [
  567. 'intitle:/^ab$/',
  568. "(e.title REGEXP ? )",
  569. ['(?-i)^ab$']
  570. ],
  571. [
  572. 'intitle:/^ab$/i',
  573. "(e.title REGEXP ? )",
  574. ['(?i)^ab$']
  575. ],
  576. [
  577. 'intitle:/^ab$/m',
  578. "(e.title REGEXP ? )",
  579. ['(?-i)(?m)^ab$']
  580. ],
  581. [
  582. 'intext:/^ab$/m',
  583. '(UNCOMPRESS(e.content_bin) REGEXP ?) )',
  584. ['(?-i)(?m)^ab$']
  585. ],
  586. ];
  587. }
  588. /**
  589. * @dataProvider provideRegexMySQL
  590. * @param array<string> $values
  591. */
  592. public function test__regex_mysql(string $input, string $sql, array $values): void {
  593. FreshRSS_DatabaseDAO::$dummyConnection = true;
  594. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  595. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  596. self::assertSame(trim($sql), trim($filterSearch));
  597. self::assertSame($values, $filterValues);
  598. }
  599. /** @return list<list<mixed>> */
  600. public static function provideRegexMySQL(): array {
  601. return [
  602. [
  603. 'intitle:/^ab$/',
  604. "(REGEXP_LIKE(e.title,?,'c') )",
  605. ['^ab$']
  606. ],
  607. [
  608. 'intitle:/^ab$/i',
  609. "(REGEXP_LIKE(e.title,?,'i') )",
  610. ['^ab$']
  611. ],
  612. [
  613. 'intitle:/^ab$/m',
  614. "(REGEXP_LIKE(e.title,?,'mc') )",
  615. ['^ab$']
  616. ],
  617. [
  618. 'intext:/^ab$/m',
  619. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc')) )",
  620. ['^ab$']
  621. ],
  622. ];
  623. }
  624. /**
  625. * @dataProvider provideRegexSQLite
  626. * @param array<string> $values
  627. */
  628. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  629. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  630. self::assertSame(trim($sql), trim($filterSearch));
  631. self::assertSame($values, $filterValues);
  632. }
  633. /** @return list<list<mixed>> */
  634. public static function provideRegexSQLite(): array {
  635. return [
  636. [
  637. 'intitle:/^ab$/',
  638. "(e.title REGEXP ? )",
  639. ['/^ab$/']
  640. ],
  641. [
  642. 'intitle:/^ab$/i',
  643. "(e.title REGEXP ? )",
  644. ['/^ab$/i']
  645. ],
  646. [
  647. 'intitle:/^ab$/m',
  648. "(e.title REGEXP ? )",
  649. ['/^ab$/m']
  650. ],
  651. [
  652. 'intitle:/^ab\\b/',
  653. '(e.title REGEXP ? )',
  654. ['/^ab\\b/']
  655. ],
  656. [
  657. 'intext:/^ab\\b/',
  658. '(e.content REGEXP ? )',
  659. ['/^ab\\b/']
  660. ],
  661. ];
  662. }
  663. }