You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
7.8 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class FindFeedsTest extends PHPUnit_Framework_TestCase {
  5. private $http;
  6. public function setUp() {
  7. $this->client = new Feeds();
  8. $this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
  9. $this->client->mc = null;
  10. }
  11. private function parse($params) {
  12. $request = new Request($params);
  13. $response = new Response();
  14. return $this->client->find($request, $response);
  15. }
  16. // h-feed with no alternates
  17. public function testMf2HFeed() {
  18. $url = 'http://feed.example.com/h-feed-with-child-author';
  19. $response = $this->parse(['url' => $url]);
  20. $body = $response->getContent();
  21. $this->assertEquals(200, $response->getStatusCode());
  22. $feeds = json_decode($body)->feeds;
  23. $this->assertEquals(1, count($feeds));
  24. $this->assertEquals('http://feed.example.com/h-feed-with-child-author', $feeds[0]->url);
  25. $this->assertEquals('microformats', $feeds[0]->type);
  26. }
  27. // h-feed that links to Atom alternate
  28. public function testMf2WithAtomAlternate() {
  29. $url = 'http://feed.example.com/h-feed-with-atom-alternate';
  30. $response = $this->parse(['url' => $url]);
  31. $body = $response->getContent();
  32. $this->assertEquals(200, $response->getStatusCode());
  33. $feeds = json_decode($body)->feeds;
  34. $this->assertEquals(2, count($feeds));
  35. // Should rank h-feed above Atom
  36. $this->assertEquals('http://feed.example.com/h-feed-with-atom-alternate', $feeds[0]->url);
  37. $this->assertEquals('microformats', $feeds[0]->type);
  38. $this->assertEquals('http://feed.example.com/atom', $feeds[1]->url);
  39. $this->assertEquals('atom', $feeds[1]->type);
  40. }
  41. // h-feed that links to RSS alternate
  42. public function testMf2WithRSSAlternate() {
  43. $url = 'http://feed.example.com/h-feed-with-rss-alternate';
  44. $response = $this->parse(['url' => $url]);
  45. $body = $response->getContent();
  46. $this->assertEquals(200, $response->getStatusCode());
  47. $feeds = json_decode($body)->feeds;
  48. $this->assertEquals(2, count($feeds));
  49. // Should rank JSONFeed above Atom
  50. $this->assertEquals('http://feed.example.com/h-feed-with-rss-alternate', $feeds[0]->url);
  51. $this->assertEquals('microformats', $feeds[0]->type);
  52. $this->assertEquals('http://feed.example.com/podcast.xml', $feeds[1]->url);
  53. $this->assertEquals('rss', $feeds[1]->type);
  54. }
  55. // No mf2 but links to Atom alternate
  56. public function testNoMf2() {
  57. $url = 'http://feed.example.com/html-with-atom-alternate';
  58. $response = $this->parse(['url' => $url]);
  59. $body = $response->getContent();
  60. $this->assertEquals(200, $response->getStatusCode());
  61. $feeds = json_decode($body)->feeds;
  62. $this->assertEquals(1, count($feeds));
  63. $this->assertEquals('http://feed.example.com/atom', $feeds[0]->url);
  64. $this->assertEquals('atom', $feeds[0]->type);
  65. }
  66. public function testNoMf2WithJSONAndAtom() {
  67. $url = 'http://feed.example.com/html-with-json-and-atom';
  68. $response = $this->parse(['url' => $url]);
  69. $body = $response->getContent();
  70. $this->assertEquals(200, $response->getStatusCode());
  71. $feeds = json_decode($body)->feeds;
  72. $this->assertEquals(2, count($feeds));
  73. // Should rank JSONFeed above Atom
  74. $this->assertEquals('http://feed.example.com/jsonfeed', $feeds[0]->url);
  75. $this->assertEquals('jsonfeed', $feeds[0]->type);
  76. $this->assertEquals('http://feed.example.com/atom', $feeds[1]->url);
  77. $this->assertEquals('atom', $feeds[1]->type);
  78. }
  79. // input URL is an Atom feed
  80. public function testInputIsAtom() {
  81. $url = 'http://feed.example.com/atom';
  82. $response = $this->parse(['url' => $url]);
  83. $body = $response->getContent();
  84. $this->assertEquals(200, $response->getStatusCode());
  85. $feeds = json_decode($body)->feeds;
  86. $this->assertEquals(1, count($feeds));
  87. $this->assertEquals('http://feed.example.com/atom', $feeds[0]->url);
  88. $this->assertEquals('atom', $feeds[0]->type);
  89. }
  90. // input URL is an RSS feed with xml content type
  91. public function testInputIsRSSWithXML() {
  92. $url = 'http://feed.example.com/rss-xml-content-type';
  93. $response = $this->parse(['url' => $url]);
  94. $body = $response->getContent();
  95. $this->assertEquals(200, $response->getStatusCode());
  96. $feeds = json_decode($body)->feeds;
  97. $this->assertEquals(1, count($feeds));
  98. $this->assertEquals('http://feed.example.com/rss-xml-content-type', $feeds[0]->url);
  99. $this->assertEquals('rss', $feeds[0]->type);
  100. }
  101. // input URL redirects to an Atom feed
  102. public function testInputIsRedirectToAtom() {
  103. $url = 'http://feed.example.com/redirect-to-atom';
  104. $response = $this->parse(['url' => $url]);
  105. $body = $response->getContent();
  106. $this->assertEquals(200, $response->getStatusCode());
  107. $feeds = json_decode($body)->feeds;
  108. $this->assertEquals(1, count($feeds));
  109. $this->assertEquals('http://feed.example.com/atom', $feeds[0]->url);
  110. $this->assertEquals('atom', $feeds[0]->type);
  111. }
  112. // input URL is a temporary redirect to another page.
  113. // report the original input URL
  114. public function testInputIsTemporaryRedirect() {
  115. $url = 'http://feed.example.com/temporary-redirect';
  116. $response = $this->parse(['url' => $url]);
  117. $body = $response->getContent();
  118. $this->assertEquals(200, $response->getStatusCode());
  119. $feeds = json_decode($body)->feeds;
  120. $this->assertEquals(1, count($feeds));
  121. $this->assertEquals('http://feed.example.com/temporary-redirect', $feeds[0]->url);
  122. $this->assertEquals('microformats', $feeds[0]->type);
  123. }
  124. public function testInputIsPermanentRedirect() {
  125. $url = 'http://feed.example.com/permanent-redirect';
  126. $response = $this->parse(['url' => $url]);
  127. $body = $response->getContent();
  128. $this->assertEquals(200, $response->getStatusCode());
  129. $feeds = json_decode($body)->feeds;
  130. $this->assertEquals(1, count($feeds));
  131. $this->assertEquals('http://feed.example.com/permanent-redirect-target', $feeds[0]->url);
  132. $this->assertEquals('microformats', $feeds[0]->type);
  133. }
  134. // input URL is an RSS feed
  135. public function testInputIsRSS() {
  136. $url = 'http://feed.example.com/rss';
  137. $response = $this->parse(['url' => $url]);
  138. $body = $response->getContent();
  139. $this->assertEquals(200, $response->getStatusCode());
  140. $feeds = json_decode($body)->feeds;
  141. $this->assertEquals(1, count($feeds));
  142. $this->assertEquals('http://feed.example.com/rss', $feeds[0]->url);
  143. $this->assertEquals('rss', $feeds[0]->type);
  144. }
  145. // input URL is a JSON feed
  146. public function testInputIsJSONFeed() {
  147. $url = 'http://feed.example.com/jsonfeed';
  148. $response = $this->parse(['url' => $url]);
  149. $body = $response->getContent();
  150. $this->assertEquals(200, $response->getStatusCode());
  151. $feeds = json_decode($body)->feeds;
  152. $this->assertEquals(1, count($feeds));
  153. $this->assertEquals('http://feed.example.com/jsonfeed', $feeds[0]->url);
  154. $this->assertEquals('jsonfeed', $feeds[0]->type);
  155. }
  156. public function testInputIsMicroformats2JSON() {
  157. $url = 'http://feed.example.com/microformats2-json';
  158. $response = $this->parse(['url' => $url]);
  159. $body = $response->getContent();
  160. $this->assertEquals(200, $response->getStatusCode());
  161. $feeds = json_decode($body)->feeds;
  162. $this->assertEquals(1, count($feeds));
  163. $this->assertEquals('http://feed.example.com/microformats2-json', $feeds[0]->url);
  164. $this->assertEquals('microformats', $feeds[0]->type);
  165. }
  166. public function testInputIsMF2JSON() {
  167. $url = 'http://feed.example.com/mf2-json';
  168. $response = $this->parse(['url' => $url]);
  169. $body = $response->getContent();
  170. $this->assertEquals(200, $response->getStatusCode());
  171. $feeds = json_decode($body)->feeds;
  172. $this->assertEquals(1, count($feeds));
  173. $this->assertEquals('http://feed.example.com/mf2-json', $feeds[0]->url);
  174. $this->assertEquals('microformats', $feeds[0]->type);
  175. }
  176. }