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.

355 lines
16 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(10, 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>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);
  190. $this->assertEquals('Lots of good feedback on the WordPress import. Made a couple improvements this morning. Overall, pretty good.', $data->items[9]->content->text);
  191. $this->assertEquals('http://www.manton.org/2017/11/5975.html', $data->items[9]->url);
  192. $this->assertEquals('http://www.manton.org/2017/11/5975.html', $data->items[9]->uid);
  193. $this->assertEquals('2017-11-07T15:04:01+00:00', $data->items[9]->published);
  194. $this->assertEquals('feed', $data->type);
  195. }
  196. public function testAtomFeed() {
  197. $url = 'http://feed.example.com/atom';
  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('xml', $result->{'source-format'});
  203. $data = $result->data;
  204. $this->assertEquals(8, count($data->items));
  205. for($i=0; $i<8; $i++) {
  206. $this->assertEquals('entry', $data->items[$i]->type);
  207. $this->assertEquals('note', $data->items[$i]->{'post-type'});
  208. $this->assertEquals('Tantek', $data->items[$i]->author->name);
  209. $this->assertEquals('http://tantek.com/', $data->items[$i]->author->url);
  210. $this->assertNotEmpty($data->items[$i]->url);
  211. $this->assertNotEmpty($data->items[$i]->published);
  212. $this->assertNotEmpty($data->items[$i]->content->html);
  213. $this->assertNotEmpty($data->items[$i]->content->text);
  214. }
  215. $this->assertEquals('2017-11-08T23:53:00-08:00', $data->items[0]->published);
  216. $this->assertEquals('http://tantek.com/2017/312/t3/tam-trail-run-first-trail-half', $data->items[0]->url);
  217. $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);
  218. $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);
  219. $this->assertEquals('feed', $data->type);
  220. }
  221. public function testRSSFeed() {
  222. $url = 'http://feed.example.com/rss';
  223. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  224. $body = $response->getContent();
  225. $this->assertEquals(200, $response->getStatusCode());
  226. $result = json_decode($body);
  227. $this->assertEquals('xml', $result->{'source-format'});
  228. $data = $result->data;
  229. $this->assertEquals(10, count($data->items));
  230. for($i=0; $i<10; $i++) {
  231. $this->assertEquals('entry', $data->items[$i]->type);
  232. $this->assertEquals('note', $data->items[$i]->{'post-type'});
  233. $this->assertEquals('Ryan Barrett', $data->items[$i]->author->name);
  234. $this->assertEquals('https://snarfed.org/', $data->items[$i]->author->url);
  235. $this->assertNotEmpty($data->items[$i]->url);
  236. $this->assertNotEmpty($data->items[$i]->published);
  237. $this->assertNotEmpty($data->items[$i]->content->html);
  238. if($i > 1)
  239. $this->assertNotEmpty($data->items[$i]->content->text);
  240. }
  241. $this->assertEquals('2017-09-12T20:09:12+00:00', $data->items[9]->published);
  242. $this->assertEquals('https://snarfed.org/2017-09-12_25492', $data->items[9]->url);
  243. $this->assertEquals('<p>new business cards <img src="https://s.w.org/images/core/emoji/2.3/72x72/1f602.png" alt="😂" /></p>
  244. <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);
  245. $this->assertEquals('new business cards', $data->items[9]->content->text);
  246. $this->assertEquals('feed', $data->type);
  247. }
  248. public function testPodcastFeed() {
  249. $url = 'http://feed.example.com/podcast-rss';
  250. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  251. $body = $response->getContent();
  252. $this->assertEquals(200, $response->getStatusCode());
  253. $result = json_decode($body);
  254. $this->assertEquals('xml', $result->{'source-format'});
  255. $data = $result->data;
  256. $this->assertEquals(12, count($data->items));
  257. for($i=0; $i<12; $i++) {
  258. $this->assertEquals('entry', $data->items[$i]->type);
  259. $this->assertEquals('audio', $data->items[$i]->{'post-type'});
  260. $this->assertEquals('Aaron Parecki', $data->items[$i]->author->name);
  261. $this->assertEquals('https://percolator.today/', $data->items[$i]->author->url);
  262. $this->assertNotEmpty($data->items[$i]->url);
  263. $this->assertNotEmpty($data->items[$i]->published);
  264. $this->assertNotEmpty($data->items[$i]->name);
  265. $this->assertNotEmpty($data->items[$i]->content->html);
  266. $this->assertNotEmpty($data->items[$i]->content->text);
  267. $this->assertNotEmpty($data->items[$i]->audio);
  268. }
  269. $this->assertEquals('Episode 1: Welcome', $data->items[11]->name);
  270. $this->assertEquals('https://percolator.today/episode/1', $data->items[11]->url);
  271. $this->assertEquals('2017-09-20T07:00:00+00:00', $data->items[11]->published);
  272. $this->assertEquals('https://percolator.today/redirect.php?url=https%3A%2F%2Fpercolator.today%2Fmedia%2FPercolator_Episode_1.mp3', $data->items[11]->audio[0]);
  273. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->text);
  274. $this->assertContains('What is Percolator? Some thoughts about multi-photos in Instagram.', $data->items[11]->content->html);
  275. $this->assertContains('<li><a href="https://indieweb.org/multi-photo_vs_collection">multi-photo vs collection</a></li>', $data->items[11]->content->html);
  276. $this->assertEquals('feed', $data->type);
  277. }
  278. public function testInstagramAtomFeed() {
  279. $url = 'http://feed.example.com/instagram-atom';
  280. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  281. $body = $response->getContent();
  282. $this->assertEquals(200, $response->getStatusCode());
  283. $result = json_decode($body);
  284. $this->assertEquals('xml', $result->{'source-format'});
  285. $data = $result->data;
  286. $this->assertEquals(12, count($data->items));
  287. $this->assertEquals('Marshall Kirkpatrick', $data->items[11]->author->name);
  288. $this->assertEquals('https://www.instagram.com/marshallk/', $data->items[11]->author->url);
  289. $this->assertEquals('https://www.instagram.com/p/BcFjw9SHYql/', $data->items[11]->url);
  290. $this->assertEquals('2017-11-29T17:04:00+00:00', $data->items[11]->published);
  291. // Should remove the "name" since it's a prefix of the content
  292. $this->assertObjectNotHasAttribute('name', $data->items[11]);
  293. $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);
  294. }
  295. public function testAscraeus() {
  296. $url = 'http://source.example.com/ascraeus';
  297. $response = $this->parse(['url' => $url, 'expect' => 'feed']);
  298. $body = $response->getContent();
  299. $this->assertEquals(200, $response->getStatusCode());
  300. $data = json_decode($body)->data;
  301. $this->assertEquals('feed', $data->type);
  302. $this->assertEquals(20, count($data->items));
  303. }
  304. }