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.

297 lines
13 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 testTopLevelHFeedWithChildAuthor() {
  73. $url = 'http://feed.example.com/h-feed-with-child-author';
  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. $this->assertEquals('Author Name', $data->items[0]->author->name);
  85. $this->assertEquals('Author Name', $data->items[1]->author->name);
  86. $this->assertEquals('Author Name', $data->items[2]->author->name);
  87. $this->assertEquals('Author Name', $data->items[3]->author->name);
  88. }
  89. public function testHCardWithChildHEntrys() {
  90. $url = 'http://feed.example.com/h-card-with-child-h-entrys';
  91. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  92. $body = $response->getContent();
  93. $this->assertEquals(200, $response->getStatusCode());
  94. $data = json_decode($body)->data;
  95. $this->assertEquals('feed', $data->type);
  96. $this->assertEquals(4, count($data->items));
  97. $this->assertEquals('One', $data->items[0]->name);
  98. $this->assertEquals('Two', $data->items[1]->name);
  99. $this->assertEquals('Three', $data->items[2]->name);
  100. $this->assertEquals('Four', $data->items[3]->name);
  101. }
  102. public function testHCardWithSiblingHEntrys() {
  103. $url = 'http://feed.example.com/h-card-with-sibling-h-entrys';
  104. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  105. $body = $response->getContent();
  106. $this->assertEquals(200, $response->getStatusCode());
  107. $data = json_decode($body)->data;
  108. $this->assertEquals('feed', $data->type);
  109. $this->assertEquals(4, count($data->items));
  110. $this->assertEquals('One', $data->items[0]->name);
  111. $this->assertEquals('Two', $data->items[1]->name);
  112. $this->assertEquals('Three', $data->items[2]->name);
  113. $this->assertEquals('Four', $data->items[3]->name);
  114. // Check that the author h-card was matched up with each h-entry
  115. $this->assertEquals('Author Name', $data->items[0]->author->name);
  116. $this->assertEquals('Author Name', $data->items[1]->author->name);
  117. $this->assertEquals('Author Name', $data->items[2]->author->name);
  118. $this->assertEquals('Author Name', $data->items[3]->author->name);
  119. }
  120. public function testHCardWithChildHFeed() {
  121. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  122. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  123. $body = $response->getContent();
  124. $this->assertEquals(200, $response->getStatusCode());
  125. $data = json_decode($body)->data;
  126. $this->assertEquals('feed', $data->type);
  127. $this->assertEquals(4, count($data->items));
  128. $this->assertEquals('One', $data->items[0]->name);
  129. $this->assertEquals('Two', $data->items[1]->name);
  130. $this->assertEquals('Three', $data->items[2]->name);
  131. $this->assertEquals('Four', $data->items[3]->name);
  132. // Check that the author h-card was matched up with each h-entry
  133. $this->assertEquals('Author Name', $data->items[0]->author->name);
  134. $this->assertEquals('Author Name', $data->items[1]->author->name);
  135. $this->assertEquals('Author Name', $data->items[2]->author->name);
  136. $this->assertEquals('Author Name', $data->items[3]->author->name);
  137. }
  138. public function testHCardWithChildHFeedNoExpect() {
  139. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  140. $response = $this->parse(['url' => $url]);
  141. $body = $response->getContent();
  142. $this->assertEquals(200, $response->getStatusCode());
  143. $data = json_decode($body)->data;
  144. $this->assertEquals('card', $data->type);
  145. $this->assertEquals('Author Name', $data->name);
  146. }
  147. public function testJSONFeed() {
  148. $url = 'http://feed.example.com/jsonfeed';
  149. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  150. $body = $response->getContent();
  151. $this->assertEquals(200, $response->getStatusCode());
  152. $data = json_decode($body)->data;
  153. $this->assertEquals(10, count($data->items));
  154. for($i=0; $i<8; $i++) {
  155. $this->assertEquals('entry', $data->items[$i]->type);
  156. $this->assertEquals('manton', $data->items[$i]->author->name);
  157. $this->assertEquals('http://www.manton.org', $data->items[$i]->author->url);
  158. $this->assertNotEmpty($data->items[$i]->url);
  159. $this->assertNotEmpty($data->items[$i]->uid);
  160. $this->assertNotEmpty($data->items[$i]->published);
  161. $this->assertNotEmpty($data->items[$i]->content->html);
  162. $this->assertNotEmpty($data->items[$i]->content->text);
  163. }
  164. $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);
  165. $this->assertEquals('Lots of good feedback on the WordPress import. Made a couple improvements this morning. Overall, pretty good.', $data->items[9]->content->text);
  166. $this->assertEquals('http://www.manton.org/2017/11/5975.html', $data->items[9]->url);
  167. $this->assertEquals('http://www.manton.org/2017/11/5975.html', $data->items[9]->uid);
  168. $this->assertEquals('2017-11-07T15:04:01+00:00', $data->items[9]->published);
  169. $this->assertEquals('feed', $data->type);
  170. }
  171. public function testAtomFeed() {
  172. $url = 'http://feed.example.com/atom';
  173. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  174. $body = $response->getContent();
  175. $this->assertEquals(200, $response->getStatusCode());
  176. $data = json_decode($body)->data;
  177. $this->assertEquals(8, count($data->items));
  178. for($i=0; $i<8; $i++) {
  179. $this->assertEquals('entry', $data->items[$i]->type);
  180. $this->assertEquals('Tantek', $data->items[$i]->author->name);
  181. $this->assertEquals('http://tantek.com/', $data->items[$i]->author->url);
  182. $this->assertNotEmpty($data->items[$i]->url);
  183. $this->assertNotEmpty($data->items[$i]->published);
  184. $this->assertNotEmpty($data->items[$i]->content->html);
  185. $this->assertNotEmpty($data->items[$i]->content->text);
  186. }
  187. $this->assertEquals('2017-11-08T23:53:00-08:00', $data->items[0]->published);
  188. $this->assertEquals('http://tantek.com/2017/312/t3/tam-trail-run-first-trail-half', $data->items[0]->url);
  189. $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);
  190. $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);
  191. $this->assertEquals('feed', $data->type);
  192. }
  193. public function testRSSFeed() {
  194. $url = 'http://feed.example.com/rss';
  195. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  196. $body = $response->getContent();
  197. $this->assertEquals(200, $response->getStatusCode());
  198. $data = json_decode($body)->data;
  199. $this->assertEquals(10, count($data->items));
  200. for($i=0; $i<10; $i++) {
  201. $this->assertEquals('entry', $data->items[$i]->type);
  202. $this->assertEquals('Ryan Barrett', $data->items[$i]->author->name);
  203. $this->assertEquals('https://snarfed.org/', $data->items[$i]->author->url);
  204. $this->assertNotEmpty($data->items[$i]->url);
  205. $this->assertNotEmpty($data->items[$i]->published);
  206. $this->assertNotEmpty($data->items[$i]->content->html);
  207. if($i > 1)
  208. $this->assertNotEmpty($data->items[$i]->content->text);
  209. }
  210. $this->assertEquals('2017-09-12T20:09:12+00:00', $data->items[9]->published);
  211. $this->assertEquals('https://snarfed.org/2017-09-12_25492', $data->items[9]->url);
  212. $this->assertEquals('<p>new business cards <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f602.png" alt="😂" /></p>
  213. <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);
  214. $this->assertEquals('new business cards', $data->items[9]->content->text);
  215. $this->assertEquals('feed', $data->type);
  216. }
  217. public function testPodcastFeed() {
  218. $url = 'http://feed.example.com/podcast-rss';
  219. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  220. $body = $response->getContent();
  221. $this->assertEquals(200, $response->getStatusCode());
  222. $data = json_decode($body)->data;
  223. $this->assertEquals(12, count($data->items));
  224. for($i=0; $i<12; $i++) {
  225. $this->assertEquals('entry', $data->items[$i]->type);
  226. $this->assertEquals('Aaron Parecki', $data->items[$i]->author->name);
  227. $this->assertEquals('https://percolator.today/', $data->items[$i]->author->url);
  228. $this->assertNotEmpty($data->items[$i]->url);
  229. $this->assertNotEmpty($data->items[$i]->published);
  230. $this->assertNotEmpty($data->items[$i]->name);
  231. $this->assertNotEmpty($data->items[$i]->content->html);
  232. $this->assertNotEmpty($data->items[$i]->content->text);
  233. $this->assertNotEmpty($data->items[$i]->audio);
  234. }
  235. $this->assertEquals('Episode 1: Welcome', $data->items[11]->name);
  236. $this->assertEquals('https://percolator.today/episode/1', $data->items[11]->url);
  237. $this->assertEquals('2017-09-20T07:00:00+00:00', $data->items[11]->published);
  238. $this->assertEquals('https://percolator.today/redirect.php?url=https%3A%2F%2Fpercolator.today%2Fmedia%2FPercolator_Episode_1.mp3', $data->items[11]->audio[0]);
  239. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->text);
  240. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->html);
  241. $this->assertContains('<li><a href="https://indieweb.org/multi-photo_vs_collection">multi-photo vs collection</a></li>', $data->items[11]->content->html);
  242. $this->assertEquals('feed', $data->type);
  243. }
  244. }