SearchTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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'), 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', array(), 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 string $input
  69. * @param string $author_value
  70. * @param string|null $search_value
  71. */
  72. public function test__construct_whenInputContainsAuthor_setsAuthorValue($input, $author_value, $search_value) {
  73. $search = new FreshRSS_Search($input);
  74. $this->assertEquals($author_value, $search->getAuthor());
  75. $this->assertEquals($search_value, $search->getSearch());
  76. }
  77. /**
  78. * @return array
  79. */
  80. public function provideAuthorSearch() {
  81. return array(
  82. array('author:word1', array('word1'), 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', array(), 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 string $input
  104. * @param string $inurl_value
  105. * @param string|null $search_value
  106. */
  107. public function test__construct_whenInputContainsInurl_setsInurlValue($input, $inurl_value, $search_value) {
  108. $search = new FreshRSS_Search($input);
  109. $this->assertEquals($inurl_value, $search->getInurl());
  110. $this->assertEquals($search_value, $search->getSearch());
  111. }
  112. /**
  113. * @return array
  114. */
  115. public function provideInurlSearch() {
  116. return array(
  117. array('inurl:word1', array('word1'), null),
  118. array('inurl: word1', array(), array('word1')),
  119. array('inurl:123', array('123'), null),
  120. array('inurl:word1 word2', array('word1'), array('word2')),
  121. array('inurl:"word1 word2"', array('"word1'), array('word2"')),
  122. array('inurl:word1 word2 inurl:word3', array('word1', 'word3'), array('word2')),
  123. array("inurl:word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
  124. ['inurl:word1+word2', ['word1+word2'], null],
  125. );
  126. }
  127. /**
  128. * @dataProvider provideDateSearch
  129. * @param string $input
  130. * @param string $min_date_value
  131. * @param string $max_date_value
  132. */
  133. public function test__construct_whenInputContainsDate_setsDateValues($input, $min_date_value, $max_date_value) {
  134. $search = new FreshRSS_Search($input);
  135. $this->assertEquals($min_date_value, $search->getMinDate());
  136. $this->assertEquals($max_date_value, $search->getMaxDate());
  137. }
  138. /**
  139. * @return array
  140. */
  141. public function provideDateSearch() {
  142. return array(
  143. array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
  144. array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210519799'),
  145. array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172754001', '1210519800'),
  146. array('date:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1),
  147. array('date:2007-03-01/', strtotime('2007-03-01'), ''),
  148. array('date:/2008-05-11', '', strtotime('2008-05-12') - 1),
  149. );
  150. }
  151. /**
  152. * @dataProvider providePubdateSearch
  153. * @param string $input
  154. * @param string $min_pubdate_value
  155. * @param string $max_pubdate_value
  156. */
  157. public function test__construct_whenInputContainsPubdate_setsPubdateValues($input, $min_pubdate_value, $max_pubdate_value) {
  158. $search = new FreshRSS_Search($input);
  159. $this->assertEquals($min_pubdate_value, $search->getMinPubdate());
  160. $this->assertEquals($max_pubdate_value, $search->getMaxPubdate());
  161. }
  162. /**
  163. * @return array
  164. */
  165. public function providePubdateSearch() {
  166. return array(
  167. array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
  168. array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210519799'),
  169. array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172754001', '1210519800'),
  170. array('pubdate:2007-03-01/2008-05-11', strtotime('2007-03-01'), strtotime('2008-05-12') - 1),
  171. array('pubdate:2007-03-01/', strtotime('2007-03-01'), ''),
  172. array('pubdate:/2008-05-11', '', strtotime('2008-05-12') - 1),
  173. );
  174. }
  175. /**
  176. * @dataProvider provideTagsSearch
  177. * @param string $input
  178. * @param string $tags_value
  179. * @param string|null $search_value
  180. */
  181. public function test__construct_whenInputContainsTags_setsTagsValue($input, $tags_value, $search_value) {
  182. $search = new FreshRSS_Search($input);
  183. $this->assertEquals($tags_value, $search->getTags());
  184. $this->assertEquals($search_value, $search->getSearch());
  185. }
  186. /**
  187. * @return array
  188. */
  189. public function provideTagsSearch() {
  190. return array(
  191. array('#word1', array('word1'), null),
  192. array('# word1', array(), array('#', 'word1')),
  193. array('#123', array('123'), null),
  194. array('#word1 word2', array('word1'), array('word2')),
  195. array('#"word1 word2"', array('"word1'), array('word2"')),
  196. array('#word1 #word2', array('word1', 'word2'), null),
  197. array("#word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
  198. ['#word1+word2', ['word1 word2'], null],
  199. );
  200. }
  201. /**
  202. * @dataProvider provideMultipleSearch
  203. * @param string $input
  204. * @param string $author_value
  205. * @param string $min_date_value
  206. * @param string $max_date_value
  207. * @param string $intitle_value
  208. * @param string $inurl_value
  209. * @param string $min_pubdate_value
  210. * @param string $max_pubdate_value
  211. * @param array $tags_value
  212. * @param string|null $search_value
  213. */
  214. public function test__construct_whenInputContainsMultipleKeywords_setsValues($input, $author_value, $min_date_value,
  215. $max_date_value, $intitle_value, $inurl_value, $min_pubdate_value, $max_pubdate_value, $tags_value, $search_value) {
  216. $search = new FreshRSS_Search($input);
  217. $this->assertEquals($author_value, $search->getAuthor());
  218. $this->assertEquals($min_date_value, $search->getMinDate());
  219. $this->assertEquals($max_date_value, $search->getMaxDate());
  220. $this->assertEquals($intitle_value, $search->getIntitle());
  221. $this->assertEquals($inurl_value, $search->getInurl());
  222. $this->assertEquals($min_pubdate_value, $search->getMinPubdate());
  223. $this->assertEquals($max_pubdate_value, $search->getMaxPubdate());
  224. $this->assertEquals($tags_value, $search->getTags());
  225. $this->assertEquals($search_value, $search->getSearch());
  226. $this->assertEquals($input, $search->getRawInput());
  227. }
  228. public function provideMultipleSearch() {
  229. return array(
  230. array(
  231. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  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. null,
  241. ),
  242. array(
  243. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 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'),
  253. ),
  254. array(
  255. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 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('word6', 'word7'),
  265. ),
  266. array(
  267. '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',
  268. array('word1'),
  269. strtotime('2007-03-01'),
  270. strtotime('2008-05-12') - 1,
  271. array('word2'),
  272. array('word3'),
  273. strtotime('2007-03-01'),
  274. strtotime('2008-05-12') - 1,
  275. array('word4', 'word5'),
  276. array('word7 word8', 'word6'),
  277. ),
  278. );
  279. }
  280. }