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:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  62. ['intitle:word1+word2', ['word1+word2'], null],
  63. ];
  64. }
  65. /**
  66. * @param array<string>|null $intext_value
  67. * @param array<string>|null $search_value
  68. */
  69. #[DataProvider('provideIntextSearch')]
  70. public static function test__construct_whenInputContainsIntext(string $input, ?array $intext_value, ?array $search_value): void {
  71. $search = new FreshRSS_Search($input);
  72. self::assertSame($intext_value, $search->getIntext());
  73. self::assertSame($search_value, $search->getSearch());
  74. }
  75. /**
  76. * @return list<list<mixed>>
  77. */
  78. public static function provideIntextSearch(): array {
  79. return [
  80. ['intext:word1', ['word1'], null],
  81. ['intext:"word1 word2"', ['word1 word2'], null],
  82. ];
  83. }
  84. /**
  85. * @param array<string>|null $author_value
  86. * @param array<string>|null $search_value
  87. */
  88. #[DataProvider('provideAuthorSearch')]
  89. public static function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void {
  90. $search = new FreshRSS_Search($input);
  91. self::assertSame($author_value, $search->getAuthor());
  92. self::assertSame($search_value, $search->getSearch());
  93. }
  94. /**
  95. * @return list<list<mixed>>
  96. */
  97. public static function provideAuthorSearch(): array {
  98. return [
  99. ['author:word1', ['word1'], null],
  100. ['author:word1-word2', ['word1-word2'], null],
  101. ['author:word1 word2', ['word1'], ['word2']],
  102. ['author:"word1 word2"', ['word1 word2'], null],
  103. ["author:'word1 word2'", ['word1 word2'], null],
  104. ['word1 author:word2', ['word2'], ['word1']],
  105. ['word1 author:word2 word3', ['word2'], ['word1', 'word3']],
  106. ['word1 author:"word2 word3"', ['word2 word3'], ['word1']],
  107. ["word1 author:'word2 word3'", ['word2 word3'], ['word1']],
  108. ['author:word1 author:word2', ['word1', 'word2'], null],
  109. ['author: word1 word2', null, ['word1', 'word2']],
  110. ['author:123', ['123'], null],
  111. ['author:"word1 word2" word3"', ['word1 word2'], ['word3"']],
  112. ["author:'word1 word2' word3'", ['word1 word2'], ["word3'"]],
  113. ['author:"word1 word2\' word3"', ["word1 word2' word3"], null],
  114. ["author:'word1 word2\" word3'", ['word1 word2" word3'], null],
  115. ["author:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  116. ['author:word1+word2', ['word1+word2'], null],
  117. ];
  118. }
  119. /**
  120. * @param array<string>|null $inurl_value
  121. * @param array<string>|null $search_value
  122. */
  123. #[DataProvider('provideInurlSearch')]
  124. public static function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void {
  125. $search = new FreshRSS_Search($input);
  126. self::assertSame($inurl_value, $search->getInurl());
  127. self::assertSame($search_value, $search->getSearch());
  128. }
  129. /**
  130. * @return list<list<mixed>>
  131. */
  132. public static function provideInurlSearch(): array {
  133. return [
  134. ['inurl:word1', ['word1'], null],
  135. ['inurl: word1', null, ['word1']],
  136. ['inurl:123', ['123'], null],
  137. ['inurl:word1 word2', ['word1'], ['word2']],
  138. ['inurl:"word1 word2"', ['word1 word2'], null],
  139. ['inurl:word1 word2 inurl:word3', ['word1', 'word3'], ['word2']],
  140. ["inurl:word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  141. ['inurl:word1+word2', ['word1+word2'], null],
  142. ];
  143. }
  144. #[DataProvider('provideDateSearch')]
  145. public static function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void {
  146. $search = new FreshRSS_Search($input);
  147. self::assertSame($min_date_value, $search->getMinDate());
  148. self::assertSame($max_date_value, $search->getMaxDate());
  149. }
  150. /**
  151. * @return list<list<mixed>>
  152. */
  153. public static function provideDateSearch(): array {
  154. return [
  155. ['date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  156. ['date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  157. ['date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  158. ['date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  159. ['date:2007-03-01/', strtotime('2007-03-01'), null],
  160. ['date:/2008-05-11', null, strtotime('2008-05-12') - 1],
  161. ];
  162. }
  163. #[DataProvider('providePubdateSearch')]
  164. public static function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void {
  165. $search = new FreshRSS_Search($input);
  166. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  167. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  168. }
  169. /**
  170. * @return list<list<mixed>>
  171. */
  172. public static function providePubdateSearch(): array {
  173. return [
  174. ['pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  175. ['pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:29:59Z')],
  176. ['pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:01Z'), strtotime('2008-05-11T15:30:00Z')],
  177. ['pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1],
  178. ['pubdate:2007-03-01/', strtotime('2007-03-01'), null],
  179. ['pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  180. ];
  181. }
  182. #[DataProvider('provideUserdateSearch')]
  183. public static function test__construct_whenInputContainsUserdate(string $input, ?int $min_userdate_value, ?int $max_userdate_value): void {
  184. $search = new FreshRSS_Search($input);
  185. self::assertSame($min_userdate_value, $search->getMinUserdate());
  186. self::assertSame($max_userdate_value, $search->getMaxUserdate());
  187. }
  188. /**
  189. * @return list<list<mixed>>
  190. */
  191. public static function provideUserdateSearch(): array {
  192. return [
  193. ['userdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', strtotime('2007-03-01T13:00:00Z'), strtotime('2008-05-11T15:30:00Z')],
  194. ['userdate:/2008-05-11', null, strtotime('2008-05-12') - 1],
  195. ];
  196. }
  197. /**
  198. * @param array<string>|null $tags_value
  199. * @param array<string>|null $search_value
  200. */
  201. #[DataProvider('provideTagsSearch')]
  202. public static function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  203. $search = new FreshRSS_Search($input);
  204. self::assertSame($tags_value, $search->getTags());
  205. self::assertSame($search_value, $search->getSearch());
  206. }
  207. /**
  208. * @return list<list<string|list<string>|null>>
  209. */
  210. public static function provideTagsSearch(): array {
  211. return [
  212. ['#word1', ['word1'], null],
  213. ['# word1', null, ['#', 'word1']],
  214. ['#123', ['123'], null],
  215. ['#word1 word2', ['word1'], ['word2']],
  216. ['#"word1 word2"', ['word1 word2'], null],
  217. ['#word1 #word2', ['word1', 'word2'], null],
  218. ["#word1 'word2 word3' word4", ['word1'], ['word2 word3', 'word4']],
  219. ['#word1+word2', ['word1 word2'], null]
  220. ];
  221. }
  222. /**
  223. * @param list<array{search:string}> $queries
  224. * @param array{0:string,1:list<string|int>} $expectedResult
  225. */
  226. #[DataProvider('provideSavedQueryIdExpansion')]
  227. public static function test__construct_whenInputContainsSavedQueryIds_expandsSavedSearches(array $queries, string $input, array $expectedResult): void {
  228. $previousUserConf = FreshRSS_Context::hasUserConf() ? FreshRSS_Context::userConf() : null;
  229. $newUserConf = $previousUserConf instanceof FreshRSS_UserConfiguration ? clone $previousUserConf : clone FreshRSS_UserConfiguration::default();
  230. $newUserConf->queries = $queries;
  231. FreshRSS_Context::$user_conf = $newUserConf;
  232. try {
  233. $search = new FreshRSS_BooleanSearch($input);
  234. [$actualValues, $actualSql] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', $search);
  235. self::assertSame($expectedResult[0], trim($actualSql));
  236. self::assertSame($expectedResult[1], $actualValues);
  237. } finally {
  238. FreshRSS_Context::$user_conf = $previousUserConf;
  239. }
  240. }
  241. /**
  242. * @return array<string,array{0:list<array{search:string}>,1:string,2:array{0:string,1:list<string|int>}}>
  243. */
  244. public static function provideSavedQueryIdExpansion(): array {
  245. return [
  246. 'expanded single group' => [
  247. [
  248. ['search' => 'author:Alice'],
  249. ['search' => 'intitle:World'],
  250. ],
  251. 'S:0,1',
  252. [
  253. '((e.author LIKE ? )) OR ((e.title LIKE ? ))',
  254. ['%Alice%', '%World%'],
  255. ],
  256. ],
  257. 'separate groups with OR' => [
  258. [
  259. ['search' => 'author:Alice'],
  260. ['search' => 'intitle:World'],
  261. ['search' => 'inurl:Example'],
  262. ['search' => 'author:Bob'],
  263. ],
  264. 'S:0,1 OR S:2,3',
  265. [
  266. '((e.author LIKE ? )) OR ((e.title LIKE ? )) OR ((e.link LIKE ? )) OR ((e.author LIKE ? ))',
  267. ['%Alice%', '%World%', '%Example%', '%Bob%'],
  268. ],
  269. ],
  270. 'mixed with other clauses' => [
  271. [
  272. ['search' => 'author:Alice'],
  273. ['search' => 'intitle:World'],
  274. ],
  275. 'intitle:Hello S:0,1 date:2025-10',
  276. [
  277. '((e.title LIKE ? )) AND ((e.author LIKE ? )) OR ((e.title LIKE ? )) AND ((e.id >= ? AND e.id <= ? ))',
  278. ['%Hello%', '%Alice%', '%World%', strtotime('2025-10-01') . '000000', (strtotime('2025-11-01') - 1) . '000000'],
  279. ],
  280. ],
  281. ];
  282. }
  283. /**
  284. * @param array<string>|null $author_value
  285. * @param array<string> $intitle_value
  286. * @param array<string>|null $inurl_value
  287. * @param array<string>|null $tags_value
  288. * @param array<string>|null $search_value
  289. */
  290. #[DataProvider('provideMultipleSearch')]
  291. public static function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  292. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  293. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  294. $search = new FreshRSS_Search($input);
  295. self::assertSame($author_value, $search->getAuthor());
  296. self::assertSame($min_date_value, $search->getMinDate());
  297. self::assertSame($max_date_value, $search->getMaxDate());
  298. self::assertSame($intitle_value, $search->getIntitle());
  299. self::assertSame($inurl_value, $search->getInurl());
  300. self::assertSame($min_pubdate_value, $search->getMinPubdate());
  301. self::assertSame($max_pubdate_value, $search->getMaxPubdate());
  302. self::assertSame($tags_value, $search->getTags());
  303. self::assertSame($search_value, $search->getSearch());
  304. self::assertSame($input, $search->getRawInput());
  305. }
  306. /** @return list<list<mixed>> */
  307. public static function provideMultipleSearch(): array {
  308. return [
  309. [
  310. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  311. ['word1'],
  312. strtotime('2007-03-01'),
  313. strtotime('2008-05-12') - 1,
  314. ['word2'],
  315. ['word3'],
  316. strtotime('2007-03-01'),
  317. strtotime('2008-05-12') - 1,
  318. ['word4', 'word5'],
  319. null
  320. ],
  321. [
  322. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  323. ['word1'],
  324. strtotime('2007-03-01'),
  325. strtotime('2008-05-12') - 1,
  326. ['word2'],
  327. ['word3'],
  328. strtotime('2007-03-01'),
  329. strtotime('2008-05-12') - 1,
  330. ['word4', 'word5'],
  331. ['word6']
  332. ],
  333. [
  334. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  335. ['word1'],
  336. strtotime('2007-03-01'),
  337. strtotime('2008-05-12') - 1,
  338. ['word2'],
  339. ['word3'],
  340. strtotime('2007-03-01'),
  341. strtotime('2008-05-12') - 1,
  342. ['word4', 'word5'],
  343. ['word6', 'word7']
  344. ],
  345. [
  346. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 "word7 word8" date:2007-03-01/2008-05-11',
  347. ['word1'],
  348. strtotime('2007-03-01'),
  349. strtotime('2008-05-12') - 1,
  350. ['word2'],
  351. ['word3'],
  352. strtotime('2007-03-01'),
  353. strtotime('2008-05-12') - 1,
  354. ['word4', 'word5'],
  355. ['word7 word8', 'word6']
  356. ]
  357. ];
  358. }
  359. #[DataProvider('provideAddOrParentheses')]
  360. public static function test__addOrParentheses(string $input, string $output): void {
  361. self::assertSame($output, FreshRSS_BooleanSearch::addOrParentheses($input));
  362. }
  363. /** @return list<list{string,string}> */
  364. public static function provideAddOrParentheses(): array {
  365. return [
  366. ['ab', 'ab'],
  367. ['ab cd', 'ab cd'],
  368. ['!ab -cd', '!ab -cd'],
  369. ['ab OR cd', '(ab) OR (cd)'],
  370. ['!ab OR -cd', '(!ab) OR (-cd)'],
  371. ['ab cd OR ef OR "gh ij"', '(ab cd) OR (ef) OR ("gh ij")'],
  372. ['ab (!cd)', 'ab (!cd)'],
  373. ['"ab" (!"cd")', '"ab" (!"cd")'],
  374. ];
  375. }
  376. #[DataProvider('provideconsistentOrParentheses')]
  377. public static function test__consistentOrParentheses(string $input, string $output): void {
  378. self::assertSame($output, FreshRSS_BooleanSearch::consistentOrParentheses($input));
  379. }
  380. /** @return list<list{string,string}> */
  381. public static function provideconsistentOrParentheses(): array {
  382. return [
  383. ['ab cd ef', 'ab cd ef'],
  384. ['(ab cd ef)', '(ab cd ef)'],
  385. ['("ab cd" ef)', '("ab cd" ef)'],
  386. ['"ab cd" (ef gh) "ij kl"', '"ab cd" (ef gh) "ij kl"'],
  387. ['ab (!cd)', 'ab (!cd)'],
  388. ['ab !(cd)', 'ab !(cd)'],
  389. ['(ab) -(cd)', '(ab) -(cd)'],
  390. ['ab cd OR ef OR "gh ij"', 'ab cd OR ef OR "gh ij"'],
  391. ['"plain or text" OR (cd)', '("plain or text") OR (cd)'],
  392. ['(ab) OR cd OR ef OR (gh)', '(ab) OR (cd) OR (ef) OR (gh)'],
  393. ['(ab (cd OR ef)) OR gh OR ij OR (kl)', '(ab (cd OR ef)) OR (gh) OR (ij) OR (kl)'],
  394. ['(ab (cd OR ef OR (gh))) OR ij', '(ab ((cd) OR (ef) OR (gh))) OR (ij)'],
  395. ['(ab (!cd OR ef OR (gh))) OR ij', '(ab ((!cd) OR (ef) OR (gh))) OR (ij)'],
  396. ['(ab !(cd OR ef OR !(gh))) OR ij', '(ab !((cd) OR (ef) OR !(gh))) OR (ij)'],
  397. ['"ab" OR (!"cd")', '("ab") OR (!"cd")'],
  398. ];
  399. }
  400. /**
  401. * @param array<string> $values
  402. */
  403. #[DataProvider('provideParentheses')]
  404. public function test__parentheses(string $input, string $sql, array $values): void {
  405. [$filterValues, $filterSearch] = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  406. self::assertSame(trim($sql), trim($filterSearch));
  407. self::assertSame($values, $filterValues);
  408. }
  409. /** @return list<list<mixed>> */
  410. public static function provideParentheses(): array {
  411. return [
  412. [
  413. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  414. ' ((e.id_feed IN (?) )) AND ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ) OR (e.id_feed IN (?) )) AND' .
  415. ' (((e.id_feed IN (?) )) OR ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ))) ',
  416. [1, 2, 3, 4, 5, 6, 7]
  417. ],
  418. [
  419. 'c:1 OR c:2,3',
  420. ' (e.id_feed IN (SELECT f.id FROM `_feed` f WHERE f.category IN (?)) ) OR (e.id_feed IN (SELECT f.id FROM `_feed` f WHERE f.category IN (?,?)) ) ',
  421. [1, 2, 3]
  422. ],
  423. [
  424. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  425. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  426. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  427. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 12]
  428. ],
  429. [
  430. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  431. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  432. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  433. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  434. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  435. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', 3, '%World%', 'Bleu']
  436. ],
  437. [
  438. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  439. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  440. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  441. ],
  442. [
  443. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  444. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  445. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  446. ],
  447. [
  448. 'intitle:"(test)"',
  449. '(e.title LIKE ? )',
  450. ['%(test)%'],
  451. ],
  452. [
  453. 'intitle:\'"hello world"\'',
  454. '(e.title LIKE ? )',
  455. ['%"hello world"%'],
  456. ],
  457. [
  458. 'intext:\'"hello world"\'',
  459. '(e.content LIKE ? )',
  460. ['%"hello world"%'],
  461. ],
  462. [
  463. '(ab) OR (cd) OR (ef)',
  464. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  465. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  466. ],
  467. [
  468. '("plain or text") OR (cd)',
  469. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  470. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  471. ],
  472. [
  473. '"plain or text" OR cd',
  474. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  475. ['%plain or text%', '%plain or text%', '%cd%', '%cd%'],
  476. ],
  477. [
  478. '"plain OR text" OR cd',
  479. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) ',
  480. ['%plain OR text%', '%plain OR text%', '%cd%', '%cd%'],
  481. ],
  482. [
  483. 'ab OR cd OR (ef)',
  484. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ',
  485. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  486. ],
  487. [
  488. 'ab OR cd OR ef',
  489. '((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )',
  490. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  491. ],
  492. [
  493. '(ab) cd OR ef OR (gh)',
  494. '(((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  495. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  496. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  497. ],
  498. [
  499. '(ab) OR cd OR ef OR (gh)',
  500. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  501. 'OR (((e.title LIKE ? OR e.content LIKE ?) )) OR (((e.title LIKE ? OR e.content LIKE ?) ))',
  502. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%'],
  503. ],
  504. [
  505. 'ab OR (!(cd OR ef))',
  506. '(((e.title LIKE ? OR e.content LIKE ?) )) OR (NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) )))',
  507. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  508. ],
  509. [
  510. 'ab !(cd OR ef)',
  511. '(((e.title LIKE ? OR e.content LIKE ?) )) AND NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  512. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  513. ],
  514. [
  515. 'ab OR !(cd OR ef)',
  516. '(((e.title LIKE ? OR e.content LIKE ?) )) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  517. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%'],
  518. ],
  519. [
  520. '(ab (!cd OR ef OR (gh))) OR !(ij OR kl)',
  521. '((((e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title NOT LIKE ? AND e.content NOT LIKE ? )) OR (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  522. 'OR (((e.title LIKE ? OR e.content LIKE ?) )))) OR NOT (((e.title LIKE ? OR e.content LIKE ?) ) OR ((e.title LIKE ? OR e.content LIKE ?) ))',
  523. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%'],
  524. ],
  525. [
  526. '"ab" "cd" ("ef") intitle:"gh" !"ij" -"kl"',
  527. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  528. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  529. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  530. ],
  531. [
  532. '&quot;ab&quot; &quot;cd&quot; (&quot;ef&quot;) intitle:&quot;gh&quot; !&quot;ij&quot; -&quot;kl&quot;',
  533. '(((e.title LIKE ? OR e.content LIKE ?) AND (e.title LIKE ? OR e.content LIKE ?) )) AND (((e.title LIKE ? OR e.content LIKE ?) )) ' .
  534. 'AND ((e.title LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? AND e.title NOT LIKE ? AND e.content NOT LIKE ? ))',
  535. ['%ab%', '%ab%', '%cd%', '%cd%', '%ef%', '%ef%', '%gh%', '%ij%', '%ij%', '%kl%', '%kl%']
  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:/Interesting/i intitle:good
  894. intext:/Interesting/i intext:good
  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. }