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.

836 lines
36 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 testContentFromJSONOnlyPlaintext() {
  63. $mf2JSON = json_encode([
  64. 'items' => [[
  65. 'type' => ['h-entry'],
  66. 'properties' => [
  67. 'content' => [
  68. 'plaintext'
  69. ]
  70. ]
  71. ]]
  72. ]);
  73. $xray = new \p3k\XRay();
  74. $parsed = $xray->process(false, $mf2JSON);
  75. $item = $parsed['data'];
  76. $this->assertEquals('entry', $item['type']);
  77. $this->assertEquals('note', $item['post-type']);
  78. $this->assertEquals('plaintext', $item['content']['text']);
  79. $this->assertArrayNotHasKey('html', $item['content']);
  80. }
  81. public function testHTMLContentFromJSONNoPlaintext() {
  82. $mf2JSON = json_encode([
  83. 'items' => [[
  84. 'type' => ['h-entry'],
  85. 'properties' => [
  86. 'content' => [[
  87. 'html' => '<b>bold</b> <i>italic</i> text'
  88. ]]
  89. ]
  90. ]]
  91. ]);
  92. $xray = new \p3k\XRay();
  93. $parsed = $xray->process(false, $mf2JSON);
  94. $item = $parsed['data'];
  95. $this->assertEquals('entry', $item['type']);
  96. $this->assertEquals('note', $item['post-type']);
  97. $this->assertEquals('bold italic text', $item['content']['text']);
  98. $this->assertEquals('<b>bold</b> <i>italic</i> text', $item['content']['html']);
  99. }
  100. public function testHTMLContentFromJSONEmptyTags() {
  101. $mf2JSON = json_encode([
  102. 'items' => [[
  103. 'type' => ['h-entry'],
  104. 'properties' => [
  105. 'content' => [[
  106. 'html' => '<b></b><i></i>'
  107. ]]
  108. ]
  109. ]]
  110. ]);
  111. $xray = new \p3k\XRay();
  112. $parsed = $xray->process(false, $mf2JSON);
  113. $item = $parsed['data'];
  114. $this->assertEquals('entry', $item['type']);
  115. $this->assertEquals('note', $item['post-type']);
  116. $this->assertArrayNotHasKey('content', $item);
  117. }
  118. public function testHTMLContentFromJSONContentMismatch() {
  119. $mf2JSON = json_encode([
  120. 'items' => [[
  121. 'type' => ['h-entry'],
  122. 'properties' => [
  123. 'content' => [[
  124. 'value' => 'foo',
  125. 'html' => '<b>bar</b>'
  126. ]]
  127. ]
  128. ]]
  129. ]);
  130. $xray = new \p3k\XRay();
  131. $parsed = $xray->process(false, $mf2JSON);
  132. $item = $parsed['data'];
  133. $this->assertEquals('entry', $item['type']);
  134. $this->assertEquals('note', $item['post-type']);
  135. $this->assertEquals('bar', $item['content']['text']);
  136. $this->assertEquals('<b>bar</b>', $item['content']['html']);
  137. }
  138. public function testHTMLContentFromJSONNoHTML() {
  139. $mf2JSON = json_encode([
  140. 'items' => [[
  141. 'type' => ['h-entry'],
  142. 'properties' => [
  143. 'content' => [[
  144. 'value' => 'foo',
  145. ]]
  146. ]
  147. ]]
  148. ]);
  149. $xray = new \p3k\XRay();
  150. $parsed = $xray->process(false, $mf2JSON);
  151. $item = $parsed['data'];
  152. $this->assertEquals('entry', $item['type']);
  153. $this->assertEquals('note', $item['post-type']);
  154. $this->assertArrayNotHasKey('content', $item);
  155. }
  156. public function testFindTargetLinkIsImage() {
  157. $url = 'http://source.example.com/link-is-img';
  158. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/photo.jpg']);
  159. $body = $response->getContent();
  160. $this->assertEquals(200, $response->getStatusCode());
  161. $data = json_decode($body);
  162. $this->assertEquals('photo', $data->data->{'post-type'});
  163. $this->assertObjectNotHasAttribute('name', $data->data);
  164. $this->assertEquals('This page has an img tag with the target URL.', $data->data->content->text);
  165. }
  166. public function testFindTargetLinkIsVideo() {
  167. $url = 'http://source.example.com/link-is-video';
  168. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/movie.mp4']);
  169. $body = $response->getContent();
  170. $this->assertEquals(200, $response->getStatusCode());
  171. $data = json_decode($body);
  172. $this->assertEquals('video', $data->data->{'post-type'});
  173. $this->assertObjectNotHasAttribute('name', $data->data);
  174. $this->assertEquals('This page has a video tag with the target URL.', $data->data->content->text);
  175. }
  176. public function testFindTargetLinkIsAudio() {
  177. $url = 'http://source.example.com/link-is-audio';
  178. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/media.mp3']);
  179. $body = $response->getContent();
  180. $this->assertEquals(200, $response->getStatusCode());
  181. $data = json_decode($body);
  182. $this->assertEquals('audio', $data->data->{'post-type'});
  183. $this->assertObjectNotHasAttribute('name', $data->data);
  184. $this->assertEquals('This page has an audio tag with the target URL.', $data->data->content->text);
  185. }
  186. public function testTextContent() {
  187. $url = 'http://source.example.com/text-content';
  188. $response = $this->parse(['url' => $url]);
  189. $body = $response->getContent();
  190. $this->assertEquals(200, $response->getStatusCode());
  191. $data = json_decode($body);
  192. $this->assertObjectNotHasAttribute('name', $data->data);
  193. $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);
  194. }
  195. public function testArticleWithFeaturedImage() {
  196. $url = 'http://source.example.com/article-with-featured-image';
  197. $response = $this->parse(['url' => $url]);
  198. $body = $response->getContent();
  199. $this->assertEquals(200, $response->getStatusCode());
  200. $data = json_decode($body);
  201. $this->assertEquals('article', $data->data->{'post-type'});
  202. $this->assertEquals('Post Title', $data->data->name);
  203. $this->assertEquals('This is a blog post.', $data->data->content->text);
  204. $this->assertEquals('http://source.example.com/featured.jpg', $data->data->featured);
  205. }
  206. public function testContentWithPrefixedName() {
  207. $url = 'http://source.example.com/content-with-prefixed-name';
  208. $response = $this->parse(['url' => $url]);
  209. $body = $response->getContent();
  210. $this->assertEquals(200, $response->getStatusCode());
  211. $data = json_decode($body);
  212. $this->assertObjectNotHasAttribute('name', $data->data);
  213. $this->assertEquals('note', $data->data->{'post-type'});
  214. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  215. $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);
  216. }
  217. public function testContentWithDistinctName() {
  218. $url = 'http://source.example.com/content-with-distinct-name';
  219. $response = $this->parse(['url' => $url]);
  220. $body = $response->getContent();
  221. $this->assertEquals(200, $response->getStatusCode());
  222. $data = json_decode($body);
  223. $this->assertEquals('Hello World', $data->data->name);
  224. $this->assertEquals('article', $data->data->{'post-type'});
  225. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  226. $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);
  227. }
  228. public function testNameWithNoContent() {
  229. $url = 'http://source.example.com/name-no-content';
  230. $response = $this->parse(['url' => $url]);
  231. $body = $response->getContent();
  232. $this->assertEquals(200, $response->getStatusCode());
  233. $data = json_decode($body);
  234. $this->assertEquals('Hello World', $data->data->name);
  235. $this->assertEquals('article', $data->data->{'post-type'});
  236. $this->assertObjectNotHasAttribute('content', $data->data);
  237. }
  238. public function testEntryWithDuplicateCategories() {
  239. $url = 'http://source.example.com/h-entry-duplicate-categories';
  240. $response = $this->parse(['url' => $url]);
  241. $body = $response->getContent();
  242. $this->assertEquals(200, $response->getStatusCode());
  243. $data = json_decode($body);
  244. $this->assertEquals(['indieweb'], $data->data->category);
  245. }
  246. public function testEntryStripHashtagWithDuplicateCategories() {
  247. $url = 'http://source.example.com/h-entry-strip-hashtag-from-categories';
  248. $response = $this->parse(['url' => $url]);
  249. $body = $response->getContent();
  250. $this->assertEquals(200, $response->getStatusCode());
  251. $data = json_decode($body);
  252. $this->assertContains('indieweb', $data->data->category);
  253. $this->assertContains('xray', $data->data->category);
  254. $this->assertEquals(2, count($data->data->category));
  255. }
  256. public function testNoHEntryMarkup() {
  257. $url = 'http://source.example.com/no-h-entry';
  258. $response = $this->parse(['url' => $url]);
  259. $body = $response->getContent();
  260. $this->assertEquals(200, $response->getStatusCode());
  261. $data = json_decode($body);
  262. $this->assertEquals('unknown', $data->data->type);
  263. }
  264. public function testReplyIsURL() {
  265. $url = 'http://source.example.com/reply-is-url';
  266. $response = $this->parse(['url' => $url]);
  267. $body = $response->getContent();
  268. $this->assertEquals(200, $response->getStatusCode());
  269. $data = json_decode($body, true);
  270. $this->assertEquals('entry', $data['data']['type']);
  271. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  272. }
  273. public function testReplyIsHCite() {
  274. $url = 'http://source.example.com/reply-is-h-cite';
  275. $response = $this->parse(['url' => $url]);
  276. $body = $response->getContent();
  277. $this->assertEquals(200, $response->getStatusCode());
  278. $data = json_decode($body, true);
  279. $this->assertEquals('entry', $data['data']['type']);
  280. $this->assertEquals('reply', $data['data']['post-type']);
  281. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  282. $this->assertArrayHasKey('http://example.com/100', $data['data']['refs']);
  283. $this->assertEquals('Example Post', $data['data']['refs']['http://example.com/100']['name']);
  284. $this->assertEquals('http://example.com/100', $data['data']['refs']['http://example.com/100']['url']);
  285. }
  286. public function testPersonTagIsURL() {
  287. $url = 'http://source.example.com/person-tag-is-url';
  288. $response = $this->parse(['url' => $url]);
  289. $body = $response->getContent();
  290. $this->assertEquals(200, $response->getStatusCode());
  291. $data = json_decode($body, true);
  292. $this->assertEquals('entry', $data['data']['type']);
  293. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  294. }
  295. public function testPersonTagIsHCard() {
  296. $url = 'http://source.example.com/person-tag-is-h-card';
  297. $response = $this->parse(['url' => $url]);
  298. $body = $response->getContent();
  299. $this->assertEquals(200, $response->getStatusCode());
  300. $data = json_decode($body, true);
  301. $this->assertEquals('entry', $data['data']['type']);
  302. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  303. $this->assertArrayHasKey('http://alice.example.com/', $data['data']['refs']);
  304. $this->assertEquals('card', $data['data']['refs']['http://alice.example.com/']['type']);
  305. $this->assertEquals('http://alice.example.com/', $data['data']['refs']['http://alice.example.com/']['url']);
  306. $this->assertEquals('Alice', $data['data']['refs']['http://alice.example.com/']['name']);
  307. }
  308. public function testSyndicationIsURL() {
  309. $url = 'http://source.example.com/has-syndication';
  310. $response = $this->parse(['url' => $url]);
  311. $body = $response->getContent();
  312. $this->assertEquals(200, $response->getStatusCode());
  313. $data = json_decode($body, true);
  314. $this->assertEquals('entry', $data['data']['type']);
  315. $this->assertEquals('http://syndicated.example/', $data['data']['syndication'][0]);
  316. }
  317. public function testHEntryNoContent() {
  318. $url = 'http://source.example.com/h-entry-no-content';
  319. $response = $this->parse(['url' => $url]);
  320. $body = $response->getContent();
  321. $this->assertEquals(200, $response->getStatusCode());
  322. $data = json_decode($body);
  323. $this->assertObjectNotHasAttribute('content', $data->data);
  324. $this->assertEquals('This is a Post', $data->data->name);
  325. }
  326. public function testHEntryIsNotFirstObject() {
  327. $url = 'http://source.example.com/h-entry-is-not-first';
  328. $response = $this->parse(['url' => $url]);
  329. $body = $response->getContent();
  330. $this->assertEquals(200, $response->getStatusCode());
  331. $data = json_decode($body, true);
  332. $this->assertEquals('entry', $data['data']['type']);
  333. $this->assertEquals('Hello World', $data['data']['content']['text']);
  334. }
  335. public function testHEntryRSVP() {
  336. $url = 'http://source.example.com/h-entry-rsvp';
  337. $response = $this->parse(['url' => $url]);
  338. $body = $response->getContent();
  339. $this->assertEquals(200, $response->getStatusCode());
  340. $data = json_decode($body, true);
  341. $this->assertEquals('entry', $data['data']['type']);
  342. $this->assertEquals('rsvp', $data['data']['post-type']);
  343. $this->assertEquals('I\'ll be there!', $data['data']['content']['text']);
  344. $this->assertEquals('yes', $data['data']['rsvp']);
  345. }
  346. public function testMultipleHEntryOnPermalink() {
  347. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  348. $response = $this->parse(['url' => $url]);
  349. $body = $response->getContent();
  350. $this->assertEquals(200, $response->getStatusCode());
  351. $data = json_decode($body, true);
  352. $this->assertEquals('entry', $data['data']['type']);
  353. $this->assertEquals('Primary Post', $data['data']['name']);
  354. }
  355. public function testHEntryWithHCardSibling() {
  356. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  357. $response = $this->parse(['url' => $url]);
  358. $body = $response->getContent();
  359. $this->assertEquals(200, $response->getStatusCode());
  360. $data = json_decode($body, true);
  361. $this->assertEquals('entry', $data['data']['type']);
  362. $this->assertEquals('Hello World', $data['data']['content']['text']);
  363. }
  364. public function testHEntryRedirectWithHCardSibling() {
  365. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  366. $response = $this->parse(['url' => $url]);
  367. $body = $response->getContent();
  368. $this->assertEquals(200, $response->getStatusCode());
  369. $data = json_decode($body, true);
  370. $this->assertEquals('entry', $data['data']['type']);
  371. $this->assertEquals('Hello World', $data['data']['content']['text']);
  372. }
  373. public function testSingleHEntryHasNoPermalink() {
  374. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  375. $response = $this->parse(['url' => $url]);
  376. $body = $response->getContent();
  377. $this->assertEquals(200, $response->getStatusCode());
  378. $data = json_decode($body, true);
  379. $this->assertEquals('entry', $data['data']['type']);
  380. $this->assertEquals('Hello World', $data['data']['content']['text']);
  381. }
  382. public function testBridgyExampleWithNoMatchingURL() {
  383. $url = 'http://source.example.com/bridgy-example';
  384. $response = $this->parse(['url' => $url]);
  385. $body = $response->getContent();
  386. $this->assertEquals(200, $response->getStatusCode());
  387. $data = json_decode($body, true);
  388. $this->assertEquals('entry', $data['data']['type']);
  389. }
  390. public function testEventWithHTMLDescription() {
  391. $url = 'http://source.example.com/h-event';
  392. $response = $this->parse(['url' => $url]);
  393. $body = $response->getContent();
  394. $this->assertEquals(200, $response->getStatusCode());
  395. $data = json_decode($body, true);
  396. $this->assertEquals('event', $data['data']['type']);
  397. $this->assertEquals('event', $data['data']['post-type']);
  398. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  399. $this->assertEquals($url, $data['data']['url']);
  400. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  401. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  402. $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']);
  403. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  404. $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']);
  405. $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']);
  406. }
  407. public function testEventWithTextDescription() {
  408. $url = 'http://source.example.com/h-event-text-description';
  409. $response = $this->parse(['url' => $url]);
  410. $body = $response->getContent();
  411. $this->assertEquals(200, $response->getStatusCode());
  412. $data = json_decode($body, true);
  413. $this->assertEquals('event', $data['data']['type']);
  414. $this->assertEquals('event', $data['data']['post-type']);
  415. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  416. $this->assertEquals($url, $data['data']['url']);
  417. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  418. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  419. $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']);
  420. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  421. $this->assertArrayNotHasKey('html', $data['data']['description']);
  422. }
  423. public function testEventWithHCardLocation() {
  424. $url = 'http://source.example.com/h-event-with-h-card-location';
  425. $response = $this->parse(['url' => $url]);
  426. $body = $response->getContent();
  427. $this->assertEquals(200, $response->getStatusCode());
  428. $data = json_decode($body, true);
  429. $this->assertEquals('event', $data['data']['type']);
  430. $this->assertEquals('event', $data['data']['post-type']);
  431. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  432. $this->assertEquals($url, $data['data']['url']);
  433. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  434. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  435. $this->assertArrayHasKey('http://source.example.com/venue', $data['data']['refs']);
  436. $this->assertEquals('card', $data['data']['refs']['http://source.example.com/venue']['type']);
  437. $this->assertEquals('http://source.example.com/venue', $data['data']['refs']['http://source.example.com/venue']['url']);
  438. $this->assertEquals('Venue', $data['data']['refs']['http://source.example.com/venue']['name']);
  439. }
  440. public function testMf2ReviewOfProduct() {
  441. $url = 'http://source.example.com/h-review-of-product';
  442. $response = $this->parse(['url' => $url]);
  443. $body = $response->getContent();
  444. $this->assertEquals(200, $response->getStatusCode());
  445. $data = json_decode($body, true);
  446. $this->assertEquals('review', $data['data']['type']);
  447. $this->assertEquals('review', $data['data']['post-type']);
  448. $this->assertEquals('Review', $data['data']['name']);
  449. $this->assertEquals('Not great', $data['data']['summary']);
  450. $this->assertEquals('3', $data['data']['rating']);
  451. $this->assertEquals('5', $data['data']['best']);
  452. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  453. $this->assertContains('red', $data['data']['category']);
  454. $this->assertContains('blue', $data['data']['category']);
  455. $this->assertContains('http://product.example.com/', $data['data']['item']);
  456. $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
  457. $this->assertEquals('product', $data['data']['refs']['http://product.example.com/']['type']);
  458. $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
  459. $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
  460. }
  461. public function testMf2ReviewOfHCard() {
  462. $url = 'http://source.example.com/h-review-of-h-card';
  463. $response = $this->parse(['url' => $url]);
  464. $body = $response->getContent();
  465. $this->assertEquals(200, $response->getStatusCode());
  466. $data = json_decode($body, true);
  467. $this->assertEquals('review', $data['data']['type']);
  468. $this->assertEquals('review', $data['data']['post-type']);
  469. $this->assertEquals('Review', $data['data']['name']);
  470. $this->assertEquals('Not great', $data['data']['summary']);
  471. $this->assertEquals('3', $data['data']['rating']);
  472. $this->assertEquals('5', $data['data']['best']);
  473. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  474. $this->assertContains('http://business.example.com/', $data['data']['item']);
  475. $this->assertArrayHasKey('http://business.example.com/', $data['data']['refs']);
  476. $this->assertEquals('card', $data['data']['refs']['http://business.example.com/']['type']);
  477. $this->assertEquals('The Reviewed Business', $data['data']['refs']['http://business.example.com/']['name']);
  478. $this->assertEquals('http://business.example.com/', $data['data']['refs']['http://business.example.com/']['url']);
  479. }
  480. public function testMf1Review() {
  481. $url = 'http://source.example.com/hReview';
  482. $response = $this->parse(['url' => $url]);
  483. $body = $response->getContent();
  484. $this->assertEquals(200, $response->getStatusCode());
  485. $data = json_decode($body, true);
  486. $this->assertEquals('review', $data['data']['type']);
  487. $this->assertEquals('review', $data['data']['post-type']);
  488. $this->assertEquals('Not great', $data['data']['name']);
  489. $this->assertEquals('3', $data['data']['rating']);
  490. $this->assertEquals('5', $data['data']['best']);
  491. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  492. $this->assertContains('http://product.example.com/', $data['data']['item']);
  493. $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
  494. $this->assertEquals('item', $data['data']['refs']['http://product.example.com/']['type']);
  495. $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
  496. $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
  497. }
  498. public function testMf2Recipe() {
  499. $url = 'http://source.example.com/h-recipe';
  500. $response = $this->parse(['url' => $url]);
  501. $body = $response->getContent();
  502. $this->assertEquals(200, $response->getStatusCode());
  503. $data = json_decode($body, true);
  504. $this->assertEquals('recipe', $data['data']['type']);
  505. $this->assertEquals('recipe', $data['data']['post-type']);
  506. $this->assertEquals('Cookie Recipe', $data['data']['name']);
  507. $this->assertEquals('12 Cookies', $data['data']['yield']);
  508. $this->assertEquals('PT30M', $data['data']['duration']);
  509. $this->assertEquals('The best chocolate chip cookie recipe', $data['data']['summary']);
  510. $this->assertContains('3 cups flour', $data['data']['ingredient']);
  511. $this->assertContains('chocolate chips', $data['data']['ingredient']);
  512. }
  513. public function testEntryIsAnInvitee() {
  514. $url = 'http://source.example.com/bridgy-invitee';
  515. $response = $this->parse(['url' => $url]);
  516. $body = $response->getContent();
  517. $this->assertEquals(200, $response->getStatusCode());
  518. $data = json_decode($body, true);
  519. $this->assertEquals('entry', $data['data']['type']);
  520. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  521. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  522. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['data']['refs']);
  523. $this->assertEquals('Tantek Çelik', $data['data']['refs']['https://www.facebook.com/tantek.celik']['name']);
  524. }
  525. public function testEntryAtFragmentID() {
  526. $url = 'http://source.example.com/fragment-id#comment-1000';
  527. $response = $this->parse(['url' => $url]);
  528. $body = $response->getContent();
  529. $this->assertEquals(200, $response->getStatusCode());
  530. $data = json_decode($body, true);
  531. $this->assertEquals('entry', $data['data']['type']);
  532. $this->assertEquals('note', $data['data']['post-type']);
  533. $this->assertEquals('Comment text', $data['data']['content']['text']);
  534. $this->assertEquals('http://source.example.com/fragment-id#comment-1000', $data['data']['url']);
  535. $this->assertTrue($data['info']['found_fragment']);
  536. }
  537. public function testEntryAtNonExistentFragmentID() {
  538. $url = 'http://source.example.com/fragment-id#comment-404';
  539. $response = $this->parse(['url' => $url]);
  540. $body = $response->getContent();
  541. $this->assertEquals(200, $response->getStatusCode());
  542. $data = json_decode($body, true);
  543. $this->assertEquals('entry', $data['data']['type']);
  544. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  545. $this->assertFalse($data['info']['found_fragment']);
  546. }
  547. public function testCheckin() {
  548. $url = 'http://source.example.com/checkin';
  549. $response = $this->parse(['url' => $url]);
  550. $body = $response->getContent();
  551. $this->assertEquals(200, $response->getStatusCode());
  552. $data = json_decode($body, true);
  553. $this->assertEquals('entry', $data['data']['type']);
  554. $venue = $data['data']['checkin'];
  555. $this->assertEquals('checkin', $data['data']['post-type']);
  556. $this->assertEquals('https://foursquare.com/v/57104d2e498ece022e169dca', $venue['url']);
  557. $this->assertEquals('DreamHost', $venue['name']);
  558. $this->assertEquals('45.518716', $venue['latitude']);
  559. $this->assertEquals('Homebrew Website Club!', $data['data']['content']['text']);
  560. $this->assertEquals('https://aaronparecki.com/2017/06/07/12/photo.jpg', $data['data']['photo'][0]);
  561. $this->assertEquals('2017-06-07T17:14:40-07:00', $data['data']['published']);
  562. $this->assertArrayNotHasKey('name', $data['data']);
  563. }
  564. public function testCheckinURLOnly() {
  565. $url = 'http://source.example.com/checkin-url';
  566. $response = $this->parse(['url' => $url]);
  567. $body = $response->getContent();
  568. $this->assertEquals(200, $response->getStatusCode());
  569. $data = json_decode($body, true);
  570. $this->assertEquals('entry', $data['data']['type']);
  571. $this->assertEquals('checkin', $data['data']['post-type']);
  572. $venue = $data['data']['checkin'];
  573. $this->assertEquals('https://foursquare.com/v/57104d2e498ece022e169dca', $venue['url']);
  574. $this->assertEquals('Homebrew Website Club!', $data['data']['content']['text']);
  575. $this->assertEquals('https://aaronparecki.com/2017/06/07/12/photo.jpg', $data['data']['photo'][0]);
  576. $this->assertEquals('2017-06-07T17:14:40-07:00', $data['data']['published']);
  577. $this->assertArrayNotHasKey('name', $data['data']);
  578. }
  579. public function testXKCD() {
  580. $url = 'http://xkcd.com/1810/';
  581. $response = $this->parse(['url' => $url]);
  582. $body = $response->getContent();
  583. $this->assertEquals(200, $response->getStatusCode());
  584. $data = json_decode($body, true);
  585. $this->assertEquals('entry', $data['data']['type']);
  586. $this->assertEquals('photo', $data['data']['post-type']);
  587. $this->assertEquals('http://xkcd.com/1810/', $data['data']['url']);
  588. $this->assertEquals('Chat Systems', $data['data']['name']);
  589. $this->assertContains('http://imgs.xkcd.com/comics/chat_systems_2x.png', $data['data']['photo']);
  590. }
  591. public function testEntryHasMultipleURLs() {
  592. $url = 'http://source.example.com/multiple-urls';
  593. $response = $this->parse(['url' => $url]);
  594. $body = $response->getContent();
  595. $this->assertEquals(200, $response->getStatusCode());
  596. $data = json_decode($body, true);
  597. // Should prioritize the URL on the same domain
  598. $this->assertEquals($url, $data['data']['url']);
  599. }
  600. public function testEntryHasMultipleURLsOffDomain() {
  601. $url = 'http://source.example.com/multiple-urls-off-domain';
  602. $response = $this->parse(['url' => $url]);
  603. $body = $response->getContent();
  604. $this->assertEquals(200, $response->getStatusCode());
  605. $data = json_decode($body, true);
  606. // Neither URL is on the same domain, so should use the first
  607. $this->assertEquals('http://one.example.com/test', $data['data']['url']);
  608. }
  609. public function testInputIsParsedMf2() {
  610. $html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
  611. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  612. $url = 'http://example.com/entry';
  613. $response = $this->parse([
  614. 'url' => $url,
  615. 'body' => json_encode($mf2)
  616. ]);
  617. $body = $response->getContent();
  618. $this->assertEquals(200, $response->getStatusCode());
  619. $data = json_decode($body, true);
  620. $this->assertEquals('Hello World', $data['data']['content']['text']);
  621. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  622. }
  623. public function testInputIsParsedMf2WithHTML() {
  624. $html = '<div class="h-entry"><p class="e-content p-name"><b>Hello</b> <i>World</i></p><img src="/photo.jpg"></p></div>';
  625. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  626. $url = 'http://example.com/entry';
  627. $response = $this->parse([
  628. 'url' => $url,
  629. 'body' => json_encode($mf2)
  630. ]);
  631. $body = $response->getContent();
  632. $this->assertEquals(200, $response->getStatusCode());
  633. $data = json_decode($body, true);
  634. $this->assertEquals('Hello World', $data['data']['content']['text']);
  635. $this->assertEquals('<b>Hello</b> <i>World</i>', $data['data']['content']['html']);
  636. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  637. }
  638. public function testHApp() {
  639. $url = 'http://source.example.com/h-app';
  640. $response = $this->parse(['url' => $url]);
  641. $body = $response->getContent();
  642. $this->assertEquals(200, $response->getStatusCode());
  643. $data = json_decode($body, true);
  644. $this->assertEquals('app', $data['data']['type']);
  645. $this->assertEquals('http://source.example.com/images/quill.png', $data['data']['logo']);
  646. $this->assertEquals('Quill', $data['data']['name']);
  647. $this->assertEquals($url, $data['data']['url']);
  648. $this->assertEquals(['http://source.example.com/redirect1','http://source.example.com/redirect2'], $data['data']['redirect-uri']);
  649. $this->assertArrayNotHasKey('photo', $data['data']);
  650. }
  651. public function testHXApp() {
  652. $url = 'http://source.example.com/h-x-app';
  653. $response = $this->parse(['url' => $url]);
  654. $body = $response->getContent();
  655. $this->assertEquals(200, $response->getStatusCode());
  656. $data = json_decode($body);
  657. $this->assertEquals('app', $data->data->type);
  658. $this->assertEquals('http://source.example.com/images/quill.png', $data->data->logo);
  659. $this->assertEquals('Quill', $data->data->name);
  660. $this->assertEquals($url, $data->data->url);
  661. $this->assertObjectNotHasAttribute('photo', $data->data);
  662. }
  663. public function testDuplicateReplyURLValues() {
  664. $url = 'http://source.example.com/duplicate-in-reply-to-urls';
  665. $response = $this->parse(['url' => $url]);
  666. $body = $response->getContent();
  667. $this->assertEquals(200, $response->getStatusCode());
  668. $data = json_decode($body, true);
  669. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  670. $this->assertEquals(1, count($data['data']['in-reply-to']));
  671. }
  672. public function testDuplicateLikeOfURLValues() {
  673. $url = 'http://source.example.com/duplicate-like-of-urls';
  674. $response = $this->parse(['url' => $url]);
  675. $body = $response->getContent();
  676. $this->assertEquals(200, $response->getStatusCode());
  677. $data = json_decode($body, true);
  678. $this->assertEquals('http://example.com/100', $data['data']['like-of'][0]);
  679. $this->assertEquals(1, count($data['data']['like-of']));
  680. }
  681. public function testQuotationOf() {
  682. $url = 'http://source.example.com/quotation-of';
  683. $response = $this->parse(['url' => $url]);
  684. $body = $response->getContent();
  685. $this->assertEquals(200, $response->getStatusCode());
  686. $data = json_decode($body, true);
  687. $this->assertEquals('I’m so making this into a t-shirt', $data['data']['content']['text']);
  688. $this->assertEquals('https://twitter.com/gitlost/status/1015005409726357504', $data['data']['quotation-of']);
  689. $this->assertArrayHasKey('https://twitter.com/gitlost/status/1015005409726357504', $data['data']['refs']);
  690. $q = $data['data']['refs']['https://twitter.com/gitlost/status/1015005409726357504'];
  691. $this->assertEquals("Still can't git fer shit", $q['content']['text']);
  692. $this->assertEquals('2018-07-05T22:52:02+00:00', $q['published']);
  693. }
  694. public function testHTML5Markup() {
  695. $url = 'http://source.example.com/html5-tags';
  696. $response = $this->parse(['url' => $url]);
  697. $body = $response->getContent();
  698. $this->assertEquals(200, $response->getStatusCode());
  699. $data = json_decode($body, true);
  700. $this->assertEquals('Hello World', $data['data']['name']);
  701. $this->assertEquals('The content of the blog post', $data['data']['content']['text']);
  702. }
  703. }