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.

337 lines
15 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('article', $data->items[0]->{'post-type'});
  26. $this->assertEquals('Two', $data->items[1]->name);
  27. $this->assertEquals('article', $data->items[1]->{'post-type'});
  28. $this->assertEquals('Three', $data->items[2]->name);
  29. $this->assertEquals('Four', $data->items[3]->name);
  30. }
  31. public function testListOfHEntrysWithHCard() {
  32. $url = 'http://feed.example.com/list-of-hentrys-with-h-card';
  33. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  34. $body = $response->getContent();
  35. $this->assertEquals(200, $response->getStatusCode());
  36. $data = json_decode($body)->data;
  37. $this->assertEquals('feed', $data->type);
  38. $this->assertEquals(4, count($data->items));
  39. $this->assertEquals('One', $data->items[0]->name);
  40. $this->assertEquals('article', $data->items[0]->{'post-type'});
  41. $this->assertEquals('Two', $data->items[1]->name);
  42. $this->assertEquals('Three', $data->items[2]->name);
  43. $this->assertEquals('Four', $data->items[3]->name);
  44. // Check that the author h-card was matched up with each h-entry
  45. $this->assertEquals('Author Name', $data->items[0]->author->name);
  46. $this->assertEquals('Author Name', $data->items[1]->author->name);
  47. $this->assertEquals('Author Name', $data->items[2]->author->name);
  48. $this->assertEquals('Author Name', $data->items[3]->author->name);
  49. }
  50. public function testShortListOfHEntrysWithHCard() {
  51. $url = 'http://feed.example.com/short-list-of-hentrys-with-h-card';
  52. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  53. $body = $response->getContent();
  54. $this->assertEquals(200, $response->getStatusCode());
  55. $data = json_decode($body)->data;
  56. $this->assertEquals('feed', $data->type);
  57. // This test should find the h-entry rather than the h-card, because expect=feed
  58. $this->assertEquals('entry', $data->items[0]->type);
  59. $this->assertEquals('http://feed.example.com/1', $data->items[0]->url);
  60. $this->assertEquals('Author', $data->items[0]->author->name);
  61. }
  62. public function testTopLevelHFeed() {
  63. $url = 'http://feed.example.com/top-level-h-feed';
  64. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  65. $body = $response->getContent();
  66. $this->assertEquals(200, $response->getStatusCode());
  67. $data = json_decode($body)->data;
  68. $this->assertEquals('feed', $data->type);
  69. $this->assertEquals(4, count($data->items));
  70. $this->assertEquals('One', $data->items[0]->name);
  71. $this->assertEquals('Two', $data->items[1]->name);
  72. $this->assertEquals('Three', $data->items[2]->name);
  73. $this->assertEquals('Four', $data->items[3]->name);
  74. }
  75. public function testTopLevelHFeedWithChildAuthor() {
  76. $url = 'http://feed.example.com/h-feed-with-child-author';
  77. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  78. $body = $response->getContent();
  79. $this->assertEquals(200, $response->getStatusCode());
  80. $data = json_decode($body)->data;
  81. $this->assertEquals('feed', $data->type);
  82. $this->assertEquals(4, count($data->items));
  83. $this->assertEquals('One', $data->items[0]->name);
  84. $this->assertEquals('Two', $data->items[1]->name);
  85. $this->assertEquals('Three', $data->items[2]->name);
  86. $this->assertEquals('Four', $data->items[3]->name);
  87. $this->assertEquals('Author Name', $data->items[0]->author->name);
  88. $this->assertEquals('Author Name', $data->items[1]->author->name);
  89. $this->assertEquals('Author Name', $data->items[2]->author->name);
  90. $this->assertEquals('Author Name', $data->items[3]->author->name);
  91. }
  92. public function testHCardWithChildHEntrys() {
  93. $url = 'http://feed.example.com/h-card-with-child-h-entrys';
  94. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  95. $body = $response->getContent();
  96. $this->assertEquals(200, $response->getStatusCode());
  97. $data = json_decode($body)->data;
  98. $this->assertEquals('feed', $data->type);
  99. $this->assertEquals(4, count($data->items));
  100. $this->assertEquals('One', $data->items[0]->name);
  101. $this->assertEquals('Two', $data->items[1]->name);
  102. $this->assertEquals('Three', $data->items[2]->name);
  103. $this->assertEquals('Four', $data->items[3]->name);
  104. }
  105. public function testHCardWithSiblingHEntrys() {
  106. $url = 'http://feed.example.com/h-card-with-sibling-h-entrys';
  107. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  108. $body = $response->getContent();
  109. $this->assertEquals(200, $response->getStatusCode());
  110. $data = json_decode($body)->data;
  111. $this->assertEquals('feed', $data->type);
  112. $this->assertEquals(4, count($data->items));
  113. $this->assertEquals('One', $data->items[0]->name);
  114. $this->assertEquals('Two', $data->items[1]->name);
  115. $this->assertEquals('Three', $data->items[2]->name);
  116. $this->assertEquals('Four', $data->items[3]->name);
  117. // Check that the author h-card was matched up with each h-entry
  118. $this->assertEquals('Author Name', $data->items[0]->author->name);
  119. $this->assertEquals('Author Name', $data->items[1]->author->name);
  120. $this->assertEquals('Author Name', $data->items[2]->author->name);
  121. $this->assertEquals('Author Name', $data->items[3]->author->name);
  122. }
  123. public function testHCardWithChildHFeed() {
  124. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  125. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  126. $body = $response->getContent();
  127. $this->assertEquals(200, $response->getStatusCode());
  128. $data = json_decode($body)->data;
  129. $this->assertEquals('feed', $data->type);
  130. $this->assertEquals(4, count($data->items));
  131. $this->assertEquals('One', $data->items[0]->name);
  132. $this->assertEquals('Two', $data->items[1]->name);
  133. $this->assertEquals('Three', $data->items[2]->name);
  134. $this->assertEquals('Four', $data->items[3]->name);
  135. // Check that the author h-card was matched up with each h-entry
  136. $this->assertEquals('Author Name', $data->items[0]->author->name);
  137. $this->assertEquals('Author Name', $data->items[1]->author->name);
  138. $this->assertEquals('Author Name', $data->items[2]->author->name);
  139. $this->assertEquals('Author Name', $data->items[3]->author->name);
  140. }
  141. public function testHCardWithChildHFeedNoExpect() {
  142. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  143. $response = $this->parse(['url' => $url]);
  144. $body = $response->getContent();
  145. $this->assertEquals(200, $response->getStatusCode());
  146. $data = json_decode($body)->data;
  147. $this->assertEquals('card', $data->type);
  148. $this->assertEquals('Author Name', $data->name);
  149. }
  150. public function testJSONFeed() {
  151. $url = 'http://feed.example.com/jsonfeed';
  152. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  153. $body = $response->getContent();
  154. $this->assertEquals(200, $response->getStatusCode());
  155. $data = json_decode($body)->data;
  156. $this->assertEquals(10, count($data->items));
  157. for($i=0; $i<8; $i++) {
  158. $this->assertEquals('entry', $data->items[$i]->type);
  159. $this->assertEquals('manton', $data->items[$i]->author->name);
  160. $this->assertEquals('http://www.manton.org', $data->items[$i]->author->url);
  161. $this->assertNotEmpty($data->items[$i]->url);
  162. $this->assertNotEmpty($data->items[$i]->uid);
  163. $this->assertNotEmpty($data->items[$i]->published);
  164. $this->assertNotEmpty($data->items[$i]->content->html);
  165. $this->assertNotEmpty($data->items[$i]->content->text);
  166. }
  167. $this->assertEquals('note', $data->items[0]->{'post-type'});
  168. $this->assertEquals('article', $data->items[4]->{'post-type'});
  169. $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);
  170. $this->assertEquals('Lots of good feedback on the WordPress import. Made a couple improvements this morning. Overall, pretty good.', $data->items[9]->content->text);
  171. $this->assertEquals('http://www.manton.org/2017/11/5975.html', $data->items[9]->url);
  172. $this->assertEquals('http://www.manton.org/2017/11/5975.html', $data->items[9]->uid);
  173. $this->assertEquals('2017-11-07T15:04:01+00:00', $data->items[9]->published);
  174. $this->assertEquals('feed', $data->type);
  175. }
  176. public function testAtomFeed() {
  177. $url = 'http://feed.example.com/atom';
  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(8, count($data->items));
  183. for($i=0; $i<8; $i++) {
  184. $this->assertEquals('entry', $data->items[$i]->type);
  185. $this->assertEquals('note', $data->items[$i]->{'post-type'});
  186. $this->assertEquals('Tantek', $data->items[$i]->author->name);
  187. $this->assertEquals('http://tantek.com/', $data->items[$i]->author->url);
  188. $this->assertNotEmpty($data->items[$i]->url);
  189. $this->assertNotEmpty($data->items[$i]->published);
  190. $this->assertNotEmpty($data->items[$i]->content->html);
  191. $this->assertNotEmpty($data->items[$i]->content->text);
  192. }
  193. $this->assertEquals('2017-11-08T23:53:00-08:00', $data->items[0]->published);
  194. $this->assertEquals('http://tantek.com/2017/312/t3/tam-trail-run-first-trail-half', $data->items[0]->url);
  195. $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);
  196. $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);
  197. $this->assertEquals('feed', $data->type);
  198. }
  199. public function testRSSFeed() {
  200. $url = 'http://feed.example.com/rss';
  201. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  202. $body = $response->getContent();
  203. $this->assertEquals(200, $response->getStatusCode());
  204. $data = json_decode($body)->data;
  205. $this->assertEquals(10, count($data->items));
  206. for($i=0; $i<10; $i++) {
  207. $this->assertEquals('entry', $data->items[$i]->type);
  208. $this->assertEquals('note', $data->items[$i]->{'post-type'});
  209. $this->assertEquals('Ryan Barrett', $data->items[$i]->author->name);
  210. $this->assertEquals('https://snarfed.org/', $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]->content->html);
  214. if($i > 1)
  215. $this->assertNotEmpty($data->items[$i]->content->text);
  216. }
  217. $this->assertEquals('2017-09-12T20:09:12+00:00', $data->items[9]->published);
  218. $this->assertEquals('https://snarfed.org/2017-09-12_25492', $data->items[9]->url);
  219. $this->assertEquals('<p>new business cards <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f602.png" alt="😂" /></p>
  220. <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);
  221. $this->assertEquals('new business cards', $data->items[9]->content->text);
  222. $this->assertEquals('feed', $data->type);
  223. }
  224. public function testPodcastFeed() {
  225. $url = 'http://feed.example.com/podcast-rss';
  226. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  227. $body = $response->getContent();
  228. $this->assertEquals(200, $response->getStatusCode());
  229. $data = json_decode($body)->data;
  230. $this->assertEquals(12, count($data->items));
  231. for($i=0; $i<12; $i++) {
  232. $this->assertEquals('entry', $data->items[$i]->type);
  233. $this->assertEquals('audio', $data->items[$i]->{'post-type'});
  234. $this->assertEquals('Aaron Parecki', $data->items[$i]->author->name);
  235. $this->assertEquals('https://percolator.today/', $data->items[$i]->author->url);
  236. $this->assertNotEmpty($data->items[$i]->url);
  237. $this->assertNotEmpty($data->items[$i]->published);
  238. $this->assertNotEmpty($data->items[$i]->name);
  239. $this->assertNotEmpty($data->items[$i]->content->html);
  240. $this->assertNotEmpty($data->items[$i]->content->text);
  241. $this->assertNotEmpty($data->items[$i]->audio);
  242. }
  243. $this->assertEquals('Episode 1: Welcome', $data->items[11]->name);
  244. $this->assertEquals('https://percolator.today/episode/1', $data->items[11]->url);
  245. $this->assertEquals('2017-09-20T07:00:00+00:00', $data->items[11]->published);
  246. $this->assertEquals('https://percolator.today/redirect.php?url=https%3A%2F%2Fpercolator.today%2Fmedia%2FPercolator_Episode_1.mp3', $data->items[11]->audio[0]);
  247. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->text);
  248. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->html);
  249. $this->assertContains('<li><a href="https://indieweb.org/multi-photo_vs_collection">multi-photo vs collection</a></li>', $data->items[11]->content->html);
  250. $this->assertEquals('feed', $data->type);
  251. }
  252. public function testInstagramAtomFeed() {
  253. $url = 'http://feed.example.com/instagram-atom';
  254. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  255. $body = $response->getContent();
  256. $this->assertEquals(200, $response->getStatusCode());
  257. $data = json_decode($body)->data;
  258. $this->assertEquals(12, count($data->items));
  259. $this->assertEquals('Marshall Kirkpatrick', $data->items[11]->author->name);
  260. $this->assertEquals('https://www.instagram.com/marshallk/', $data->items[11]->author->url);
  261. $this->assertEquals('https://www.instagram.com/p/BcFjw9SHYql/', $data->items[11]->url);
  262. $this->assertEquals('2017-11-29T17:04:00+00:00', $data->items[11]->published);
  263. // Should remove the "name" since it's a prefix of the content
  264. $this->assertObjectNotHasAttribute('name', $data->items[11]);
  265. $this->assertEquals('Sometimes my job requires me to listen to 55 minutes of an hour long phone call while I go for a long walk on a sunny morning and wait for my turn to give an update. Pretty nice!', $data->items[11]->content->text);
  266. }
  267. public function testAscraeus() {
  268. $url = 'http://source.example.com/ascraeus';
  269. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  270. $body = $response->getContent();
  271. $this->assertEquals(200, $response->getStatusCode());
  272. $data = json_decode($body)->data;
  273. $this->assertEquals('feed', $data->type);
  274. $this->assertEquals(20, count($data->items));
  275. }
  276. }