4
0

SearchTest.php 50 KB

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