ContextTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. require_once(LIB_PATH . '/lib_date.php');
  3. class ContextTest extends \PHPUnit_Framework_TestCase {
  4. private $context;
  5. public function setUp() {
  6. $this->context = new FreshRSS_Context();
  7. }
  8. public function testParseSearch_whenEmpty_returnsEmptyArray() {
  9. $this->assertCount(0, $this->context->parseSearch());
  10. }
  11. /**
  12. * @dataProvider provideMultipleKeywordSearch
  13. * @param string $search
  14. * @param string $expected_values
  15. */
  16. public function testParseSearch_whenMultipleKeywords_returnArrayWithMultipleValues($search, $expected_values) {
  17. FreshRSS_Context::$search = $search;
  18. $parsed_search = $this->context->parseSearch();
  19. $this->assertEquals($expected_values, $parsed_search);
  20. }
  21. /**
  22. * @return array
  23. */
  24. public function provideMultipleKeywordSearch() {
  25. return array(
  26. array(
  27. 'intitle:word1 author:word2',
  28. array(
  29. 'intitle' => 'word1',
  30. 'author' => 'word2',
  31. ),
  32. ),
  33. array(
  34. 'author:word2 intitle:word1',
  35. array(
  36. 'intitle' => 'word1',
  37. 'author' => 'word2',
  38. ),
  39. ),
  40. array(
  41. 'author:word1 inurl:word2',
  42. array(
  43. 'author' => 'word1',
  44. 'inurl' => 'word2',
  45. ),
  46. ),
  47. array(
  48. 'inurl:word2 author:word1',
  49. array(
  50. 'author' => 'word1',
  51. 'inurl' => 'word2',
  52. ),
  53. ),
  54. array(
  55. 'date:2008-01-01/2008-02-01 pubdate:2007-01-01/2007-02-01',
  56. array(
  57. 'min_date' => '1199163600',
  58. 'max_date' => '1201928399',
  59. 'min_pubdate' => '1167627600',
  60. 'max_pubdate' => '1170392399',
  61. ),
  62. ),
  63. array(
  64. 'pubdate:2007-01-01/2007-02-01 date:2008-01-01/2008-02-01',
  65. array(
  66. 'min_date' => '1199163600',
  67. 'max_date' => '1201928399',
  68. 'min_pubdate' => '1167627600',
  69. 'max_pubdate' => '1170392399',
  70. ),
  71. ),
  72. array(
  73. 'inurl:word1 author:word2 intitle:word3 pubdate:2007-01-01/2007-02-01 date:2008-01-01/2008-02-01 hello world',
  74. array(
  75. 'inurl' => 'word1',
  76. 'author' => 'word2',
  77. 'intitle' => 'word3',
  78. 'min_date' => '1199163600',
  79. 'max_date' => '1201928399',
  80. 'min_pubdate' => '1167627600',
  81. 'max_pubdate' => '1170392399',
  82. 'search' => 'hello world',
  83. ),
  84. ),
  85. );
  86. }
  87. /**
  88. * @dataProvider provideIntitleSearch
  89. * @param string $search
  90. * @param string $expected_value
  91. */
  92. public function testParseSearch_whenIntitleKeyword_returnArrayWithIntitleValue($search, $expected_value) {
  93. FreshRSS_Context::$search = $search;
  94. $parsed_search = $this->context->parseSearch();
  95. $this->assertEquals($expected_value, $parsed_search['intitle']);
  96. }
  97. /**
  98. * @return array
  99. */
  100. public function provideIntitleSearch() {
  101. return array(
  102. array('intitle:word1', 'word1'),
  103. array('intitle:word1 word2', 'word1'),
  104. array('intitle:"word1 word2"', 'word1 word2'),
  105. array("intitle:'word1 word2'", 'word1 word2'),
  106. array('word1 intitle:word2', 'word2'),
  107. array('word1 intitle:word2 word3', 'word2'),
  108. array('word1 intitle:"word2 word3"', 'word2 word3'),
  109. array("word1 intitle:'word2 word3'", 'word2 word3'),
  110. array('intitle:word1 intitle:word2', 'word1'),
  111. array('intitle: word1 word2', ''),
  112. array('intitle:123', '123'),
  113. array('intitle:"word1 word2" word3"', 'word1 word2'),
  114. array("intitle:'word1 word2' word3'", 'word1 word2'),
  115. array('intitle:"word1 word2\' word3"', "word1 word2' word3"),
  116. array("intitle:'word1 word2\" word3'", 'word1 word2" word3'),
  117. );
  118. }
  119. /**
  120. * @dataProvider provideAuthorSearch
  121. * @param string $search
  122. * @param string $expected_value
  123. */
  124. public function testParseSearch_whenAuthorKeyword_returnArrayWithAuthorValue($search, $expected_value) {
  125. FreshRSS_Context::$search = $search;
  126. $parsed_search = $this->context->parseSearch();
  127. $this->assertEquals($expected_value, $parsed_search['author']);
  128. }
  129. /**
  130. * @return array
  131. */
  132. public function provideAuthorSearch() {
  133. return array(
  134. array('author:word1', 'word1'),
  135. array('author:word1 word2', 'word1'),
  136. array('author:"word1 word2"', 'word1 word2'),
  137. array("author:'word1 word2'", 'word1 word2'),
  138. array('word1 author:word2', 'word2'),
  139. array('word1 author:word2 word3', 'word2'),
  140. array('word1 author:"word2 word3"', 'word2 word3'),
  141. array("word1 author:'word2 word3'", 'word2 word3'),
  142. array('author:word1 author:word2', 'word1'),
  143. array('author: word1 word2', ''),
  144. array('author:123', '123'),
  145. array('author:"word1 word2" word3"', 'word1 word2'),
  146. array("author:'word1 word2' word3'", 'word1 word2'),
  147. array('author:"word1 word2\' word3"', "word1 word2' word3"),
  148. array("author:'word1 word2\" word3'", 'word1 word2" word3'),
  149. );
  150. }
  151. /**
  152. * @dataProvider provideInurlSearch
  153. * @param string $search
  154. * @param string $expected_value
  155. */
  156. public function testParseSearch_whenInurlKeyword_returnArrayWithInurlValue($search, $expected_value) {
  157. FreshRSS_Context::$search = $search;
  158. $parsed_search = $this->context->parseSearch();
  159. $this->assertEquals($expected_value, $parsed_search['inurl']);
  160. }
  161. /**
  162. * @return array
  163. */
  164. public function provideInurlSearch() {
  165. return array(
  166. array('inurl:word1', 'word1'),
  167. array('inurl: word1', ''),
  168. array('inurl:123', '123'),
  169. array('inurl:word1 word2', 'word1'),
  170. array('inurl:"word1 word2"', '"word1'),
  171. );
  172. }
  173. /**
  174. * @dataProvider provideDateSearch
  175. * @param string $search
  176. * @param string $expected_min_value
  177. * @param string $expected_max_value
  178. */
  179. public function testParseSearch_whenDateKeyword_returnArrayWithDateValues($search, $expected_min_value, $expected_max_value) {
  180. FreshRSS_Context::$search = $search;
  181. $parsed_search = $this->context->parseSearch();
  182. $this->assertEquals($expected_min_value, $parsed_search['min_date']);
  183. $this->assertEquals($expected_max_value, $parsed_search['max_date']);
  184. }
  185. /**
  186. * @return array
  187. */
  188. public function provideDateSearch() {
  189. return array(
  190. array('date:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
  191. array('date:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
  192. array('date:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
  193. array('date:2007-03-01/2008-05-11', '1172725200', '1210564799'),
  194. array('date:2007-03-01/', '1172725200', ''),
  195. array('date:/2008-05-11', '', '1210564799'),
  196. );
  197. }
  198. /**
  199. * @dataProvider providePubdateSearch
  200. * @param string $search
  201. * @param string $expected_min_value
  202. * @param string $expected_max_value
  203. */
  204. public function testParseSearch_whenPubdateKeyword_returnArrayWithPubdateValues($search, $expected_min_value, $expected_max_value) {
  205. FreshRSS_Context::$search = $search;
  206. $parsed_search = $this->context->parseSearch();
  207. $this->assertEquals($expected_min_value, $parsed_search['min_pubdate']);
  208. $this->assertEquals($expected_max_value, $parsed_search['max_pubdate']);
  209. }
  210. /**
  211. * @return array
  212. */
  213. public function providePubdateSearch() {
  214. return array(
  215. array('pubdate:2007-03-01T13:00:00Z/2008-05-11T15:30:00Z', '1172754000', '1210519800'),
  216. array('pubdate:2007-03-01T13:00:00Z/P1Y2M10DT2H30M', '1172754000', '1210516199'),
  217. array('pubdate:P1Y2M10DT2H30M/2008-05-11T15:30:00Z', '1172757601', '1210519800'),
  218. array('pubdate:2007-03-01/2008-05-11', '1172725200', '1210564799'),
  219. array('pubdate:2007-03-01/', '1172725200', ''),
  220. array('pubdate:/2008-05-11', '', '1210564799'),
  221. );
  222. }
  223. }