SearchTest.php 36 KB

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