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.

944 lines
42 KiB

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