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.

449 lines
20 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 testListOfHEntrysWithHCardNoExpect() {
  55. $url = 'http://feed.example.com/list-of-hentrys-with-h-card';
  56. $response = $this->parse(['url' => $url]);
  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->assertEquals(4, count($data->items));
  64. $this->assertEquals('One', $data->items[0]->name);
  65. $this->assertEquals('article', $data->items[0]->{'post-type'});
  66. $this->assertEquals('Two', $data->items[1]->name);
  67. $this->assertEquals('Three', $data->items[2]->name);
  68. $this->assertEquals('Four', $data->items[3]->name);
  69. // Check that the author h-card was matched up with each h-entry
  70. $this->assertEquals('Author Name', $data->items[0]->author->name);
  71. $this->assertEquals('Author Name', $data->items[1]->author->name);
  72. $this->assertEquals('Author Name', $data->items[2]->author->name);
  73. $this->assertEquals('Author Name', $data->items[3]->author->name);
  74. }
  75. public function testShortListOfHEntrysWithHCardNoExpect() {
  76. $url = 'http://feed.example.com/short-list-of-hentrys-with-h-card';
  77. $response = $this->parse(['url' => $url]);
  78. $body = $response->getContent();
  79. $this->assertEquals(200, $response->getStatusCode());
  80. $result = json_decode($body);
  81. $this->assertEquals('mf2+html', $result->{'source-format'});
  82. $data = $result->data;
  83. // In this case, this looks like a page permalink
  84. $this->assertEquals('entry', $data->type);
  85. // This test should find the h-entry rather than the h-card, because the h-card does not contain the page URL
  86. $this->assertEquals('http://feed.example.com/1', $data->url);
  87. $this->assertEquals('Author', $data->author->name);
  88. }
  89. public function testShortListOfHEntrysWithHCard() {
  90. $url = 'http://feed.example.com/short-list-of-hentrys-with-h-card';
  91. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  92. $body = $response->getContent();
  93. $this->assertEquals(200, $response->getStatusCode());
  94. $result = json_decode($body);
  95. $this->assertEquals('mf2+html', $result->{'source-format'});
  96. $data = $result->data;
  97. $this->assertEquals('feed', $data->type);
  98. // This test should find the h-entry rather than the h-card, because expect=feed
  99. $this->assertEquals('entry', $data->items[0]->type);
  100. $this->assertEquals('http://feed.example.com/1', $data->items[0]->url);
  101. $this->assertEquals('Author', $data->items[0]->author->name);
  102. }
  103. public function testTopLevelHFeed() {
  104. $url = 'http://feed.example.com/top-level-h-feed';
  105. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  106. $body = $response->getContent();
  107. $this->assertEquals(200, $response->getStatusCode());
  108. $result = json_decode($body);
  109. $this->assertEquals('mf2+html', $result->{'source-format'});
  110. $data = $result->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. }
  118. public function testTopLevelHFeedWithChildAuthor() {
  119. $url = 'http://feed.example.com/h-feed-with-child-author';
  120. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  121. $body = $response->getContent();
  122. $this->assertEquals(200, $response->getStatusCode());
  123. $result = json_decode($body);
  124. $this->assertEquals('mf2+html', $result->{'source-format'});
  125. $data = $result->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. $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 testHCardWithChildHEntrys() {
  138. $url = 'http://feed.example.com/h-card-with-child-h-entrys';
  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. }
  152. public function testHCardWithSiblingHEntrys() {
  153. $url = 'http://feed.example.com/h-card-with-sibling-h-entrys';
  154. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  155. $body = $response->getContent();
  156. $this->assertEquals(200, $response->getStatusCode());
  157. $result = json_decode($body);
  158. $this->assertEquals('mf2+html', $result->{'source-format'});
  159. $data = $result->data;
  160. $this->assertEquals('feed', $data->type);
  161. $this->assertEquals(4, count($data->items));
  162. $this->assertEquals('One', $data->items[0]->name);
  163. $this->assertEquals('Two', $data->items[1]->name);
  164. $this->assertEquals('Three', $data->items[2]->name);
  165. $this->assertEquals('Four', $data->items[3]->name);
  166. // Check that the author h-card was matched up with each h-entry
  167. $this->assertEquals('Author Name', $data->items[0]->author->name);
  168. $this->assertEquals('Author Name', $data->items[1]->author->name);
  169. $this->assertEquals('Author Name', $data->items[2]->author->name);
  170. $this->assertEquals('Author Name', $data->items[3]->author->name);
  171. }
  172. public function testHCardWithChildHFeed() {
  173. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  174. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  175. $body = $response->getContent();
  176. $this->assertEquals(200, $response->getStatusCode());
  177. $result = json_decode($body);
  178. $this->assertEquals('mf2+html', $result->{'source-format'});
  179. $data = $result->data;
  180. $this->assertEquals('feed', $data->type);
  181. $this->assertEquals(4, count($data->items));
  182. $this->assertEquals('One', $data->items[0]->name);
  183. $this->assertEquals('Two', $data->items[1]->name);
  184. $this->assertEquals('Three', $data->items[2]->name);
  185. $this->assertEquals('Four', $data->items[3]->name);
  186. // Check that the author h-card was matched up with each h-entry
  187. $this->assertEquals('Author Name', $data->items[0]->author->name);
  188. $this->assertEquals('Author Name', $data->items[1]->author->name);
  189. $this->assertEquals('Author Name', $data->items[2]->author->name);
  190. $this->assertEquals('Author Name', $data->items[3]->author->name);
  191. }
  192. public function testHCardWithChildHFeedNoExpect() {
  193. $url = 'http://feed.example.com/h-card-with-child-h-feed';
  194. $response = $this->parse(['url' => $url]);
  195. $body = $response->getContent();
  196. $this->assertEquals(200, $response->getStatusCode());
  197. $result = json_decode($body);
  198. $this->assertEquals('mf2+html', $result->{'source-format'});
  199. $data = $result->data;
  200. $this->assertEquals('card', $data->type);
  201. $this->assertEquals('Author Name', $data->name);
  202. }
  203. public function testJSONFeed() {
  204. $url = 'http://feed.example.com/jsonfeed';
  205. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  206. $body = $response->getContent();
  207. $this->assertEquals(200, $response->getStatusCode());
  208. $result = json_decode($body);
  209. $this->assertEquals('feed+json', $result->{'source-format'});
  210. $data = $result->data;
  211. $this->assertEquals(11, count($data->items));
  212. for($i=0; $i<8; $i++) {
  213. $this->assertEquals('entry', $data->items[$i]->type);
  214. $this->assertEquals('manton', $data->items[$i]->author->name);
  215. $this->assertEquals('http://www.manton.org', $data->items[$i]->author->url);
  216. $this->assertNotEmpty($data->items[$i]->url);
  217. $this->assertNotEmpty($data->items[$i]->uid);
  218. $this->assertNotEmpty($data->items[$i]->published);
  219. $this->assertNotEmpty($data->items[$i]->content->html);
  220. $this->assertNotEmpty($data->items[$i]->content->text);
  221. }
  222. $this->assertEquals('note', $data->items[0]->{'post-type'});
  223. $this->assertEquals('article', $data->items[4]->{'post-type'});
  224. $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);
  225. $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);
  226. $this->assertEquals('http://www.manton.org/2017/11/5979.html', $data->items[7]->url);
  227. $this->assertEquals('http://www.manton.org/2017/11/5979.html', $data->items[7]->uid);
  228. $this->assertEquals('2017-11-07T21:00:42+00:00', $data->items[7]->published);
  229. $this->assertEquals('feed', $data->type);
  230. }
  231. public function testJSONFeedRelativeImages() {
  232. $url = 'http://feed.example.com/jsonfeed';
  233. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  234. $body = $response->getContent();
  235. $this->assertEquals(200, $response->getStatusCode());
  236. $result = json_decode($body);
  237. $this->assertEquals('feed+json', $result->{'source-format'});
  238. $data = $result->data;
  239. // Relative image on an item that has a url
  240. $this->assertEquals('http://www.manton.org/2017/11/image.jpg', $data->items[9]->photo);
  241. // Relative image on an item that has no URL, fall back to feed URL
  242. $this->assertEquals('http://feed.example.com/image.jpg', $data->items[10]->photo);
  243. // Relative image inside the content html
  244. $this->assertContains('http://www.manton.org/2017/11/img.jpg', $data->items[9]->content->html);
  245. }
  246. public function testAtomFeed() {
  247. $url = 'http://feed.example.com/atom';
  248. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  249. $body = $response->getContent();
  250. $this->assertEquals(200, $response->getStatusCode());
  251. $result = json_decode($body);
  252. $this->assertEquals('xml', $result->{'source-format'});
  253. $data = $result->data;
  254. $this->assertEquals(8, count($data->items));
  255. for($i=0; $i<8; $i++) {
  256. $this->assertEquals('entry', $data->items[$i]->type);
  257. $this->assertEquals('note', $data->items[$i]->{'post-type'});
  258. $this->assertEquals('Tantek', $data->items[$i]->author->name);
  259. $this->assertEquals('http://tantek.com/', $data->items[$i]->author->url);
  260. $this->assertNotEmpty($data->items[$i]->url);
  261. $this->assertNotEmpty($data->items[$i]->published);
  262. $this->assertNotEmpty($data->items[$i]->content->html);
  263. $this->assertNotEmpty($data->items[$i]->content->text);
  264. }
  265. $this->assertEquals('2017-11-08T23:53:00-08:00', $data->items[0]->published);
  266. $this->assertEquals('http://tantek.com/2017/312/t3/tam-trail-run-first-trail-half', $data->items[0]->url);
  267. $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);
  268. $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);
  269. $this->assertEquals('feed', $data->type);
  270. }
  271. public function testRSSFeed() {
  272. $url = 'http://feed.example.com/rss';
  273. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  274. $body = $response->getContent();
  275. $this->assertEquals(200, $response->getStatusCode());
  276. $result = json_decode($body);
  277. $this->assertEquals('xml', $result->{'source-format'});
  278. $data = $result->data;
  279. $this->assertEquals(10, count($data->items));
  280. for($i=0; $i<10; $i++) {
  281. $this->assertEquals('entry', $data->items[$i]->type);
  282. $this->assertEquals('note', $data->items[$i]->{'post-type'});
  283. $this->assertEquals('Ryan Barrett', $data->items[$i]->author->name);
  284. $this->assertEquals('https://snarfed.org/', $data->items[$i]->author->url);
  285. $this->assertNotEmpty($data->items[$i]->url);
  286. $this->assertNotEmpty($data->items[$i]->published);
  287. $this->assertNotEmpty($data->items[$i]->content->html);
  288. if($i > 1)
  289. $this->assertNotEmpty($data->items[$i]->content->text);
  290. }
  291. $this->assertEquals('2017-09-12T20:09:12+00:00', $data->items[9]->published);
  292. $this->assertEquals('https://snarfed.org/2017-09-12_25492', $data->items[9]->url);
  293. $this->assertEquals('<p>new business cards <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f602.png" alt="😂" /></p>
  294. <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);
  295. $this->assertEquals('new business cards', $data->items[9]->content->text);
  296. $this->assertEquals('feed', $data->type);
  297. }
  298. public function testPodcastFeed() {
  299. $url = 'http://feed.example.com/podcast-rss';
  300. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  301. $body = $response->getContent();
  302. $this->assertEquals(200, $response->getStatusCode());
  303. $result = json_decode($body);
  304. $this->assertEquals('xml', $result->{'source-format'});
  305. $data = $result->data;
  306. $this->assertEquals(12, count($data->items));
  307. for($i=0; $i<12; $i++) {
  308. $this->assertEquals('entry', $data->items[$i]->type);
  309. $this->assertEquals('audio', $data->items[$i]->{'post-type'});
  310. $this->assertEquals('Aaron Parecki', $data->items[$i]->author->name);
  311. $this->assertEquals('https://percolator.today/', $data->items[$i]->author->url);
  312. $this->assertNotEmpty($data->items[$i]->url);
  313. $this->assertNotEmpty($data->items[$i]->published);
  314. $this->assertNotEmpty($data->items[$i]->name);
  315. $this->assertNotEmpty($data->items[$i]->content->html);
  316. $this->assertNotEmpty($data->items[$i]->content->text);
  317. $this->assertNotEmpty($data->items[$i]->audio);
  318. }
  319. $this->assertEquals('Episode 1: Welcome', $data->items[11]->name);
  320. $this->assertEquals('https://percolator.today/episode/1', $data->items[11]->url);
  321. $this->assertEquals('2017-09-20T07:00:00+00:00', $data->items[11]->published);
  322. $this->assertEquals('https://percolator.today/redirect.php?url=https%3A%2F%2Fpercolator.today%2Fmedia%2FPercolator_Episode_1.mp3', $data->items[11]->audio[0]);
  323. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->text);
  324. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->html);
  325. $this->assertContains('<li><a href="https://indieweb.org/multi-photo_vs_collection">multi-photo vs collection</a></li>', $data->items[11]->content->html);
  326. $this->assertEquals('feed', $data->type);
  327. }
  328. public function testInstagramAtomFeed() {
  329. $url = 'http://feed.example.com/instagram-atom';
  330. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  331. $body = $response->getContent();
  332. $this->assertEquals(200, $response->getStatusCode());
  333. $result = json_decode($body);
  334. $this->assertEquals('xml', $result->{'source-format'});
  335. $data = $result->data;
  336. $this->assertEquals(12, count($data->items));
  337. $this->assertEquals('Marshall Kirkpatrick', $data->items[11]->author->name);
  338. $this->assertEquals('https://www.instagram.com/marshallk/', $data->items[11]->author->url);
  339. $this->assertEquals('https://www.instagram.com/p/BcFjw9SHYql/', $data->items[11]->url);
  340. $this->assertEquals('2017-11-29T17:04:00+00:00', $data->items[11]->published);
  341. // Should remove the "name" since it's a prefix of the content
  342. $this->assertObjectNotHasAttribute('name', $data->items[11]);
  343. $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);
  344. }
  345. public function testAscraeus() {
  346. $url = 'http://source.example.com/ascraeus';
  347. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  348. $body = $response->getContent();
  349. $this->assertEquals(200, $response->getStatusCode());
  350. $data = json_decode($body)->data;
  351. $this->assertEquals('feed', $data->type);
  352. $this->assertEquals(20, count($data->items));
  353. }
  354. public function testAdactioLinks() {
  355. $url = 'http://feed.example.com/adactio-links';
  356. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  357. $body = $response->getContent();
  358. $this->assertEquals(200, $response->getStatusCode());
  359. $data = json_decode($body)->data;
  360. $this->assertEquals('feed', $data->type);
  361. // 20 h-entrys followed by one h-card, which should have been removed and used as the author instead
  362. $this->assertEquals(20, count($data->items));
  363. $this->assertEquals('http://feed.example.com/links/14501', $data->items[0]->url);
  364. $this->assertEquals('http://feed.example.com/links/14445', $data->items[19]->url);
  365. $item = $data->items[0];
  366. $this->assertEquals('Jeremy Keith', $item->author->name);
  367. $this->assertEquals('https://adactio.com/', $item->author->url);
  368. }
  369. public function testWaterpigsFeed() {
  370. $url = 'http://feed.example.com/waterpigs';
  371. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  372. $body = $response->getContent();
  373. $this->assertEquals(200, $response->getStatusCode());
  374. $data = json_decode($body)->data;
  375. $this->assertEquals('feed', $data->type);
  376. $this->assertEquals(21, count($data->items));
  377. $item = $data->items[16];
  378. $this->assertEquals('Barnaby Walters', $item->author->name);
  379. $this->assertEquals('https://waterpigs.co.uk', $item->author->url);
  380. }
  381. }