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.

375 lines
17 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. $result = json_decode($body);
  22. $this->assertEquals('mf2+html', $result->{'source-format'});
  23. $data = $result->data;
  24. $this->assertEquals('feed', $data->type);
  25. $this->assertEquals(4, count($data->items));
  26. $this->assertEquals('One', $data->items[0]->name);
  27. $this->assertEquals('article', $data->items[0]->{'post-type'});
  28. $this->assertEquals('Two', $data->items[1]->name);
  29. $this->assertEquals('article', $data->items[1]->{'post-type'});
  30. $this->assertEquals('Three', $data->items[2]->name);
  31. $this->assertEquals('Four', $data->items[3]->name);
  32. }
  33. public function testListOfHEntrysWithHCard() {
  34. $url = 'http://feed.example.com/list-of-hentrys-with-h-card';
  35. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  36. $body = $response->getContent();
  37. $this->assertEquals(200, $response->getStatusCode());
  38. $result = json_decode($body);
  39. $this->assertEquals('mf2+html', $result->{'source-format'});
  40. $data = $result->data;
  41. $this->assertEquals('feed', $data->type);
  42. $this->assertEquals(4, count($data->items));
  43. $this->assertEquals('One', $data->items[0]->name);
  44. $this->assertEquals('article', $data->items[0]->{'post-type'});
  45. $this->assertEquals('Two', $data->items[1]->name);
  46. $this->assertEquals('Three', $data->items[2]->name);
  47. $this->assertEquals('Four', $data->items[3]->name);
  48. // Check that the author h-card was matched up with each h-entry
  49. $this->assertEquals('Author Name', $data->items[0]->author->name);
  50. $this->assertEquals('Author Name', $data->items[1]->author->name);
  51. $this->assertEquals('Author Name', $data->items[2]->author->name);
  52. $this->assertEquals('Author Name', $data->items[3]->author->name);
  53. }
  54. public function testShortListOfHEntrysWithHCard() {
  55. $url = 'http://feed.example.com/short-list-of-hentrys-with-h-card';
  56. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  57. $body = $response->getContent();
  58. $this->assertEquals(200, $response->getStatusCode());
  59. $result = json_decode($body);
  60. $this->assertEquals('mf2+html', $result->{'source-format'});
  61. $data = $result->data;
  62. $this->assertEquals('feed', $data->type);
  63. // This test should find the h-entry rather than the h-card, because expect=feed
  64. $this->assertEquals('entry', $data->items[0]->type);
  65. $this->assertEquals('http://feed.example.com/1', $data->items[0]->url);
  66. $this->assertEquals('Author', $data->items[0]->author->name);
  67. }
  68. public function testTopLevelHFeed() {
  69. $url = 'http://feed.example.com/top-level-h-feed';
  70. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  71. $body = $response->getContent();
  72. $this->assertEquals(200, $response->getStatusCode());
  73. $result = json_decode($body);
  74. $this->assertEquals('mf2+html', $result->{'source-format'});
  75. $data = $result->data;
  76. $this->assertEquals('feed', $data->type);
  77. $this->assertEquals(4, count($data->items));
  78. $this->assertEquals('One', $data->items[0]->name);
  79. $this->assertEquals('Two', $data->items[1]->name);
  80. $this->assertEquals('Three', $data->items[2]->name);
  81. $this->assertEquals('Four', $data->items[3]->name);
  82. }
  83. public function testTopLevelHFeedWithChildAuthor() {
  84. $url = 'http://feed.example.com/h-feed-with-child-author';
  85. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  86. $body = $response->getContent();
  87. $this->assertEquals(200, $response->getStatusCode());
  88. $result = json_decode($body);
  89. $this->assertEquals('mf2+html', $result->{'source-format'});
  90. $data = $result->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. $this->assertEquals('Author Name', $data->items[0]->author->name);
  98. $this->assertEquals('Author Name', $data->items[1]->author->name);
  99. $this->assertEquals('Author Name', $data->items[2]->author->name);
  100. $this->assertEquals('Author Name', $data->items[3]->author->name);
  101. }
  102. public function testHCardWithChildHEntrys() {
  103. $url = 'http://feed.example.com/h-card-with-child-h-entrys';
  104. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  105. $body = $response->getContent();
  106. $this->assertEquals(200, $response->getStatusCode());
  107. $result = json_decode($body);
  108. $this->assertEquals('mf2+html', $result->{'source-format'});
  109. $data = $result->data;
  110. $this->assertEquals('feed', $data->type);
  111. $this->assertEquals(4, count($data->items));
  112. $this->assertEquals('One', $data->items[0]->name);
  113. $this->assertEquals('Two', $data->items[1]->name);
  114. $this->assertEquals('Three', $data->items[2]->name);
  115. $this->assertEquals('Four', $data->items[3]->name);
  116. }
  117. public function testHCardWithSiblingHEntrys() {
  118. $url = 'http://feed.example.com/h-card-with-sibling-h-entrys';
  119. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  120. $body = $response->getContent();
  121. $this->assertEquals(200, $response->getStatusCode());
  122. $result = json_decode($body);
  123. $this->assertEquals('mf2+html', $result->{'source-format'});
  124. $data = $result->data;
  125. $this->assertEquals('feed', $data->type);
  126. $this->assertEquals(4, count($data->items));
  127. $this->assertEquals('One', $data->items[0]->name);
  128. $this->assertEquals('Two', $data->items[1]->name);
  129. $this->assertEquals('Three', $data->items[2]->name);
  130. $this->assertEquals('Four', $data->items[3]->name);
  131. // Check that the author h-card was matched up with each h-entry
  132. $this->assertEquals('Author Name', $data->items[0]->author->name);
  133. $this->assertEquals('Author Name', $data->items[1]->author->name);
  134. $this->assertEquals('Author Name', $data->items[2]->author->name);
  135. $this->assertEquals('Author Name', $data->items[3]->author->name);
  136. }
  137. public function testHCardWithChildHFeed() {
  138. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  139. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  140. $body = $response->getContent();
  141. $this->assertEquals(200, $response->getStatusCode());
  142. $result = json_decode($body);
  143. $this->assertEquals('mf2+html', $result->{'source-format'});
  144. $data = $result->data;
  145. $this->assertEquals('feed', $data->type);
  146. $this->assertEquals(4, count($data->items));
  147. $this->assertEquals('One', $data->items[0]->name);
  148. $this->assertEquals('Two', $data->items[1]->name);
  149. $this->assertEquals('Three', $data->items[2]->name);
  150. $this->assertEquals('Four', $data->items[3]->name);
  151. // Check that the author h-card was matched up with each h-entry
  152. $this->assertEquals('Author Name', $data->items[0]->author->name);
  153. $this->assertEquals('Author Name', $data->items[1]->author->name);
  154. $this->assertEquals('Author Name', $data->items[2]->author->name);
  155. $this->assertEquals('Author Name', $data->items[3]->author->name);
  156. }
  157. public function testHCardWithChildHFeedNoExpect() {
  158. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  159. $response = $this->parse(['url' => $url]);
  160. $body = $response->getContent();
  161. $this->assertEquals(200, $response->getStatusCode());
  162. $result = json_decode($body);
  163. $this->assertEquals('mf2+html', $result->{'source-format'});
  164. $data = $result->data;
  165. $this->assertEquals('card', $data->type);
  166. $this->assertEquals('Author Name', $data->name);
  167. }
  168. public function testJSONFeed() {
  169. $url = 'http://feed.example.com/jsonfeed';
  170. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  171. $body = $response->getContent();
  172. $this->assertEquals(200, $response->getStatusCode());
  173. $result = json_decode($body);
  174. $this->assertEquals('feed+json', $result->{'source-format'});
  175. $data = $result->data;
  176. $this->assertEquals(11, count($data->items));
  177. for($i=0; $i<8; $i++) {
  178. $this->assertEquals('entry', $data->items[$i]->type);
  179. $this->assertEquals('manton', $data->items[$i]->author->name);
  180. $this->assertEquals('http://www.manton.org', $data->items[$i]->author->url);
  181. $this->assertNotEmpty($data->items[$i]->url);
  182. $this->assertNotEmpty($data->items[$i]->uid);
  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('note', $data->items[0]->{'post-type'});
  188. $this->assertEquals('article', $data->items[4]->{'post-type'});
  189. $this->assertEquals('<p>Coming up on a year since I wrote about how <a href="http://www.manton.org/2016/11/todays-social-networks-are-broken.html">today’s social networks are broken</a>. Still what I believe.</p>', $data->items[7]->content->html);
  190. $this->assertEquals('Coming up on a year since I wrote about how today’s social networks are broken. Still what I believe.', $data->items[7]->content->text);
  191. $this->assertEquals('http://www.manton.org/2017/11/5979.html', $data->items[7]->url);
  192. $this->assertEquals('http://www.manton.org/2017/11/5979.html', $data->items[7]->uid);
  193. $this->assertEquals('2017-11-07T21:00:42+00:00', $data->items[7]->published);
  194. $this->assertEquals('feed', $data->type);
  195. }
  196. public function testJSONFeedRelativeImages() {
  197. $url = 'http://feed.example.com/jsonfeed';
  198. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  199. $body = $response->getContent();
  200. $this->assertEquals(200, $response->getStatusCode());
  201. $result = json_decode($body);
  202. $this->assertEquals('feed+json', $result->{'source-format'});
  203. $data = $result->data;
  204. // Relative image on an item that has a url
  205. $this->assertEquals('http://www.manton.org/2017/11/image.jpg', $data->items[9]->photo);
  206. // Relative image on an item that has no URL, fall back to feed URL
  207. $this->assertEquals('http://feed.example.com/image.jpg', $data->items[10]->photo);
  208. // Relative image inside the content html
  209. $this->assertContains('http://www.manton.org/2017/11/img.jpg', $data->items[9]->content->html);
  210. }
  211. public function testAtomFeed() {
  212. $url = 'http://feed.example.com/atom';
  213. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  214. $body = $response->getContent();
  215. $this->assertEquals(200, $response->getStatusCode());
  216. $result = json_decode($body);
  217. $this->assertEquals('xml', $result->{'source-format'});
  218. $data = $result->data;
  219. $this->assertEquals(8, count($data->items));
  220. for($i=0; $i<8; $i++) {
  221. $this->assertEquals('entry', $data->items[$i]->type);
  222. $this->assertEquals('note', $data->items[$i]->{'post-type'});
  223. $this->assertEquals('Tantek', $data->items[$i]->author->name);
  224. $this->assertEquals('http://tantek.com/', $data->items[$i]->author->url);
  225. $this->assertNotEmpty($data->items[$i]->url);
  226. $this->assertNotEmpty($data->items[$i]->published);
  227. $this->assertNotEmpty($data->items[$i]->content->html);
  228. $this->assertNotEmpty($data->items[$i]->content->text);
  229. }
  230. $this->assertEquals('2017-11-08T23:53:00-08:00', $data->items[0]->published);
  231. $this->assertEquals('http://tantek.com/2017/312/t3/tam-trail-run-first-trail-half', $data->items[0]->url);
  232. $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);
  233. $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);
  234. $this->assertEquals('feed', $data->type);
  235. }
  236. public function testRSSFeed() {
  237. $url = 'http://feed.example.com/rss';
  238. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  239. $body = $response->getContent();
  240. $this->assertEquals(200, $response->getStatusCode());
  241. $result = json_decode($body);
  242. $this->assertEquals('xml', $result->{'source-format'});
  243. $data = $result->data;
  244. $this->assertEquals(10, count($data->items));
  245. for($i=0; $i<10; $i++) {
  246. $this->assertEquals('entry', $data->items[$i]->type);
  247. $this->assertEquals('note', $data->items[$i]->{'post-type'});
  248. $this->assertEquals('Ryan Barrett', $data->items[$i]->author->name);
  249. $this->assertEquals('https://snarfed.org/', $data->items[$i]->author->url);
  250. $this->assertNotEmpty($data->items[$i]->url);
  251. $this->assertNotEmpty($data->items[$i]->published);
  252. $this->assertNotEmpty($data->items[$i]->content->html);
  253. if($i > 1)
  254. $this->assertNotEmpty($data->items[$i]->content->text);
  255. }
  256. $this->assertEquals('2017-09-12T20:09:12+00:00', $data->items[9]->published);
  257. $this->assertEquals('https://snarfed.org/2017-09-12_25492', $data->items[9]->url);
  258. $this->assertEquals('<p>new business cards <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f602.png" alt="😂" /></p>
  259. <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);
  260. $this->assertEquals('new business cards', $data->items[9]->content->text);
  261. $this->assertEquals('feed', $data->type);
  262. }
  263. public function testPodcastFeed() {
  264. $url = 'http://feed.example.com/podcast-rss';
  265. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  266. $body = $response->getContent();
  267. $this->assertEquals(200, $response->getStatusCode());
  268. $result = json_decode($body);
  269. $this->assertEquals('xml', $result->{'source-format'});
  270. $data = $result->data;
  271. $this->assertEquals(12, count($data->items));
  272. for($i=0; $i<12; $i++) {
  273. $this->assertEquals('entry', $data->items[$i]->type);
  274. $this->assertEquals('audio', $data->items[$i]->{'post-type'});
  275. $this->assertEquals('Aaron Parecki', $data->items[$i]->author->name);
  276. $this->assertEquals('https://percolator.today/', $data->items[$i]->author->url);
  277. $this->assertNotEmpty($data->items[$i]->url);
  278. $this->assertNotEmpty($data->items[$i]->published);
  279. $this->assertNotEmpty($data->items[$i]->name);
  280. $this->assertNotEmpty($data->items[$i]->content->html);
  281. $this->assertNotEmpty($data->items[$i]->content->text);
  282. $this->assertNotEmpty($data->items[$i]->audio);
  283. }
  284. $this->assertEquals('Episode 1: Welcome', $data->items[11]->name);
  285. $this->assertEquals('https://percolator.today/episode/1', $data->items[11]->url);
  286. $this->assertEquals('2017-09-20T07:00:00+00:00', $data->items[11]->published);
  287. $this->assertEquals('https://percolator.today/redirect.php?url=https%3A%2F%2Fpercolator.today%2Fmedia%2FPercolator_Episode_1.mp3', $data->items[11]->audio[0]);
  288. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->text);
  289. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->html);
  290. $this->assertContains('<li><a href="https://indieweb.org/multi-photo_vs_collection">multi-photo vs collection</a></li>', $data->items[11]->content->html);
  291. $this->assertEquals('feed', $data->type);
  292. }
  293. public function testInstagramAtomFeed() {
  294. $url = 'http://feed.example.com/instagram-atom';
  295. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  296. $body = $response->getContent();
  297. $this->assertEquals(200, $response->getStatusCode());
  298. $result = json_decode($body);
  299. $this->assertEquals('xml', $result->{'source-format'});
  300. $data = $result->data;
  301. $this->assertEquals(12, count($data->items));
  302. $this->assertEquals('Marshall Kirkpatrick', $data->items[11]->author->name);
  303. $this->assertEquals('https://www.instagram.com/marshallk/', $data->items[11]->author->url);
  304. $this->assertEquals('https://www.instagram.com/p/BcFjw9SHYql/', $data->items[11]->url);
  305. $this->assertEquals('2017-11-29T17:04:00+00:00', $data->items[11]->published);
  306. // Should remove the "name" since it's a prefix of the content
  307. $this->assertObjectNotHasAttribute('name', $data->items[11]);
  308. $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);
  309. }
  310. public function testAscraeus() {
  311. $url = 'http://source.example.com/ascraeus';
  312. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  313. $body = $response->getContent();
  314. $this->assertEquals(200, $response->getStatusCode());
  315. $data = json_decode($body)->data;
  316. $this->assertEquals('feed', $data->type);
  317. $this->assertEquals(20, count($data->items));
  318. }
  319. }