SearchTest.php 13 KB

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