SearchTest.php 13 KB

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