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.

239 lines
8.5 KiB

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