SearchTest.php 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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->__toString());
  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:"< & >"', ['&lt; &amp; &gt;'], null],
  62. ["intitle:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  63. ['intitle:word1+word2', ['word1+word2'], null],
  64. ];
  65. }
  66. /**
  67. * @param array<string>|null $intext_value
  68. * @param array<string>|null $search_value
  69. */
  70. #[DataProvider('provideIntextSearch')]
  71. public static function test__construct_whenInputContainsIntext(string $input, ?array $intext_value, ?array $search_value): void {
  72. $search = new FreshRSS_Search($input);
  73. self::assertSame($intext_value, $search->getIntext());
  74. self::assertSame($search_value, $search->getSearch());
  75. }
  76. /**
  77. * @return list<list<mixed>>
  78. */
  79. public static function provideIntextSearch(): array {
  80. return [
  81. ['intext:word1', ['word1'], null],
  82. ['intext:"word1 word2"', ['word1 word2'], null],
  83. ];
  84. }
  85. /**
  86. * @param array<string>|null $author_value
  87. * @param array<string>|null $search_value
  88. */
  89. #[DataProvider('provideAuthorSearch')]
  90. public static function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void {
  91. $search = new FreshRSS_Search($input);
  92. self::assertSame($author_value, $search->getAuthor());
  93. self::assertSame($search_value, $search->getSearch());
  94. }
  95. /**
  96. * @return list<list<mixed>>
  97. */
  98. public static function provideAuthorSearch(): array {
  99. return [
  100. ['author:word1', ['word1'], null],
  101. ['author:word1-word2', ['word1-word2'], null],
  102. ['author:word1 word2', ['word1'], ['word2']],
  103. ['author:"word1 word2"', ['word1 word2'], null],
  104. ["author:'word1 word2'", ['word1 word2'], null],
  105. ['word1 author:word2', ['word2'], ['word1']],
  106. ['word1 author:word2 word3', ['word2'], ['word1', 'word3']],
  107. ['word1 author:"word2 word3"', ['word2 word3'], ['word1']],
  108. ["word1 author:'word2 word3'", ['word2 word3'], ['word1']],
  109. ['author:word1 author:word2', ['word1', 'word2'], null],
  110. ['author: word1 word2', null, ['word1', 'word2']],
  111. ['author:123', ['123'], null],
  112. ['author:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  113. ["author:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  114. ['author:"word1 word2\' word3"', ["word1 word2' word3"], null],
  115. ["author:'word1 word2\" word3'", ['word1 word2" word3'], null],
  116. ["author:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  117. ['author:word1+word2', ['word1+word2'], null],
  118. ];
  119. }
  120. /**
  121. * @param array<string>|null $inurl_value
  122. * @param array<string>|null $search_value
  123. */
  124. #[DataProvider('provideInurlSearch')]
  125. public static function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void {
  126. $search = new FreshRSS_Search($input);
  127. self::assertSame($inurl_value, $search->getInurl());
  128. self::assertSame($search_value, $search->getSearch());
  129. }
  130. /**
  131. * @return list<list<mixed>>
  132. */
  133. public static function provideInurlSearch(): array {
  134. return [
  135. ['inurl:word1', ['word1'], null],
  136. ['inurl: word1', null, ['word1']],
  137. ['inurl:123', ['123'], null],
  138. ['inurl:word1 word2', ['word1'], ['word2']],
  139. ['inurl:"word1 word2"', ['word1 word2'], null],
  140. ['inurl:word1 word2 inurl:word3', ['word1', 'word3'], ['word2']],
  141. ["inurl:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  142. ['inurl:word1+word2', ['word1+word2'], null],
  143. ];
  144. }
  145. #[DataProvider('provideDateSearch')]
  146. public static function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void {
  147. $search = new FreshRSS_Search($input);
  148. self::assertSame($min_date_value, $search->getMinDate());
  149. self::assertSame($max_date_value, $search->getMaxDate());
  150. }
  151. /**
  152. * @return list<list<mixed>>
  153. */
  154. public static function provideDateSearch(): array {
  155. return [
  156. ['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  157. ['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  158. ['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  159. ['date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  160. ['date:2007-03-01/', strtotime('2007-03-01'), null],
  161. ['date:/2008-05-11', null, strtotime('2008-05-12') - 1],
  162. ];
  163. }
  164. #[DataProvider('providePubdateSearch')]
  165. public static function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void {
  166. $search = new FreshRSS_Search($input);
  167. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  168. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  169. }
  170. /**
  171. * @return list<list<mixed>>
  172. */
  173. public static function providePubdateSearch(): array {
  174. return [
  175. ['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  176. ['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  177. ['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  178. ['pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  179. ['pubdate:2007-03-01/', strtotime('2007-03-01'), null],
  180. ['pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  181. ];
  182. }
  183. #[DataProvider('provideUserdateSearch')]
  184. public static function test__construct_whenInputContainsUserdate(string $input, ?int $min_userdate_value, ?int $max_userdate_value): void {
  185. $search = new FreshRSS_Search($input);
  186. self::assertSame($min_userdate_value, $search->getMinUserdate());
  187. self::assertSame($max_userdate_value, $search->getMaxUserdate());
  188. }
  189. /**
  190. * @return list<list<mixed>>
  191. */
  192. public static function provideUserdateSearch(): array {
  193. return [
  194. ['userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  195. ['userdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  196. ];
  197. }
  198. /**
  199. * @param array<string>|null $tags_value
  200. * @param array<string>|null $search_value
  201. */
  202. #[DataProvider('provideTagsSearch')]
  203. public static function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  204. $search = new FreshRSS_Search($input);
  205. self::assertSame($tags_value, $search->getTags());
  206. self::assertSame($search_value, $search->getSearch());
  207. }
  208. /**
  209. * @return list<list<string|list<string>|null>>
  210. */
  211. public static function provideTagsSearch(): array {
  212. return [
  213. ['#word1', ['word1'], null],
  214. ['# word1', null, ['#', 'word1']],
  215. ['#123', ['123'], null],
  216. ['#word1 word2', ['word1'], ['word2']],
  217. ['#"word1 word2"', ['word1 word2'], null],
  218. ['#word1 #word2', ['word1', 'word2'], null],
  219. ["#word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  220. ['#word1+word2', ['word1 word2'], null]
  221. ];
  222. }
  223. /**
  224. * @param list<array{search:string}> $queries
  225. * @param array{0:string,1:list<string|int>} $expectedResult
  226. */
  227. #[DataProvider('provideSavedQueryIdExpansion')]
  228. public static function test__construct_whenInputContainsSavedQueryIds_expandsSavedSearches(array $queries, string $input, array $expectedResult): void {
  229. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  230. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  231. $newUserConf->queries = $queries;
  232. FreshRSS_Context::setUserConf($newUserConf);
  233. try {
  234. $search = new FreshRSS_BooleanSearch($input);
  235. [$actualValues, $actualSql] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $search);
  236. self::assertSame($expectedResult[0], trim($actualSql));
  237. self::assertSame($expectedResult[1], $actualValues);
  238. } finally {
  239. FreshRSS_Context::setUserConf($previousUserConf);
  240. }
  241. }
  242. /**
  243. * @return array<string,array{0:list<array{search:string}>,1:string,2:array{0:string,1:list<string|int>}}>
  244. */
  245. public static function provideSavedQueryIdExpansion(): array {
  246. return [
  247. 'expanded single group' => [
  248. [
  249. ['search' => 'author:Alice'],
  250. ['search' => 'intitle:World'],
  251. ],
  252. 'S:0,1',
  253. [
  254. '((e.author LIKE ? )) OR ((e.title LIKE ? ))',
  255. ['%Alice%', '%World%'],
  256. ],
  257. ],
  258. 'separate groups with OR' => [
  259. [
  260. ['search' => 'author:Alice'],
  261. ['search' => 'intitle:World'],
  262. ['search' => 'inurl:Example'],
  263. ['search' => 'author:Bob'],
  264. ],
  265. 'S:0,1 OR S:2,3',
  266. [
  267. '((e.author LIKE ? )) OR ((e.title LIKE ? )) OR ((e.link LIKE ? )) OR ((e.author LIKE ? ))',
  268. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  269. ],
  270. ],
  271. 'mixed with other clauses' => [
  272. [
  273. ['search' => 'author:Alice'],
  274. ['search' => 'intitle:World'],
  275. ],
  276. 'intitle:Hello S:0,1 date:2025-10',
  277. [
  278. '((e.title LIKE ? )) AND ((e.author LIKE ? )) OR ((e.title LIKE ? )) AND ((e.id >= ? AND e.id <= ? ))',
  279. ['%Hello%', '%Alice%', '%World%', strtotime('2025-10-01') . '000000', (strtotime('2025-11-01') - 1) . '000000'],
  280. ],
  281. ],
  282. ];
  283. }
  284. /**
  285. * @param array<string>|null $author_value
  286. * @param array<string> $intitle_value
  287. * @param array<string>|null $inurl_value
  288. * @param array<string>|null $tags_value
  289. * @param array<string>|null $search_value
  290. */
  291. #[DataProvider('provideMultipleSearch')]
  292. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  293. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  294. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  295. $search = new FreshRSS_Search($input);
  296. self::assertSame($author_value, $search->getAuthor());
  297. self::assertSame($min_date_value, $search->getMinDate());
  298. self::assertSame($max_date_value, $search->getMaxDate());
  299. self::assertSame($intitle_value, $search->getIntitle());
  300. self::assertSame($inurl_value, $search->getInurl());
  301. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  302. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  303. self::assertSame($tags_value, $search->getTags());
  304. self::assertSame($search_value, $search->getSearch());
  305. }
  306. /** @return list<list<mixed>> */
  307. public static function provideMultipleSearch(): array {
  308. return [
  309. [
  310. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  311. ['word1'],
  312. strtotime('2007-03-01'),
  313. strtotime('2008-05-12') - 1,
  314. ['word2'],
  315. ['word3'],
  316. strtotime('2007-03-01'),
  317. strtotime('2008-05-12') - 1,
  318. ['word4', 'word5'],
  319. null
  320. ],
  321. [
  322. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  323. ['word1'],
  324. strtotime('2007-03-01'),
  325. strtotime('2008-05-12') - 1,
  326. ['word2'],
  327. ['word3'],
  328. strtotime('2007-03-01'),
  329. strtotime('2008-05-12') - 1,
  330. ['word4', 'word5'],
  331. ['word6']
  332. ],
  333. [
  334. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  335. ['word1'],
  336. strtotime('2007-03-01'),
  337. strtotime('2008-05-12') - 1,
  338. ['word2'],
  339. ['word3'],
  340. strtotime('2007-03-01'),
  341. strtotime('2008-05-12') - 1,
  342. ['word4', 'word5'],
  343. ['word6', 'word7']
  344. ],
  345. [
  346. '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',
  347. ['word1'],
  348. strtotime('2007-03-01'),
  349. strtotime('2008-05-12') - 1,
  350. ['word2'],
  351. ['word3'],
  352. strtotime('2007-03-01'),
  353. strtotime('2008-05-12') - 1,
  354. ['word4', 'word5'],
  355. ['word7 word8', 'word6']
  356. ]
  357. ];
  358. }
  359. #[DataProvider('provideAddOrParentheses')]
  360. public static function test__addOrParentheses(string $input, string $output): void {
  361. self::assertSame($output, FreshRSS_BooleanSearch::addOrParentheses($input));
  362. }
  363. /** @return list<list{string,string}> */
  364. public static function provideAddOrParentheses(): array {
  365. return [
  366. ['ab', 'ab'],
  367. ['ab cd', 'ab cd'],
  368. ['!ab -cd', '!ab -cd'],
  369. ['ab OR cd', '(ab) OR (cd)'],
  370. ['!ab OR -cd', '(!ab) OR (-cd)'],
  371. ['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],
  372. ['ab (!cd)', 'ab (!cd)'],
  373. ['"ab" (!"cd")', '"ab" (!"cd")'],
  374. ];
  375. }
  376. #[DataProvider('provideconsistentOrParentheses')]
  377. public static function test__consistentOrParentheses(string $input, string $output): void {
  378. self::assertSame($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  379. }
  380. /** @return list<list{string,string}> */
  381. public static function provideconsistentOrParentheses(): array {
  382. return [
  383. ['ab cd ef', 'ab cd ef'],
  384. ['(ab cd ef)', '(ab cd ef)'],
  385. ['("ab cd" ef)', '("ab cd" ef)'],
  386. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  387. ['ab (!cd)', 'ab (!cd)'],
  388. ['ab !(cd)', 'ab !(cd)'],
  389. ['(ab) -(cd)', '(ab) -(cd)'],
  390. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  391. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  392. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  393. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  394. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  395. ['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],
  396. ['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],
  397. ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],
  398. ];
  399. }
  400. /**
  401. * @param array<string> $values
  402. */
  403. #[DataProvider('provideParentheses')]
  404. public function test__parentheses(string $input, string $sql, array $values): void {
  405. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  406. self::assertSame(trim($sql), trim($filterSearch));
  407. self::assertSame($values, $filterValues);
  408. }
  409. /** @return list<list<mixed>> */
  410. public static function provideParentheses(): array {
  411. return [
  412. [
  413. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  414. ' ((e.id_feed IN (?) )) AND ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ) OR (e.id_feed IN (?) )) AND' .
  415. ' (((e.id_feed IN (?) )) OR ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ))) ',
  416. [1, 2, 3, 4, 5, 6, 7]
  417. ],
  418. [
  419. 'c:1 OR c:2,3',
  420. ' (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 (?,?)) ) ',
  421. [1, 2, 3]
  422. ],
  423. [
  424. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  425. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  426. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  427. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 12]
  428. ],
  429. [
  430. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  431. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  432. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  433. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  434. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  435. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 'Bleu']
  436. ],
  437. [
  438. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  439. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  440. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  441. ],
  442. [
  443. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  444. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  445. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  446. ],
  447. [
  448. 'intitle:"(test)"',
  449. '(e.title LIKE ? )',
  450. ['%(test)%'],
  451. ],
  452. [
  453. 'intitle:\'"hello world"\'',
  454. '(e.title LIKE ? )',
  455. ['%"hello world"%'],
  456. ],
  457. [
  458. 'intext:\'"hello world"\'',
  459. '(e.content LIKE ? )',
  460. ['%"hello world"%'],
  461. ],
  462. [
  463. '(ab) OR (cd) OR (ef)',
  464. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  465. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  466. ],
  467. [
  468. '("plain or text") OR (cd)',
  469. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  470. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  471. ],
  472. [
  473. '"plain or text" OR cd',
  474. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  475. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  476. ],
  477. [
  478. '"plain OR text" OR cd',
  479. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) ',
  480. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  481. ],
  482. [
  483. 'ab OR cd OR (ef)',
  484. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ',
  485. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  486. ],
  487. [
  488. 'ab OR cd OR ef',
  489. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  490. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  491. ],
  492. [
  493. '(ab) cd OR ef OR (gh)',
  494. '(((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  495. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  496. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  497. ],
  498. [
  499. '(ab) OR cd OR ef OR (gh)',
  500. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  501. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  502. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  503. ],
  504. [
  505. 'ab OR (!(cd OR ef))',
  506. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )))',
  507. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  508. ],
  509. [
  510. 'ab !(cd OR ef)',
  511. '(((e.title LIKE ? OR e.content LIKE ?) )) AND NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  512. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  513. ],
  514. [
  515. 'ab OR !(cd OR ef)',
  516. '(((e.title LIKE ? OR e.content LIKE ?) )) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  517. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  518. ],
  519. [
  520. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  521. '((((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 ?) )) ' .
  522. '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 ?) ))',
  523. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  524. ],
  525. [
  526. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  527. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  528. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  529. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  530. ],
  531. [
  532. 'intitle:"é & \' è" intext:/<&>/ \'< & " >\'',
  533. '(e.title LIKE ? AND e.content ~ ? AND (e.title LIKE ? OR e.content LIKE ?) )',
  534. ['%é &amp; \' è%', '<&>', '%&lt; &amp; " &gt;%', '%&lt; &amp; " &gt;%']
  535. ],
  536. [
  537. '/^(ab|cd) [(] \\) (ef|gh)/',
  538. '((e.title ~ ? OR e.content ~ ?) )',
  539. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  540. ],
  541. [
  542. '!/^(ab|cd)/',
  543. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  544. ['^(ab|cd)', '^(ab|cd)']
  545. ],
  546. [
  547. 'intitle:/^(ab|cd)/',
  548. '(e.title ~ ? )',
  549. ['^(ab|cd)']
  550. ],
  551. [
  552. 'intext:/^(ab|cd)/',
  553. '(e.content ~ ? )',
  554. ['^(ab|cd)']
  555. ],
  556. [
  557. 'L:1 L:2',
  558. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) AND ' .
  559. 'e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )',
  560. [1, 2]
  561. ],
  562. [
  563. 'L:1,2',
  564. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?,?)) )',
  565. [1, 2]
  566. ],
  567. ];
  568. }
  569. /**
  570. * @param array<string> $values
  571. */
  572. #[DataProvider('provideDateOperators')]
  573. public function test__date_operators(string $input, string $sql, array $values): void {
  574. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  575. self::assertSame(trim($sql), trim($filterSearch));
  576. self::assertSame($values, $filterValues);
  577. }
  578. /** @return list<list<mixed>> */
  579. public static function provideDateOperators(): array {
  580. return [
  581. // Basic date operator tests
  582. [
  583. 'date:2007-03-01/2008-05-11',
  584. '(e.id >= ? AND e.id <= ? )',
  585. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  586. ],
  587. [
  588. 'date:2007-03-01/',
  589. '(e.id >= ? )',
  590. [strtotime('2007-03-01T00:00:00Z') . '000000'],
  591. ],
  592. [
  593. 'date:/2008-05-11',
  594. '(e.id <= ? )',
  595. [strtotime('2008-05-11T23:59:59Z') . '000000'],
  596. ],
  597. // Basic pubdate operator tests
  598. [
  599. 'pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  600. '(e.date >= ? AND e.date <= ? )',
  601. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  602. ],
  603. [
  604. 'pubdate:2007-03-01/',
  605. '(e.date >= ? )',
  606. [strtotime('2007-03-01T00:00:00Z')],
  607. ],
  608. [
  609. 'pubdate:/2008-05-11',
  610. '(e.date <= ? )',
  611. [strtotime('2008-05-11T23:59:59Z')],
  612. ],
  613. // Basic userdate operator tests
  614. [
  615. 'userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  616. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? )',
  617. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  618. ],
  619. [
  620. 'userdate:2007-03-01/',
  621. '(e.`lastUserModified` >= ? )',
  622. [strtotime('2007-03-01T00:00:00Z')],
  623. ],
  624. [
  625. 'userdate:/2008-05-11',
  626. '(e.`lastUserModified` <= ? )',
  627. [strtotime('2008-05-11T23:59:59Z')],
  628. ],
  629. // Negative date operator tests
  630. [
  631. '-date:2007-03-01/2008-05-11',
  632. '((e.id < ? OR e.id > ?) )',
  633. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  634. ],
  635. [
  636. '!pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  637. '((e.date < ? OR e.date > ?) )',
  638. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  639. ],
  640. [
  641. '!userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  642. '((e.`lastUserModified` < ? OR e.`lastUserModified` > ?) )',
  643. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  644. ],
  645. // Combined date operators
  646. [
  647. 'date:2007-03-01/ pubdate:/2008-05-11',
  648. '(e.id >= ? AND e.date <= ? )',
  649. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z')],
  650. ],
  651. [
  652. 'pubdate:2007-03-01/ userdate:/2008-05-11',
  653. '(e.date >= ? AND e.`lastUserModified` <= ? )',
  654. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  655. ],
  656. [
  657. 'date:2007-03-01/ userdate:2007-06-01/',
  658. '(e.id >= ? AND e.`lastUserModified` >= ? )',
  659. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2007-06-01T00:00:00Z')],
  660. ],
  661. // Complex combinations with other operators
  662. [
  663. 'intitle:test date:2007-03-01/ pubdate:/2008-05-11',
  664. '(e.id >= ? AND e.date <= ? AND e.title LIKE ? )',
  665. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z'), '%test%'],
  666. ],
  667. [
  668. 'author:john userdate:2007-03-01/2008-05-11',
  669. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? AND e.author LIKE ? )',
  670. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z'), '%john%'],
  671. ],
  672. // Mixed positive and negative date operators
  673. [
  674. 'date:2007-03-01/ !pubdate:2008-01-01/2008-05-11',
  675. '(e.id >= ? AND (e.date < ? OR e.date > ?) )',
  676. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-01-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  677. ],
  678. ];
  679. }
  680. /**
  681. * @dataProvider provideRegexPostreSQL
  682. * @param array<string> $values
  683. */
  684. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  685. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  686. self::assertSame(trim($sql), trim($filterSearch));
  687. self::assertSame($values, $filterValues);
  688. }
  689. /** @return list<list<mixed>> */
  690. public static function provideRegexPostreSQL(): array {
  691. return [
  692. [
  693. 'intitle:/^ab$/',
  694. '(e.title ~ ? )',
  695. ['^ab$']
  696. ],
  697. [
  698. 'intitle:/^ab$/i',
  699. '(e.title ~* ? )',
  700. ['^ab$']
  701. ],
  702. [
  703. 'intitle:/^ab$/m',
  704. '(e.title ~ ? )',
  705. ['(?m)^ab$']
  706. ],
  707. [
  708. 'intitle:/^ab\\M/',
  709. '(e.title ~ ? )',
  710. ['^ab\\M']
  711. ],
  712. [
  713. 'intext:/^ab\\M/',
  714. '(e.content ~ ? )',
  715. ['^ab\\M']
  716. ],
  717. [
  718. 'intitle:/\\b\\d+/',
  719. '(e.title ~ ? )',
  720. ['\\y\\d+']
  721. ],
  722. [
  723. 'author:/^ab$/',
  724. "(REPLACE(e.author, ';', '\n') ~ ? )",
  725. ['^ab$']
  726. ],
  727. [
  728. 'inurl:/^ab$/',
  729. '(e.link ~ ? )',
  730. ['^ab$']
  731. ],
  732. [
  733. '/^ab$/',
  734. '((e.title ~ ? OR e.content ~ ?) )',
  735. ['^ab$', '^ab$']
  736. ],
  737. [
  738. '!/^ab$/',
  739. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  740. ['^ab$', '^ab$']
  741. ],
  742. [
  743. '#/^a(b|c)$/im',
  744. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ? )",
  745. ['(?m)^a(b|c)$']
  746. ],
  747. [ // Not a regex
  748. 'inurl:https://example.net/test/',
  749. '(e.link LIKE ? )',
  750. ['%https://example.net/test/%']
  751. ],
  752. [ // Not a regex
  753. 'https://example.net/test/',
  754. '((e.title LIKE ? OR e.content LIKE ?) )',
  755. ['%https://example.net/test/%', '%https://example.net/test/%']
  756. ],
  757. ];
  758. }
  759. /**
  760. * @dataProvider provideRegexMariaDB
  761. * @param array<string> $values
  762. */
  763. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  764. FreshRSS_DatabaseDAO::$dummyConnection = true;
  765. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  766. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  767. self::assertSame(trim($sql), trim($filterSearch));
  768. self::assertSame($values, $filterValues);
  769. }
  770. /** @return list<list<mixed>> */
  771. public static function provideRegexMariaDB(): array {
  772. return [
  773. [
  774. 'intitle:/^ab$/',
  775. "(e.title REGEXP ? )",
  776. ['(?-i)^ab$']
  777. ],
  778. [
  779. 'intitle:/^ab$/i',
  780. "(e.title REGEXP ? )",
  781. ['(?i)^ab$']
  782. ],
  783. [
  784. 'intitle:/^ab$/m',
  785. "(e.title REGEXP ? )",
  786. ['(?-i)(?m)^ab$']
  787. ],
  788. [
  789. 'intitle:/\\b\\d+/',
  790. "(e.title REGEXP ? )",
  791. ['(?-i)\\b\\d+']
  792. ],
  793. [
  794. 'intext:/^ab$/m',
  795. '(UNCOMPRESS(e.content_bin) REGEXP ?) )',
  796. ['(?-i)(?m)^ab$']
  797. ],
  798. ];
  799. }
  800. /**
  801. * @dataProvider provideRegexMySQL
  802. * @param array<string> $values
  803. */
  804. public function test__regex_mysql(string $input, string $sql, array $values): void {
  805. FreshRSS_DatabaseDAO::$dummyConnection = true;
  806. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  807. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  808. self::assertSame(trim($sql), trim($filterSearch));
  809. self::assertSame($values, $filterValues);
  810. }
  811. /** @return list<list<mixed>> */
  812. public static function provideRegexMySQL(): array {
  813. return [
  814. [
  815. 'intitle:/^ab$/',
  816. "(REGEXP_LIKE(e.title,?,'c') )",
  817. ['^ab$']
  818. ],
  819. [
  820. 'intitle:/^ab$/i',
  821. "(REGEXP_LIKE(e.title,?,'i') )",
  822. ['^ab$']
  823. ],
  824. [
  825. 'intitle:/^ab$/m',
  826. "(REGEXP_LIKE(e.title,?,'mc') )",
  827. ['^ab$']
  828. ],
  829. [
  830. 'intext:/^ab$/m',
  831. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc')) )",
  832. ['^ab$']
  833. ],
  834. ];
  835. }
  836. /**
  837. * @dataProvider provideRegexSQLite
  838. * @param array<string> $values
  839. */
  840. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  841. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  842. self::assertSame(trim($sql), trim($filterSearch));
  843. self::assertSame($values, $filterValues);
  844. }
  845. /** @return list<list<mixed>> */
  846. public static function provideRegexSQLite(): array {
  847. return [
  848. [
  849. 'intitle:/^ab$/',
  850. "(e.title REGEXP ? )",
  851. ['/^ab$/']
  852. ],
  853. [
  854. 'intitle:/^ab$/i',
  855. "(e.title REGEXP ? )",
  856. ['/^ab$/i']
  857. ],
  858. [
  859. 'intitle:/^ab$/m',
  860. "(e.title REGEXP ? )",
  861. ['/^ab$/m']
  862. ],
  863. [
  864. 'intitle:/^ab\\b/',
  865. '(e.title REGEXP ? )',
  866. ['/^ab\\b/']
  867. ],
  868. [
  869. 'intext:/^ab\\b/',
  870. '(e.content REGEXP ? )',
  871. ['/^ab\\b/']
  872. ],
  873. ];
  874. }
  875. #[DataProvider('provideToString')]
  876. public static function test__toString(string $input): void {
  877. $search = new FreshRSS_Search($input);
  878. $expected = str_replace("\n", ' ', $input);
  879. self::assertSame($expected, $search->__toString());
  880. }
  881. /**
  882. * @return array<array<string>>
  883. */
  884. public static function provideToString(): array {
  885. return [
  886. [
  887. <<<'EOD'
  888. e:1,2 f:10,11 c:20,21 L:30,31 labels:"My label,My other label"
  889. userdate:2025-01-01T00:00:00/2026-01-01T00:00:00
  890. pubdate:2025-02-01T00:00:00/2026-01-01T00:00:00
  891. date:2025-03-01T00:00:00/2026-01-01T00:00:00
  892. intitle:/<Inter&sting>/i intitle:"g ' & d"
  893. intext:/<Inter&sting>/i intext:g&d
  894. author:/Bob/ author:Alice
  895. inurl:/https/ inurl:example.net
  896. #/tag2/ #tag1
  897. /search_regex/i "quoted search" search
  898. -e:3,4 -f:12,13 -c:22,23 -L:32,33 -labels:"Not label,Not other label"
  899. -userdate:2025-06-01T00:00:00/2025-09-01T00:00:00
  900. -pubdate:2025-06-01T00:00:00/2025-09-01T00:00:00
  901. -date:2025-06-01T00:00:00/2025-09-01T00:00:00
  902. -intitle:/Spam/i -intitle:"'bad"
  903. -intext:/Spam/i -intext:"'bad"
  904. -author:/Dave/i -author:Charlie
  905. -inurl:/ftp/ -inurl:example.com
  906. -#/tag4/ -#tag3
  907. -/not_regex/i -"not quoted" -not_search
  908. EOD
  909. ],
  910. ];
  911. }
  912. #[DataProvider('provideBooleanSearchToString')]
  913. public static function testBooleanSearch__toString(string $input, string $expected): void {
  914. $search = new FreshRSS_BooleanSearch($input);
  915. self::assertSame($expected, $search->__toString());
  916. }
  917. /**
  918. * @return array<array<string>>
  919. */
  920. public static function provideBooleanSearchToString(): array {
  921. return [
  922. [
  923. '((a OR b) (c OR d) -e) OR -(f g)',
  924. '((a OR b) (c OR d) (-e)) OR -(f g)',
  925. ],
  926. [
  927. '((a OR b) ((c) OR ((d))) (-e)) OR -(((f g)))',
  928. '((a OR b) (c OR d) (-e)) OR -(f g)',
  929. ],
  930. [
  931. '!((b c))',
  932. '-(b c)',
  933. ],
  934. [
  935. '(a) OR !((b c))',
  936. 'a OR -(b c)',
  937. ],
  938. [
  939. '((a) (b))',
  940. 'a b',
  941. ],
  942. [
  943. '((a) OR (b))',
  944. 'a OR b',
  945. ],
  946. [
  947. ' ( !( !( ( a ) ) ) ) ( ) ',
  948. '-(-a)',
  949. ],
  950. [
  951. '-intitle:a -inurl:b',
  952. '-intitle:a -inurl:b',
  953. ],
  954. ];
  955. }
  956. #[DataProvider('provideHasSameOperators')]
  957. public function testHasSameOperators(string $input1, string $input2, bool $expected): void {
  958. $search1 = new FreshRSS_Search($input1);
  959. $search2 = new FreshRSS_Search($input2);
  960. self::assertSame($expected, $search1->hasSameOperators($search2));
  961. }
  962. /**
  963. * @return array<array{string,string,bool}>
  964. */
  965. public static function provideHasSameOperators(): array {
  966. return [
  967. ['', '', true],
  968. ['intitle:a intext:b', 'intitle:c intext:d', true],
  969. ['intitle:a intext:b', 'intitle:c inurl:d', false],
  970. ];
  971. }
  972. #[DataProvider('provideBooleanSearchEnforce')]
  973. public function testBooleanSearchEnforce(string $initialInput, string $enforceInput, string $expectedOutput): void {
  974. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  975. $searchToEnforce = new FreshRSS_Search($enforceInput);
  976. $newBooleanSearch = $booleanSearch->enforce($searchToEnforce);
  977. self::assertNotSame($booleanSearch, $newBooleanSearch);
  978. self::assertSame($expectedOutput, $newBooleanSearch->__toString());
  979. }
  980. /**
  981. * @return array<array{string,string,string}>
  982. */
  983. public static function provideBooleanSearchEnforce(): array {
  984. return [
  985. ['', '', ''],
  986. ['', 'intitle:b', 'intitle:b'],
  987. ['intitle:a', '', 'intitle:a'],
  988. ['intitle:a', 'intitle:b', 'intitle:b'],
  989. ['a', 'intitle:b', 'intitle:b a'],
  990. ['intitle:a intext:a', 'intitle:b', 'intitle:b intext:a'],
  991. ['intitle:a inurl:a', 'intitle:b', 'intitle:b inurl:a'],
  992. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:b (intitle:a OR inurl:a)'],
  993. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', 'intitle:b (inurl:a intitle:c)'],
  994. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', 'intitle:b (inurl:a OR intitle:c)'],
  995. ['(intitle:a) (inurl:a)', 'intitle:b', 'intitle:b inurl:a'],
  996. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a intitle:b'],
  997. ['(a b) OR (c d)', 'e', 'e ((a b) OR (c d))'],
  998. ['(a b) (c d)', 'e', 'e ((a b) (c d))'],
  999. ['(a b)', 'e', 'e (a b)'],
  1000. ['date:2024/', 'date:/2025', 'date:/2025-12-31T23:59:59'],
  1001. ['a', 'date:/2025', 'date:/2025-12-31T23:59:59 a'],
  1002. ];
  1003. }
  1004. #[DataProvider('provideBooleanSearchRemove')]
  1005. public function testBooleanSearchRemove(string $initialInput, string $removeInput, string $expectedOutput): void {
  1006. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1007. $searchToRemove = new FreshRSS_Search($removeInput);
  1008. $newBooleanSearch = $booleanSearch->remove($searchToRemove);
  1009. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1010. self::assertSame($expectedOutput, $newBooleanSearch->__toString());
  1011. }
  1012. /**
  1013. * @return array<array{string,string,string}>
  1014. */
  1015. public static function provideBooleanSearchRemove(): array {
  1016. return [
  1017. ['', 'intitle:b', ''],
  1018. ['intitle:a', 'intitle:b', ''],
  1019. ['intitle:a intext:a', 'intitle:b', 'intext:a'],
  1020. ['intitle:a inurl:a', 'intitle:b', 'inurl:a'],
  1021. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:a OR inurl:a'],
  1022. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', '(inurl:a intitle:c)'],
  1023. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', '(inurl:a OR intitle:c)'],
  1024. ['(intitle:a) (inurl:a)', 'intitle:b', 'inurl:a'],
  1025. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a'],
  1026. ['e ((a b) OR (c d))', 'e', '((a b) OR (c d))'],
  1027. ['e ((a b) (c d))', 'e', '((a b) (c d))'],
  1028. ['date:2024/', 'date:/2025', ''],
  1029. ['date:2024/ a', 'date:/2025', 'a'],
  1030. ];
  1031. }
  1032. }