SearchTest.php 44 KB

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