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.

663 lines
30 KiB

8 years ago
  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class ParseTest 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 testMissingURL() {
  17. $response = $this->parse([]);
  18. $body = $response->getContent();
  19. $this->assertEquals(400, $response->getStatusCode());
  20. $data = json_decode($body);
  21. $this->assertObjectHasAttribute('error', $data);
  22. $this->assertEquals('missing_url', $data->error);
  23. }
  24. public function testInvalidURL() {
  25. $url = 'ftp://example.com/foo';
  26. $response = $this->parse(['url' => $url]);
  27. $body = $response->getContent();
  28. $this->assertEquals(400, $response->getStatusCode());
  29. $data = json_decode($body);
  30. $this->assertObjectHasAttribute('error', $data);
  31. $this->assertEquals('invalid_url', $data->error);
  32. }
  33. public function testTargetNotFound() {
  34. $url = 'http://source.example.com/basictest';
  35. $response = $this->parse(['url' => $url, 'target' => 'http://example.net']);
  36. $body = $response->getContent();
  37. $this->assertEquals(200, $response->getStatusCode());
  38. $data = json_decode($body);
  39. $this->assertObjectHasAttribute('error', $data);
  40. $this->assertEquals('no_link_found', $data->error);
  41. $this->assertEquals('200', $data->code);
  42. $this->assertEquals($url, $data->url);
  43. }
  44. public function testTargetFound() {
  45. $url = 'http://source.example.com/basictest';
  46. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com']);
  47. $body = $response->getContent();
  48. $this->assertEquals(200, $response->getStatusCode());
  49. $data = json_decode($body);
  50. $this->assertObjectNotHasAttribute('error', $data);
  51. }
  52. public function testHTMLContent() {
  53. $url = 'http://source.example.com/html-content';
  54. $response = $this->parse(['url' => $url]);
  55. $body = $response->getContent();
  56. $this->assertEquals(200, $response->getStatusCode());
  57. $data = json_decode($body);
  58. $this->assertObjectNotHasAttribute('name', $data->data);
  59. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  60. $this->assertEquals('This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.', $data->data->content->html);
  61. }
  62. public function testFindTargetLinkIsImage() {
  63. $url = 'http://source.example.com/link-is-img';
  64. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/photo.jpg']);
  65. $body = $response->getContent();
  66. $this->assertEquals(200, $response->getStatusCode());
  67. $data = json_decode($body);
  68. $this->assertObjectNotHasAttribute('name', $data->data);
  69. $this->assertEquals('This page has an img tag with the target URL.', $data->data->content->text);
  70. }
  71. public function testFindTargetLinkIsVideo() {
  72. $url = 'http://source.example.com/link-is-video';
  73. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/movie.mp4']);
  74. $body = $response->getContent();
  75. $this->assertEquals(200, $response->getStatusCode());
  76. $data = json_decode($body);
  77. $this->assertObjectNotHasAttribute('name', $data->data);
  78. $this->assertEquals('This page has a video tag with the target URL.', $data->data->content->text);
  79. }
  80. public function testFindTargetLinkIsAudio() {
  81. $url = 'http://source.example.com/link-is-audio';
  82. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/media.mp3']);
  83. $body = $response->getContent();
  84. $this->assertEquals(200, $response->getStatusCode());
  85. $data = json_decode($body);
  86. $this->assertObjectNotHasAttribute('name', $data->data);
  87. $this->assertEquals('This page has an audio tag with the target URL.', $data->data->content->text);
  88. }
  89. public function testTextContent() {
  90. $url = 'http://source.example.com/text-content';
  91. $response = $this->parse(['url' => $url]);
  92. $body = $response->getContent();
  93. $this->assertEquals(200, $response->getStatusCode());
  94. $data = json_decode($body);
  95. $this->assertObjectNotHasAttribute('name', $data->data);
  96. $this->assertEquals('This page has a link to target.example.com and some formatted text but is in a p-content element so is plaintext.', $data->data->content->text);
  97. }
  98. public function testArticleWithFeaturedImage() {
  99. $url = 'http://source.example.com/article-with-featured-image';
  100. $response = $this->parse(['url' => $url]);
  101. $body = $response->getContent();
  102. $this->assertEquals(200, $response->getStatusCode());
  103. $data = json_decode($body);
  104. $this->assertEquals('Post Title', $data->data->name);
  105. $this->assertEquals('This is a blog post.', $data->data->content->text);
  106. $this->assertEquals('http://source.example.com/featured.jpg', $data->data->featured);
  107. }
  108. public function testContentWithPrefixedName() {
  109. $url = 'http://source.example.com/content-with-prefixed-name';
  110. $response = $this->parse(['url' => $url]);
  111. $body = $response->getContent();
  112. $this->assertEquals(200, $response->getStatusCode());
  113. $data = json_decode($body);
  114. $this->assertObjectNotHasAttribute('name', $data->data);
  115. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  116. $this->assertEquals('This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.', $data->data->content->html);
  117. }
  118. public function testContentWithDistinctName() {
  119. $url = 'http://source.example.com/content-with-distinct-name';
  120. $response = $this->parse(['url' => $url]);
  121. $body = $response->getContent();
  122. $this->assertEquals(200, $response->getStatusCode());
  123. $data = json_decode($body);
  124. $this->assertEquals('Hello World', $data->data->name);
  125. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  126. $this->assertEquals('This page has a link to <a href="http://target.example.com">target.example.com</a> and some <b>formatted text</b>.', $data->data->content->html);
  127. }
  128. public function testNameWithNoContent() {
  129. $url = 'http://source.example.com/name-no-content';
  130. $response = $this->parse(['url' => $url]);
  131. $body = $response->getContent();
  132. $this->assertEquals(200, $response->getStatusCode());
  133. $data = json_decode($body);
  134. $this->assertEquals('Hello World', $data->data->name);
  135. $this->assertObjectNotHasAttribute('content', $data->data);
  136. }
  137. public function testEntryWithDuplicateCategories() {
  138. $url = 'http://source.example.com/h-entry-duplicate-categories';
  139. $response = $this->parse(['url' => $url]);
  140. $body = $response->getContent();
  141. $this->assertEquals(200, $response->getStatusCode());
  142. $data = json_decode($body);
  143. $this->assertEquals(['indieweb'], $data->data->category);
  144. }
  145. public function testEntryStripHashtagWithDuplicateCategories() {
  146. $url = 'http://source.example.com/h-entry-strip-hashtag-from-categories';
  147. $response = $this->parse(['url' => $url]);
  148. $body = $response->getContent();
  149. $this->assertEquals(200, $response->getStatusCode());
  150. $data = json_decode($body);
  151. $this->assertContains('indieweb', $data->data->category);
  152. $this->assertContains('xray', $data->data->category);
  153. $this->assertEquals(2, count($data->data->category));
  154. }
  155. public function testNoHEntryMarkup() {
  156. $url = 'http://source.example.com/no-h-entry';
  157. $response = $this->parse(['url' => $url]);
  158. $body = $response->getContent();
  159. $this->assertEquals(200, $response->getStatusCode());
  160. $data = json_decode($body);
  161. $this->assertEquals('unknown', $data->data->type);
  162. }
  163. public function testReplyIsURL() {
  164. $url = 'http://source.example.com/reply-is-url';
  165. $response = $this->parse(['url' => $url]);
  166. $body = $response->getContent();
  167. $this->assertEquals(200, $response->getStatusCode());
  168. $data = json_decode($body, true);
  169. $this->assertEquals('entry', $data['data']['type']);
  170. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  171. }
  172. public function testReplyIsHCite() {
  173. $url = 'http://source.example.com/reply-is-h-cite';
  174. $response = $this->parse(['url' => $url]);
  175. $body = $response->getContent();
  176. $this->assertEquals(200, $response->getStatusCode());
  177. $data = json_decode($body, true);
  178. $this->assertEquals('entry', $data['data']['type']);
  179. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  180. $this->assertArrayHasKey('http://example.com/100', $data['data']['refs']);
  181. $this->assertEquals('Example Post', $data['data']['refs']['http://example.com/100']['name']);
  182. $this->assertEquals('http://example.com/100', $data['data']['refs']['http://example.com/100']['url']);
  183. }
  184. public function testPersonTagIsURL() {
  185. $url = 'http://source.example.com/person-tag-is-url';
  186. $response = $this->parse(['url' => $url]);
  187. $body = $response->getContent();
  188. $this->assertEquals(200, $response->getStatusCode());
  189. $data = json_decode($body, true);
  190. $this->assertEquals('entry', $data['data']['type']);
  191. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  192. }
  193. public function testPersonTagIsHCard() {
  194. $url = 'http://source.example.com/person-tag-is-h-card';
  195. $response = $this->parse(['url' => $url]);
  196. $body = $response->getContent();
  197. $this->assertEquals(200, $response->getStatusCode());
  198. $data = json_decode($body, true);
  199. $this->assertEquals('entry', $data['data']['type']);
  200. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  201. $this->assertArrayHasKey('http://alice.example.com/', $data['data']['refs']);
  202. $this->assertEquals('card', $data['data']['refs']['http://alice.example.com/']['type']);
  203. $this->assertEquals('http://alice.example.com/', $data['data']['refs']['http://alice.example.com/']['url']);
  204. $this->assertEquals('Alice', $data['data']['refs']['http://alice.example.com/']['name']);
  205. }
  206. public function testSyndicationIsURL() {
  207. $url = 'http://source.example.com/has-syndication';
  208. $response = $this->parse(['url' => $url]);
  209. $body = $response->getContent();
  210. $this->assertEquals(200, $response->getStatusCode());
  211. $data = json_decode($body, true);
  212. $this->assertEquals('entry', $data['data']['type']);
  213. $this->assertEquals('http://syndicated.example/', $data['data']['syndication'][0]);
  214. }
  215. public function testHEntryNoContent() {
  216. $url = 'http://source.example.com/h-entry-no-content';
  217. $response = $this->parse(['url' => $url]);
  218. $body = $response->getContent();
  219. $this->assertEquals(200, $response->getStatusCode());
  220. $data = json_decode($body);
  221. $this->assertObjectNotHasAttribute('content', $data->data);
  222. $this->assertEquals('This is a Post', $data->data->name);
  223. }
  224. public function testHEntryIsNotFirstObject() {
  225. $url = 'http://source.example.com/h-entry-is-not-first';
  226. $response = $this->parse(['url' => $url]);
  227. $body = $response->getContent();
  228. $this->assertEquals(200, $response->getStatusCode());
  229. $data = json_decode($body, true);
  230. $this->assertEquals('entry', $data['data']['type']);
  231. $this->assertEquals('Hello World', $data['data']['content']['text']);
  232. }
  233. public function testHEntryRSVP() {
  234. $url = 'http://source.example.com/h-entry-rsvp';
  235. $response = $this->parse(['url' => $url]);
  236. $body = $response->getContent();
  237. $this->assertEquals(200, $response->getStatusCode());
  238. $data = json_decode($body, true);
  239. $this->assertEquals('entry', $data['data']['type']);
  240. $this->assertEquals('I\'ll be there!', $data['data']['name']);
  241. $this->assertEquals('yes', $data['data']['rsvp']);
  242. }
  243. public function testMultipleHEntryOnPermalink() {
  244. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  245. $response = $this->parse(['url' => $url]);
  246. $body = $response->getContent();
  247. $this->assertEquals(200, $response->getStatusCode());
  248. $data = json_decode($body, true);
  249. $this->assertEquals('entry', $data['data']['type']);
  250. $this->assertEquals('Primary Post', $data['data']['name']);
  251. }
  252. public function testHEntryWithHCardSibling() {
  253. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  254. $response = $this->parse(['url' => $url]);
  255. $body = $response->getContent();
  256. $this->assertEquals(200, $response->getStatusCode());
  257. $data = json_decode($body, true);
  258. $this->assertEquals('entry', $data['data']['type']);
  259. $this->assertEquals('Hello World', $data['data']['content']['text']);
  260. }
  261. public function testHEntryRedirectWithHCardSibling() {
  262. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  263. $response = $this->parse(['url' => $url]);
  264. $body = $response->getContent();
  265. $this->assertEquals(200, $response->getStatusCode());
  266. $data = json_decode($body, true);
  267. $this->assertEquals('entry', $data['data']['type']);
  268. $this->assertEquals('Hello World', $data['data']['content']['text']);
  269. }
  270. public function testSingleHEntryHasNoPermalink() {
  271. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  272. $response = $this->parse(['url' => $url]);
  273. $body = $response->getContent();
  274. $this->assertEquals(200, $response->getStatusCode());
  275. $data = json_decode($body, true);
  276. $this->assertEquals('entry', $data['data']['type']);
  277. $this->assertEquals('Hello World', $data['data']['content']['text']);
  278. }
  279. public function testBridgyExampleWithNoMatchingURL() {
  280. $url = 'http://source.example.com/bridgy-example';
  281. $response = $this->parse(['url' => $url]);
  282. $body = $response->getContent();
  283. $this->assertEquals(200, $response->getStatusCode());
  284. $data = json_decode($body, true);
  285. $this->assertEquals('entry', $data['data']['type']);
  286. }
  287. public function testEventWithHTMLDescription() {
  288. $url = 'http://source.example.com/h-event';
  289. $response = $this->parse(['url' => $url]);
  290. $body = $response->getContent();
  291. $this->assertEquals(200, $response->getStatusCode());
  292. $data = json_decode($body, true);
  293. $this->assertEquals('event', $data['data']['type']);
  294. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  295. $this->assertEquals($url, $data['data']['url']);
  296. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  297. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  298. $this->assertStringStartsWith("Are you building your own website? Indie reader? Personal publishing web app? Or some other digital magic-cloud proxy? If so, come on by and join a gathering of people with likeminded interests. Bring your friends that want to start a personal web site. Exchange information, swap ideas, talk shop, help work on a project...", $data['data']['description']['text']);
  299. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  300. $this->assertStringStartsWith("<p>Are you building your own website? Indie reader? Personal publishing web app? Or some other digital magic-cloud proxy? If so, come on by and join a gathering of people with likeminded interests. Bring your friends that want to start a personal web site. Exchange information, swap ideas, talk shop, help work on a project...</p>", $data['data']['description']['html']);
  301. $this->assertStringEndsWith('<p>See the <a href="http://tantek.com/2013/332/b1/homebrew-website-club-newsletter">Homebrew Website Club Newsletter Volume 1 Issue 1</a> for a description of the first meeting.</p>', $data['data']['description']['html']);
  302. }
  303. public function testEventWithTextDescription() {
  304. $url = 'http://source.example.com/h-event-text-description';
  305. $response = $this->parse(['url' => $url]);
  306. $body = $response->getContent();
  307. $this->assertEquals(200, $response->getStatusCode());
  308. $data = json_decode($body, true);
  309. $this->assertEquals('event', $data['data']['type']);
  310. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  311. $this->assertEquals($url, $data['data']['url']);
  312. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  313. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  314. $this->assertStringStartsWith("Are you building your own website? Indie reader? Personal publishing web app? Or some other digital magic-cloud proxy? If so, come on by and join a gathering of people with likeminded interests. Bring your friends that want to start a personal web site. Exchange information, swap ideas, talk shop, help work on a project...", $data['data']['description']['text']);
  315. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  316. $this->assertArrayNotHasKey('html', $data['data']['description']);
  317. }
  318. public function testEventWithHCardLocation() {
  319. $url = 'http://source.example.com/h-event-with-h-card-location';
  320. $response = $this->parse(['url' => $url]);
  321. $body = $response->getContent();
  322. $this->assertEquals(200, $response->getStatusCode());
  323. $data = json_decode($body, true);
  324. $this->assertEquals('event', $data['data']['type']);
  325. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  326. $this->assertEquals($url, $data['data']['url']);
  327. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  328. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  329. $this->assertArrayHasKey('http://source.example.com/venue', $data['data']['refs']);
  330. $this->assertEquals('card', $data['data']['refs']['http://source.example.com/venue']['type']);
  331. $this->assertEquals('http://source.example.com/venue', $data['data']['refs']['http://source.example.com/venue']['url']);
  332. $this->assertEquals('Venue', $data['data']['refs']['http://source.example.com/venue']['name']);
  333. }
  334. public function testMf2ReviewOfProduct() {
  335. $url = 'http://source.example.com/h-review-of-product';
  336. $response = $this->parse(['url' => $url]);
  337. $body = $response->getContent();
  338. $this->assertEquals(200, $response->getStatusCode());
  339. $data = json_decode($body, true);
  340. $this->assertEquals('review', $data['data']['type']);
  341. $this->assertEquals('Review', $data['data']['name']);
  342. $this->assertEquals('Not great', $data['data']['summary']);
  343. $this->assertEquals('3', $data['data']['rating']);
  344. $this->assertEquals('5', $data['data']['best']);
  345. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  346. $this->assertContains('red', $data['data']['category']);
  347. $this->assertContains('blue', $data['data']['category']);
  348. $this->assertContains('http://product.example.com/', $data['data']['item']);
  349. $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
  350. $this->assertEquals('product', $data['data']['refs']['http://product.example.com/']['type']);
  351. $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
  352. $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
  353. }
  354. public function testMf2ReviewOfHCard() {
  355. $url = 'http://source.example.com/h-review-of-h-card';
  356. $response = $this->parse(['url' => $url]);
  357. $body = $response->getContent();
  358. $this->assertEquals(200, $response->getStatusCode());
  359. $data = json_decode($body, true);
  360. $this->assertEquals('review', $data['data']['type']);
  361. $this->assertEquals('Review', $data['data']['name']);
  362. $this->assertEquals('Not great', $data['data']['summary']);
  363. $this->assertEquals('3', $data['data']['rating']);
  364. $this->assertEquals('5', $data['data']['best']);
  365. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  366. $this->assertContains('http://business.example.com/', $data['data']['item']);
  367. $this->assertArrayHasKey('http://business.example.com/', $data['data']['refs']);
  368. $this->assertEquals('card', $data['data']['refs']['http://business.example.com/']['type']);
  369. $this->assertEquals('The Reviewed Business', $data['data']['refs']['http://business.example.com/']['name']);
  370. $this->assertEquals('http://business.example.com/', $data['data']['refs']['http://business.example.com/']['url']);
  371. }
  372. public function testMf1Review() {
  373. $url = 'http://source.example.com/hReview';
  374. $response = $this->parse(['url' => $url]);
  375. $body = $response->getContent();
  376. $this->assertEquals(200, $response->getStatusCode());
  377. $data = json_decode($body, true);
  378. $this->assertEquals('review', $data['data']['type']);
  379. $this->assertEquals('Not great', $data['data']['summary']);
  380. $this->assertEquals('3', $data['data']['rating']);
  381. $this->assertEquals('5', $data['data']['best']);
  382. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  383. $this->assertContains('http://product.example.com/', $data['data']['item']);
  384. $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
  385. $this->assertEquals('item', $data['data']['refs']['http://product.example.com/']['type']);
  386. $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
  387. $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
  388. }
  389. public function testMf2Recipe() {
  390. $url = 'http://source.example.com/h-recipe';
  391. $response = $this->parse(['url' => $url]);
  392. $body = $response->getContent();
  393. $this->assertEquals(200, $response->getStatusCode());
  394. $data = json_decode($body, true);
  395. $this->assertEquals('recipe', $data['data']['type']);
  396. $this->assertEquals('Cookie Recipe', $data['data']['name']);
  397. $this->assertEquals('12 Cookies', $data['data']['yield']);
  398. $this->assertEquals('PT30M', $data['data']['duration']);
  399. $this->assertEquals('The best chocolate chip cookie recipe', $data['data']['summary']);
  400. $this->assertContains('3 cups flour', $data['data']['ingredient']);
  401. $this->assertContains('chocolate chips', $data['data']['ingredient']);
  402. }
  403. public function testEntryIsAnInvitee() {
  404. $url = 'http://source.example.com/bridgy-invitee';
  405. $response = $this->parse(['url' => $url]);
  406. $body = $response->getContent();
  407. $this->assertEquals(200, $response->getStatusCode());
  408. $data = json_decode($body, true);
  409. $this->assertEquals('entry', $data['data']['type']);
  410. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  411. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  412. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['data']['refs']);
  413. $this->assertEquals('Tantek Çelik', $data['data']['refs']['https://www.facebook.com/tantek.celik']['name']);
  414. }
  415. public function testEntryAtFragmentID() {
  416. $url = 'http://source.example.com/fragment-id#comment-1000';
  417. $response = $this->parse(['url' => $url]);
  418. $body = $response->getContent();
  419. $this->assertEquals(200, $response->getStatusCode());
  420. $data = json_decode($body, true);
  421. $this->assertEquals('entry', $data['data']['type']);
  422. $this->assertEquals('Comment text', $data['data']['content']['text']);
  423. $this->assertEquals('http://source.example.com/fragment-id#comment-1000', $data['data']['url']);
  424. $this->assertTrue($data['info']['found_fragment']);
  425. }
  426. public function testEntryAtNonExistentFragmentID() {
  427. $url = 'http://source.example.com/fragment-id#comment-404';
  428. $response = $this->parse(['url' => $url]);
  429. $body = $response->getContent();
  430. $this->assertEquals(200, $response->getStatusCode());
  431. $data = json_decode($body, true);
  432. $this->assertEquals('entry', $data['data']['type']);
  433. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  434. $this->assertFalse($data['info']['found_fragment']);
  435. }
  436. public function testCheckin() {
  437. $url = 'http://source.example.com/checkin';
  438. $response = $this->parse(['url' => $url]);
  439. $body = $response->getContent();
  440. $this->assertEquals(200, $response->getStatusCode());
  441. $data = json_decode($body, true);
  442. $this->assertEquals('entry', $data['data']['type']);
  443. $venue = $data['data']['checkin'];
  444. $this->assertEquals('https://foursquare.com/v/57104d2e498ece022e169dca', $venue['url']);
  445. $this->assertEquals('DreamHost', $venue['name']);
  446. $this->assertEquals('45.518716', $venue['latitude']);
  447. $this->assertEquals('Homebrew Website Club!', $data['data']['content']['text']);
  448. $this->assertEquals('https://aaronparecki.com/2017/06/07/12/photo.jpg', $data['data']['photo'][0]);
  449. $this->assertEquals('2017-06-07T17:14:40-07:00', $data['data']['published']);
  450. $this->assertArrayNotHasKey('name', $data['data']);
  451. }
  452. public function testCheckinURLOnly() {
  453. $url = 'http://source.example.com/checkin-url';
  454. $response = $this->parse(['url' => $url]);
  455. $body = $response->getContent();
  456. $this->assertEquals(200, $response->getStatusCode());
  457. $data = json_decode($body, true);
  458. $this->assertEquals('entry', $data['data']['type']);
  459. $venue = $data['data']['checkin'];
  460. $this->assertEquals('https://foursquare.com/v/57104d2e498ece022e169dca', $venue['url']);
  461. $this->assertEquals('Homebrew Website Club!', $data['data']['content']['text']);
  462. $this->assertEquals('https://aaronparecki.com/2017/06/07/12/photo.jpg', $data['data']['photo'][0]);
  463. $this->assertEquals('2017-06-07T17:14:40-07:00', $data['data']['published']);
  464. $this->assertArrayNotHasKey('name', $data['data']);
  465. }
  466. public function testXKCD() {
  467. $url = 'http://xkcd.com/1810/';
  468. $response = $this->parse(['url' => $url]);
  469. $body = $response->getContent();
  470. $this->assertEquals(200, $response->getStatusCode());
  471. $data = json_decode($body, true);
  472. $this->assertEquals('entry', $data['data']['type']);
  473. $this->assertEquals('http://xkcd.com/1810/', $data['data']['url']);
  474. $this->assertEquals('Chat Systems', $data['data']['name']);
  475. $this->assertContains('http://imgs.xkcd.com/comics/chat_systems_2x.png', $data['data']['photo']);
  476. }
  477. public function testEntryHasMultipleURLs() {
  478. $url = 'http://source.example.com/multiple-urls';
  479. $response = $this->parse(['url' => $url]);
  480. $body = $response->getContent();
  481. $this->assertEquals(200, $response->getStatusCode());
  482. $data = json_decode($body, true);
  483. // Should prioritize the URL on the same domain
  484. $this->assertEquals($url, $data['data']['url']);
  485. }
  486. public function testEntryHasMultipleURLsOffDomain() {
  487. $url = 'http://source.example.com/multiple-urls-off-domain';
  488. $response = $this->parse(['url' => $url]);
  489. $body = $response->getContent();
  490. $this->assertEquals(200, $response->getStatusCode());
  491. $data = json_decode($body, true);
  492. // Neither URL is on the same domain, so should use the first
  493. $this->assertEquals('http://one.example.com/test', $data['data']['url']);
  494. }
  495. public function testInputIsParsedMf2() {
  496. $html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
  497. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  498. $url = 'http://example.com/entry';
  499. $response = $this->parse([
  500. 'url' => $url,
  501. 'body' => json_encode($mf2)
  502. ]);
  503. $body = $response->getContent();
  504. $this->assertEquals(200, $response->getStatusCode());
  505. $data = json_decode($body, true);
  506. $this->assertEquals('Hello World', $data['data']['content']['text']);
  507. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  508. }
  509. public function testInputIsParsedMf2WithHTML() {
  510. $html = '<div class="h-entry"><p class="e-content p-name"><b>Hello</b> <i>World</i></p><img src="/photo.jpg"></p></div>';
  511. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  512. $url = 'http://example.com/entry';
  513. $response = $this->parse([
  514. 'url' => $url,
  515. 'body' => json_encode($mf2)
  516. ]);
  517. $body = $response->getContent();
  518. $this->assertEquals(200, $response->getStatusCode());
  519. $data = json_decode($body, true);
  520. $this->assertEquals('Hello World', $data['data']['content']['text']);
  521. $this->assertEquals('<b>Hello</b> <i>World</i>', $data['data']['content']['html']);
  522. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  523. }
  524. public function testHApp() {
  525. $url = 'http://source.example.com/h-app';
  526. $response = $this->parse(['url' => $url]);
  527. $body = $response->getContent();
  528. $this->assertEquals(200, $response->getStatusCode());
  529. $data = json_decode($body);
  530. $this->assertEquals('app', $data->data->type);
  531. $this->assertEquals('http://source.example.com/images/quill.png', $data->data->logo);
  532. $this->assertEquals('Quill', $data->data->name);
  533. $this->assertEquals($url, $data->data->url);
  534. $this->assertObjectNotHasAttribute('photo', $data->data);
  535. }
  536. public function testHXApp() {
  537. $url = 'http://source.example.com/h-x-app';
  538. $response = $this->parse(['url' => $url]);
  539. $body = $response->getContent();
  540. $this->assertEquals(200, $response->getStatusCode());
  541. $data = json_decode($body);
  542. $this->assertEquals('app', $data->data->type);
  543. $this->assertEquals('http://source.example.com/images/quill.png', $data->data->logo);
  544. $this->assertEquals('Quill', $data->data->name);
  545. $this->assertEquals($url, $data->data->url);
  546. $this->assertObjectNotHasAttribute('photo', $data->data);
  547. }
  548. }