SearchTest.php 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  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. public function __construct(string $name) {
  7. parent::__construct($name);
  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. /**
  821. * @dataProvider provideRegexPostreSQL
  822. * @param array<string> $values
  823. */
  824. public function test__regex_postgresql(string $input, string $sql, array $values): void {
  825. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  826. self::assertSame(trim($sql), trim($filterSearch));
  827. self::assertSame($values, $filterValues);
  828. }
  829. /** @return list<list<mixed>> */
  830. public static function provideRegexPostreSQL(): array {
  831. return [
  832. [
  833. 'intitle:/^ab$/',
  834. '(e.title ~ ?)',
  835. ['^ab$']
  836. ],
  837. [
  838. 'intitle:/^ab$/i',
  839. '(e.title ~* ?)',
  840. ['^ab$']
  841. ],
  842. [
  843. 'intitle:/^ab$/m',
  844. '(e.title ~ ?)',
  845. ['(?m)^ab$']
  846. ],
  847. [
  848. 'intitle:/^ab\\M/',
  849. '(e.title ~ ?)',
  850. ['^ab\\M']
  851. ],
  852. [
  853. 'intext:/^ab\\M/',
  854. '(e.content ~ ?)',
  855. ['^ab\\M']
  856. ],
  857. [
  858. 'intitle:/\\b\\d+/',
  859. '(e.title ~ ?)',
  860. ['\\y\\d+']
  861. ],
  862. [
  863. 'author:/^ab$/',
  864. "(REPLACE(e.author, ';', '\n') ~ ?)",
  865. ['^ab$']
  866. ],
  867. [
  868. 'inurl:/^ab$/',
  869. '(e.link ~ ?)',
  870. ['^ab$']
  871. ],
  872. [
  873. '/^ab$/',
  874. '((e.title ~ ? OR e.content ~ ?))',
  875. ['^ab$', '^ab$']
  876. ],
  877. [
  878. '!/^ab$/',
  879. '(NOT e.title ~ ? AND NOT e.content ~ ?)',
  880. ['^ab$', '^ab$']
  881. ],
  882. [
  883. '#/^a(b|c)$/im',
  884. "(REPLACE(REPLACE(e.tags, ' #', '#'), '#', '\n') ~* ?)",
  885. ['(?m)^a(b|c)$']
  886. ],
  887. [ // Not a regex
  888. 'inurl:https://example.net/test/',
  889. '(e.link LIKE ?)',
  890. ['%https://example.net/test/%']
  891. ],
  892. [ // Not a regex
  893. 'https://example.net/test/',
  894. '((e.title LIKE ? OR e.content LIKE ?))',
  895. ['%https://example.net/test/%', '%https://example.net/test/%']
  896. ],
  897. [ // Not a regex
  898. "author:'/u/Alice'",
  899. "(e.author LIKE ?)",
  900. ['%/u/Alice%'],
  901. ],
  902. [ // Not a regex
  903. "inurl:'/shorts/'",
  904. "(e.link LIKE ?)",
  905. ['%/shorts/%'],
  906. ],
  907. [ // Not a regex
  908. 'inurl:"/shorts/" OR inurl:"/spam/"',
  909. '(e.link LIKE ?) OR (e.link LIKE ?)',
  910. ['%/shorts/%', '%/spam/%'],
  911. ],
  912. [ // Regex with literal 'or'
  913. 'intitle:/^A or B/i',
  914. '(e.title ~* ?)',
  915. ['^A or B']
  916. ],
  917. [ // Regex with literal 'OR'
  918. 'intitle:/^A B OR C D/i OR intitle:/^A B OR C D/i',
  919. '(e.title ~* ?) OR (e.title ~* ?)',
  920. ['^A B OR C D', '^A B OR C D']
  921. ],
  922. [ // Quote with literal 'OR'
  923. 'intitle:"A B OR C D" OR intitle:"E or F"',
  924. '(e.title LIKE ?) OR (e.title LIKE ?)',
  925. ['%A B OR C D%', '%E or F%']
  926. ],
  927. ];
  928. }
  929. /**
  930. * @dataProvider provideRegexMariaDB
  931. * @param array<string> $values
  932. */
  933. public function test__regex_mariadb(string $input, string $sql, array $values): void {
  934. FreshRSS_DatabaseDAO::$dummyConnection = true;
  935. FreshRSS_DatabaseDAO::setStaticVersion('11.4.3-MariaDB-ubu2404');
  936. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  937. self::assertSame(trim($sql), trim($filterSearch));
  938. self::assertSame($values, $filterValues);
  939. }
  940. /** @return list<list<mixed>> */
  941. public static function provideRegexMariaDB(): array {
  942. return [
  943. [
  944. 'intitle:/^ab$/',
  945. "(e.title REGEXP ?)",
  946. ['(?-i)^ab$']
  947. ],
  948. [
  949. 'intitle:/^ab$/i',
  950. "(e.title REGEXP ?)",
  951. ['(?i)^ab$']
  952. ],
  953. [
  954. 'intitle:/^ab$/m',
  955. "(e.title REGEXP ?)",
  956. ['(?-i)(?m)^ab$']
  957. ],
  958. [
  959. 'intitle:/\\b\\d+/',
  960. "(e.title REGEXP ?)",
  961. ['(?-i)\\b\\d+']
  962. ],
  963. [
  964. 'intext:/^ab$/m',
  965. '(UNCOMPRESS(e.content_bin) REGEXP ?)',
  966. ['(?-i)(?m)^ab$']
  967. ],
  968. [
  969. '-intext:/^ab$/m',
  970. '(NOT UNCOMPRESS(e.content_bin) REGEXP ?)',
  971. ['(?-i)(?m)^ab$']
  972. ],
  973. [
  974. '-/^ab$/',
  975. '(NOT e.title REGEXP ? AND NOT UNCOMPRESS(e.content_bin) REGEXP ?)',
  976. ['(?-i)^ab$', '(?-i)^ab$']
  977. ],
  978. ];
  979. }
  980. /**
  981. * @dataProvider provideRegexMySQL
  982. * @param array<string> $values
  983. */
  984. public function test__regex_mysql(string $input, string $sql, array $values): void {
  985. FreshRSS_DatabaseDAO::$dummyConnection = true;
  986. FreshRSS_DatabaseDAO::setStaticVersion('9.0.1');
  987. [$filterValues, $filterSearch] = FreshRSS_EntryDAO::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  988. self::assertSame(trim($sql), trim($filterSearch));
  989. self::assertSame($values, $filterValues);
  990. }
  991. /** @return list<list<mixed>> */
  992. public static function provideRegexMySQL(): array {
  993. return [
  994. [
  995. 'intitle:/^ab$/',
  996. "(REGEXP_LIKE(e.title,?,'c'))",
  997. ['^ab$']
  998. ],
  999. [
  1000. 'intitle:/^ab$/i',
  1001. "(REGEXP_LIKE(e.title,?,'i'))",
  1002. ['^ab$']
  1003. ],
  1004. [
  1005. 'intitle:/^ab$/m',
  1006. "(REGEXP_LIKE(e.title,?,'mc'))",
  1007. ['^ab$']
  1008. ],
  1009. [
  1010. 'intext:/^ab$/m',
  1011. "(REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc'))",
  1012. ['^ab$']
  1013. ],
  1014. [
  1015. '-intext:/^ab$/m',
  1016. "(NOT REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'mc'))",
  1017. ['^ab$']
  1018. ],
  1019. [
  1020. '-/^ab$/',
  1021. "(NOT REGEXP_LIKE(e.title,?,'c') AND NOT REGEXP_LIKE(UNCOMPRESS(e.content_bin),?,'c'))",
  1022. ['^ab$', '^ab$']
  1023. ],
  1024. ];
  1025. }
  1026. /**
  1027. * @dataProvider provideRegexSQLite
  1028. * @param array<string> $values
  1029. */
  1030. public function test__regex_sqlite(string $input, string $sql, array $values): void {
  1031. [$filterValues, $filterSearch] = FreshRSS_EntryDAOSQLite::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  1032. self::assertSame(trim($sql), trim($filterSearch));
  1033. self::assertSame($values, $filterValues);
  1034. }
  1035. /** @return list<list<mixed>> */
  1036. public static function provideRegexSQLite(): array {
  1037. return [
  1038. [
  1039. 'intitle:/^ab$/',
  1040. "(e.title REGEXP ?)",
  1041. ['/^ab$/']
  1042. ],
  1043. [
  1044. 'intitle:/^ab$/i',
  1045. "(e.title REGEXP ?)",
  1046. ['/^ab$/i']
  1047. ],
  1048. [
  1049. 'intitle:/^ab$/m',
  1050. "(e.title REGEXP ?)",
  1051. ['/^ab$/m']
  1052. ],
  1053. [
  1054. 'intitle:/^ab\\b/',
  1055. '(e.title REGEXP ?)',
  1056. ['/^ab\\b/']
  1057. ],
  1058. [
  1059. 'intext:/^ab\\b/',
  1060. '(e.content REGEXP ?)',
  1061. ['/^ab\\b/']
  1062. ],
  1063. ];
  1064. }
  1065. #[DataProvider('provideToString')]
  1066. public static function test__toString(string $input): void {
  1067. $search = new FreshRSS_Search($input);
  1068. $expected = str_replace("\n", ' ', $input);
  1069. self::assertSame($expected, $search->__toString());
  1070. }
  1071. /**
  1072. * @return array<array<string>>
  1073. */
  1074. public static function provideToString(): array {
  1075. return [
  1076. [
  1077. <<<'EOD'
  1078. e:1,2 f:10,11 c:20,21 L:30,31 labels:"My label,My other label"
  1079. userdate:2025-01-01T00:00:00/2026-01-01T00:00:00
  1080. mdate:2025-12
  1081. pubdate:2025-02-01T00:00:00/2026-01-01T00:00:00
  1082. date:2025-03-01T00:00:00/2026-01-01T00:00:00
  1083. intitle:/<Inter&sting>/i intitle:"g ' & d\\:"
  1084. intext:/<Inter&sting>/i intext:g&d
  1085. author:/Bob/ author:"/u/Alice" author:Alice
  1086. inurl:/https/ inurl:"/shorts/" inurl:example.net
  1087. #/tag2/ #tag1
  1088. /search_regex/i "quoted search" search search:"A user search" search:U1
  1089. -e:3,4 -f:12,13 -c:22,23 -L:32,33 -labels:"Not label,Not other label"
  1090. -userdate:2025-06-01T00:00:00/2025-09-01T00:00:00
  1091. -mdate:2025-12-27
  1092. -pubdate:2025
  1093. -date:P30D
  1094. -intitle:/Spam/i -intitle:"'bad"
  1095. -intext:/Spam/i -intext:"'bad"
  1096. -author:/Dave/i -author:"/u/Charlie" -author:Charlie
  1097. -inurl:/ftp/ -inurl:"/spam/" -inurl:example.com
  1098. -#/tag4/ -#tag3
  1099. -/not_regex/i -"not quoted" -not_search -search:"Negative user search" -search:U2
  1100. EOD
  1101. ],
  1102. ];
  1103. }
  1104. #[DataProvider('provideBooleanSearchToString')]
  1105. public static function testBooleanSearchToString(string $input, string $expected): void {
  1106. $search = new FreshRSS_BooleanSearch($input);
  1107. self::assertSame($expected, $search->toString());
  1108. }
  1109. /**
  1110. * @return array<array<string>>
  1111. */
  1112. public static function provideBooleanSearchToString(): array {
  1113. return [
  1114. [
  1115. '((a OR b) (c OR d) -e) OR -(f g)',
  1116. '((a OR b) (c OR d) (-e)) OR -(f g)',
  1117. ],
  1118. [
  1119. '((a OR b) ((c) OR ((d))) (-e)) OR -(((f g)))',
  1120. '((a OR b) (c OR d) (-e)) OR -(f g)',
  1121. ],
  1122. [
  1123. '!((b c))',
  1124. '-(b c)',
  1125. ],
  1126. [
  1127. '(a) OR !((b c))',
  1128. 'a OR -(b c)',
  1129. ],
  1130. [
  1131. '((a) (b))',
  1132. 'a b',
  1133. ],
  1134. [
  1135. '((a) OR (b))',
  1136. 'a OR b',
  1137. ],
  1138. [
  1139. ' ( !( !( ( a ) ) ) ) ( ) ',
  1140. '-(-a)',
  1141. ],
  1142. [
  1143. '-intitle:a -inurl:b',
  1144. '-intitle:a -inurl:b',
  1145. ],
  1146. [
  1147. 'intitle:/^A or B/i',
  1148. 'intitle:/^A or B/i',
  1149. ],
  1150. [
  1151. 'intitle:/^A B OR C D/i',
  1152. 'intitle:/^A B OR C D/i',
  1153. ],
  1154. ];
  1155. }
  1156. /**
  1157. * @param list<array{search:string,name?:string}> $queries
  1158. */
  1159. #[DataProvider('provideBooleanSearchToStringExpansion')]
  1160. public static function testBooleanSearchToStringExpansion(array $queries, string $input,
  1161. string $expectedNotExpanded, string $expectedExpanded): void {
  1162. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  1163. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  1164. $newUserConf->queries = $queries;
  1165. FreshRSS_Context::setUserConf($newUserConf);
  1166. try {
  1167. $booleanSearch = new FreshRSS_BooleanSearch($input);
  1168. self::assertSame($expectedNotExpanded, $booleanSearch->toString(expandUserQueries: false));
  1169. self::assertSame($expectedExpanded, $booleanSearch->toString());
  1170. } finally {
  1171. FreshRSS_Context::setUserConf($previousUserConf);
  1172. }
  1173. }
  1174. /**
  1175. * @return array<string,array{0:list<array{search:string,name?:string}>,1:string,2:string,3:string}>
  1176. */
  1177. public static function provideBooleanSearchToStringExpansion(): array {
  1178. return [
  1179. 'Not found ID' => [
  1180. [
  1181. ['search' => 'author:Alice'],
  1182. ['search' => 'intitle:World'],
  1183. ],
  1184. 'S:3 S:4,5 ',
  1185. 'S:3 S:4,5',
  1186. '',
  1187. ],
  1188. 'Not found name' => [
  1189. [
  1190. ['search' => 'author:Alice', 'name' => 'First'],
  1191. ['search' => 'intitle:World', 'name' => 'Second'],
  1192. ],
  1193. 'search:Third ',
  1194. 'search:Third',
  1195. '',
  1196. ],
  1197. 'Found IDs' => [
  1198. [
  1199. ['search' => 'author:Alice', 'name' => 'First'],
  1200. ['search' => 'intitle:World', 'name' => 'Second'],
  1201. ],
  1202. 'S:0,1 ',
  1203. 'S:0,1',
  1204. 'author:Alice OR intitle:World',
  1205. ],
  1206. 'Found names' => [
  1207. [
  1208. ['search' => 'author:Alice', 'name' => 'First'],
  1209. ['search' => 'intitle:World', 'name' => 'Second'],
  1210. ],
  1211. 'search:First search:Second ',
  1212. 'search:First search:Second',
  1213. 'author:Alice intitle:World',
  1214. ],
  1215. ];
  1216. }
  1217. #[DataProvider('provideHasSameOperators')]
  1218. public function testHasSameOperators(string $input1, string $input2, bool $expected): void {
  1219. $search1 = new FreshRSS_Search($input1);
  1220. $search2 = new FreshRSS_Search($input2);
  1221. self::assertSame($expected, $search1->hasSameOperators($search2));
  1222. }
  1223. /**
  1224. * @return array<array{string,string,bool}>
  1225. */
  1226. public static function provideHasSameOperators(): array {
  1227. return [
  1228. ['', '', true],
  1229. ['intitle:a intext:b', 'intitle:c intext:d', true],
  1230. ['intitle:a intext:b', 'intitle:c inurl:d', false],
  1231. ];
  1232. }
  1233. #[DataProvider('provideBooleanSearchEnforce')]
  1234. public function testBooleanSearchEnforce(string $initialInput, string $enforceInput, string $expectedOutput): void {
  1235. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1236. $searchToEnforce = new FreshRSS_Search($enforceInput);
  1237. $newBooleanSearch = $booleanSearch->enforce($searchToEnforce);
  1238. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1239. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1240. }
  1241. /**
  1242. * @return array<array{string,string,string}>
  1243. */
  1244. public static function provideBooleanSearchEnforce(): array {
  1245. return [
  1246. ['', '', ''],
  1247. ['', 'intitle:b', 'intitle:b'],
  1248. ['intitle:a', '', 'intitle:a'],
  1249. ['intitle:a', 'intitle:b', 'intitle:b'],
  1250. ['a', 'intitle:b', 'intitle:b a'],
  1251. ['intitle:a intext:a', 'intitle:b', 'intitle:b intext:a'],
  1252. ['intitle:a inurl:a', 'intitle:b', 'intitle:b inurl:a'],
  1253. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:b (intitle:a OR inurl:a)'],
  1254. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', 'intitle:b (inurl:a intitle:c)'],
  1255. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', 'intitle:b (inurl:a OR intitle:c)'],
  1256. ['(intitle:a) (inurl:a)', 'intitle:b', 'intitle:b inurl:a'],
  1257. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a intitle:b'],
  1258. ['(a b) OR (c d)', 'e', 'e ((a b) OR (c d))'],
  1259. ['(a b) (c d)', 'e', 'e ((a b) (c d))'],
  1260. ['(a b)', 'e', 'e (a b)'],
  1261. ['date:2024/', 'date:/2025', 'date:/2025'],
  1262. ['a', 'date:/2025', 'date:/2025 a'],
  1263. ];
  1264. }
  1265. #[DataProvider('provideBooleanSearchRemove')]
  1266. public function testBooleanSearchRemove(string $initialInput, string $removeInput, string $expectedOutput): void {
  1267. $booleanSearch = new FreshRSS_BooleanSearch($initialInput);
  1268. $searchToRemove = new FreshRSS_Search($removeInput);
  1269. $newBooleanSearch = $booleanSearch->remove($searchToRemove);
  1270. self::assertNotSame($booleanSearch, $newBooleanSearch);
  1271. self::assertSame($expectedOutput, $newBooleanSearch->toString());
  1272. }
  1273. /**
  1274. * @return array<array{string,string,string}>
  1275. */
  1276. public static function provideBooleanSearchRemove(): array {
  1277. return [
  1278. ['', 'intitle:b', ''],
  1279. ['intitle:a', 'intitle:b', ''],
  1280. ['intitle:a intext:a', 'intitle:b', 'intext:a'],
  1281. ['intitle:a inurl:a', 'intitle:b', 'inurl:a'],
  1282. ['intitle:a OR inurl:a', 'intitle:b', 'intitle:a OR inurl:a'],
  1283. ['intitle:a ((inurl:a) (intitle:c))', 'intitle:b', '(inurl:a intitle:c)'],
  1284. ['intitle:a ((inurl:a) OR (intitle:c))', 'intitle:b', '(inurl:a OR intitle:c)'],
  1285. ['(intitle:a) (inurl:a)', 'intitle:b', 'inurl:a'],
  1286. ['(inurl:a) (intitle:a)', 'intitle:b', 'inurl:a'],
  1287. ['e ((a b) OR (c d))', 'e', '((a b) OR (c d))'],
  1288. ['e ((a b) (c d))', 'e', '((a b) (c d))'],
  1289. ['date:2024/', 'date:/2025', ''],
  1290. ['date:2024/ a', 'date:/2025', 'a'],
  1291. ];
  1292. }
  1293. #[DataProvider('provideNeedVisibility')]
  1294. public function testNeedVisibility(string $input, ?int $expected): void {
  1295. $search = new FreshRSS_Search($input);
  1296. self::assertSame($expected, $search->needVisibility());
  1297. }
  1298. /** @return list<list<string|int|null>> */
  1299. public static function provideNeedVisibility(): array {
  1300. return [
  1301. ['', null],
  1302. ['f:1', FreshRSS_Feed::PRIORITY_HIDDEN],
  1303. ['c:2', FreshRSS_Feed::PRIORITY_CATEGORY],
  1304. ['f:1 c:2', FreshRSS_Feed::PRIORITY_HIDDEN],
  1305. ['-f:1', null],
  1306. ['-c:2', null],
  1307. ];
  1308. }
  1309. }