SearchTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. <?php
  2. require_once(LIB_PATH . '/lib_date.php');
  3. class SearchTest extends PHPUnit\Framework\TestCase {
  4. /**
  5. * @dataProvider provideEmptyInput
  6. */
  7. public function test__construct_whenInputIsEmpty_getsOnlyNullValues(string $input): void {
  8. $search = new FreshRSS_Search($input);
  9. self::assertEquals('', $search->getRawInput());
  10. self::assertNull($search->getIntitle());
  11. self::assertNull($search->getMinDate());
  12. self::assertNull($search->getMaxDate());
  13. self::assertNull($search->getMinPubdate());
  14. self::assertNull($search->getMaxPubdate());
  15. self::assertNull($search->getAuthor());
  16. self::assertNull($search->getTags());
  17. self::assertNull($search->getSearch());
  18. }
  19. /**
  20. * Return an array of values for the search object.
  21. * Here is the description of the values
  22. * @return array{array{''},array{' '}}
  23. */
  24. public function provideEmptyInput(): array {
  25. return [
  26. [''],
  27. [' '],
  28. ];
  29. }
  30. /**
  31. * @dataProvider provideIntitleSearch
  32. * @param array<string>|null $intitle_value
  33. * @param array<string>|null $search_value
  34. */
  35. public function test__construct_whenInputContainsIntitle_setsIntitleProperty(string $input, ?array $intitle_value, ?array $search_value): void {
  36. $search = new FreshRSS_Search($input);
  37. self::assertEquals($intitle_value, $search->getIntitle());
  38. self::assertEquals($search_value, $search->getSearch());
  39. }
  40. /**
  41. * @return array<array<mixed>>
  42. */
  43. public function provideIntitleSearch(): array {
  44. return array(
  45. array('intitle:word1', array('word1'), null),
  46. array('intitle:word1-word2', array('word1-word2'), null),
  47. array('intitle:word1 word2', array('word1'), array('word2')),
  48. array('intitle:"word1 word2"', array('word1 word2'), null),
  49. array("intitle:'word1 word2'", array('word1 word2'), null),
  50. array('word1 intitle:word2', array('word2'), array('word1')),
  51. array('word1 intitle:word2 word3', array('word2'), array('word1', 'word3')),
  52. array('word1 intitle:"word2 word3"', array('word2 word3'), array('word1')),
  53. array("word1 intitle:'word2 word3'", array('word2 word3'), array('word1')),
  54. array('intitle:word1 intitle:word2', array('word1', 'word2'), null),
  55. array('intitle: word1 word2', null, array('word1', 'word2')),
  56. array('intitle:123', array('123'), null),
  57. array('intitle:"word1 word2" word3"', array('word1 word2'), array('word3"')),
  58. array("intitle:'word1 word2' word3'", array('word1 word2'), array("word3'")),
  59. array('intitle:"word1 word2\' word3"', array("word1 word2' word3"), null),
  60. array("intitle:'word1 word2\" word3'", array('word1 word2" word3'), null),
  61. array("intitle:word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
  62. ['intitle:word1+word2', ['word1+word2'], null],
  63. );
  64. }
  65. /**
  66. * @dataProvider provideAuthorSearch
  67. * @param array<string>|null $author_value
  68. * @param array<string>|null $search_value
  69. */
  70. public function test__construct_whenInputContainsAuthor_setsAuthorValue(string $input, ?array $author_value, ?array $search_value): void {
  71. $search = new FreshRSS_Search($input);
  72. self::assertEquals($author_value, $search->getAuthor());
  73. self::assertEquals($search_value, $search->getSearch());
  74. }
  75. /**
  76. * @return array<array<mixed>>
  77. */
  78. public function provideAuthorSearch(): array {
  79. return array(
  80. array('author:word1', array('word1'), null),
  81. array('author:word1-word2', array('word1-word2'), null),
  82. array('author:word1 word2', array('word1'), array('word2')),
  83. array('author:"word1 word2"', array('word1 word2'), null),
  84. array("author:'word1 word2'", array('word1 word2'), null),
  85. array('word1 author:word2', array('word2'), array('word1')),
  86. array('word1 author:word2 word3', array('word2'), array('word1', 'word3')),
  87. array('word1 author:"word2 word3"', array('word2 word3'), array('word1')),
  88. array("word1 author:'word2 word3'", array('word2 word3'), array('word1')),
  89. array('author:word1 author:word2', array('word1', 'word2'), null),
  90. array('author: word1 word2', null, array('word1', 'word2')),
  91. array('author:123', array('123'), null),
  92. array('author:"word1 word2" word3"', array('word1 word2'), array('word3"')),
  93. array("author:'word1 word2' word3'", array('word1 word2'), array("word3'")),
  94. array('author:"word1 word2\' word3"', array("word1 word2' word3"), null),
  95. array("author:'word1 word2\" word3'", array('word1 word2" word3'), null),
  96. array("author:word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
  97. ['author:word1+word2', ['word1+word2'], null],
  98. );
  99. }
  100. /**
  101. * @dataProvider provideInurlSearch
  102. * @param array<string>|null $inurl_value
  103. * @param array<string>|null $search_value
  104. */
  105. public function test__construct_whenInputContainsInurl_setsInurlValue(string $input, ?array $inurl_value, ?array $search_value): void {
  106. $search = new FreshRSS_Search($input);
  107. self::assertEquals($inurl_value, $search->getInurl());
  108. self::assertEquals($search_value, $search->getSearch());
  109. }
  110. /**
  111. * @return array<array<mixed>>
  112. */
  113. public function provideInurlSearch(): array {
  114. return array(
  115. array('inurl:word1', array('word1'), null),
  116. array('inurl: word1', array(), array('word1')),
  117. array('inurl:123', array('123'), null),
  118. array('inurl:word1 word2', array('word1'), array('word2')),
  119. array('inurl:"word1 word2"', array('"word1'), array('word2"')),
  120. array('inurl:word1 word2 inurl:word3', array('word1', 'word3'), array('word2')),
  121. array("inurl:word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
  122. ['inurl:word1+word2', ['word1+word2'], null],
  123. );
  124. }
  125. /**
  126. * @dataProvider provideDateSearch
  127. */
  128. public function test__construct_whenInputContainsDate_setsDateValues(string $input, ?int $min_date_value, ?int $max_date_value): void {
  129. $search = new FreshRSS_Search($input);
  130. self::assertEquals($min_date_value, $search->getMinDate());
  131. self::assertEquals($max_date_value, $search->getMaxDate());
  132. }
  133. /**
  134. * @return array<array<mixed>>
  135. */
  136. public function provideDateSearch(): array {
  137. return array(
  138. array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800),
  139. array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799),
  140. array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800),
  141. array('date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1),
  142. array('date:2007-03-01/', strtotime('2007-03-01'), null),
  143. array('date:/2008-05-11', null, strtotime('2008-05-12') - 1),
  144. );
  145. }
  146. /**
  147. * @dataProvider providePubdateSearch
  148. */
  149. public function test__construct_whenInputContainsPubdate_setsPubdateValues(string $input, ?int $min_pubdate_value, ?int $max_pubdate_value): void {
  150. $search = new FreshRSS_Search($input);
  151. self::assertEquals($min_pubdate_value, $search->getMinPubdate());
  152. self::assertEquals($max_pubdate_value, $search->getMaxPubdate());
  153. }
  154. /**
  155. * @return array<array<mixed>>
  156. */
  157. public function providePubdateSearch(): array {
  158. return array(
  159. array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', 1172754000, 1210519800),
  160. array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', 1172754000, 1210519799),
  161. array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', 1172754001, 1210519800),
  162. array('pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1),
  163. array('pubdate:2007-03-01/', strtotime('2007-03-01'), null),
  164. array('pubdate:/2008-05-11', null, strtotime('2008-05-12') - 1),
  165. );
  166. }
  167. /**
  168. * @dataProvider provideTagsSearch
  169. * @param array<string>|null $tags_value
  170. * @param array<string>|null $search_value
  171. */
  172. public function test__construct_whenInputContainsTags_setsTagsValue(string $input, ?array $tags_value, ?array $search_value): void {
  173. $search = new FreshRSS_Search($input);
  174. self::assertEquals($tags_value, $search->getTags());
  175. self::assertEquals($search_value, $search->getSearch());
  176. }
  177. /**
  178. * @return array<array<string|array<string>|null>>
  179. */
  180. public function provideTagsSearch(): array {
  181. return array(
  182. array('#word1', array('word1'), null),
  183. array('# word1', null, array('#', 'word1')),
  184. array('#123', array('123'), null),
  185. array('#word1 word2', array('word1'), array('word2')),
  186. array('#"word1 word2"', array('"word1'), array('word2"')),
  187. array('#word1 #word2', array('word1', 'word2'), null),
  188. array("#word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
  189. ['#word1+word2', ['word1 word2'], null],
  190. );
  191. }
  192. /**
  193. * @dataProvider provideMultipleSearch
  194. * @param array<string>|null $author_value
  195. * @param array<string> $intitle_value
  196. * @param array<string>|null $inurl_value
  197. * @param array<string>|null $tags_value
  198. * @param array<string>|null $search_value
  199. */
  200. public function test__construct_whenInputContainsMultipleKeywords_setsValues(string $input, ?array $author_value, ?int $min_date_value,
  201. ?int $max_date_value, ?array $intitle_value, ?array $inurl_value, ?int $min_pubdate_value,
  202. ?int $max_pubdate_value, ?array $tags_value, ?array $search_value): void {
  203. $search = new FreshRSS_Search($input);
  204. self::assertEquals($author_value, $search->getAuthor());
  205. self::assertEquals($min_date_value, $search->getMinDate());
  206. self::assertEquals($max_date_value, $search->getMaxDate());
  207. self::assertEquals($intitle_value, $search->getIntitle());
  208. self::assertEquals($inurl_value, $search->getInurl());
  209. self::assertEquals($min_pubdate_value, $search->getMinPubdate());
  210. self::assertEquals($max_pubdate_value, $search->getMaxPubdate());
  211. self::assertEquals($tags_value, $search->getTags());
  212. self::assertEquals($search_value, $search->getSearch());
  213. self::assertEquals($input, $search->getRawInput());
  214. }
  215. /** @return array<array<mixed>> */
  216. public function provideMultipleSearch(): array {
  217. return array(
  218. array(
  219. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  220. array('word1'),
  221. strtotime('2007-03-01'),
  222. strtotime('2008-05-12') - 1,
  223. array('word2'),
  224. array('word3'),
  225. strtotime('2007-03-01'),
  226. strtotime('2008-05-12') - 1,
  227. array('word4', 'word5'),
  228. null,
  229. ),
  230. array(
  231. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  232. array('word1'),
  233. strtotime('2007-03-01'),
  234. strtotime('2008-05-12') - 1,
  235. array('word2'),
  236. array('word3'),
  237. strtotime('2007-03-01'),
  238. strtotime('2008-05-12') - 1,
  239. array('word4', 'word5'),
  240. array('word6'),
  241. ),
  242. array(
  243. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  244. array('word1'),
  245. strtotime('2007-03-01'),
  246. strtotime('2008-05-12') - 1,
  247. array('word2'),
  248. array('word3'),
  249. strtotime('2007-03-01'),
  250. strtotime('2008-05-12') - 1,
  251. array('word4', 'word5'),
  252. array('word6', 'word7'),
  253. ),
  254. array(
  255. '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',
  256. array('word1'),
  257. strtotime('2007-03-01'),
  258. strtotime('2008-05-12') - 1,
  259. array('word2'),
  260. array('word3'),
  261. strtotime('2007-03-01'),
  262. strtotime('2008-05-12') - 1,
  263. array('word4', 'word5'),
  264. array('word7 word8', 'word6'),
  265. ),
  266. );
  267. }
  268. /**
  269. * @dataProvider provideParentheses
  270. * @param array<string> $values
  271. */
  272. public function test__construct_parentheses(string $input, string $sql, array $values): void {
  273. list($filterValues, $filterSearch) = FreshRSS_EntryDAOPGSQL::sqlBooleanSearch('e.', new FreshRSS_BooleanSearch($input));
  274. self::assertEquals($sql, $filterSearch);
  275. self::assertEquals($values, $filterValues);
  276. }
  277. /** @return array<array<mixed>> */
  278. public function provideParentheses(): array {
  279. return [
  280. [
  281. 'f:1 (f:2 OR f:3 OR f:4) (f:5 OR (f:6 OR f:7))',
  282. ' ((e.id_feed IN (?) )) AND ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ) OR (e.id_feed IN (?) )) AND' .
  283. ' (((e.id_feed IN (?) )) OR ((e.id_feed IN (?) ) OR (e.id_feed IN (?) ))) ',
  284. ['1', '2', '3', '4', '5', '6', '7']
  285. ],
  286. [
  287. '#tag Hello OR (author:Alice inurl:example) OR (f:3 intitle:World) OR L:12',
  288. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) OR ((e.author LIKE ? AND e.link LIKE ? )) OR" .
  289. ' ((e.id_feed IN (?) AND e.title LIKE ? )) OR ((e.id IN (SELECT et.id_entry FROM `_entrytag` et WHERE et.id_tag IN (?)) )) ',
  290. ['%tag #%','%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', '12']
  291. ],
  292. [
  293. '#tag Hello (author:Alice inurl:example) (f:3 intitle:World) label:Bleu',
  294. " ((TRIM(e.tags) || ' #' LIKE ? AND (e.title LIKE ? OR e.content LIKE ?) )) AND" .
  295. ' ((e.author LIKE ? AND e.link LIKE ? )) AND' .
  296. ' ((e.id_feed IN (?) AND e.title LIKE ? )) AND' .
  297. ' ((e.id IN (SELECT et.id_entry FROM `_entrytag` et, `_tag` t WHERE et.id_tag = t.id AND t.name IN (?)) )) ',
  298. ['%tag #%', '%Hello%', '%Hello%', '%Alice%', '%example%', '3', '%World%', 'Bleu']
  299. ],
  300. [
  301. '!((author:Alice intitle:hello) OR (author:Bob intitle:world))',
  302. ' NOT (((e.author LIKE ? AND e.title LIKE ? )) OR ((e.author LIKE ? AND e.title LIKE ? ))) ',
  303. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  304. ],
  305. [
  306. '(author:Alice intitle:hello) !(author:Bob intitle:world)',
  307. ' ((e.author LIKE ? AND e.title LIKE ? )) AND NOT ((e.author LIKE ? AND e.title LIKE ? )) ',
  308. ['%Alice%', '%hello%', '%Bob%', '%world%'],
  309. ],
  310. [
  311. 'intitle:"\\(test\\)"',
  312. '(e.title LIKE ? )',
  313. ['%(test)%'],
  314. ],
  315. ];
  316. }
  317. }