SearchTest.php 39 KB

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