4
0

SearchTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  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::assertEquals('', $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::assertEquals($intitle_value, $search->getIntitle());
  38. self::assertEquals($search_value, $search->getSearch());
  39. }
  40. /**
  41. * @return array<array<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 $author_value
  67. * @param array<string>|null $search_value
  68. */
  69. #[DataProvider('provideAuthorSearch')]
  70. public static function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void {
  71. $search = new FreshRSS_Search($input);
  72. self::assertEquals($author_value, $search->getAuthor());
  73. self::assertEquals($search_value, $search->getSearch());
  74. }
  75. /**
  76. * @return array<array<mixed>>
  77. */
  78. public static function provideAuthorSearch(): array {
  79. return [
  80. ['author:word1', ['word1'], null],
  81. ['author:word1-word2', ['word1-word2'], null],
  82. ['author:word1 word2', ['word1'], ['word2']],
  83. ['author:"word1 word2"', ['word1 word2'], null],
  84. ["author:'word1 word2'", ['word1 word2'], null],
  85. ['word1 author:word2', ['word2'], ['word1']],
  86. ['word1 author:word2 word3', ['word2'], ['word1', 'word3']],
  87. ['word1 author:"word2 word3"', ['word2 word3'], ['word1']],
  88. ["word1 author:'word2 word3'", ['word2 word3'], ['word1']],
  89. ['author:word1 author:word2', ['word1', 'word2'], null],
  90. ['author: word1 word2', null, ['word1', 'word2']],
  91. ['author:123', ['123'], null],
  92. ['author:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  93. ["author:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  94. ['author:"word1 word2\' word3"', ["word1 word2' word3"], null],
  95. ["author:'word1 word2\" word3'", ['word1 word2" word3'], null],
  96. ["author:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  97. ['author:word1+word2', ['word1+word2'], null],
  98. ];
  99. }
  100. /**
  101. * @param array<string>|null $inurl_value
  102. * @param array<string>|null $search_value
  103. */
  104. #[DataProvider('provideInurlSearch')]
  105. public static function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void {
  106. $search = new FreshRSS_Search($input);
  107. self::assertEquals($inurl_value, $search->getInurl());
  108. self::assertEquals($search_value, $search->getSearch());
  109. }
  110. /**
  111. * @return array<array<mixed>>
  112. */
  113. public static function provideInurlSearch(): array {
  114. return [
  115. ['inurl:word1', ['word1'], null],
  116. ['inurl: word1', null, ['word1']],
  117. ['inurl:123', ['123'], null],
  118. ['inurl:word1 word2', ['word1'], ['word2']],
  119. ['inurl:"word1 word2"', ['word1 word2'], null],
  120. ['inurl:word1 word2 inurl:word3', ['word1', 'word3'], ['word2']],
  121. ["inurl:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  122. ['inurl:word1+word2', ['word1+word2'], null],
  123. ];
  124. }
  125. #[DataProvider('provideDateSearch')]
  126. public static function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void {
  127. $search = new FreshRSS_Search($input);
  128. self::assertEquals($min_date_value, $search->getMinDate());
  129. self::assertEquals($max_date_value, $search->getMaxDate());
  130. }
  131. /**
  132. * @return array<array<mixed>>
  133. */
  134. public static function provideDateSearch(): array {
  135. return [
  136. ['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800],
  137. ['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799],
  138. ['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800],
  139. ['date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  140. ['date:2007-03-01/', strtotime('2007-03-01'), null],
  141. ['date:/2008-05-11', null, strtotime('2008-05-12') - 1],
  142. ];
  143. }
  144. #[DataProvider('providePubdateSearch')]
  145. public static function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void {
  146. $search = new FreshRSS_Search($input);
  147. self::assertEquals($min_pubdate_value, $search->getMinPubdate());
  148. self::assertEquals($max_pubdate_value, $search->getMaxPubdate());
  149. }
  150. /**
  151. * @return array<array<mixed>>
  152. */
  153. public static function providePubdateSearch(): array {
  154. return [
  155. ['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800],
  156. ['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799],
  157. ['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800],
  158. ['pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  159. ['pubdate:2007-03-01/', strtotime('2007-03-01'), null],
  160. ['pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  161. ];
  162. }
  163. /**
  164. * @param array<string>|null $tags_value
  165. * @param array<string>|null $search_value
  166. */
  167. #[DataProvider('provideTagsSearch')]
  168. public static function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  169. $search = new FreshRSS_Search($input);
  170. self::assertEquals($tags_value, $search->getTags());
  171. self::assertEquals($search_value, $search->getSearch());
  172. }
  173. /**
  174. * @return array<array<string|array<string>|null>>
  175. */
  176. public static function provideTagsSearch(): array {
  177. return [
  178. ['#word1', ['word1'], null],
  179. ['# word1', null, ['#', 'word1']],
  180. ['#123', ['123'], null],
  181. ['#word1 word2', ['word1'], ['word2']],
  182. ['#"word1 word2"', ['word1 word2'], null],
  183. ['#word1 #word2', ['word1', 'word2'], null],
  184. ["#word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  185. ['#word1+word2', ['word1 word2'], null]
  186. ];
  187. }
  188. /**
  189. * @param array<string>|null $author_value
  190. * @param array<string> $intitle_value
  191. * @param array<string>|null $inurl_value
  192. * @param array<string>|null $tags_value
  193. * @param array<string>|null $search_value
  194. */
  195. #[DataProvider('provideMultipleSearch')]
  196. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  197. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  198. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  199. $search = new FreshRSS_Search($input);
  200. self::assertEquals($author_value, $search->getAuthor());
  201. self::assertEquals($min_date_value, $search->getMinDate());
  202. self::assertEquals($max_date_value, $search->getMaxDate());
  203. self::assertEquals($intitle_value, $search->getIntitle());
  204. self::assertEquals($inurl_value, $search->getInurl());
  205. self::assertEquals($min_pubdate_value, $search->getMinPubdate());
  206. self::assertEquals($max_pubdate_value, $search->getMaxPubdate());
  207. self::assertEquals($tags_value, $search->getTags());
  208. self::assertEquals($search_value, $search->getSearch());
  209. self::assertEquals($input, $search->getRawInput());
  210. }
  211. /** @return array<array<mixed>> */
  212. public static function provideMultipleSearch(): array {
  213. return [
  214. [
  215. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  216. ['word1'],
  217. strtotime('2007-03-01'),
  218. strtotime('2008-05-12') - 1,
  219. ['word2'],
  220. ['word3'],
  221. strtotime('2007-03-01'),
  222. strtotime('2008-05-12') - 1,
  223. ['word4', 'word5'],
  224. null
  225. ],
  226. [
  227. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  228. ['word1'],
  229. strtotime('2007-03-01'),
  230. strtotime('2008-05-12') - 1,
  231. ['word2'],
  232. ['word3'],
  233. strtotime('2007-03-01'),
  234. strtotime('2008-05-12') - 1,
  235. ['word4', 'word5'],
  236. ['word6']
  237. ],
  238. [
  239. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  240. ['word1'],
  241. strtotime('2007-03-01'),
  242. strtotime('2008-05-12') - 1,
  243. ['word2'],
  244. ['word3'],
  245. strtotime('2007-03-01'),
  246. strtotime('2008-05-12') - 1,
  247. ['word4', 'word5'],
  248. ['word6', 'word7']
  249. ],
  250. [
  251. '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',
  252. ['word1'],
  253. strtotime('2007-03-01'),
  254. strtotime('2008-05-12') - 1,
  255. ['word2'],
  256. ['word3'],
  257. strtotime('2007-03-01'),
  258. strtotime('2008-05-12') - 1,
  259. ['word4', 'word5'],
  260. ['word7 word8', 'word6']
  261. ]
  262. ];
  263. }
  264. #[DataProvider('provideAddOrParentheses')]
  265. public static function test__addOrParentheses(string $input, string $output): void {
  266. self::assertEquals($output, FreshRSS_BooleanSearch::addOrParentheses($input));
  267. }
  268. /** @return array<array{string,string}> */
  269. public static function provideAddOrParentheses(): array {
  270. return [
  271. ['ab', 'ab'],
  272. ['ab cd', 'ab cd'],
  273. ['!ab -cd', '!ab -cd'],
  274. ['ab OR cd', '(ab) OR (cd)'],
  275. ['!ab OR -cd', '(!ab) OR (-cd)'],
  276. ['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],
  277. ['ab (!cd)', 'ab (!cd)'],
  278. ['"ab" (!"cd")', '"ab" (!"cd")'],
  279. ];
  280. }
  281. #[DataProvider('provideconsistentOrParentheses')]
  282. public static function test__consistentOrParentheses(string $input, string $output): void {
  283. self::assertEquals($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  284. }
  285. /** @return array<array{string,string}> */
  286. public static function provideconsistentOrParentheses(): array {
  287. return [
  288. ['ab cd ef', 'ab cd ef'],
  289. ['(ab cd ef)', '(ab cd ef)'],
  290. ['("ab cd" ef)', '("ab cd" ef)'],
  291. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  292. ['ab (!cd)', 'ab (!cd)'],
  293. ['ab !(cd)', 'ab !(cd)'],
  294. ['(ab) -(cd)', '(ab) -(cd)'],
  295. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  296. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  297. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  298. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  299. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  300. ['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],
  301. ['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],
  302. ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],
  303. ];
  304. }
  305. /**
  306. * @param array<string> $values
  307. */
  308. #[DataProvider('provideParentheses')]
  309. public function test__parentheses(string $input, string $sql, array $values): void {
  310. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  311. self::assertEquals(trim($sql), trim($filterSearch));
  312. self::assertEquals($values, $filterValues);
  313. }
  314. /** @return array<array<mixed>> */
  315. public static function provideParentheses(): array {
  316. return [
  317. [
  318. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  319. ' ((e.id_feed IN (?) )) AND ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ) OR (e.id_feed IN (?) )) AND' .
  320. ' (((e.id_feed IN (?) )) OR ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ))) ',
  321. ['1', '2', '3', '4', '5', '6', '7']
  322. ],
  323. [
  324. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  325. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  326. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  327. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', '12']
  328. ],
  329. [
  330. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  331. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  332. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  333. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  334. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  335. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', 'Bleu']
  336. ],
  337. [
  338. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  339. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  340. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  341. ],
  342. [
  343. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  344. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  345. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  346. ],
  347. [
  348. 'intitle:"(test)"',
  349. '(e.title LIKE ? )',
  350. ['%(test)%'],
  351. ],
  352. [
  353. 'intitle:\'"hello world"\'',
  354. '(e.title LIKE ? )',
  355. ['%"hello world"%'],
  356. ],
  357. [
  358. '(ab) OR (cd) OR (ef)',
  359. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  360. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  361. ],
  362. [
  363. '("plain or text") OR (cd)',
  364. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  365. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  366. ],
  367. [
  368. '"plain or text" OR cd',
  369. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  370. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  371. ],
  372. [
  373. '"plain OR text" OR cd',
  374. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) ',
  375. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  376. ],
  377. [
  378. 'ab OR cd OR (ef)',
  379. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ',
  380. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  381. ],
  382. [
  383. 'ab OR cd OR ef',
  384. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  385. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  386. ],
  387. [
  388. '(ab) cd OR ef OR (gh)',
  389. '(((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  390. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  391. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  392. ],
  393. [
  394. '(ab) OR cd OR ef OR (gh)',
  395. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  396. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  397. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  398. ],
  399. [
  400. 'ab OR (!(cd OR ef))',
  401. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )))',
  402. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  403. ],
  404. [
  405. 'ab !(cd OR ef)',
  406. '(((e.title LIKE ? OR e.content LIKE ?) )) AND NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  407. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  408. ],
  409. [
  410. 'ab OR !(cd OR ef)',
  411. '(((e.title LIKE ? OR e.content LIKE ?) )) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  412. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  413. ],
  414. [
  415. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  416. '((((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 ?) )) ' .
  417. '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 ?) ))',
  418. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  419. ],
  420. [
  421. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  422. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  423. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  424. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  425. ],
  426. [
  427. '&quot;ab&quot; &quot;cd&quot; (&quot;ef&quot;) intitle:&quot;gh&quot; !&quot;ij&quot; -&quot;kl&quot;',
  428. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  429. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  430. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  431. ],
  432. [
  433. '/^(ab|cd) [(] \\) (ef|gh)/',
  434. '((e.title ~ ? OR e.content ~ ?) )',
  435. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  436. ],
  437. [
  438. '!/^(ab|cd)/',
  439. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  440. ['^(ab|cd)', '^(ab|cd)']
  441. ],
  442. [
  443. 'intitle:/^(ab|cd)/',
  444. '(e.title ~ ? )',
  445. ['^(ab|cd)']
  446. ],
  447. ];
  448. }
  449. /**
  450. * @dataProvider provideRegexPostreSQL
  451. * @param array<string> $values
  452. */
  453. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  454. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  455. self::assertEquals(trim($sql), trim($filterSearch));
  456. self::assertEquals($values, $filterValues);
  457. }
  458. /** @return array<array<mixed>> */
  459. public static function provideRegexPostreSQL(): array {
  460. return [
  461. [
  462. 'intitle:/^ab$/',
  463. '(e.title ~ ? )',
  464. ['^ab$']
  465. ],
  466. [
  467. 'intitle:/^ab$/i',
  468. '(e.title ~* ? )',
  469. ['^ab$']
  470. ],
  471. [
  472. 'intitle:/^ab$/m',
  473. '(e.title ~ ? )',
  474. ['(?m)^ab$']
  475. ],
  476. [
  477. 'intitle:/^ab\\M/',
  478. '(e.title ~ ? )',
  479. ['^ab\\M']
  480. ],
  481. [
  482. 'author:/^ab$/',
  483. "(REPLACE(e.author, ';', '\n') ~ ? )",
  484. ['^ab$']
  485. ],
  486. [
  487. 'inurl:/^ab$/',
  488. '(e.link ~ ? )',
  489. ['^ab$']
  490. ],
  491. [
  492. '/^ab$/',
  493. '((e.title ~ ? OR e.content ~ ?) )',
  494. ['^ab$', '^ab$']
  495. ],
  496. [
  497. '!/^ab$/',
  498. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  499. ['^ab$', '^ab$']
  500. ],
  501. [
  502. '#/^a(b|c)$/im',
  503. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ? )",
  504. ['(?m)^a(b|c)$']
  505. ],
  506. [ // Not a regex
  507. 'inurl:https://example.net/test/',
  508. '(e.link LIKE ? )',
  509. ['%https://example.net/test/%']
  510. ],
  511. [ // Not a regex
  512. 'https://example.net/test/',
  513. '((e.title LIKE ? OR e.content LIKE ?) )',
  514. ['%https://example.net/test/%', '%https://example.net/test/%']
  515. ],
  516. ];
  517. }
  518. /**
  519. * @dataProvider provideRegexMariaDB
  520. * @param array<string> $values
  521. */
  522. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  523. FreshRSS_DatabaseDAO::$dummyConnection = true;
  524. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  525. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  526. self::assertEquals(trim($sql), trim($filterSearch));
  527. self::assertEquals($values, $filterValues);
  528. }
  529. /** @return array<array<mixed>> */
  530. public static function provideRegexMariaDB(): array {
  531. return [
  532. [
  533. 'intitle:/^ab$/',
  534. "(e.title REGEXP ? )",
  535. ['(?-i)^ab$']
  536. ],
  537. [
  538. 'intitle:/^ab$/i',
  539. "(e.title REGEXP ? )",
  540. ['(?i)^ab$']
  541. ],
  542. [
  543. 'intitle:/^ab$/m',
  544. "(e.title REGEXP ? )",
  545. ['(?-i)(?m)^ab$']
  546. ],
  547. ];
  548. }
  549. /**
  550. * @dataProvider provideRegexMySQL
  551. * @param array<string> $values
  552. */
  553. public function test__regex_mysql(string $input, string $sql, array $values): void {
  554. FreshRSS_DatabaseDAO::$dummyConnection = true;
  555. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  556. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  557. self::assertEquals(trim($sql), trim($filterSearch));
  558. self::assertEquals($values, $filterValues);
  559. }
  560. /** @return array<array<mixed>> */
  561. public static function provideRegexMySQL(): array {
  562. return [
  563. [
  564. 'intitle:/^ab$/',
  565. "(REGEXP_LIKE(e.title,?,'c') )",
  566. ['^ab$']
  567. ],
  568. [
  569. 'intitle:/^ab$/i',
  570. "(REGEXP_LIKE(e.title,?,'i') )",
  571. ['^ab$']
  572. ],
  573. [
  574. 'intitle:/^ab$/m',
  575. "(REGEXP_LIKE(e.title,?,'mc') )",
  576. ['^ab$']
  577. ],
  578. ];
  579. }
  580. /**
  581. * @dataProvider provideRegexSQLite
  582. * @param array<string> $values
  583. */
  584. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  585. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  586. self::assertEquals(trim($sql), trim($filterSearch));
  587. self::assertEquals($values, $filterValues);
  588. }
  589. /** @return array<array<mixed>> */
  590. public static function provideRegexSQLite(): array {
  591. return [
  592. [
  593. 'intitle:/^ab$/',
  594. "(e.title REGEXP ? )",
  595. ['/^ab$/']
  596. ],
  597. [
  598. 'intitle:/^ab$/i',
  599. "(e.title REGEXP ? )",
  600. ['/^ab$/i']
  601. ],
  602. [
  603. 'intitle:/^ab$/m',
  604. "(e.title REGEXP ? )",
  605. ['/^ab$/m']
  606. ],
  607. [
  608. 'intitle:/^ab\\b/',
  609. '(e.title REGEXP ? )',
  610. ['/^ab\\b/']
  611. ],
  612. ];
  613. }
  614. }