4
0

SearchTest.php 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372
  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. #[\Override]
  7. protected function setUp(): void {
  8. if (!FreshRSS_Context::hasSystemConf()) {
  9. FreshRSS_Context::initSystem();
  10. }
  11. }
  12. #[DataProvider('provideEmptyInput')]
  13. public static function test__construct_whenInputIsEmpty_getsOnlyNullValues(string $input): void {
  14. $search = new FreshRSS_Search($input);
  15. self::assertSame('', $search->__toString());
  16. self::assertNull($search->getIntitle());
  17. self::assertNull($search->getMinDate());
  18. self::assertNull($search->getMaxDate());
  19. self::assertNull($search->getMinPubdate());
  20. self::assertNull($search->getMaxPubdate());
  21. self::assertNull($search->getAuthor());
  22. self::assertNull($search->getTags());
  23. self::assertNull($search->getSearch());
  24. }
  25. /**
  26. * Return an array of values for the search object.
  27. * Here is the description of the values
  28. * @return array{array{''},array{' '}}
  29. */
  30. public static function provideEmptyInput(): array {
  31. return [
  32. [''],
  33. [' '],
  34. ];
  35. }
  36. /**
  37. * @param array<string>|null $intitle_value
  38. * @param array<string>|null $search_value
  39. */
  40. #[DataProvider('provideIntitleSearch')]
  41. public static function test__construct_whenInputContainsIntitle_setsIntitleProperty(string $input, ?array $intitle_value, ?array $search_value): void {
  42. $search = new FreshRSS_Search($input);
  43. self::assertSame($intitle_value, $search->getIntitle());
  44. self::assertSame($search_value, $search->getSearch());
  45. }
  46. /**
  47. * @return list<list<mixed>>
  48. */
  49. public static function provideIntitleSearch(): array {
  50. return [
  51. ['intitle:word1', ['word1'], null],
  52. ['intitle:word1-word2', ['word1-word2'], null],
  53. ['intitle:word1 word2', ['word1'], ['word2']],
  54. ['intitle:"word1 word2"', ['word1 word2'], null],
  55. ["intitle:'word1 word2'", ['word1 word2'], null],
  56. ['word1 intitle:word2', ['word2'], ['word1']],
  57. ['word1 intitle:word2 word3', ['word2'], ['word1', 'word3']],
  58. ['word1 intitle:"word2 word3"', ['word2 word3'], ['word1']],
  59. ["word1 intitle:'word2 word3'", ['word2 word3'], ['word1']],
  60. ['intitle:word1 intitle:word2', ['word1', 'word2'], null],
  61. ['intitle: word1 word2', null, ['word1', 'word2']],
  62. ['intitle:123', ['123'], null],
  63. ['intitle:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  64. ["intitle:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  65. ['intitle:"word1 word2\' word3"', ["word1 word2' word3"], null],
  66. ["intitle:'word1 word2\" word3'", ['word1 word2" word3'], null],
  67. ['intitle:"< & >"', ['&lt; &amp; &gt;'], null],
  68. ["intitle:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  69. ['intitle:word1+word2', ['word1+word2'], null],
  70. ];
  71. }
  72. /**
  73. * @param array<string>|null $intext_value
  74. * @param array<string>|null $search_value
  75. */
  76. #[DataProvider('provideIntextSearch')]
  77. public static function test__construct_whenInputContainsIntext(string $input, ?array $intext_value, ?array $search_value): void {
  78. $search = new FreshRSS_Search($input);
  79. self::assertSame($intext_value, $search->getIntext());
  80. self::assertSame($search_value, $search->getSearch());
  81. }
  82. /**
  83. * @return list<list<mixed>>
  84. */
  85. public static function provideIntextSearch(): array {
  86. return [
  87. ['intext:word1', ['word1'], null],
  88. ['intext:"word1 word2"', ['word1 word2'], null],
  89. ];
  90. }
  91. /**
  92. * @param array<string>|null $author_value
  93. * @param array<string>|null $search_value
  94. */
  95. #[DataProvider('provideAuthorSearch')]
  96. public static function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void {
  97. $search = new FreshRSS_Search($input);
  98. self::assertSame($author_value, $search->getAuthor());
  99. self::assertSame($search_value, $search->getSearch());
  100. }
  101. /**
  102. * @return list<list<mixed>>
  103. */
  104. public static function provideAuthorSearch(): array {
  105. return [
  106. ['author:word1', ['word1'], null],
  107. ['author:word1-word2', ['word1-word2'], null],
  108. ['author:word1 word2', ['word1'], ['word2']],
  109. ['author:"word1 word2"', ['word1 word2'], null],
  110. ["author:'word1 word2'", ['word1 word2'], null],
  111. ['word1 author:word2', ['word2'], ['word1']],
  112. ['word1 author:word2 word3', ['word2'], ['word1', 'word3']],
  113. ['word1 author:"word2 word3"', ['word2 word3'], ['word1']],
  114. ["word1 author:'word2 word3'", ['word2 word3'], ['word1']],
  115. ['author:word1 author:word2', ['word1', 'word2'], null],
  116. ['author: word1 word2', null, ['word1', 'word2']],
  117. ['author:123', ['123'], null],
  118. ['author:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  119. ["author:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  120. ['author:"word1 word2\' word3"', ["word1 word2' word3"], null],
  121. ["author:'word1 word2\" word3'", ['word1 word2" word3'], null],
  122. ["author:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  123. ['author:word1+word2', ['word1+word2'], null],
  124. ];
  125. }
  126. /**
  127. * @param array<string>|null $inurl_value
  128. * @param array<string>|null $search_value
  129. */
  130. #[DataProvider('provideInurlSearch')]
  131. public static function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void {
  132. $search = new FreshRSS_Search($input);
  133. self::assertSame($inurl_value, $search->getInurl());
  134. self::assertSame($search_value, $search->getSearch());
  135. }
  136. /**
  137. * @return list<list<mixed>>
  138. */
  139. public static function provideInurlSearch(): array {
  140. return [
  141. ['inurl:word1', ['word1'], null],
  142. ['inurl: word1', null, ['word1']],
  143. ['inurl:123', ['123'], null],
  144. ['inurl:word1 word2', ['word1'], ['word2']],
  145. ['inurl:"word1 word2"', ['word1 word2'], null],
  146. ['inurl:word1 word2 inurl:word3', ['word1', 'word3'], ['word2']],
  147. ["inurl:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  148. ['inurl:word1+word2', ['word1+word2'], null],
  149. ];
  150. }
  151. #[DataProvider('provideDateSearch')]
  152. public static function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void {
  153. $search = new FreshRSS_Search($input);
  154. self::assertSame($min_date_value, $search->getMinDate());
  155. self::assertSame($max_date_value, $search->getMaxDate());
  156. }
  157. /**
  158. * @return list<list<mixed>>
  159. */
  160. public static function provideDateSearch(): array {
  161. return [
  162. ['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  163. ['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  164. ['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  165. ['date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  166. ['date:2007-03-01/', strtotime('2007-03-01'), null],
  167. ['date:/2008-05-11', null, strtotime('2008-05-12') - 1],
  168. ];
  169. }
  170. #[DataProvider('providePubdateSearch')]
  171. public static function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void {
  172. $search = new FreshRSS_Search($input);
  173. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  174. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  175. }
  176. /**
  177. * @return list<list<mixed>>
  178. */
  179. public static function providePubdateSearch(): array {
  180. return [
  181. ['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  182. ['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  183. ['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  184. ['pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  185. ['pubdate:2007-03-01/', strtotime('2007-03-01'), null],
  186. ['pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  187. ];
  188. }
  189. #[DataProvider('provideModifiedDateSearch')]
  190. public static function test__construct_whenInputContainsModifiedDate(string $input, ?int $min_modified_value, ?int $max_modified_value): void {
  191. $search = new FreshRSS_Search($input);
  192. self::assertSame($min_modified_value, $search->getMinModifiedDate());
  193. self::assertSame($max_modified_value, $search->getMaxModifiedDate());
  194. }
  195. /**
  196. * @return list<list<mixed>>
  197. */
  198. public static function provideModifiedDateSearch(): array {
  199. return [
  200. ['mdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  201. ['mdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  202. ];
  203. }
  204. #[DataProvider('provideUserdateSearch')]
  205. public static function test__construct_whenInputContainsUserdate(string $input, ?int $min_userdate_value, ?int $max_userdate_value): void {
  206. $search = new FreshRSS_Search($input);
  207. self::assertSame($min_userdate_value, $search->getMinUserdate());
  208. self::assertSame($max_userdate_value, $search->getMaxUserdate());
  209. }
  210. /**
  211. * @return list<list<mixed>>
  212. */
  213. public static function provideUserdateSearch(): array {
  214. return [
  215. ['userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  216. ['userdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  217. ];
  218. }
  219. /**
  220. * @param array<string>|null $tags_value
  221. * @param array<string>|null $search_value
  222. */
  223. #[DataProvider('provideTagsSearch')]
  224. public static function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  225. $search = new FreshRSS_Search($input);
  226. self::assertSame($tags_value, $search->getTags());
  227. self::assertSame($search_value, $search->getSearch());
  228. }
  229. /**
  230. * @return list<list<string|list<string>|null>>
  231. */
  232. public static function provideTagsSearch(): array {
  233. return [
  234. ['#word1', ['word1'], null],
  235. ['# word1', null, ['#', 'word1']],
  236. ['#123', ['123'], null],
  237. ['#word1 word2', ['word1'], ['word2']],
  238. ['#"word1 word2"', ['word1 word2'], null],
  239. ['#word1 #word2', ['word1', 'word2'], null],
  240. ["#word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  241. ['#word1+word2', ['word1 word2'], null]
  242. ];
  243. }
  244. /**
  245. * @param list<array{search:string,name?:string}> $queries
  246. * @param array{0:string,1:list<string|int>} $expectedResult
  247. */
  248. #[DataProvider('provideSavedQueriesExpansion')]
  249. public static function test__construct_whenInputContainsSavedQueries_expandsSavedSearches(array $queries, string $input,
  250. array $expectedResult, string $expectedToString): void {
  251. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  252. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  253. $newUserConf->queries = $queries;
  254. FreshRSS_Context::setUserConf($newUserConf);
  255. try {
  256. $search = new FreshRSS_BooleanSearch($input);
  257. [$actualValues, $actualSql] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $search);
  258. self::assertSame($expectedResult[0], trim($actualSql));
  259. self::assertSame($expectedResult[1], $actualValues);
  260. self::assertSame($expectedToString, $search->toString(expandUserQueries: false));
  261. } finally {
  262. FreshRSS_Context::setUserConf($previousUserConf);
  263. }
  264. }
  265. /**
  266. * @return array<string,array{0:list<array{search:string}>,1:string,2:array{0:string,1:list<string|int>},3:string}>
  267. */
  268. public static function provideSavedQueriesExpansion(): array {
  269. return [
  270. 'not found ID' => [
  271. [
  272. ['search' => 'author:Alice'],
  273. ['search' => 'intitle:World'],
  274. ],
  275. 'S:3',
  276. [
  277. '',
  278. [],
  279. ],
  280. 'S:3',
  281. ],
  282. 'not found name' => [
  283. [
  284. ['search' => 'author:Alice', 'name' => 'First'],
  285. ['search' => 'intitle:World', 'name' => 'Second'],
  286. ],
  287. 'search:Third',
  288. [
  289. '',
  290. [],
  291. ],
  292. 'search:Third',
  293. ],
  294. 'expanded single group name' => [
  295. [
  296. ['search' => 'author:Alice', 'name' => 'First'],
  297. ['search' => 'intitle:World', 'name' => 'Second'],
  298. ],
  299. 'search:First OR search:Second',
  300. [
  301. '((e.author LIKE ?)) OR ((e.title LIKE ?))',
  302. ['%Alice%', '%World%'],
  303. ],
  304. 'search:First OR search:Second',
  305. ],
  306. 'expanded single group name quotes' => [
  307. [
  308. ['search' => 'author:Alice', 'name' => 'A First'],
  309. ['search' => 'intitle:World', 'name' => 'A Second'],
  310. ],
  311. 'search:"A First" OR search:\'A Second\'',
  312. [
  313. '((e.author LIKE ?)) OR ((e.title LIKE ?))',
  314. ['%Alice%', '%World%'],
  315. ],
  316. '(search:"A First") OR (search:"A Second")',
  317. ],
  318. 'expanded single group name quotes special characters' => [
  319. [
  320. ['search' => 'author:Alice', 'name' => 'A or B'],
  321. ['search' => 'intitle:World', 'name' => '(C OR D)'],
  322. ],
  323. 'search:"A or B" OR search:\'(C OR D)\'',
  324. [
  325. '((e.author LIKE ?)) OR ((e.title LIKE ?))',
  326. ['%Alice%', '%World%'],
  327. ],
  328. '(search:"A or B") OR (search:"(C OR D)")',
  329. ],
  330. 'separate groups with AND' => [
  331. [
  332. ['search' => 'author:Alice'],
  333. ['search' => 'intitle:World'],
  334. ['search' => 'inurl:Example'],
  335. ['search' => 'author:Bob'],
  336. ],
  337. 'S:0,1 S:2,3,5',
  338. [
  339. '(((e.author LIKE ?)) OR ((e.title LIKE ?))) AND (((e.link LIKE ?)) OR ((e.author LIKE ?)))',
  340. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  341. ],
  342. 'S:0,1 S:2,3,5',
  343. ],
  344. 'separate groups with OR' => [
  345. [
  346. ['search' => 'author:Alice'],
  347. ['search' => 'intitle:World'],
  348. ['search' => 'inurl:Example'],
  349. ['search' => 'author:Bob'],
  350. ],
  351. 'S:0,1 OR S:2,3,5',
  352. [
  353. '(((e.author LIKE ?)) OR ((e.title LIKE ?))) OR (((e.link LIKE ?)) OR ((e.author LIKE ?)))',
  354. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  355. ],
  356. 'S:0,1 OR S:2,3,5',
  357. ],
  358. 'mixed with other clauses' => [
  359. [
  360. ['search' => 'author:Alice'],
  361. ['search' => 'intitle:World'],
  362. ],
  363. 'date:2025-10 intitle:Hello S:0,1',
  364. [
  365. '((e.id >= ? AND e.id <= ? AND e.title LIKE ?)) AND (((e.author LIKE ?)) OR ((e.title LIKE ?)))',
  366. [strtotime('2025-10-01') . '000000', (strtotime('2025-11-01') - 1) . '000000', '%Hello%', '%Alice%', '%World%'],
  367. ],
  368. 'date:2025-10 intitle:Hello S:0,1',
  369. ],
  370. ];
  371. }
  372. /**
  373. * @param array<string>|null $author_value
  374. * @param array<string> $intitle_value
  375. * @param array<string>|null $inurl_value
  376. * @param array<string>|null $tags_value
  377. * @param array<string>|null $search_value
  378. */
  379. #[DataProvider('provideMultipleSearch')]
  380. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  381. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  382. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  383. $search = new FreshRSS_Search($input);
  384. self::assertSame($author_value, $search->getAuthor());
  385. self::assertSame($min_date_value, $search->getMinDate());
  386. self::assertSame($max_date_value, $search->getMaxDate());
  387. self::assertSame($intitle_value, $search->getIntitle());
  388. self::assertSame($inurl_value, $search->getInurl());
  389. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  390. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  391. self::assertSame($tags_value, $search->getTags());
  392. self::assertSame($search_value, $search->getSearch());
  393. }
  394. /** @return list<list<mixed>> */
  395. public static function provideMultipleSearch(): array {
  396. return [
  397. [
  398. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  399. ['word1'],
  400. strtotime('2007-03-01'),
  401. strtotime('2008-05-12') - 1,
  402. ['word2'],
  403. ['word3'],
  404. strtotime('2007-03-01'),
  405. strtotime('2008-05-12') - 1,
  406. ['word4', 'word5'],
  407. null
  408. ],
  409. [
  410. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  411. ['word1'],
  412. strtotime('2007-03-01'),
  413. strtotime('2008-05-12') - 1,
  414. ['word2'],
  415. ['word3'],
  416. strtotime('2007-03-01'),
  417. strtotime('2008-05-12') - 1,
  418. ['word4', 'word5'],
  419. ['word6']
  420. ],
  421. [
  422. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  423. ['word1'],
  424. strtotime('2007-03-01'),
  425. strtotime('2008-05-12') - 1,
  426. ['word2'],
  427. ['word3'],
  428. strtotime('2007-03-01'),
  429. strtotime('2008-05-12') - 1,
  430. ['word4', 'word5'],
  431. ['word6', 'word7']
  432. ],
  433. [
  434. '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',
  435. ['word1'],
  436. strtotime('2007-03-01'),
  437. strtotime('2008-05-12') - 1,
  438. ['word2'],
  439. ['word3'],
  440. strtotime('2007-03-01'),
  441. strtotime('2008-05-12') - 1,
  442. ['word4', 'word5'],
  443. ['word7 word8', 'word6']
  444. ]
  445. ];
  446. }
  447. #[DataProvider('provideAddOrParentheses')]
  448. public static function test__addOrParentheses(string $input, string $output): void {
  449. self::assertSame($output, FreshRSS_BooleanSearch::addOrParentheses($input));
  450. }
  451. /** @return list<list{string,string}> */
  452. public static function provideAddOrParentheses(): array {
  453. return [
  454. ['ab', 'ab'],
  455. ['ab cd', 'ab cd'],
  456. ['!ab -cd', '!ab -cd'],
  457. ['ab OR cd', '(ab) OR (cd)'],
  458. ['!ab OR -cd', '(!ab) OR (-cd)'],
  459. ['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],
  460. ['ab (!cd)', 'ab (!cd)'],
  461. ['"ab" (!"cd")', '"ab" (!"cd")'],
  462. ];
  463. }
  464. #[DataProvider('provideconsistentOrParentheses')]
  465. public static function test__consistentOrParentheses(string $input, string $output): void {
  466. self::assertSame($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  467. }
  468. /** @return list<list{string,string}> */
  469. public static function provideconsistentOrParentheses(): array {
  470. return [
  471. ['ab cd ef', 'ab cd ef'],
  472. ['(ab cd ef)', '(ab cd ef)'],
  473. ['("ab cd" ef)', '("ab cd" ef)'],
  474. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  475. ['ab (!cd)', 'ab (!cd)'],
  476. ['ab !(cd)', 'ab !(cd)'],
  477. ['(ab) -(cd)', '(ab) -(cd)'],
  478. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  479. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  480. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  481. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  482. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  483. ['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],
  484. ['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],
  485. ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],
  486. ];
  487. }
  488. /**
  489. * @param array<string> $values
  490. */
  491. #[DataProvider('provideParentheses')]
  492. public function test__parentheses(string $input, string $sql, array $values): void {
  493. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  494. self::assertSame(trim($sql), trim($filterSearch));
  495. self::assertSame($values, $filterValues);
  496. }
  497. /** @return list<list<mixed>> */
  498. public static function provideParentheses(): array {
  499. return [
  500. [
  501. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  502. ' ((e.id_feed IN (?))) AND ((e.id_feed IN (?)) OR (e.id_feed IN (?)) OR (e.id_feed IN (?))) AND' .
  503. ' (((e.id_feed IN (?))) OR ((e.id_feed IN (?)) OR (e.id_feed IN (?)))) ',
  504. [1, 2, 3, 4, 5, 6, 7]
  505. ],
  506. [
  507. 'c:1 OR c:2,3',
  508. ' (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 (?,?))) ',
  509. [1, 2, 3]
  510. ],
  511. [
  512. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  513. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?))) OR ((e.author LIKE ? AND e.link LIKE ?)) OR" .
  514. ' ((e.id_feed IN (?) AND e.title LIKE ?)) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)))) ',
  515. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 12]
  516. ],
  517. [
  518. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  519. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?))) AND" .
  520. ' ((e.author LIKE ? AND e.link LIKE ?)) AND' .
  521. ' ((e.id_feed IN (?) AND e.title LIKE ?)) AND' .
  522. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)))) ',
  523. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 'Bleu']
  524. ],
  525. [
  526. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  527. ' NOT (((e.author LIKE ? AND e.title LIKE ?)) OR ((e.author LIKE ? AND e.title LIKE ?))) ',
  528. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  529. ],
  530. [
  531. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  532. ' ((e.author LIKE ? AND e.title LIKE ?)) AND NOT ((e.author LIKE ? AND e.title LIKE ?)) ',
  533. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  534. ],
  535. [
  536. 'intitle:"(test)"',
  537. '(e.title LIKE ?)',
  538. ['%(test)%'],
  539. ],
  540. [
  541. 'intitle:\'"hello world"\'',
  542. '(e.title LIKE ?)',
  543. ['%"hello world"%'],
  544. ],
  545. [
  546. 'intext:\'"hello world"\'',
  547. '(e.content LIKE ?)',
  548. ['%"hello world"%'],
  549. ],
  550. [
  551. '(ab) OR (cd) OR (ef)',
  552. '(((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?)))',
  553. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  554. ],
  555. [
  556. '("plain or text") OR (cd)',
  557. '(((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?)))',
  558. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  559. ],
  560. [
  561. '"plain or text" OR cd',
  562. '((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?))',
  563. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  564. ],
  565. [
  566. '"plain OR text" OR cd',
  567. '((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)) ',
  568. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  569. ],
  570. [
  571. 'ab OR cd OR (ef)',
  572. '(((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?))) ',
  573. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  574. ],
  575. [
  576. 'ab OR cd OR ef',
  577. '((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?))',
  578. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  579. ],
  580. [
  581. '(ab) cd OR ef OR (gh)',
  582. '(((e.title LIKE ? OR e.content LIKE ?))) AND (((e.title LIKE ? OR e.content LIKE ?))) ' .
  583. 'OR (((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?)))',
  584. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  585. ],
  586. [
  587. '(ab) OR cd OR ef OR (gh)',
  588. '(((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?))) ' .
  589. 'OR (((e.title LIKE ? OR e.content LIKE ?))) OR (((e.title LIKE ? OR e.content LIKE ?)))',
  590. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  591. ],
  592. [
  593. 'ab OR (!(cd OR ef))',
  594. '(((e.title LIKE ? OR e.content LIKE ?))) OR (NOT (((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?))))',
  595. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  596. ],
  597. [
  598. 'ab !(cd OR ef)',
  599. '(((e.title LIKE ? OR e.content LIKE ?))) AND NOT (((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)))',
  600. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  601. ],
  602. [
  603. 'ab OR !(cd OR ef)',
  604. '(((e.title LIKE ? OR e.content LIKE ?))) OR NOT (((e.title LIKE ? OR e.content LIKE ?)) OR ((e.title LIKE ? OR e.content LIKE ?)))',
  605. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  606. ],
  607. [
  608. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  609. '((((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 ?))) ' .
  610. '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 ?)))',
  611. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  612. ],
  613. [
  614. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  615. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?))) AND (((e.title LIKE ? OR e.content LIKE ?))) ' .
  616. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ?))',
  617. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  618. ],
  619. [
  620. 'intitle:"é & \' è" intext:/<&>/ \'< & " >\'',
  621. '(e.title LIKE ? AND e.content ~ ? AND (e.title LIKE ? OR e.content LIKE ?))',
  622. ['%é &amp; \' è%', '<&>', '%&lt; &amp; " &gt;%', '%&lt; &amp; " &gt;%']
  623. ],
  624. [
  625. '/^(ab|cd) [(] \\) (ef|gh)/',
  626. '((e.title ~ ? OR e.content ~ ?))',
  627. ['^(ab|cd) [(] \\) (ef|gh)', '^(ab|cd) [(] \\) (ef|gh)']
  628. ],
  629. [
  630. '!/^(ab|cd)/',
  631. '(NOT e.title ~ ? AND NOT e.content ~ ?)',
  632. ['^(ab|cd)', '^(ab|cd)']
  633. ],
  634. [
  635. 'intitle:/^(ab|cd)/',
  636. '(e.title ~ ?)',
  637. ['^(ab|cd)']
  638. ],
  639. [
  640. 'intext:/^(ab|cd)/',
  641. '(e.content ~ ?)',
  642. ['^(ab|cd)']
  643. ],
  644. [
  645. 'L:1 L:2',
  646. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) AND ' .
  647. 'e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)))',
  648. [1, 2]
  649. ],
  650. [
  651. 'L:1,2',
  652. '(e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?,?)))',
  653. [1, 2]
  654. ],
  655. ];
  656. }
  657. public function test__add_single_search_combines_conditions_with_and(): void {
  658. $startTime = strtotime('2026-02-21T12:00:00Z');
  659. $searches = new FreshRSS_BooleanSearch('');
  660. $search = new FreshRSS_Search('');
  661. $search->setMinDate($startTime);
  662. $search->setMinModifiedDate($startTime);
  663. $searches->add($search);
  664. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $searches);
  665. $filterSearch = preg_replace('/\s+/', ' ', trim($filterSearch)) ?? '';
  666. self::assertSame('(e.id >= ? AND e.`lastModified` >= ?)', $filterSearch);
  667. self::assertSame([$startTime . '000000', $startTime], $filterValues);
  668. }
  669. public function test__add_multiple_searches_combines_conditions_with_or(): void {
  670. $startTime = strtotime('2026-02-21T12:00:00Z');
  671. $searches = new FreshRSS_BooleanSearch('');
  672. $search = new FreshRSS_Search('');
  673. $search->setMinDate($startTime);
  674. $searches->add($search);
  675. $search = new FreshRSS_Search('');
  676. $search->setMinModifiedDate($startTime);
  677. $searches->add($search);
  678. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $searches);
  679. $filterSearch = preg_replace('/\s+/', ' ', trim($filterSearch)) ?? '';
  680. self::assertSame('(e.id >= ?) OR (e.`lastModified` >= ?)', $filterSearch);
  681. self::assertSame([$startTime . '000000', $startTime], $filterValues);
  682. }
  683. /**
  684. * @param array<string> $values
  685. */
  686. #[DataProvider('provideDateOperators')]
  687. public function test__date_operators(string $input, string $sql, array $values): void {
  688. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  689. self::assertSame(trim($sql), trim($filterSearch));
  690. self::assertSame($values, $filterValues);
  691. }
  692. /** @return list<list<mixed>> */
  693. public static function provideDateOperators(): array {
  694. return [
  695. // Basic date operator tests
  696. [
  697. 'date:2007-03-01/2008-05-11',
  698. '(e.id >= ? AND e.id <= ?)',
  699. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  700. ],
  701. [
  702. 'date:2007-03-01/',
  703. '(e.id >= ?)',
  704. [strtotime('2007-03-01T00:00:00Z') . '000000'],
  705. ],
  706. [
  707. 'date:/2008-05-11',
  708. '(e.id <= ?)',
  709. [strtotime('2008-05-11T23:59:59Z') . '000000'],
  710. ],
  711. // Basic pubdate operator tests
  712. [
  713. 'pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  714. '(e.date >= ? AND e.date <= ?)',
  715. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  716. ],
  717. [
  718. 'pubdate:2007-03-01/',
  719. '(e.date >= ?)',
  720. [strtotime('2007-03-01T00:00:00Z')],
  721. ],
  722. [
  723. 'pubdate:/2008-05-11',
  724. '(e.date <= ?)',
  725. [strtotime('2008-05-11T23:59:59Z')],
  726. ],
  727. // Basic modified date operator tests
  728. [
  729. 'mdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  730. '(e.`lastModified` >= ? AND COALESCE(e.`lastModified`, 0) <= ?)',
  731. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  732. ],
  733. [
  734. 'mdate:2007-03-01/',
  735. '(e.`lastModified` >= ?)',
  736. [strtotime('2007-03-01T00:00:00Z')],
  737. ],
  738. [
  739. 'mdate:/2008-05-11',
  740. '(COALESCE(e.`lastModified`, 0) <= ?)',
  741. [strtotime('2008-05-11T23:59:59Z')],
  742. ],
  743. // Basic userdate operator tests
  744. [
  745. 'userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  746. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ?)',
  747. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  748. ],
  749. [
  750. 'userdate:2007-03-01/',
  751. '(e.`lastUserModified` >= ?)',
  752. [strtotime('2007-03-01T00:00:00Z')],
  753. ],
  754. [
  755. 'userdate:/2008-05-11',
  756. '(e.`lastUserModified` <= ?)',
  757. [strtotime('2008-05-11T23:59:59Z')],
  758. ],
  759. // Negative date operator tests
  760. [
  761. '-date:2007-03-01/2008-05-11',
  762. '((e.id < ? OR e.id > ?))',
  763. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z') . '000000'],
  764. ],
  765. [
  766. '!pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  767. '((e.date < ? OR e.date > ?))',
  768. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  769. ],
  770. [
  771. '!mdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  772. '((COALESCE(e.`lastModified`, 0) < ? OR e.`lastModified` > ?))',
  773. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  774. ],
  775. [
  776. '!userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z',
  777. '((COALESCE(e.`lastUserModified`, 0) < ? OR e.`lastUserModified` > ?))',
  778. [strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  779. ],
  780. // Combined date operators
  781. [
  782. 'date:2007-03-01/ pubdate:/2008-05-11',
  783. '(e.id >= ? AND e.date <= ?)',
  784. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z')],
  785. ],
  786. [
  787. 'pubdate:2007-03-01/ userdate:/2008-05-11',
  788. '(e.date >= ? AND e.`lastUserModified` <= ?)',
  789. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  790. ],
  791. [
  792. 'userdate:2007-03-01/ mdate:/2008-05-11',
  793. '(COALESCE(e.`lastModified`, 0) <= ? AND e.`lastUserModified` >= ?)',
  794. [strtotime('2008-05-11T23:59:59Z'), strtotime('2007-03-01T00:00:00Z')],
  795. ],
  796. [
  797. 'date:2007-03-01/ userdate:2007-06-01/',
  798. '(e.id >= ? AND e.`lastUserModified` >= ?)',
  799. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2007-06-01T00:00:00Z')],
  800. ],
  801. // Complex combinations with other operators
  802. [
  803. 'intitle:test date:2007-03-01/ pubdate:/2008-05-11',
  804. '(e.id >= ? AND e.date <= ? AND e.title LIKE ?)',
  805. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-05-11T23:59:59Z'), '%test%'],
  806. ],
  807. [
  808. 'author:john userdate:2007-03-01/2008-05-11',
  809. '(e.`lastUserModified` >= ? AND e.`lastUserModified` <= ? AND e.author LIKE ?)',
  810. [strtotime('2007-03-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z'), '%john%'],
  811. ],
  812. // Mixed positive and negative date operators
  813. [
  814. 'date:2007-03-01/ !pubdate:2008-01-01/2008-05-11',
  815. '(e.id >= ? AND (e.date < ? OR e.date > ?))',
  816. [strtotime('2007-03-01T00:00:00Z') . '000000', strtotime('2008-01-01T00:00:00Z'), strtotime('2008-05-11T23:59:59Z')],
  817. ],
  818. ];
  819. }
  820. /** @param array<string> $values */
  821. #[DataProvider('provideRegexPostreSQL')]
  822. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  823. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  824. self::assertSame(trim($sql), trim($filterSearch));
  825. self::assertSame($values, $filterValues);
  826. }
  827. /** @return list<list<mixed>> */
  828. public static function provideRegexPostreSQL(): array {
  829. return [
  830. [
  831. 'intitle:/^ab$/',
  832. '(e.title ~ ?)',
  833. ['^ab$']
  834. ],
  835. [
  836. 'intitle:/^ab$/i',
  837. '(e.title ~* ?)',
  838. ['^ab$']
  839. ],
  840. [
  841. 'intitle:/^ab$/m',
  842. '(e.title ~ ?)',
  843. ['(?m)^ab$']
  844. ],
  845. [
  846. 'intitle:/^ab\\M/',
  847. '(e.title ~ ?)',
  848. ['^ab\\M']
  849. ],
  850. [
  851. 'intext:/^ab\\M/',
  852. '(e.content ~ ?)',
  853. ['^ab\\M']
  854. ],
  855. [
  856. 'intitle:/\\b\\d+/',
  857. '(e.title ~ ?)',
  858. ['\\y\\d+']
  859. ],
  860. [
  861. 'author:/^ab$/',
  862. "(REPLACE(e.author, ';', '\n') ~ ?)",
  863. ['^ab$']
  864. ],
  865. [
  866. 'inurl:/^ab$/',
  867. '(e.link ~ ?)',
  868. ['^ab$']
  869. ],
  870. [
  871. '/^ab$/',
  872. '((e.title ~ ? OR e.content ~ ?))',
  873. ['^ab$', '^ab$']
  874. ],
  875. [
  876. '!/^ab$/',
  877. '(NOT e.title ~ ? AND NOT e.content ~ ?)',
  878. ['^ab$', '^ab$']
  879. ],
  880. [
  881. '#/^a(b|c)$/im',
  882. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ?)",
  883. ['(?m)^a(b|c)$']
  884. ],
  885. [ // Not a regex
  886. 'inurl:https://example.net/test/',
  887. '(e.link LIKE ?)',
  888. ['%https://example.net/test/%']
  889. ],
  890. [ // Not a regex
  891. 'https://example.net/test/',
  892. '((e.title LIKE ? OR e.content LIKE ?))',
  893. ['%https://example.net/test/%', '%https://example.net/test/%']
  894. ],
  895. [ // Not a regex
  896. "author:'/u/Alice'",
  897. "(e.author LIKE ?)",
  898. ['%/u/Alice%'],
  899. ],
  900. [ // Not a regex
  901. "inurl:'/shorts/'",
  902. "(e.link LIKE ?)",
  903. ['%/shorts/%'],
  904. ],
  905. [ // Not a regex
  906. 'inurl:"/shorts/" OR inurl:"/spam/"',
  907. '(e.link LIKE ?) OR (e.link LIKE ?)',
  908. ['%/shorts/%', '%/spam/%'],
  909. ],
  910. [ // Regex with literal 'or'
  911. 'intitle:/^A or B/i',
  912. '(e.title ~* ?)',
  913. ['^A or B']
  914. ],
  915. [ // Regex with literal 'OR'
  916. 'intitle:/^A B OR C D/i OR intitle:/^A B OR C D/i',
  917. '(e.title ~* ?) OR (e.title ~* ?)',
  918. ['^A B OR C D', '^A B OR C D']
  919. ],
  920. [ // Quote with literal 'OR'
  921. 'intitle:"A B OR C D" OR intitle:"E or F"',
  922. '(e.title LIKE ?) OR (e.title LIKE ?)',
  923. ['%A B OR C D%', '%E or F%']
  924. ],
  925. ];
  926. }
  927. /** @param array<string> $values */
  928. #[DataProvider('provideRegexMariaDB')]
  929. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  930. FreshRSS_DatabaseDAO::$dummyConnection = true;
  931. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  932. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  933. self::assertSame(trim($sql), trim($filterSearch));
  934. self::assertSame($values, $filterValues);
  935. }
  936. /** @return list<list<mixed>> */
  937. public static function provideRegexMariaDB(): array {
  938. return [
  939. [
  940. 'intitle:/^ab$/',
  941. "(e.title REGEXP ?)",
  942. ['(?-i)^ab$']
  943. ],
  944. [
  945. 'intitle:/^ab$/i',
  946. "(e.title REGEXP ?)",
  947. ['(?i)^ab$']
  948. ],
  949. [
  950. 'intitle:/^ab$/m',
  951. "(e.title REGEXP ?)",
  952. ['(?-i)(?m)^ab$']
  953. ],
  954. [
  955. 'intitle:/\\b\\d+/',
  956. "(e.title REGEXP ?)",
  957. ['(?-i)\\b\\d+']
  958. ],
  959. [
  960. 'intext:/^ab$/m',
  961. '(UNCOMPRESS(e.content_bin) REGEXP ?)',
  962. ['(?-i)(?m)^ab$']
  963. ],
  964. [
  965. '-intext:/^ab$/m',
  966. '(NOT UNCOMPRESS(e.content_bin) REGEXP ?)',
  967. ['(?-i)(?m)^ab$']
  968. ],
  969. [
  970. '-/^ab$/',
  971. '(NOT e.title REGEXP ? AND NOT UNCOMPRESS(e.content_bin) REGEXP ?)',
  972. ['(?-i)^ab$', '(?-i)^ab$']
  973. ],
  974. ];
  975. }
  976. /** @param array<string> $values */
  977. #[DataProvider('provideRegexMySQL')]
  978. public function test__regex_mysql(string $input, string $sql, array $values): void {
  979. FreshRSS_DatabaseDAO::$dummyConnection = true;
  980. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  981. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  982. self::assertSame(trim($sql), trim($filterSearch));
  983. self::assertSame($values, $filterValues);
  984. }
  985. /** @return list<list<mixed>> */
  986. public static function provideRegexMySQL(): array {
  987. return [
  988. [
  989. 'intitle:/^ab$/',
  990. "(REGEXP_LIKE(e.title,?,'c'))",
  991. ['^ab$']
  992. ],
  993. [
  994. 'intitle:/^ab$/i',
  995. "(REGEXP_LIKE(e.title,?,'i'))",
  996. ['^ab$']
  997. ],
  998. [
  999. 'intitle:/^ab$/m',
  1000. "(REGEXP_LIKE(e.title,?,'mc'))",
  1001. ['^ab$']
  1002. ],
  1003. [
  1004. 'intext:/^ab$/m',
  1005. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc'))",
  1006. ['^ab$']
  1007. ],
  1008. [
  1009. '-intext:/^ab$/m',
  1010. "(NOT REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc'))",
  1011. ['^ab$']
  1012. ],
  1013. [
  1014. '-/^ab$/',
  1015. "(NOT REGEXP_LIKE(e.title,?,'c') AND NOT REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'c'))",
  1016. ['^ab$', '^ab$']
  1017. ],
  1018. ];
  1019. }
  1020. /** @param array<string> $values */
  1021. #[DataProvider('provideRegexSQLite')]
  1022. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  1023. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  1024. self::assertSame(trim($sql), trim($filterSearch));
  1025. self::assertSame($values, $filterValues);
  1026. }
  1027. /** @return list<list<mixed>> */
  1028. public static function provideRegexSQLite(): array {
  1029. return [
  1030. [
  1031. 'intitle:/^ab$/',
  1032. "(e.title REGEXP ?)",
  1033. ['/^ab$/']
  1034. ],
  1035. [
  1036. 'intitle:/^ab$/i',
  1037. "(e.title REGEXP ?)",
  1038. ['/^ab$/i']
  1039. ],
  1040. [
  1041. 'intitle:/^ab$/m',
  1042. "(e.title REGEXP ?)",
  1043. ['/^ab$/m']
  1044. ],
  1045. [
  1046. 'intitle:/^ab\\b/',
  1047. '(e.title REGEXP ?)',
  1048. ['/^ab\\b/']
  1049. ],
  1050. [
  1051. 'intext:/^ab\\b/',
  1052. '(e.content REGEXP ?)',
  1053. ['/^ab\\b/']
  1054. ],
  1055. ];
  1056. }
  1057. #[DataProvider('provideToString')]
  1058. public static function test__toString(string $input): void {
  1059. $search = new FreshRSS_Search($input);
  1060. $expected = str_replace("\n", ' ', $input);
  1061. self::assertSame($expected, $search->__toString());
  1062. }
  1063. /**
  1064. * @return array<array<string>>
  1065. */
  1066. public static function provideToString(): array {
  1067. return [
  1068. [
  1069. <<<'EOD'
  1070. e:1,2 f:10,11 c:20,21 L:30,31 labels:"My label,My other label"
  1071. userdate:2025-01-01T00:00:00/2026-01-01T00:00:00
  1072. mdate:2025-12
  1073. pubdate:2025-02-01T00:00:00/2026-01-01T00:00:00
  1074. date:2025-03-01T00:00:00/2026-01-01T00:00:00
  1075. intitle:/<Inter&sting>/i intitle:"g ' & d\\:"
  1076. intext:/<Inter&sting>/i intext:g&d
  1077. author:/Bob/ author:"/u/Alice" author:Alice
  1078. inurl:/https/ inurl:"/shorts/" inurl:example.net
  1079. #/tag2/ #tag1
  1080. /search_regex/i "quoted search" search search:"A user search" search:U1
  1081. -e:3,4 -f:12,13 -c:22,23 -L:32,33 -labels:"Not label,Not other label"
  1082. -userdate:2025-06-01T00:00:00/2025-09-01T00:00:00
  1083. -mdate:2025-12-27
  1084. -pubdate:2025
  1085. -date:P30D
  1086. -intitle:/Spam/i -intitle:"'bad"
  1087. -intext:/Spam/i -intext:"'bad"
  1088. -author:/Dave/i -author:"/u/Charlie" -author:Charlie
  1089. -inurl:/ftp/ -inurl:"/spam/" -inurl:example.com
  1090. -#/tag4/ -#tag3
  1091. -/not_regex/i -"not quoted" -not_search -search:"Negative user search" -search:U2
  1092. EOD
  1093. ],
  1094. ];
  1095. }
  1096. #[DataProvider('provideBooleanSearchToString')]
  1097. public static function testBooleanSearchToString(string $input, string $expected): void {
  1098. $search = new FreshRSS_BooleanSearch($input);
  1099. self::assertSame($expected, $search->toString());
  1100. }
  1101. /**
  1102. * @return array<array<string>>
  1103. */
  1104. public static function provideBooleanSearchToString(): array {
  1105. return [
  1106. [
  1107. '((a OR b) (c OR d) -e) OR -(f g)',
  1108. '((a OR b) (c OR d) (-e)) OR -(f g)',
  1109. ],
  1110. [
  1111. '((a OR b) ((c) OR ((d))) (-e)) OR -(((f g)))',
  1112. '((a OR b) (c OR d) (-e)) OR -(f g)',
  1113. ],
  1114. [
  1115. '!((b c))',
  1116. '-(b c)',
  1117. ],
  1118. [
  1119. '(a) OR !((b c))',
  1120. 'a OR -(b c)',
  1121. ],
  1122. [
  1123. '((a) (b))',
  1124. 'a b',
  1125. ],
  1126. [
  1127. '((a) OR (b))',
  1128. 'a OR b',
  1129. ],
  1130. [
  1131. ' ( !( !( ( a ) ) ) ) ( ) ',
  1132. '-(-a)',
  1133. ],
  1134. [
  1135. '-intitle:a -inurl:b',
  1136. '-intitle:a -inurl:b',
  1137. ],
  1138. [
  1139. 'intitle:/^A or B/i',
  1140. 'intitle:/^A or B/i',
  1141. ],
  1142. [
  1143. 'intitle:/^A B OR C D/i',
  1144. 'intitle:/^A B OR C D/i',
  1145. ],
  1146. ];
  1147. }
  1148. /**
  1149. * @param list<array{search:string,name?:string}> $queries
  1150. */
  1151. #[DataProvider('provideBooleanSearchToStringExpansion')]
  1152. public static function testBooleanSearchToStringExpansion(array $queries, string $input,
  1153. string $expectedNotExpanded, string $expectedExpanded): void {
  1154. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  1155. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  1156. $newUserConf->queries = $queries;
  1157. FreshRSS_Context::setUserConf($newUserConf);
  1158. try {
  1159. $booleanSearch = new FreshRSS_BooleanSearch($input);
  1160. self::assertSame($expectedNotExpanded, $booleanSearch->toString(expandUserQueries: false));
  1161. self::assertSame($expectedExpanded, $booleanSearch->toString());
  1162. } finally {
  1163. FreshRSS_Context::setUserConf($previousUserConf);
  1164. }
  1165. }
  1166. /**
  1167. * @return array<string,array{0:list<array{search:string,name?:string}>,1:string,2:string,3:string}>
  1168. */
  1169. public static function provideBooleanSearchToStringExpansion(): array {
  1170. return [
  1171. 'Not found ID' => [
  1172. [
  1173. ['search' => 'author:Alice'],
  1174. ['search' => 'intitle:World'],
  1175. ],
  1176. 'S:3 S:4,5 ',
  1177. 'S:3 S:4,5',
  1178. '',
  1179. ],
  1180. 'Not found name' => [
  1181. [
  1182. ['search' => 'author:Alice', 'name' => 'First'],
  1183. ['search' => 'intitle:World', 'name' => 'Second'],
  1184. ],
  1185. 'search:Third ',
  1186. 'search:Third',
  1187. '',
  1188. ],
  1189. 'Found IDs' => [
  1190. [
  1191. ['search' => 'author:Alice', 'name' => 'First'],
  1192. ['search' => 'intitle:World', 'name' => 'Second'],
  1193. ],
  1194. 'S:0,1 ',
  1195. 'S:0,1',
  1196. 'author:Alice OR intitle:World',
  1197. ],
  1198. 'Found names' => [
  1199. [
  1200. ['search' => 'author:Alice', 'name' => 'First'],
  1201. ['search' => 'intitle:World', 'name' => 'Second'],
  1202. ],
  1203. 'search:First search:Second ',
  1204. 'search:First search:Second',
  1205. 'author:Alice intitle:World',
  1206. ],
  1207. ];
  1208. }
  1209. #[DataProvider('provideHasSameOperators')]
  1210. public function testHasSameOperators(string $input1, string $input2, bool $expected): void {
  1211. $search1 = new FreshRSS_Search($input1);
  1212. $search2 = new FreshRSS_Search($input2);
  1213. self::assertSame($expected, $search1->hasSameOperators($search2));
  1214. }
  1215. /**
  1216. * @return array<array{string,string,bool}>
  1217. */
  1218. public static function provideHasSameOperators(): array {
  1219. return [
  1220. ['', '', true],
  1221. ['intitle:a intext:b', 'intitle:c intext:d', true],
  1222. ['intitle:a intext:b', 'intitle:c inurl:d', false],
  1223. ];
  1224. }
  1225. #[DataProvider('provideBooleanSearchEnforce')]
  1226. public function testBooleanSearchEnforce(string $initialInput, string $enforceInput, string $expectedOutput): void {
  1227. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1228. $searchToEnforce = new FreshRSS_Search($enforceInput);
  1229. $newBooleanSearch = $booleanSearch->enforce($searchToEnforce);
  1230. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1231. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1232. }
  1233. /**
  1234. * @return array<array{string,string,string}>
  1235. */
  1236. public static function provideBooleanSearchEnforce(): array {
  1237. return [
  1238. ['', '', ''],
  1239. ['', 'intitle:b', 'intitle:b'],
  1240. ['intitle:a', '', 'intitle:a'],
  1241. ['intitle:a', 'intitle:b', 'intitle:b'],
  1242. ['a', 'intitle:b', 'intitle:b a'],
  1243. ['intitle:a intext:a', 'intitle:b', 'intitle:b intext:a'],
  1244. ['intitle:a inurl:a', 'intitle:b', 'intitle:b inurl:a'],
  1245. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:b (intitle:a OR inurl:a)'],
  1246. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', 'intitle:b (inurl:a intitle:c)'],
  1247. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', 'intitle:b (inurl:a OR intitle:c)'],
  1248. ['(intitle:a) (inurl:a)', 'intitle:b', 'intitle:b inurl:a'],
  1249. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a intitle:b'],
  1250. ['(a b) OR (c d)', 'e', 'e ((a b) OR (c d))'],
  1251. ['(a b) (c d)', 'e', 'e ((a b) (c d))'],
  1252. ['(a b)', 'e', 'e (a b)'],
  1253. ['date:2024/', 'date:/2025', 'date:/2025'],
  1254. ['a', 'date:/2025', 'date:/2025 a'],
  1255. ];
  1256. }
  1257. #[DataProvider('provideBooleanSearchRemove')]
  1258. public function testBooleanSearchRemove(string $initialInput, string $removeInput, string $expectedOutput): void {
  1259. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1260. $searchToRemove = new FreshRSS_Search($removeInput);
  1261. $newBooleanSearch = $booleanSearch->remove($searchToRemove);
  1262. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1263. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1264. }
  1265. /**
  1266. * @return array<array{string,string,string}>
  1267. */
  1268. public static function provideBooleanSearchRemove(): array {
  1269. return [
  1270. ['', 'intitle:b', ''],
  1271. ['intitle:a', 'intitle:b', ''],
  1272. ['intitle:a intext:a', 'intitle:b', 'intext:a'],
  1273. ['intitle:a inurl:a', 'intitle:b', 'inurl:a'],
  1274. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:a OR inurl:a'],
  1275. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', '(inurl:a intitle:c)'],
  1276. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', '(inurl:a OR intitle:c)'],
  1277. ['(intitle:a) (inurl:a)', 'intitle:b', 'inurl:a'],
  1278. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a'],
  1279. ['e ((a b) OR (c d))', 'e', '((a b) OR (c d))'],
  1280. ['e ((a b) (c d))', 'e', '((a b) (c d))'],
  1281. ['date:2024/', 'date:/2025', ''],
  1282. ['date:2024/ a', 'date:/2025', 'a'],
  1283. ];
  1284. }
  1285. #[DataProvider('provideNeedVisibility')]
  1286. public function testNeedVisibility(string $input, ?int $expected): void {
  1287. $search = new FreshRSS_Search($input);
  1288. self::assertSame($expected, $search->needVisibility());
  1289. }
  1290. /** @return list<list<string|int|null>> */
  1291. public static function provideNeedVisibility(): array {
  1292. return [
  1293. ['', null],
  1294. ['f:1', FreshRSS_Feed::PRIORITY_HIDDEN],
  1295. ['c:2', FreshRSS_Feed::PRIORITY_CATEGORY],
  1296. ['f:1 c:2', FreshRSS_Feed::PRIORITY_HIDDEN],
  1297. ['-f:1', null],
  1298. ['-c:2', null],
  1299. ];
  1300. }
  1301. }