SearchTest.php 46 KB

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