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.

170 lines
5.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 JSONFeed 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 an RSS feed
  113. public function testInputIsRSS() {
  114. $url = 'http://feed.example.com/rss';
  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/rss', $feeds[0]->url);
  121. $this->assertEquals('rss', $feeds[0]->type);
  122. }
  123. // input URL is a JSON feed
  124. public function testInputIsJSONFeed() {
  125. $url = 'http://feed.example.com/jsonfeed';
  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/jsonfeed', $feeds[0]->url);
  132. $this->assertEquals('jsonfeed', $feeds[0]->type);
  133. }
  134. }