SearchTest.php 42 KB

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