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.

277 lines
12 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class FeedTest extends PHPUnit_Framework_TestCase {
  5. private $http;
  6. public function setUp() {
  7. $this->client = new Parse();
  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->parse($request, $response);
  15. }
  16. public function testListOfHEntrys() {
  17. $url = 'http://feed.example.com/list-of-hentrys';
  18. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  19. $body = $response->getContent();
  20. $this->assertEquals(200, $response->getStatusCode());
  21. $data = json_decode($body)->data;
  22. $this->assertEquals('feed', $data->type);
  23. $this->assertEquals(4, count($data->items));
  24. $this->assertEquals('One', $data->items[0]->name);
  25. $this->assertEquals('Two', $data->items[1]->name);
  26. $this->assertEquals('Three', $data->items[2]->name);
  27. $this->assertEquals('Four', $data->items[3]->name);
  28. }
  29. public function testListOfHEntrysWithHCard() {
  30. $url = 'http://feed.example.com/list-of-hentrys-with-h-card';
  31. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  32. $body = $response->getContent();
  33. $this->assertEquals(200, $response->getStatusCode());
  34. $data = json_decode($body)->data;
  35. $this->assertEquals('feed', $data->type);
  36. $this->assertEquals(4, count($data->items));
  37. $this->assertEquals('One', $data->items[0]->name);
  38. $this->assertEquals('Two', $data->items[1]->name);
  39. $this->assertEquals('Three', $data->items[2]->name);
  40. $this->assertEquals('Four', $data->items[3]->name);
  41. // Check that the author h-card was matched up with each h-entry
  42. $this->assertEquals('Author Name', $data->items[0]->author->name);
  43. $this->assertEquals('Author Name', $data->items[1]->author->name);
  44. $this->assertEquals('Author Name', $data->items[2]->author->name);
  45. $this->assertEquals('Author Name', $data->items[3]->author->name);
  46. }
  47. public function testShortListOfHEntrysWithHCard() {
  48. $url = 'http://feed.example.com/short-list-of-hentrys-with-h-card';
  49. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  50. $body = $response->getContent();
  51. $this->assertEquals(200, $response->getStatusCode());
  52. $data = json_decode($body)->data;
  53. $this->assertEquals('feed', $data->type);
  54. // This test should find the h-entry rather than the h-card, because expect=feed
  55. $this->assertEquals('entry', $data->items[0]->type);
  56. $this->assertEquals('http://feed.example.com/1', $data->items[0]->url);
  57. $this->assertEquals('Author', $data->items[0]->author->name);
  58. }
  59. public function testTopLevelHFeed() {
  60. $url = 'http://feed.example.com/top-level-h-feed';
  61. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  62. $body = $response->getContent();
  63. $this->assertEquals(200, $response->getStatusCode());
  64. $data = json_decode($body)->data;
  65. $this->assertEquals('feed', $data->type);
  66. $this->assertEquals(4, count($data->items));
  67. $this->assertEquals('One', $data->items[0]->name);
  68. $this->assertEquals('Two', $data->items[1]->name);
  69. $this->assertEquals('Three', $data->items[2]->name);
  70. $this->assertEquals('Four', $data->items[3]->name);
  71. }
  72. public function testHCardWithChildHEntrys() {
  73. $url = 'http://feed.example.com/h-card-with-child-h-entrys';
  74. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  75. $body = $response->getContent();
  76. $this->assertEquals(200, $response->getStatusCode());
  77. $data = json_decode($body)->data;
  78. $this->assertEquals('feed', $data->type);
  79. $this->assertEquals(4, count($data->items));
  80. $this->assertEquals('One', $data->items[0]->name);
  81. $this->assertEquals('Two', $data->items[1]->name);
  82. $this->assertEquals('Three', $data->items[2]->name);
  83. $this->assertEquals('Four', $data->items[3]->name);
  84. }
  85. public function testHCardWithSiblingHEntrys() {
  86. $url = 'http://feed.example.com/h-card-with-sibling-h-entrys';
  87. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  88. $body = $response->getContent();
  89. $this->assertEquals(200, $response->getStatusCode());
  90. $data = json_decode($body)->data;
  91. $this->assertEquals('feed', $data->type);
  92. $this->assertEquals(4, count($data->items));
  93. $this->assertEquals('One', $data->items[0]->name);
  94. $this->assertEquals('Two', $data->items[1]->name);
  95. $this->assertEquals('Three', $data->items[2]->name);
  96. $this->assertEquals('Four', $data->items[3]->name);
  97. // Check that the author h-card was matched up with each h-entry
  98. $this->assertEquals('Author Name', $data->items[0]->author->name);
  99. $this->assertEquals('Author Name', $data->items[1]->author->name);
  100. $this->assertEquals('Author Name', $data->items[2]->author->name);
  101. $this->assertEquals('Author Name', $data->items[3]->author->name);
  102. }
  103. public function testHCardWithChildHFeed() {
  104. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  105. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  106. $body = $response->getContent();
  107. $this->assertEquals(200, $response->getStatusCode());
  108. $data = json_decode($body)->data;
  109. $this->assertEquals('feed', $data->type);
  110. $this->assertEquals(4, count($data->items));
  111. $this->assertEquals('One', $data->items[0]->name);
  112. $this->assertEquals('Two', $data->items[1]->name);
  113. $this->assertEquals('Three', $data->items[2]->name);
  114. $this->assertEquals('Four', $data->items[3]->name);
  115. // Check that the author h-card was matched up with each h-entry
  116. $this->assertEquals('Author Name', $data->items[0]->author->name);
  117. $this->assertEquals('Author Name', $data->items[1]->author->name);
  118. $this->assertEquals('Author Name', $data->items[2]->author->name);
  119. $this->assertEquals('Author Name', $data->items[3]->author->name);
  120. }
  121. public function testHCardWithChildHFeedNoExpect() {
  122. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  123. $response = $this->parse(['url' => $url]);
  124. $body = $response->getContent();
  125. $this->assertEquals(200, $response->getStatusCode());
  126. $data = json_decode($body)->data;
  127. $this->assertEquals('card', $data->type);
  128. $this->assertEquals('Author Name', $data->name);
  129. }
  130. public function testJSONFeed() {
  131. $url = 'http://feed.example.com/jsonfeed';
  132. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  133. $body = $response->getContent();
  134. $this->assertEquals(200, $response->getStatusCode());
  135. $data = json_decode($body)->data;
  136. $this->assertEquals(10, count($data->items));
  137. for($i=0; $i<8; $i++) {
  138. $this->assertEquals('entry', $data->items[$i]->type);
  139. $this->assertEquals('manton', $data->items[$i]->author->name);
  140. $this->assertEquals('http://www.manton.org', $data->items[$i]->author->url);
  141. $this->assertNotEmpty($data->items[$i]->url);
  142. $this->assertNotEmpty($data->items[$i]->uid);
  143. $this->assertNotEmpty($data->items[$i]->published);
  144. $this->assertNotEmpty($data->items[$i]->content->html);
  145. $this->assertNotEmpty($data->items[$i]->content->text);
  146. }
  147. $this->assertEquals('<p>Lots of good feedback on <a href="http://help.micro.blog/2017/wordpress-import/">the WordPress import</a>. Made a couple improvements this morning. Overall, pretty good.</p>', $data->items[9]->content->html);
  148. $this->assertEquals('Lots of good feedback on the WordPress import. Made a couple improvements this morning. Overall, pretty good.', $data->items[9]->content->text);
  149. $this->assertEquals('http://www.manton.org/2017/11/5975.html', $data->items[9]->url);
  150. $this->assertEquals('http://www.manton.org/2017/11/5975.html', $data->items[9]->uid);
  151. $this->assertEquals('2017-11-07T15:04:01+00:00', $data->items[9]->published);
  152. $this->assertEquals('feed', $data->type);
  153. }
  154. public function testAtomFeed() {
  155. $url = 'http://feed.example.com/atom';
  156. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  157. $body = $response->getContent();
  158. $this->assertEquals(200, $response->getStatusCode());
  159. $data = json_decode($body)->data;
  160. $this->assertEquals(8, count($data->items));
  161. for($i=0; $i<8; $i++) {
  162. $this->assertEquals('entry', $data->items[$i]->type);
  163. $this->assertEquals('Tantek', $data->items[$i]->author->name);
  164. $this->assertEquals('http://tantek.com/', $data->items[$i]->author->url);
  165. $this->assertNotEmpty($data->items[$i]->url);
  166. $this->assertNotEmpty($data->items[$i]->published);
  167. $this->assertNotEmpty($data->items[$i]->content->html);
  168. $this->assertNotEmpty($data->items[$i]->content->text);
  169. }
  170. $this->assertEquals('2017-11-08T23:53:00-08:00', $data->items[0]->published);
  171. $this->assertEquals('http://tantek.com/2017/312/t3/tam-trail-run-first-trail-half', $data->items[0]->url);
  172. $this->assertEquals('went to MORE Pancakes! this morning @RunJanji pop-up on California st after #NPSF. Picked up a new running shirt.', $data->items[1]->content->text);
  173. $this->assertEquals('went to MORE Pancakes! this morning <a href="https://twitter.com/RunJanji">@RunJanji</a> pop-up on California st after #NPSF. Picked up a new running shirt.', $data->items[1]->content->html);
  174. $this->assertEquals('feed', $data->type);
  175. }
  176. public function testRSSFeed() {
  177. $url = 'http://feed.example.com/rss';
  178. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  179. $body = $response->getContent();
  180. $this->assertEquals(200, $response->getStatusCode());
  181. $data = json_decode($body)->data;
  182. $this->assertEquals(10, count($data->items));
  183. for($i=0; $i<10; $i++) {
  184. $this->assertEquals('entry', $data->items[$i]->type);
  185. $this->assertEquals('Ryan Barrett', $data->items[$i]->author->name);
  186. $this->assertEquals('https://snarfed.org/', $data->items[$i]->author->url);
  187. $this->assertNotEmpty($data->items[$i]->url);
  188. $this->assertNotEmpty($data->items[$i]->published);
  189. $this->assertNotEmpty($data->items[$i]->content->html);
  190. if($i > 1)
  191. $this->assertNotEmpty($data->items[$i]->content->text);
  192. }
  193. $this->assertEquals('2017-09-12T20:09:12+00:00', $data->items[9]->published);
  194. $this->assertEquals('https://snarfed.org/2017-09-12_25492', $data->items[9]->url);
  195. $this->assertEquals('<p>new business cards <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f602.png" alt="😂" /></p>
  196. <p><img src="https://i0.wp.com/snarfed.org/w/wp-content/uploads/2017/09/IMG_20170912_131414_767.jpg?w=696&amp;ssl=1" alt="IMG_20170912_131414_767.jpg?w=696&amp;ssl=1" /></p>', $data->items[9]->content->html);
  197. $this->assertEquals('new business cards', $data->items[9]->content->text);
  198. $this->assertEquals('feed', $data->type);
  199. }
  200. public function testPodcastFeed() {
  201. $url = 'http://feed.example.com/podcast-rss';
  202. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  203. $body = $response->getContent();
  204. $this->assertEquals(200, $response->getStatusCode());
  205. $data = json_decode($body)->data;
  206. $this->assertEquals(12, count($data->items));
  207. for($i=0; $i<12; $i++) {
  208. $this->assertEquals('entry', $data->items[$i]->type);
  209. $this->assertEquals('Aaron Parecki', $data->items[$i]->author->name);
  210. $this->assertEquals('https://percolator.today/', $data->items[$i]->author->url);
  211. $this->assertNotEmpty($data->items[$i]->url);
  212. $this->assertNotEmpty($data->items[$i]->published);
  213. $this->assertNotEmpty($data->items[$i]->name);
  214. $this->assertNotEmpty($data->items[$i]->content->html);
  215. $this->assertNotEmpty($data->items[$i]->content->text);
  216. $this->assertNotEmpty($data->items[$i]->audio);
  217. }
  218. $this->assertEquals('Episode 1: Welcome', $data->items[11]->name);
  219. $this->assertEquals('https://percolator.today/episode/1', $data->items[11]->url);
  220. $this->assertEquals('2017-09-20T07:00:00+00:00', $data->items[11]->published);
  221. $this->assertEquals('https://percolator.today/redirect.php?url=https%3A%2F%2Fpercolator.today%2Fmedia%2FPercolator_Episode_1.mp3', $data->items[11]->audio);
  222. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->text);
  223. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->html);
  224. $this->assertContains('<li><a href="https://indieweb.org/multi-photo_vs_collection">multi-photo vs collection</a></li>', $data->items[11]->content->html);
  225. $this->assertEquals('feed', $data->type);
  226. }
  227. }