SearchTest.php 9.3 KB

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