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.

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