SearchTest.php 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  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,name?:string}> $queries
  225. * @param array{0:string,1:list<string|int>} $expectedResult
  226. */
  227. #[DataProvider('provideSavedQueriesExpansion')]
  228. public static function test__construct_whenInputContainsSavedQueries_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 provideSavedQueriesExpansion(): array {
  246. return [
  247. 'not found ID' => [
  248. [
  249. ['search' => 'author:Alice'],
  250. ['search' => 'intitle:World'],
  251. ],
  252. 'S:3',
  253. [
  254. '',
  255. [],
  256. ],
  257. ],
  258. 'not found name' => [
  259. [
  260. ['search' => 'author:Alice', 'name' => 'First'],
  261. ['search' => 'intitle:World', 'name' => 'Second'],
  262. ],
  263. 'search:Third',
  264. [
  265. '',
  266. [],
  267. ],
  268. ],
  269. 'expanded single group name' => [
  270. [
  271. ['search' => 'author:Alice', 'name' => 'First'],
  272. ['search' => 'intitle:World', 'name' => 'Second'],
  273. ],
  274. 'search:First OR search:Second',
  275. [
  276. '((e.author LIKE ? )) OR ((e.title LIKE ? ))',
  277. ['%Alice%', '%World%'],
  278. ],
  279. ],
  280. 'separate groups with OR' => [
  281. [
  282. ['search' => 'author:Alice'],
  283. ['search' => 'intitle:World'],
  284. ['search' => 'inurl:Example'],
  285. ['search' => 'author:Bob'],
  286. ],
  287. 'S:0,1 OR S:2,3,5',
  288. [
  289. '((e.author LIKE ? )) OR ((e.title LIKE ? )) OR ((e.link LIKE ? )) OR ((e.author LIKE ? ))',
  290. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  291. ],
  292. ],
  293. 'mixed with other clauses' => [
  294. [
  295. ['search' => 'author:Alice'],
  296. ['search' => 'intitle:World'],
  297. ],
  298. 'intitle:Hello S:0,1 date:2025-10',
  299. [
  300. '((e.title LIKE ? )) AND ((e.author LIKE ? )) OR ((e.title LIKE ? )) AND ((e.id >= ? AND e.id <= ? ))',
  301. ['%Hello%', '%Alice%', '%World%', strtotime('2025-10-01') . '000000', (strtotime('2025-11-01') - 1) . '000000'],
  302. ],
  303. ],
  304. ];
  305. }
  306. /**
  307. * @param array<string>|null $author_value
  308. * @param array<string> $intitle_value
  309. * @param array<string>|null $inurl_value
  310. * @param array<string>|null $tags_value
  311. * @param array<string>|null $search_value
  312. */
  313. #[DataProvider('provideMultipleSearch')]
  314. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  315. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  316. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  317. $search = new FreshRSS_Search($input);
  318. self::assertSame($author_value, $search->getAuthor());
  319. self::assertSame($min_date_value, $search->getMinDate());
  320. self::assertSame($max_date_value, $search->getMaxDate());
  321. self::assertSame($intitle_value, $search->getIntitle());
  322. self::assertSame($inurl_value, $search->getInurl());
  323. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  324. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  325. self::assertSame($tags_value, $search->getTags());
  326. self::assertSame($search_value, $search->getSearch());
  327. }
  328. /** @return list<list<mixed>> */
  329. public static function provideMultipleSearch(): array {
  330. return [
  331. [
  332. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  333. ['word1'],
  334. strtotime('2007-03-01'),
  335. strtotime('2008-05-12') - 1,
  336. ['word2'],
  337. ['word3'],
  338. strtotime('2007-03-01'),
  339. strtotime('2008-05-12') - 1,
  340. ['word4', 'word5'],
  341. null
  342. ],
  343. [
  344. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  345. ['word1'],
  346. strtotime('2007-03-01'),
  347. strtotime('2008-05-12') - 1,
  348. ['word2'],
  349. ['word3'],
  350. strtotime('2007-03-01'),
  351. strtotime('2008-05-12') - 1,
  352. ['word4', 'word5'],
  353. ['word6']
  354. ],
  355. [
  356. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  357. ['word1'],
  358. strtotime('2007-03-01'),
  359. strtotime('2008-05-12') - 1,
  360. ['word2'],
  361. ['word3'],
  362. strtotime('2007-03-01'),
  363. strtotime('2008-05-12') - 1,
  364. ['word4', 'word5'],
  365. ['word6', 'word7']
  366. ],
  367. [
  368. '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',
  369. ['word1'],
  370. strtotime('2007-03-01'),
  371. strtotime('2008-05-12') - 1,
  372. ['word2'],
  373. ['word3'],
  374. strtotime('2007-03-01'),
  375. strtotime('2008-05-12') - 1,
  376. ['word4', 'word5'],
  377. ['word7 word8', 'word6']
  378. ]
  379. ];
  380. }
  381. #[DataProvider('provideAddOrParentheses')]
  382. public static function test__addOrParentheses(string $input, string $output): void {
  383. self::assertSame($output, FreshRSS_BooleanSearch::addOrParentheses($input));
  384. }
  385. /** @return list<list{string,string}> */
  386. public static function provideAddOrParentheses(): array {
  387. return [
  388. ['ab', 'ab'],
  389. ['ab cd', 'ab cd'],
  390. ['!ab -cd', '!ab -cd'],
  391. ['ab OR cd', '(ab) OR (cd)'],
  392. ['!ab OR -cd', '(!ab) OR (-cd)'],
  393. ['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],
  394. ['ab (!cd)', 'ab (!cd)'],
  395. ['"ab" (!"cd")', '"ab" (!"cd")'],
  396. ];
  397. }
  398. #[DataProvider('provideconsistentOrParentheses')]
  399. public static function test__consistentOrParentheses(string $input, string $output): void {
  400. self::assertSame($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  401. }
  402. /** @return list<list{string,string}> */
  403. public static function provideconsistentOrParentheses(): array {
  404. return [
  405. ['ab cd ef', 'ab cd ef'],
  406. ['(ab cd ef)', '(ab cd ef)'],
  407. ['("ab cd" ef)', '("ab cd" ef)'],
  408. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  409. ['ab (!cd)', 'ab (!cd)'],
  410. ['ab !(cd)', 'ab !(cd)'],
  411. ['(ab) -(cd)', '(ab) -(cd)'],
  412. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  413. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  414. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  415. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  416. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  417. ['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],
  418. ['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],
  419. ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],
  420. ];
  421. }
  422. /**
  423. * @param array<string> $values
  424. */
  425. #[DataProvider('provideParentheses')]
  426. public function test__parentheses(string $input, string $sql, array $values): void {
  427. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  428. self::assertSame(trim($sql), trim($filterSearch));
  429. self::assertSame($values, $filterValues);
  430. }
  431. /** @return list<list<mixed>> */
  432. public static function provideParentheses(): array {
  433. return [
  434. [
  435. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  436. ' ((e.id_feed IN (?) )) AND ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ) OR (e.id_feed IN (?) )) AND' .
  437. ' (((e.id_feed IN (?) )) OR ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ))) ',
  438. [1, 2, 3, 4, 5, 6, 7]
  439. ],
  440. [
  441. 'c:1 OR c:2,3',
  442. ' (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 (?,?)) ) ',
  443. [1, 2, 3]
  444. ],
  445. [
  446. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  447. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  448. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  449. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 12]
  450. ],
  451. [
  452. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  453. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  454. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  455. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  456. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  457. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 'Bleu']
  458. ],
  459. [
  460. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  461. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  462. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  463. ],
  464. [
  465. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  466. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  467. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  468. ],
  469. [
  470. 'intitle:"(test)"',
  471. '(e.title LIKE ? )',
  472. ['%(test)%'],
  473. ],
  474. [
  475. 'intitle:\'"hello world"\'',
  476. '(e.title LIKE ? )',
  477. ['%"hello world"%'],
  478. ],
  479. [
  480. 'intext:\'"hello world"\'',
  481. '(e.content LIKE ? )',
  482. ['%"hello world"%'],
  483. ],
  484. [
  485. '(ab) OR (cd) OR (ef)',
  486. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  487. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  488. ],
  489. [
  490. '("plain or text") OR (cd)',
  491. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  492. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  493. ],
  494. [
  495. '"plain or text" OR cd',
  496. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  497. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  498. ],
  499. [
  500. '"plain OR text" OR cd',
  501. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) ',
  502. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  503. ],
  504. [
  505. 'ab OR cd OR (ef)',
  506. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((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 OR cd OR ef',
  511. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((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) cd OR ef OR (gh)',
  516. '(((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  517. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  518. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  519. ],
  520. [
  521. '(ab) OR cd OR ef OR (gh)',
  522. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  523. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  524. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  525. ],
  526. [
  527. 'ab OR (!(cd OR ef))',
  528. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )))',
  529. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  530. ],
  531. [
  532. 'ab !(cd OR ef)',
  533. '(((e.title LIKE ? OR e.content LIKE ?) )) AND NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  534. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  535. ],
  536. [
  537. 'ab OR !(cd OR ef)',
  538. '(((e.title LIKE ? OR e.content LIKE ?) )) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  539. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  540. ],
  541. [
  542. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  543. '((((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 ?) )) ' .
  544. '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 ?) ))',
  545. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  546. ],
  547. [
  548. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  549. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  550. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  551. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  552. ],
  553. [
  554. 'intitle:"é & \' è" intext:/<&>/ \'< & " >\'',
  555. '(e.title LIKE ? AND e.content ~ ? AND (e.title LIKE ? OR e.content LIKE ?) )',
  556. ['%é &amp; \' è%', '<&>', '%&lt; &amp; " &gt;%', '%&lt; &amp; " &gt;%']
  557. ],
  558. [
  559. '/^(ab|cd) [(] \\) (ef|gh)/',
  560. '((e.title ~ ? OR e.content ~ ?) )',
  561. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  562. ],
  563. [
  564. '!/^(ab|cd)/',
  565. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  566. ['^(ab|cd)', '^(ab|cd)']
  567. ],
  568. [
  569. 'intitle:/^(ab|cd)/',
  570. '(e.title ~ ? )',
  571. ['^(ab|cd)']
  572. ],
  573. [
  574. 'intext:/^(ab|cd)/',
  575. '(e.content ~ ? )',
  576. ['^(ab|cd)']
  577. ],
  578. [
  579. 'L:1 L:2',
  580. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) AND ' .
  581. 'e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )',
  582. [1, 2]
  583. ],
  584. [
  585. 'L:1,2',
  586. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?,?)) )',
  587. [1, 2]
  588. ],
  589. ];
  590. }
  591. /**
  592. * @param array<string> $values
  593. */
  594. #[DataProvider('provideDateOperators')]
  595. public function test__date_operators(string $input, string $sql, array $values): void {
  596. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  597. self::assertSame(trim($sql), trim($filterSearch));
  598. self::assertSame($values, $filterValues);
  599. }
  600. /** @return list<list<mixed>> */
  601. public static function provideDateOperators(): array {
  602. return [
  603. // Basic date operator tests
  604. [
  605. 'date:2007-03-01/2008-05-11',
  606. '(e.id >= ? AND e.id <= ? )',
  607. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  608. ],
  609. [
  610. 'date:2007-03-01/',
  611. '(e.id >= ? )',
  612. [strtotime('2007-03-01T00:00:00Z') . '000000'],
  613. ],
  614. [
  615. 'date:/2008-05-11',
  616. '(e.id <= ? )',
  617. [strtotime('2008-05-11T23:59:59Z') . '000000'],
  618. ],
  619. // Basic pubdate operator tests
  620. [
  621. 'pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  622. '(e.date >= ? AND e.date <= ? )',
  623. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  624. ],
  625. [
  626. 'pubdate:2007-03-01/',
  627. '(e.date >= ? )',
  628. [strtotime('2007-03-01T00:00:00Z')],
  629. ],
  630. [
  631. 'pubdate:/2008-05-11',
  632. '(e.date <= ? )',
  633. [strtotime('2008-05-11T23:59:59Z')],
  634. ],
  635. // Basic userdate operator tests
  636. [
  637. 'userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  638. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? )',
  639. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  640. ],
  641. [
  642. 'userdate:2007-03-01/',
  643. '(e.`lastUserModified` >= ? )',
  644. [strtotime('2007-03-01T00:00:00Z')],
  645. ],
  646. [
  647. 'userdate:/2008-05-11',
  648. '(e.`lastUserModified` <= ? )',
  649. [strtotime('2008-05-11T23:59:59Z')],
  650. ],
  651. // Negative date operator tests
  652. [
  653. '-date:2007-03-01/2008-05-11',
  654. '((e.id < ? OR e.id > ?) )',
  655. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  656. ],
  657. [
  658. '!pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  659. '((e.date < ? OR e.date > ?) )',
  660. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  661. ],
  662. [
  663. '!userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  664. '((e.`lastUserModified` < ? OR e.`lastUserModified` > ?) )',
  665. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  666. ],
  667. // Combined date operators
  668. [
  669. 'date:2007-03-01/ pubdate:/2008-05-11',
  670. '(e.id >= ? AND e.date <= ? )',
  671. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z')],
  672. ],
  673. [
  674. 'pubdate:2007-03-01/ userdate:/2008-05-11',
  675. '(e.date >= ? AND e.`lastUserModified` <= ? )',
  676. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  677. ],
  678. [
  679. 'date:2007-03-01/ userdate:2007-06-01/',
  680. '(e.id >= ? AND e.`lastUserModified` >= ? )',
  681. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2007-06-01T00:00:00Z')],
  682. ],
  683. // Complex combinations with other operators
  684. [
  685. 'intitle:test date:2007-03-01/ pubdate:/2008-05-11',
  686. '(e.id >= ? AND e.date <= ? AND e.title LIKE ? )',
  687. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z'), '%test%'],
  688. ],
  689. [
  690. 'author:john userdate:2007-03-01/2008-05-11',
  691. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? AND e.author LIKE ? )',
  692. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z'), '%john%'],
  693. ],
  694. // Mixed positive and negative date operators
  695. [
  696. 'date:2007-03-01/ !pubdate:2008-01-01/2008-05-11',
  697. '(e.id >= ? AND (e.date < ? OR e.date > ?) )',
  698. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-01-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  699. ],
  700. ];
  701. }
  702. /**
  703. * @dataProvider provideRegexPostreSQL
  704. * @param array<string> $values
  705. */
  706. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  707. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  708. self::assertSame(trim($sql), trim($filterSearch));
  709. self::assertSame($values, $filterValues);
  710. }
  711. /** @return list<list<mixed>> */
  712. public static function provideRegexPostreSQL(): array {
  713. return [
  714. [
  715. 'intitle:/^ab$/',
  716. '(e.title ~ ? )',
  717. ['^ab$']
  718. ],
  719. [
  720. 'intitle:/^ab$/i',
  721. '(e.title ~* ? )',
  722. ['^ab$']
  723. ],
  724. [
  725. 'intitle:/^ab$/m',
  726. '(e.title ~ ? )',
  727. ['(?m)^ab$']
  728. ],
  729. [
  730. 'intitle:/^ab\\M/',
  731. '(e.title ~ ? )',
  732. ['^ab\\M']
  733. ],
  734. [
  735. 'intext:/^ab\\M/',
  736. '(e.content ~ ? )',
  737. ['^ab\\M']
  738. ],
  739. [
  740. 'intitle:/\\b\\d+/',
  741. '(e.title ~ ? )',
  742. ['\\y\\d+']
  743. ],
  744. [
  745. 'author:/^ab$/',
  746. "(REPLACE(e.author, ';', '\n') ~ ? )",
  747. ['^ab$']
  748. ],
  749. [
  750. 'inurl:/^ab$/',
  751. '(e.link ~ ? )',
  752. ['^ab$']
  753. ],
  754. [
  755. '/^ab$/',
  756. '((e.title ~ ? OR e.content ~ ?) )',
  757. ['^ab$', '^ab$']
  758. ],
  759. [
  760. '!/^ab$/',
  761. '(NOT e.title ~ ? AND NOT e.content ~ ? )',
  762. ['^ab$', '^ab$']
  763. ],
  764. [
  765. '#/^a(b|c)$/im',
  766. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ? )",
  767. ['(?m)^a(b|c)$']
  768. ],
  769. [ // Not a regex
  770. 'inurl:https://example.net/test/',
  771. '(e.link LIKE ? )',
  772. ['%https://example.net/test/%']
  773. ],
  774. [ // Not a regex
  775. 'https://example.net/test/',
  776. '((e.title LIKE ? OR e.content LIKE ?) )',
  777. ['%https://example.net/test/%', '%https://example.net/test/%']
  778. ],
  779. [ // Not a regex
  780. "author:'/u/Alice'",
  781. "(e.author LIKE ? )",
  782. ['%/u/Alice%'],
  783. ],
  784. [ // Regex with literal 'or'
  785. 'intitle:/^A or B/i',
  786. '(e.title ~* ? )',
  787. ['^A or B']
  788. ],
  789. [ // Regex with literal 'OR'
  790. 'intitle:/^A B OR C D/i OR intitle:/^A B OR C D/i',
  791. '(e.title ~* ? ) OR (e.title ~* ? )',
  792. ['^A B OR C D', '^A B OR C D']
  793. ],
  794. [ // Quote with literal 'OR'
  795. 'intitle:"A B OR C D" OR intitle:"E or F"',
  796. '(e.title LIKE ? ) OR (e.title LIKE ? )',
  797. ['%A B OR C D%', '%E or F%']
  798. ],
  799. ];
  800. }
  801. /**
  802. * @dataProvider provideRegexMariaDB
  803. * @param array<string> $values
  804. */
  805. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  806. FreshRSS_DatabaseDAO::$dummyConnection = true;
  807. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  808. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  809. self::assertSame(trim($sql), trim($filterSearch));
  810. self::assertSame($values, $filterValues);
  811. }
  812. /** @return list<list<mixed>> */
  813. public static function provideRegexMariaDB(): array {
  814. return [
  815. [
  816. 'intitle:/^ab$/',
  817. "(e.title REGEXP ? )",
  818. ['(?-i)^ab$']
  819. ],
  820. [
  821. 'intitle:/^ab$/i',
  822. "(e.title REGEXP ? )",
  823. ['(?i)^ab$']
  824. ],
  825. [
  826. 'intitle:/^ab$/m',
  827. "(e.title REGEXP ? )",
  828. ['(?-i)(?m)^ab$']
  829. ],
  830. [
  831. 'intitle:/\\b\\d+/',
  832. "(e.title REGEXP ? )",
  833. ['(?-i)\\b\\d+']
  834. ],
  835. [
  836. 'intext:/^ab$/m',
  837. '(UNCOMPRESS(e.content_bin) REGEXP ?) )',
  838. ['(?-i)(?m)^ab$']
  839. ],
  840. ];
  841. }
  842. /**
  843. * @dataProvider provideRegexMySQL
  844. * @param array<string> $values
  845. */
  846. public function test__regex_mysql(string $input, string $sql, array $values): void {
  847. FreshRSS_DatabaseDAO::$dummyConnection = true;
  848. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  849. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  850. self::assertSame(trim($sql), trim($filterSearch));
  851. self::assertSame($values, $filterValues);
  852. }
  853. /** @return list<list<mixed>> */
  854. public static function provideRegexMySQL(): array {
  855. return [
  856. [
  857. 'intitle:/^ab$/',
  858. "(REGEXP_LIKE(e.title,?,'c') )",
  859. ['^ab$']
  860. ],
  861. [
  862. 'intitle:/^ab$/i',
  863. "(REGEXP_LIKE(e.title,?,'i') )",
  864. ['^ab$']
  865. ],
  866. [
  867. 'intitle:/^ab$/m',
  868. "(REGEXP_LIKE(e.title,?,'mc') )",
  869. ['^ab$']
  870. ],
  871. [
  872. 'intext:/^ab$/m',
  873. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc')) )",
  874. ['^ab$']
  875. ],
  876. ];
  877. }
  878. /**
  879. * @dataProvider provideRegexSQLite
  880. * @param array<string> $values
  881. */
  882. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  883. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  884. self::assertSame(trim($sql), trim($filterSearch));
  885. self::assertSame($values, $filterValues);
  886. }
  887. /** @return list<list<mixed>> */
  888. public static function provideRegexSQLite(): array {
  889. return [
  890. [
  891. 'intitle:/^ab$/',
  892. "(e.title REGEXP ? )",
  893. ['/^ab$/']
  894. ],
  895. [
  896. 'intitle:/^ab$/i',
  897. "(e.title REGEXP ? )",
  898. ['/^ab$/i']
  899. ],
  900. [
  901. 'intitle:/^ab$/m',
  902. "(e.title REGEXP ? )",
  903. ['/^ab$/m']
  904. ],
  905. [
  906. 'intitle:/^ab\\b/',
  907. '(e.title REGEXP ? )',
  908. ['/^ab\\b/']
  909. ],
  910. [
  911. 'intext:/^ab\\b/',
  912. '(e.content REGEXP ? )',
  913. ['/^ab\\b/']
  914. ],
  915. ];
  916. }
  917. #[DataProvider('provideToString')]
  918. public static function test__toString(string $input): void {
  919. $search = new FreshRSS_Search($input);
  920. $expected = str_replace("\n", ' ', $input);
  921. self::assertSame($expected, $search->__toString());
  922. }
  923. /**
  924. * @return array<array<string>>
  925. */
  926. public static function provideToString(): array {
  927. return [
  928. [
  929. <<<'EOD'
  930. e:1,2 f:10,11 c:20,21 L:30,31 labels:"My label,My other label"
  931. userdate:2025-01-01T00:00:00/2026-01-01T00:00:00
  932. pubdate:2025-02-01T00:00:00/2026-01-01T00:00:00
  933. date:2025-03-01T00:00:00/2026-01-01T00:00:00
  934. intitle:/<Inter&sting>/i intitle:"g ' & d"
  935. intext:/<Inter&sting>/i intext:g&d
  936. author:/Bob/ author:"/u/Alice" author:Alice
  937. inurl:/https/ inurl:example.net
  938. #/tag2/ #tag1
  939. /search_regex/i "quoted search" search
  940. -e:3,4 -f:12,13 -c:22,23 -L:32,33 -labels:"Not label,Not other label"
  941. -userdate:2025-06-01T00:00:00/2025-09-01T00:00:00
  942. -pubdate:2025
  943. -date:P30D
  944. -intitle:/Spam/i -intitle:"'bad"
  945. -intext:/Spam/i -intext:"'bad"
  946. -author:/Dave/i -author:"/u/Charlie" -author:Charlie
  947. -inurl:/ftp/ -inurl:example.com
  948. -#/tag4/ -#tag3
  949. -/not_regex/i -"not quoted" -not_search
  950. EOD
  951. ],
  952. ];
  953. }
  954. #[DataProvider('provideBooleanSearchToString')]
  955. public static function testBooleanSearchToString(string $input, string $expected): void {
  956. $search = new FreshRSS_BooleanSearch($input);
  957. self::assertSame($expected, $search->toString());
  958. }
  959. /**
  960. * @return array<array<string>>
  961. */
  962. public static function provideBooleanSearchToString(): array {
  963. return [
  964. [
  965. '((a OR b) (c OR d) -e) OR -(f g)',
  966. '((a OR b) (c OR d) (-e)) OR -(f g)',
  967. ],
  968. [
  969. '((a OR b) ((c) OR ((d))) (-e)) OR -(((f g)))',
  970. '((a OR b) (c OR d) (-e)) OR -(f g)',
  971. ],
  972. [
  973. '!((b c))',
  974. '-(b c)',
  975. ],
  976. [
  977. '(a) OR !((b c))',
  978. 'a OR -(b c)',
  979. ],
  980. [
  981. '((a) (b))',
  982. 'a b',
  983. ],
  984. [
  985. '((a) OR (b))',
  986. 'a OR b',
  987. ],
  988. [
  989. ' ( !( !( ( a ) ) ) ) ( ) ',
  990. '-(-a)',
  991. ],
  992. [
  993. '-intitle:a -inurl:b',
  994. '-intitle:a -inurl:b',
  995. ],
  996. [
  997. 'intitle:/^A or B/i',
  998. 'intitle:/^A or B/i',
  999. ],
  1000. [
  1001. 'intitle:/^A B OR C D/i',
  1002. 'intitle:/^A B OR C D/i',
  1003. ],
  1004. ];
  1005. }
  1006. /**
  1007. * @param list<array{search:string,name?:string}> $queries
  1008. */
  1009. #[DataProvider('provideBooleanSearchToStringExpansion')]
  1010. public static function testBooleanSearchToStringExpansion(array $queries, string $input,
  1011. string $expectedNotExpanded, string $expectedExpanded): void {
  1012. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  1013. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  1014. $newUserConf->queries = $queries;
  1015. FreshRSS_Context::setUserConf($newUserConf);
  1016. try {
  1017. $booleanSearch = new FreshRSS_BooleanSearch($input);
  1018. self::assertSame($expectedNotExpanded, $booleanSearch->toString(expandUserQueries: false));
  1019. self::assertSame($expectedExpanded, $booleanSearch->toString());
  1020. } finally {
  1021. FreshRSS_Context::setUserConf($previousUserConf);
  1022. }
  1023. }
  1024. /**
  1025. * @return array<string,array{0:list<array{search:string,name?:string}>,1:string,2:string,3:string}>
  1026. */
  1027. public static function provideBooleanSearchToStringExpansion(): array {
  1028. return [
  1029. 'Not found ID' => [
  1030. [
  1031. ['search' => 'author:Alice'],
  1032. ['search' => 'intitle:World'],
  1033. ],
  1034. 'S:3 S:4,5 ',
  1035. 'S:3 S:4,5',
  1036. '',
  1037. ],
  1038. 'Not found name' => [
  1039. [
  1040. ['search' => 'author:Alice', 'name' => 'First'],
  1041. ['search' => 'intitle:World', 'name' => 'Second'],
  1042. ],
  1043. 'search:Third ',
  1044. 'search:Third',
  1045. '',
  1046. ],
  1047. 'Found IDs' => [
  1048. [
  1049. ['search' => 'author:Alice', 'name' => 'First'],
  1050. ['search' => 'intitle:World', 'name' => 'Second'],
  1051. ],
  1052. 'S:0,1 ',
  1053. 'S:0,1',
  1054. 'author:Alice OR intitle:World',
  1055. ],
  1056. 'Found names' => [
  1057. [
  1058. ['search' => 'author:Alice', 'name' => 'First'],
  1059. ['search' => 'intitle:World', 'name' => 'Second'],
  1060. ],
  1061. 'search:First search:Second ',
  1062. 'search:First search:Second',
  1063. 'author:Alice intitle:World',
  1064. ],
  1065. ];
  1066. }
  1067. #[DataProvider('provideHasSameOperators')]
  1068. public function testHasSameOperators(string $input1, string $input2, bool $expected): void {
  1069. $search1 = new FreshRSS_Search($input1);
  1070. $search2 = new FreshRSS_Search($input2);
  1071. self::assertSame($expected, $search1->hasSameOperators($search2));
  1072. }
  1073. /**
  1074. * @return array<array{string,string,bool}>
  1075. */
  1076. public static function provideHasSameOperators(): array {
  1077. return [
  1078. ['', '', true],
  1079. ['intitle:a intext:b', 'intitle:c intext:d', true],
  1080. ['intitle:a intext:b', 'intitle:c inurl:d', false],
  1081. ];
  1082. }
  1083. #[DataProvider('provideBooleanSearchEnforce')]
  1084. public function testBooleanSearchEnforce(string $initialInput, string $enforceInput, string $expectedOutput): void {
  1085. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1086. $searchToEnforce = new FreshRSS_Search($enforceInput);
  1087. $newBooleanSearch = $booleanSearch->enforce($searchToEnforce);
  1088. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1089. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1090. }
  1091. /**
  1092. * @return array<array{string,string,string}>
  1093. */
  1094. public static function provideBooleanSearchEnforce(): array {
  1095. return [
  1096. ['', '', ''],
  1097. ['', 'intitle:b', 'intitle:b'],
  1098. ['intitle:a', '', 'intitle:a'],
  1099. ['intitle:a', 'intitle:b', 'intitle:b'],
  1100. ['a', 'intitle:b', 'intitle:b a'],
  1101. ['intitle:a intext:a', 'intitle:b', 'intitle:b intext:a'],
  1102. ['intitle:a inurl:a', 'intitle:b', 'intitle:b inurl:a'],
  1103. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:b (intitle:a OR inurl:a)'],
  1104. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', 'intitle:b (inurl:a intitle:c)'],
  1105. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', 'intitle:b (inurl:a OR intitle:c)'],
  1106. ['(intitle:a) (inurl:a)', 'intitle:b', 'intitle:b inurl:a'],
  1107. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a intitle:b'],
  1108. ['(a b) OR (c d)', 'e', 'e ((a b) OR (c d))'],
  1109. ['(a b) (c d)', 'e', 'e ((a b) (c d))'],
  1110. ['(a b)', 'e', 'e (a b)'],
  1111. ['date:2024/', 'date:/2025', 'date:/2025'],
  1112. ['a', 'date:/2025', 'date:/2025 a'],
  1113. ];
  1114. }
  1115. #[DataProvider('provideBooleanSearchRemove')]
  1116. public function testBooleanSearchRemove(string $initialInput, string $removeInput, string $expectedOutput): void {
  1117. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1118. $searchToRemove = new FreshRSS_Search($removeInput);
  1119. $newBooleanSearch = $booleanSearch->remove($searchToRemove);
  1120. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1121. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1122. }
  1123. /**
  1124. * @return array<array{string,string,string}>
  1125. */
  1126. public static function provideBooleanSearchRemove(): array {
  1127. return [
  1128. ['', 'intitle:b', ''],
  1129. ['intitle:a', 'intitle:b', ''],
  1130. ['intitle:a intext:a', 'intitle:b', 'intext:a'],
  1131. ['intitle:a inurl:a', 'intitle:b', 'inurl:a'],
  1132. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:a OR inurl:a'],
  1133. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', '(inurl:a intitle:c)'],
  1134. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', '(inurl:a OR intitle:c)'],
  1135. ['(intitle:a) (inurl:a)', 'intitle:b', 'inurl:a'],
  1136. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a'],
  1137. ['e ((a b) OR (c d))', 'e', '((a b) OR (c d))'],
  1138. ['e ((a b) (c d))', 'e', '((a b) (c d))'],
  1139. ['date:2024/', 'date:/2025', ''],
  1140. ['date:2024/ a', 'date:/2025', 'a'],
  1141. ];
  1142. }
  1143. }