SearchTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 array(
  136. array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800),
  137. array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799),
  138. array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800),
  139. array('date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1),
  140. array('date:2007-03-01/', strtotime('2007-03-01'), null),
  141. array('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 array(
  155. array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800),
  156. array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799),
  157. array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800),
  158. array('pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1),
  159. array('pubdate:2007-03-01/', strtotime('2007-03-01'), null),
  160. array('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 array(
  214. array(
  215. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  216. array('word1'),
  217. strtotime('2007-03-01'),
  218. strtotime('2008-05-12') - 1,
  219. array('word2'),
  220. array('word3'),
  221. strtotime('2007-03-01'),
  222. strtotime('2008-05-12') - 1,
  223. array('word4', 'word5'),
  224. null,
  225. ),
  226. array(
  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. array('word1'),
  229. strtotime('2007-03-01'),
  230. strtotime('2008-05-12') - 1,
  231. array('word2'),
  232. array('word3'),
  233. strtotime('2007-03-01'),
  234. strtotime('2008-05-12') - 1,
  235. array('word4', 'word5'),
  236. array('word6'),
  237. ),
  238. array(
  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. array('word1'),
  241. strtotime('2007-03-01'),
  242. strtotime('2008-05-12') - 1,
  243. array('word2'),
  244. array('word3'),
  245. strtotime('2007-03-01'),
  246. strtotime('2008-05-12') - 1,
  247. array('word4', 'word5'),
  248. array('word6', 'word7'),
  249. ),
  250. array(
  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. array('word1'),
  253. strtotime('2007-03-01'),
  254. strtotime('2008-05-12') - 1,
  255. array('word2'),
  256. array('word3'),
  257. strtotime('2007-03-01'),
  258. strtotime('2008-05-12') - 1,
  259. array('word4', 'word5'),
  260. array('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. ];
  279. }
  280. #[DataProvider('provideconsistentOrParentheses')]
  281. public static function test__consistentOrParentheses(string $input, string $output): void {
  282. self::assertEquals($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  283. }
  284. /** @return array<array{string,string}> */
  285. public static function provideconsistentOrParentheses(): array {
  286. return [
  287. ['ab cd ef', 'ab cd ef'],
  288. ['(ab cd ef)', '(ab cd ef)'],
  289. ['("ab cd" ef)', '("ab cd" ef)'],
  290. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  291. ['ab (!cd)', 'ab (!cd)'],
  292. ['ab !(cd)', 'ab !(cd)'],
  293. ['(ab) -(cd)', '(ab) -(cd)'],
  294. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  295. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  296. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  297. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  298. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  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. ];
  302. }
  303. /**
  304. * @param array<string> $values
  305. */
  306. #[DataProvider('provideParentheses')]
  307. public function test__parentheses(string $input, string $sql, array $values): void {
  308. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  309. self::assertEquals(trim($sql), trim($filterSearch));
  310. self::assertEquals($values, $filterValues);
  311. }
  312. /** @return array<array<mixed>> */
  313. public static function provideParentheses(): array {
  314. return [
  315. [
  316. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  317. ' ((e.id_feed IN (?) )) AND ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ) OR (e.id_feed IN (?) )) AND' .
  318. ' (((e.id_feed IN (?) )) OR ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ))) ',
  319. ['1', '2', '3', '4', '5', '6', '7']
  320. ],
  321. [
  322. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  323. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  324. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  325. ['%tag #%','%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', '12']
  326. ],
  327. [
  328. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  329. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  330. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  331. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  332. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  333. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', 'Bleu']
  334. ],
  335. [
  336. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  337. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  338. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  339. ],
  340. [
  341. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  342. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  343. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  344. ],
  345. [
  346. 'intitle:"\\(test\\)"',
  347. '(e.title LIKE ? )',
  348. ['%(test)%'],
  349. ],
  350. [
  351. 'intitle:\'"hello world"\'',
  352. '(e.title LIKE ? )',
  353. ['%"hello world"%'],
  354. ],
  355. [
  356. '(ab) OR (cd) OR (ef)',
  357. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  358. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  359. ],
  360. [
  361. '("plain or text") OR (cd)',
  362. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  363. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  364. ],
  365. [
  366. '"plain or text" OR cd',
  367. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  368. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  369. ],
  370. [
  371. '"plain OR text" OR cd',
  372. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) ',
  373. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  374. ],
  375. [
  376. 'ab OR cd OR (ef)',
  377. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ',
  378. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  379. ],
  380. [
  381. 'ab OR cd OR ef',
  382. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  383. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  384. ],
  385. [
  386. '(ab) cd OR ef OR (gh)',
  387. '(((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  388. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  389. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  390. ],
  391. [
  392. '(ab) OR cd OR ef OR (gh)',
  393. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  394. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  395. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  396. ],
  397. [
  398. 'ab OR (!(cd OR ef))',
  399. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )))',
  400. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  401. ],
  402. [
  403. 'ab !(cd OR ef)',
  404. '(((e.title LIKE ? OR e.content LIKE ?) )) AND NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  405. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  406. ],
  407. [
  408. 'ab OR !(cd OR ef)',
  409. '(((e.title LIKE ? OR e.content LIKE ?) )) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  410. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  411. ],
  412. [
  413. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  414. '((((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 ?) )) ' .
  415. '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 ?) ))',
  416. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  417. ],
  418. ];
  419. }
  420. /**
  421. * @dataProvider provideRegexPostreSQL
  422. * @param array<string> $values
  423. */
  424. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  425. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  426. self::assertEquals(trim($sql), trim($filterSearch));
  427. self::assertEquals($values, $filterValues);
  428. }
  429. /** @return array<array<mixed>> */
  430. public static function provideRegexPostreSQL(): array {
  431. return [
  432. [
  433. 'intitle:/^ab$/',
  434. '(e.title ~ ? )',
  435. ['^ab$']
  436. ],
  437. [
  438. 'intitle:/^ab$/i',
  439. '(e.title ~* ? )',
  440. ['^ab$']
  441. ],
  442. [
  443. 'intitle:/^ab$/m',
  444. '(e.title ~ ? )',
  445. ['(?m)^ab$']
  446. ],
  447. [
  448. 'intitle:/^ab\\M/',
  449. '(e.title ~ ? )',
  450. ['^ab\\M']
  451. ],
  452. [
  453. 'author:/^ab$/',
  454. "(REPLACE(e.author, ';', '\n') ~ ? )",
  455. ['^ab$']
  456. ],
  457. [
  458. 'inurl:/^ab$/',
  459. '(e.link ~ ? )',
  460. ['^ab$']
  461. ],
  462. [
  463. '/^ab$/',
  464. '((e.title ~ ? OR e.content ~ ?) )',
  465. ['^ab$', '^ab$']
  466. ],
  467. [
  468. '!/^ab$/',
  469. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  470. ['^ab$', '^ab$']
  471. ],
  472. [ // Not a regex
  473. 'inurl:https://example.net/test/',
  474. '(e.link LIKE ? )',
  475. ['%https://example.net/test/%']
  476. ],
  477. [ // Not a regex
  478. 'https://example.net/test/',
  479. '((e.title LIKE ? OR e.content LIKE ?) )',
  480. ['%https://example.net/test/%', '%https://example.net/test/%']
  481. ],
  482. ];
  483. }
  484. /**
  485. * @dataProvider provideRegexMariaDB
  486. * @param array<string> $values
  487. */
  488. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  489. FreshRSS_DatabaseDAO::$dummyConnection = true;
  490. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  491. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  492. self::assertEquals(trim($sql), trim($filterSearch));
  493. self::assertEquals($values, $filterValues);
  494. }
  495. /** @return array<array<mixed>> */
  496. public static function provideRegexMariaDB(): array {
  497. return [
  498. [
  499. 'intitle:/^ab$/',
  500. "(e.title REGEXP ? )",
  501. ['(?-i)^ab$']
  502. ],
  503. [
  504. 'intitle:/^ab$/i',
  505. "(e.title REGEXP ? )",
  506. ['(?i)^ab$']
  507. ],
  508. [
  509. 'intitle:/^ab$/m',
  510. "(e.title REGEXP ? )",
  511. ['(?-i)(?m)^ab$']
  512. ],
  513. ];
  514. }
  515. /**
  516. * @dataProvider provideRegexMySQL
  517. * @param array<string> $values
  518. */
  519. public function test__regex_mysql(string $input, string $sql, array $values): void {
  520. FreshRSS_DatabaseDAO::$dummyConnection = true;
  521. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  522. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  523. self::assertEquals(trim($sql), trim($filterSearch));
  524. self::assertEquals($values, $filterValues);
  525. }
  526. /** @return array<array<mixed>> */
  527. public static function provideRegexMySQL(): array {
  528. return [
  529. [
  530. 'intitle:/^ab$/',
  531. "(REGEXP_LIKE(e.title,?,'c') )",
  532. ['^ab$']
  533. ],
  534. [
  535. 'intitle:/^ab$/i',
  536. "(REGEXP_LIKE(e.title,?,'i') )",
  537. ['^ab$']
  538. ],
  539. [
  540. 'intitle:/^ab$/m',
  541. "(REGEXP_LIKE(e.title,?,'mc') )",
  542. ['^ab$']
  543. ],
  544. ];
  545. }
  546. /**
  547. * @dataProvider provideRegexSQLite
  548. * @param array<string> $values
  549. */
  550. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  551. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  552. self::assertEquals(trim($sql), trim($filterSearch));
  553. self::assertEquals($values, $filterValues);
  554. }
  555. /** @return array<array<mixed>> */
  556. public static function provideRegexSQLite(): array {
  557. return [
  558. [
  559. 'intitle:/^ab$/',
  560. "(e.title REGEXP ? )",
  561. ['/^ab$/']
  562. ],
  563. [
  564. 'intitle:/^ab$/i',
  565. "(e.title REGEXP ? )",
  566. ['/^ab$/i']
  567. ],
  568. [
  569. 'intitle:/^ab$/m',
  570. "(e.title REGEXP ? )",
  571. ['/^ab$/m']
  572. ],
  573. [
  574. 'intitle:/^ab\\b/',
  575. '(e.title REGEXP ? )',
  576. ['/^ab\\b/']
  577. ],
  578. ];
  579. }
  580. }