SearchTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_setsIntitlePropery($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', 'word1', null),
  48. array('intitle:word1 word2', 'word1', array('word2')),
  49. array('intitle:"word1 word2"', 'word1 word2', null),
  50. array("intitle:'word1 word2'", 'word1 word2', null),
  51. array('word1 intitle:word2', 'word2', array('word1')),
  52. array('word1 intitle:word2 word3', 'word2', array('word1', 'word3')),
  53. array('word1 intitle:"word2 word3"', 'word2 word3', array('word1')),
  54. array("word1 intitle:'word2 word3'", 'word2 word3', array('word1')),
  55. array('intitle:word1 intitle:word2', 'word1', array('intitle:word2')),
  56. array('intitle: word1 word2', null, array('word1', 'word2')),
  57. array('intitle:123', '123', null),
  58. array('intitle:"word1 word2" word3"', 'word1 word2', array('word3"')),
  59. array("intitle:'word1 word2' word3'", 'word1 word2', array("word3'")),
  60. array('intitle:"word1 word2\' word3"', "word1 word2' word3", null),
  61. array("intitle:'word1 word2\" word3'", 'word1 word2" word3', null),
  62. array("intitle:word1 'word2 word3' word4", 'word1', array('word2 word3', 'word4')),
  63. );
  64. }
  65. /**
  66. * @dataProvider provideAuthorSearch
  67. * @param string $input
  68. * @param string $author_value
  69. * @param string|null $search_value
  70. */
  71. public function test__construct_whenInputContainsAuthor_setsAuthorValue($input, $author_value, $search_value) {
  72. $search = new FreshRSS_Search($input);
  73. $this->assertEquals($author_value, $search->getAuthor());
  74. $this->assertEquals($search_value, $search->getSearch());
  75. }
  76. /**
  77. * @return array
  78. */
  79. public function provideAuthorSearch() {
  80. return array(
  81. array('author:word1', 'word1', null),
  82. array('author:word1 word2', 'word1', array('word2')),
  83. array('author:"word1 word2"', 'word1 word2', null),
  84. array("author:'word1 word2'", 'word1 word2', null),
  85. array('word1 author:word2', 'word2', array('word1')),
  86. array('word1 author:word2 word3', 'word2', array('word1', 'word3')),
  87. array('word1 author:"word2 word3"', 'word2 word3', array('word1')),
  88. array("word1 author:'word2 word3'", 'word2 word3', array('word1')),
  89. array('author:word1 author:word2', 'word1', array('author:word2')),
  90. array('author: word1 word2', null, array('word1', 'word2')),
  91. array('author:123', '123', null),
  92. array('author:"word1 word2" word3"', 'word1 word2', array('word3"')),
  93. array("author:'word1 word2' word3'", 'word1 word2', array("word3'")),
  94. array('author:"word1 word2\' word3"', "word1 word2' word3", null),
  95. array("author:'word1 word2\" word3'", 'word1 word2" word3', null),
  96. array("author:word1 'word2 word3' word4", 'word1', array('word2 word3', 'word4')),
  97. );
  98. }
  99. /**
  100. * @dataProvider provideInurlSearch
  101. * @param string $input
  102. * @param string $inurl_value
  103. * @param string|null $search_value
  104. */
  105. public function test__construct_whenInputContainsInurl_setsInurlValue($input, $inurl_value, $search_value) {
  106. $search = new FreshRSS_Search($input);
  107. $this->assertEquals($inurl_value, $search->getInurl());
  108. $this->assertEquals($search_value, $search->getSearch());
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function provideInurlSearch() {
  114. return array(
  115. array('inurl:word1', 'word1', null),
  116. array('inurl: word1', null, array('word1')),
  117. array('inurl:123', '123', null),
  118. array('inurl:word1 word2', 'word1', array('word2')),
  119. array('inurl:"word1 word2"', '"word1', array('word2"')),
  120. array("inurl:word1 'word2 word3' word4", 'word1', array('word2 word3', 'word4')),
  121. );
  122. }
  123. /**
  124. * @dataProvider provideDateSearch
  125. * @param string $input
  126. * @param string $min_date_value
  127. * @param string $max_date_value
  128. */
  129. public function test__construct_whenInputContainsDate_setsDateValues($input, $min_date_value, $max_date_value) {
  130. $search = new FreshRSS_Search($input);
  131. $this->assertEquals($min_date_value, $search->getMinDate());
  132. $this->assertEquals($max_date_value, $search->getMaxDate());
  133. }
  134. /**
  135. * @return array
  136. */
  137. public function provideDateSearch() {
  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', '1210516199'),
  141. array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
  142. array('date:2007-03-01/2008-05-11', '1172725200', '1210564799'),
  143. array('date:2007-03-01/', '1172725200', ''),
  144. array('date:/2008-05-11', '', '1210564799'),
  145. );
  146. }
  147. /**
  148. * @dataProvider providePubdateSearch
  149. * @param string $input
  150. * @param string $min_pubdate_value
  151. * @param string $max_pubdate_value
  152. */
  153. public function test__construct_whenInputContainsPubdate_setsPubdateValues($input, $min_pubdate_value, $max_pubdate_value) {
  154. $search = new FreshRSS_Search($input);
  155. $this->assertEquals($min_pubdate_value, $search->getMinPubdate());
  156. $this->assertEquals($max_pubdate_value, $search->getMaxPubdate());
  157. }
  158. /**
  159. * @return array
  160. */
  161. public function providePubdateSearch() {
  162. return array(
  163. array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
  164. array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
  165. array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
  166. array('pubdate:2007-03-01/2008-05-11', '1172725200', '1210564799'),
  167. array('pubdate:2007-03-01/', '1172725200', ''),
  168. array('pubdate:/2008-05-11', '', '1210564799'),
  169. );
  170. }
  171. /**
  172. * @dataProvider provideTagsSearch
  173. * @param string $input
  174. * @param string $tags_value
  175. * @param string|null $search_value
  176. */
  177. public function test__construct_whenInputContainsTags_setsTagsValue($input, $tags_value, $search_value) {
  178. $search = new FreshRSS_Search($input);
  179. $this->assertEquals($tags_value, $search->getTags());
  180. $this->assertEquals($search_value, $search->getSearch());
  181. }
  182. /**
  183. * @return array
  184. */
  185. public function provideTagsSearch() {
  186. return array(
  187. array('#word1', array('word1'), null),
  188. array('# word1', null, array('#', 'word1')),
  189. array('#123', array('123'), null),
  190. array('#word1 word2', array('word1'), array('word2')),
  191. array('#"word1 word2"', array('"word1'), array('word2"')),
  192. array('#word1 #word2', array('word1', 'word2'), null),
  193. array("#word1 'word2 word3' word4", array('word1'), array('word2 word3', 'word4')),
  194. );
  195. }
  196. /**
  197. * @dataProvider provideMultipleSearch
  198. * @param string $input
  199. * @param string $author_value
  200. * @param string $min_date_value
  201. * @param string $max_date_value
  202. * @param string $intitle_value
  203. * @param string $inurl_value
  204. * @param string $min_pubdate_value
  205. * @param string $max_pubdate_value
  206. * @param array $tags_value
  207. * @param string|null $search_value
  208. */
  209. public function test__construct_whenInputContainsMultipleKeywords_setsValues($input, $author_value, $min_date_value, $max_date_value, $intitle_value, $inurl_value, $min_pubdate_value, $max_pubdate_value, $tags_value, $search_value) {
  210. $search = new FreshRSS_Search($input);
  211. $this->assertEquals($author_value, $search->getAuthor());
  212. $this->assertEquals($min_date_value, $search->getMinDate());
  213. $this->assertEquals($max_date_value, $search->getMaxDate());
  214. $this->assertEquals($intitle_value, $search->getIntitle());
  215. $this->assertEquals($inurl_value, $search->getInurl());
  216. $this->assertEquals($min_pubdate_value, $search->getMinPubdate());
  217. $this->assertEquals($max_pubdate_value, $search->getMaxPubdate());
  218. $this->assertEquals($tags_value, $search->getTags());
  219. $this->assertEquals($search_value, $search->getSearch());
  220. $this->assertEquals($input, $search->getRawInput());
  221. }
  222. public function provideMultipleSearch() {
  223. return array(
  224. array(
  225. 'author:word1 date:2007-03-01/2008-05-11 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 #word5',
  226. 'word1',
  227. '1172725200',
  228. '1210564799',
  229. 'word2',
  230. 'word3',
  231. '1172725200',
  232. '1210564799',
  233. array('word4', 'word5'),
  234. null,
  235. ),
  236. array(
  237. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 date:2007-03-01/2008-05-11',
  238. 'word1',
  239. '1172725200',
  240. '1210564799',
  241. 'word2',
  242. 'word3',
  243. '1172725200',
  244. '1210564799',
  245. array('word4', 'word5'),
  246. array('word6'),
  247. ),
  248. array(
  249. 'word6 intitle:word2 inurl:word3 pubdate:2007-03-01/2008-05-11 #word4 author:word1 #word5 word7 date:2007-03-01/2008-05-11',
  250. 'word1',
  251. '1172725200',
  252. '1210564799',
  253. 'word2',
  254. 'word3',
  255. '1172725200',
  256. '1210564799',
  257. array('word4', 'word5'),
  258. array('word6', 'word7'),
  259. ),
  260. array(
  261. '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',
  262. 'word1',
  263. '1172725200',
  264. '1210564799',
  265. 'word2',
  266. 'word3',
  267. '1172725200',
  268. '1210564799',
  269. array('word4', 'word5'),
  270. array('word7 word8', 'word6'),
  271. ),
  272. );
  273. }
  274. }