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.

1350 lines
58 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class ParseTest extends PHPUnit\Framework\TestCase
  5. {
  6. private $http;
  7. public function setUp(): void
  8. {
  9. $this->client = new Parse();
  10. $this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
  11. $this->client->mc = null;
  12. }
  13. private function parse($params)
  14. {
  15. $request = new Request($params);
  16. $response = new Response();
  17. return $this->client->parse($request, $response);
  18. }
  19. public function testMissingURL()
  20. {
  21. $response = $this->parse([]);
  22. $body = $response->getContent();
  23. $this->assertEquals(400, $response->getStatusCode());
  24. $data = json_decode($body);
  25. $this->assertObjectHasAttribute('error', $data);
  26. $this->assertEquals('missing_url', $data->error);
  27. }
  28. public function testInvalidURL()
  29. {
  30. $url = 'ftp://example.com/foo';
  31. $response = $this->parse(['url' => $url]);
  32. $body = $response->getContent();
  33. $this->assertEquals(400, $response->getStatusCode());
  34. $data = json_decode($body);
  35. $this->assertObjectHasAttribute('error', $data);
  36. $this->assertEquals('invalid_url', $data->error);
  37. }
  38. public function testTargetNotFound()
  39. {
  40. $url = 'http://source.example.com/basictest';
  41. $response = $this->parse(['url' => $url, 'target' => 'http://example.net']);
  42. $body = $response->getContent();
  43. $this->assertEquals(200, $response->getStatusCode());
  44. $data = json_decode($body);
  45. $this->assertObjectHasAttribute('error', $data);
  46. $this->assertEquals('no_link_found', $data->error);
  47. $this->assertEquals('200', $data->code);
  48. $this->assertEquals($url, $data->url);
  49. }
  50. public function testTargetFound()
  51. {
  52. $url = 'http://source.example.com/basictest';
  53. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com']);
  54. $body = $response->getContent();
  55. $this->assertEquals(200, $response->getStatusCode());
  56. $data = json_decode($body);
  57. $this->assertObjectNotHasAttribute('error', $data);
  58. }
  59. public function testTargetNotFoundInXML()
  60. {
  61. $url = 'http://feed.example.com/atom';
  62. $response = $this->parse(['url' => $url, 'target' => 'http://example.net']);
  63. $body = $response->getContent();
  64. $this->assertEquals(200, $response->getStatusCode());
  65. $data = json_decode($body);
  66. $this->assertObjectHasAttribute('error', $data);
  67. $this->assertEquals('no_link_found', $data->error);
  68. $this->assertEquals('200', $data->code);
  69. $this->assertEquals($url, $data->url);
  70. }
  71. public function testHTMLContent()
  72. {
  73. $url = 'http://source.example.com/html-content';
  74. $response = $this->parse(['url' => $url]);
  75. $body = $response->getContent();
  76. $this->assertEquals(200, $response->getStatusCode());
  77. $data = json_decode($body);
  78. $this->assertObjectNotHasAttribute('name', $data->data);
  79. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  80. $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);
  81. }
  82. public function testContentFromJSONOnlyPlaintext()
  83. {
  84. $mf2JSON = json_encode(
  85. [
  86. 'items' => [[
  87. 'type' => ['h-entry'],
  88. 'properties' => [
  89. 'content' => [
  90. 'plaintext'
  91. ]
  92. ]
  93. ]]
  94. ]
  95. );
  96. $xray = new \p3k\XRay();
  97. $parsed = $xray->process(false, $mf2JSON);
  98. $this->assertEquals('mf2+json', $parsed['source-format']);
  99. $item = $parsed['data'];
  100. $this->assertEquals('entry', $item['type']);
  101. $this->assertEquals('note', $item['post-type']);
  102. $this->assertEquals('plaintext', $item['content']['text']);
  103. $this->assertArrayNotHasKey('html', $item['content']);
  104. }
  105. public function testHTMLContentFromJSONNoPlaintext()
  106. {
  107. $mf2JSON = json_encode(
  108. [
  109. 'items' => [[
  110. 'type' => ['h-entry'],
  111. 'properties' => [
  112. 'content' => [[
  113. 'html' => '<b>bold</b> <i>italic</i> text'
  114. ]]
  115. ]
  116. ]]
  117. ]
  118. );
  119. $xray = new \p3k\XRay();
  120. $parsed = $xray->process(false, $mf2JSON);
  121. $this->assertEquals('mf2+json', $parsed['source-format']);
  122. $item = $parsed['data'];
  123. $this->assertEquals('entry', $item['type']);
  124. $this->assertEquals('note', $item['post-type']);
  125. $this->assertEquals('bold italic text', $item['content']['text']);
  126. $this->assertEquals('<b>bold</b> <i>italic</i> text', $item['content']['html']);
  127. }
  128. public function testHTMLContentFromJSONEmptyTags()
  129. {
  130. $mf2JSON = json_encode(
  131. [
  132. 'items' => [[
  133. 'type' => ['h-entry'],
  134. 'properties' => [
  135. 'content' => [[
  136. 'html' => '<b></b><i></i>'
  137. ]]
  138. ]
  139. ]]
  140. ]
  141. );
  142. $xray = new \p3k\XRay();
  143. $parsed = $xray->process(false, $mf2JSON);
  144. $this->assertEquals('mf2+json', $parsed['source-format']);
  145. $item = $parsed['data'];
  146. $this->assertEquals('entry', $item['type']);
  147. $this->assertEquals('note', $item['post-type']);
  148. $this->assertArrayNotHasKey('content', $item);
  149. }
  150. public function testHTMLContentFromJSONContentMismatch()
  151. {
  152. $mf2JSON = json_encode(
  153. [
  154. 'items' => [[
  155. 'type' => ['h-entry'],
  156. 'properties' => [
  157. 'content' => [[
  158. 'value' => 'foo',
  159. 'html' => '<b>bar</b>'
  160. ]]
  161. ]
  162. ]]
  163. ]
  164. );
  165. $xray = new \p3k\XRay();
  166. $parsed = $xray->process(false, $mf2JSON);
  167. $this->assertEquals('mf2+json', $parsed['source-format']);
  168. $item = $parsed['data'];
  169. $this->assertEquals('entry', $item['type']);
  170. $this->assertEquals('note', $item['post-type']);
  171. $this->assertEquals('bar', $item['content']['text']);
  172. $this->assertEquals('<b>bar</b>', $item['content']['html']);
  173. }
  174. public function testHTMLContentFromJSONNoHTML()
  175. {
  176. $mf2JSON = json_encode(
  177. [
  178. 'items' => [[
  179. 'type' => ['h-entry'],
  180. 'properties' => [
  181. 'content' => [[
  182. 'value' => 'foo',
  183. ]]
  184. ]
  185. ]]
  186. ]
  187. );
  188. $xray = new \p3k\XRay();
  189. $parsed = $xray->process(false, $mf2JSON);
  190. $this->assertEquals('mf2+json', $parsed['source-format']);
  191. $item = $parsed['data'];
  192. $this->assertEquals('entry', $item['type']);
  193. $this->assertEquals('note', $item['post-type']);
  194. $this->assertArrayNotHasKey('content', $item);
  195. }
  196. public function testFindTargetLinkIsImage()
  197. {
  198. $url = 'http://source.example.com/link-is-img';
  199. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/photo.jpg']);
  200. $body = $response->getContent();
  201. $this->assertEquals(200, $response->getStatusCode());
  202. $data = json_decode($body);
  203. $this->assertEquals('mf2+html', $data->{'source-format'});
  204. $this->assertEquals('photo', $data->data->{'post-type'});
  205. $this->assertObjectNotHasAttribute('name', $data->data);
  206. $this->assertEquals('This page has an img tag with the target URL.', $data->data->content->text);
  207. }
  208. public function testFindTargetLinkIsVideo()
  209. {
  210. $url = 'http://source.example.com/link-is-video';
  211. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/movie.mp4']);
  212. $body = $response->getContent();
  213. $this->assertEquals(200, $response->getStatusCode());
  214. $data = json_decode($body);
  215. $this->assertEquals('mf2+html', $data->{'source-format'});
  216. $this->assertEquals('video', $data->data->{'post-type'});
  217. $this->assertObjectNotHasAttribute('name', $data->data);
  218. $this->assertEquals('This page has a video tag with the target URL.', $data->data->content->text);
  219. }
  220. public function testFindTargetLinkIsAudio()
  221. {
  222. $url = 'http://source.example.com/link-is-audio';
  223. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/media.mp3']);
  224. $body = $response->getContent();
  225. $this->assertEquals(200, $response->getStatusCode());
  226. $data = json_decode($body);
  227. $this->assertEquals('mf2+html', $data->{'source-format'});
  228. $this->assertEquals('audio', $data->data->{'post-type'});
  229. $this->assertObjectNotHasAttribute('name', $data->data);
  230. $this->assertEquals('This page has an audio tag with the target URL.', $data->data->content->text);
  231. }
  232. public function testFindTargetLinkInFeed()
  233. {
  234. $url = 'http://feed.example.com/jsonfeed';
  235. $response = $this->parse(['url' => $url, 'target' => 'http://www.manton.org/2017/11/5993.html']);
  236. $body = $response->getContent();
  237. $this->assertEquals(200, $response->getStatusCode());
  238. $data = json_decode($body);
  239. $this->assertObjectNotHasAttribute('error', $data);
  240. }
  241. public function testFindTargetLinkInHTMLInFeed()
  242. {
  243. $url = 'http://feed.example.com/jsonfeed';
  244. $response = $this->parse(['url' => $url, 'target' => 'http://www.manton.org/2016/11/todays-social-networks-are-broken.html']);
  245. $body = $response->getContent();
  246. $this->assertEquals(200, $response->getStatusCode());
  247. $data = json_decode($body);
  248. $this->assertObjectNotHasAttribute('error', $data);
  249. }
  250. public function testNotFindTargetLinkInHTMLInFeed()
  251. {
  252. $url = 'http://feed.example.com/jsonfeed';
  253. $response = $this->parse(['url' => $url, 'target' => 'http://example.com/']);
  254. $body = $response->getContent();
  255. $this->assertEquals(200, $response->getStatusCode());
  256. $data = json_decode($body);
  257. $this->assertObjectHasAttribute('error', $data);
  258. $this->assertEquals('no_link_found', $data->error);
  259. }
  260. public function testFindRelativeTargetLink()
  261. {
  262. $url = 'http://source.example.com/multiple-urls';
  263. $response = $this->parse(['url' => $url, 'target' => 'http://source.example.com/photo.jpg']);
  264. $body = $response->getContent();
  265. $this->assertEquals(200, $response->getStatusCode());
  266. $data = json_decode($body);
  267. $this->assertObjectNotHasAttribute('error', $data);
  268. }
  269. public function testTextContent()
  270. {
  271. $url = 'http://source.example.com/text-content';
  272. $response = $this->parse(['url' => $url]);
  273. $body = $response->getContent();
  274. $this->assertEquals(200, $response->getStatusCode());
  275. $data = json_decode($body);
  276. $this->assertEquals('mf2+html', $data->{'source-format'});
  277. $this->assertObjectNotHasAttribute('name', $data->data);
  278. $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);
  279. }
  280. public function testNewlinesInTextContent() {
  281. $url = 'http://source.example.com/text-content-with-p-tags';
  282. $response = $this->parse(['url' => $url]);
  283. $body = $response->getContent();
  284. $this->assertEquals(200, $response->getStatusCode());
  285. $data = json_decode($body);
  286. $this->assertEquals('mf2+html', $data->{'source-format'});
  287. $this->assertObjectNotHasAttribute('name', $data->data);
  288. $this->assertEquals("Hello\nWorld", $data->data->content->text);
  289. }
  290. public function testArticleWithFeaturedImage()
  291. {
  292. $url = 'http://source.example.com/article-with-featured-image';
  293. $response = $this->parse(['url' => $url]);
  294. $body = $response->getContent();
  295. $this->assertEquals(200, $response->getStatusCode());
  296. $data = json_decode($body);
  297. $this->assertEquals('mf2+html', $data->{'source-format'});
  298. $this->assertEquals('article', $data->data->{'post-type'});
  299. $this->assertEquals('Post Title', $data->data->name);
  300. $this->assertEquals('This is a blog post.', $data->data->content->text);
  301. $this->assertEquals('http://source.example.com/featured.jpg', $data->data->featured);
  302. }
  303. public function testContentWithPrefixedName()
  304. {
  305. $url = 'http://source.example.com/content-with-prefixed-name';
  306. $response = $this->parse(['url' => $url]);
  307. $body = $response->getContent();
  308. $this->assertEquals(200, $response->getStatusCode());
  309. $data = json_decode($body);
  310. $this->assertEquals('mf2+html', $data->{'source-format'});
  311. $this->assertObjectNotHasAttribute('name', $data->data);
  312. $this->assertEquals('note', $data->data->{'post-type'});
  313. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  314. $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);
  315. }
  316. public function testContentWithDistinctName()
  317. {
  318. $url = 'http://source.example.com/content-with-distinct-name';
  319. $response = $this->parse(['url' => $url]);
  320. $body = $response->getContent();
  321. $this->assertEquals(200, $response->getStatusCode());
  322. $data = json_decode($body);
  323. $this->assertEquals('mf2+html', $data->{'source-format'});
  324. $this->assertEquals('Hello World', $data->data->name);
  325. $this->assertEquals('article', $data->data->{'post-type'});
  326. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  327. $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);
  328. }
  329. public function testNameWithNoContent()
  330. {
  331. $url = 'http://source.example.com/name-no-content';
  332. $response = $this->parse(['url' => $url]);
  333. $body = $response->getContent();
  334. $this->assertEquals(200, $response->getStatusCode());
  335. $data = json_decode($body);
  336. $this->assertEquals('mf2+html', $data->{'source-format'});
  337. $this->assertEquals('Hello World', $data->data->name);
  338. $this->assertEquals('article', $data->data->{'post-type'});
  339. $this->assertObjectNotHasAttribute('content', $data->data);
  340. }
  341. public function testEntryWithDuplicateCategories()
  342. {
  343. $url = 'http://source.example.com/h-entry-duplicate-categories';
  344. $response = $this->parse(['url' => $url]);
  345. $body = $response->getContent();
  346. $this->assertEquals(200, $response->getStatusCode());
  347. $data = json_decode($body);
  348. $this->assertEquals('mf2+html', $data->{'source-format'});
  349. $this->assertEquals(['indieweb'], $data->data->category);
  350. }
  351. public function testEntryStripHashtagWithDuplicateCategories()
  352. {
  353. $url = 'http://source.example.com/h-entry-strip-hashtag-from-categories';
  354. $response = $this->parse(['url' => $url]);
  355. $body = $response->getContent();
  356. $this->assertEquals(200, $response->getStatusCode());
  357. $data = json_decode($body);
  358. $this->assertEquals('mf2+html', $data->{'source-format'});
  359. $this->assertContains('indieweb', $data->data->category);
  360. $this->assertContains('xray', $data->data->category);
  361. $this->assertEquals(2, count($data->data->category));
  362. }
  363. public function testNoHEntryMarkup()
  364. {
  365. $url = 'http://source.example.com/no-h-entry';
  366. $response = $this->parse(['url' => $url]);
  367. $body = $response->getContent();
  368. $this->assertEquals(200, $response->getStatusCode());
  369. $data = json_decode($body);
  370. $this->assertEquals('unknown', $data->data->type);
  371. $this->assertObjectNotHasAttribute('html', $data);
  372. }
  373. public function testFindTargetInNoParsedResult()
  374. {
  375. $url = 'http://source.example.com/no-h-entry';
  376. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com']);
  377. $body = $response->getContent();
  378. $this->assertEquals(200, $response->getStatusCode());
  379. $data = json_decode($body);
  380. $this->assertObjectNotHasAttribute('error', $data);
  381. $this->assertEquals('unknown', $data->data->type);
  382. }
  383. public function testReplyIsURL()
  384. {
  385. $url = 'http://source.example.com/reply-is-url';
  386. $response = $this->parse(['url' => $url]);
  387. $body = $response->getContent();
  388. $this->assertEquals(200, $response->getStatusCode());
  389. $data = json_decode($body, true);
  390. $this->assertEquals('mf2+html', $data['source-format']);
  391. $this->assertEquals('entry', $data['data']['type']);
  392. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  393. }
  394. public function testReplyIsHCite()
  395. {
  396. $url = 'http://source.example.com/reply-is-h-cite';
  397. $response = $this->parse(['url' => $url]);
  398. $body = $response->getContent();
  399. $this->assertEquals(200, $response->getStatusCode());
  400. $data = json_decode($body, true);
  401. $this->assertEquals('mf2+html', $data['source-format']);
  402. $this->assertEquals('entry', $data['data']['type']);
  403. $this->assertEquals('reply', $data['data']['post-type']);
  404. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  405. $this->assertArrayHasKey('http://example.com/100', $data['data']['refs']);
  406. $this->assertEquals('Example Post', $data['data']['refs']['http://example.com/100']['name']);
  407. $this->assertEquals('http://example.com/100', $data['data']['refs']['http://example.com/100']['url']);
  408. }
  409. public function testPersonTagIsURL()
  410. {
  411. $url = 'http://source.example.com/person-tag-is-url';
  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. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  419. }
  420. public function testPersonTagIsHCard()
  421. {
  422. $url = 'http://source.example.com/person-tag-is-h-card';
  423. $response = $this->parse(['url' => $url]);
  424. $body = $response->getContent();
  425. $this->assertEquals(200, $response->getStatusCode());
  426. $data = json_decode($body, true);
  427. $this->assertEquals('mf2+html', $data['source-format']);
  428. $this->assertEquals('entry', $data['data']['type']);
  429. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  430. $this->assertArrayHasKey('http://alice.example.com/', $data['data']['refs']);
  431. $this->assertEquals('card', $data['data']['refs']['http://alice.example.com/']['type']);
  432. $this->assertEquals('http://alice.example.com/', $data['data']['refs']['http://alice.example.com/']['url']);
  433. $this->assertEquals('Alice', $data['data']['refs']['http://alice.example.com/']['name']);
  434. }
  435. public function testSyndicationIsURL()
  436. {
  437. $url = 'http://source.example.com/has-syndication';
  438. $response = $this->parse(['url' => $url]);
  439. $body = $response->getContent();
  440. $this->assertEquals(200, $response->getStatusCode());
  441. $data = json_decode($body, true);
  442. $this->assertEquals('mf2+html', $data['source-format']);
  443. $this->assertEquals('entry', $data['data']['type']);
  444. $this->assertEquals('http://syndicated.example/', $data['data']['syndication'][0]);
  445. }
  446. public function testHEntryNoContent()
  447. {
  448. $url = 'http://source.example.com/h-entry-no-content';
  449. $response = $this->parse(['url' => $url]);
  450. $body = $response->getContent();
  451. $this->assertEquals(200, $response->getStatusCode());
  452. $data = json_decode($body);
  453. $this->assertEquals('mf2+html', $data->{'source-format'});
  454. $this->assertObjectNotHasAttribute('content', $data->data);
  455. $this->assertEquals('This is a Post', $data->data->name);
  456. }
  457. public function testHEntryIsNotFirstObject()
  458. {
  459. $url = 'http://source.example.com/h-entry-is-not-first';
  460. $response = $this->parse(['url' => $url]);
  461. $body = $response->getContent();
  462. $this->assertEquals(200, $response->getStatusCode());
  463. $data = json_decode($body, true);
  464. $this->assertEquals('mf2+html', $data['source-format']);
  465. $this->assertEquals('entry', $data['data']['type']);
  466. $this->assertEquals('Hello World', $data['data']['content']['text']);
  467. }
  468. public function testHEntryRSVP()
  469. {
  470. $url = 'http://source.example.com/h-entry-rsvp';
  471. $response = $this->parse(['url' => $url]);
  472. $body = $response->getContent();
  473. $this->assertEquals(200, $response->getStatusCode());
  474. $data = json_decode($body, true);
  475. $this->assertEquals('mf2+html', $data['source-format']);
  476. $this->assertEquals('entry', $data['data']['type']);
  477. $this->assertEquals('rsvp', $data['data']['post-type']);
  478. $this->assertEquals('I\'ll be there!', $data['data']['content']['text']);
  479. $this->assertEquals('yes', $data['data']['rsvp']);
  480. }
  481. public function testMultipleHEntryOnPermalink()
  482. {
  483. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  484. $response = $this->parse(['url' => $url]);
  485. $body = $response->getContent();
  486. $this->assertEquals(200, $response->getStatusCode());
  487. $data = json_decode($body, true);
  488. $this->assertEquals('mf2+html', $data['source-format']);
  489. $this->assertEquals('entry', $data['data']['type']);
  490. $this->assertEquals('Primary Post', $data['data']['name']);
  491. }
  492. public function testHEntryWithHCardBeforeIt()
  493. {
  494. $url = 'http://source.example.com/h-entry-with-h-card-before-it';
  495. $response = $this->parse(['url' => $url]);
  496. $body = $response->getContent();
  497. $this->assertEquals(200, $response->getStatusCode());
  498. $data = json_decode($body, true);
  499. $this->assertEquals('mf2+html', $data['source-format']);
  500. $this->assertEquals('entry', $data['data']['type']);
  501. $this->assertEquals('Hello World', $data['data']['content']['text']);
  502. }
  503. public function testHEntryWithHCardSibling()
  504. {
  505. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  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('mf2+html', $data['source-format']);
  511. $this->assertEquals('entry', $data['data']['type']);
  512. $this->assertEquals('Hello World', $data['data']['content']['text']);
  513. }
  514. public function testHEntryWithTwoHCardsBeforeIt()
  515. {
  516. $url = 'http://source.example.com/h-entry-with-two-h-cards-before-it';
  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('mf2+html', $data['source-format']);
  522. $this->assertEquals('entry', $data['data']['type']);
  523. $this->assertEquals('Hello World', $data['data']['content']['text']);
  524. }
  525. public function testHEntryRedirectWithHCardSibling()
  526. {
  527. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  528. $response = $this->parse(['url' => $url]);
  529. $body = $response->getContent();
  530. $this->assertEquals(200, $response->getStatusCode());
  531. $data = json_decode($body, true);
  532. $this->assertEquals('mf2+html', $data['source-format']);
  533. $this->assertEquals('entry', $data['data']['type']);
  534. $this->assertEquals('Hello World', $data['data']['content']['text']);
  535. }
  536. public function testSingleHEntryHasNoPermalink()
  537. {
  538. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  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('mf2+html', $data['source-format']);
  544. $this->assertEquals('entry', $data['data']['type']);
  545. $this->assertEquals('Hello World', $data['data']['content']['text']);
  546. }
  547. public function testBridgyExampleWithNoMatchingURL()
  548. {
  549. $url = 'http://source.example.com/bridgy-example';
  550. $response = $this->parse(['url' => $url]);
  551. $body = $response->getContent();
  552. $this->assertEquals(200, $response->getStatusCode());
  553. $data = json_decode($body, true);
  554. $this->assertEquals('mf2+html', $data['source-format']);
  555. $this->assertEquals('entry', $data['data']['type']);
  556. }
  557. public function testEventWithHTMLDescription()
  558. {
  559. $url = 'http://source.example.com/h-event';
  560. $response = $this->parse(['url' => $url]);
  561. $body = $response->getContent();
  562. $this->assertEquals(200, $response->getStatusCode());
  563. $data = json_decode($body, true);
  564. $this->assertEquals('mf2+html', $data['source-format']);
  565. $this->assertEquals('event', $data['data']['type']);
  566. $this->assertEquals('event', $data['data']['post-type']);
  567. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  568. $this->assertEquals($url, $data['data']['url']);
  569. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  570. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  571. $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']['content']['text']);
  572. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['content']['text']);
  573. $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']['content']['html']);
  574. $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']['content']['html']);
  575. }
  576. public function testEventWithTextDescription()
  577. {
  578. $url = 'http://source.example.com/h-event-text-description';
  579. $response = $this->parse(['url' => $url]);
  580. $body = $response->getContent();
  581. $this->assertEquals(200, $response->getStatusCode());
  582. $data = json_decode($body, true);
  583. $this->assertEquals('mf2+html', $data['source-format']);
  584. $this->assertEquals('event', $data['data']['type']);
  585. $this->assertEquals('event', $data['data']['post-type']);
  586. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  587. $this->assertEquals($url, $data['data']['url']);
  588. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  589. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  590. $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']['content']['text']);
  591. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['content']['text']);
  592. $this->assertArrayNotHasKey('html', $data['data']['content']);
  593. }
  594. public function testEventWithTextContent()
  595. {
  596. $url = 'http://source.example.com/h-event-text-content';
  597. $response = $this->parse(['url' => $url]);
  598. $body = $response->getContent();
  599. $this->assertEquals(200, $response->getStatusCode());
  600. $data = json_decode($body, true);
  601. $this->assertEquals('mf2+html', $data['source-format']);
  602. $this->assertEquals('event', $data['data']['type']);
  603. $this->assertEquals('event', $data['data']['post-type']);
  604. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  605. $this->assertEquals($url, $data['data']['url']);
  606. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  607. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  608. $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']['content']['text']);
  609. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['content']['text']);
  610. $this->assertArrayNotHasKey('html', $data['data']['content']);
  611. $this->assertEquals('card', $data['data']['author']['type']);
  612. $this->assertEquals('Event Author', $data['data']['author']['name']);
  613. $this->assertEquals('http://source.example.com/', $data['data']['author']['url']);
  614. }
  615. public function testEventWithHCardLocation()
  616. {
  617. $url = 'http://source.example.com/h-event-with-h-card-location';
  618. $response = $this->parse(['url' => $url]);
  619. $body = $response->getContent();
  620. $this->assertEquals(200, $response->getStatusCode());
  621. $data = json_decode($body, true);
  622. $this->assertEquals('mf2+html', $data['source-format']);
  623. $this->assertEquals('event', $data['data']['type']);
  624. $this->assertEquals('event', $data['data']['post-type']);
  625. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  626. $this->assertEquals($url, $data['data']['url']);
  627. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  628. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  629. $this->assertEquals('card', $data['data']['location']['type']);
  630. $this->assertEquals('http://source.example.com/venue', $data['data']['location']['url']);
  631. $this->assertEquals('Venue', $data['data']['location']['name']);
  632. $this->assertEquals('45.5', $data['data']['location']['latitude']);
  633. $this->assertEquals('-122.6', $data['data']['location']['longitude']);
  634. $this->assertEquals('1234 Main St', $data['data']['location']['street-address']);
  635. $this->assertEquals('Portland', $data['data']['location']['locality']);
  636. $this->assertEquals('Oregon', $data['data']['location']['region']);
  637. $this->assertEquals('USA', $data['data']['location']['country-name']);
  638. }
  639. public function testEventWithFeaturedImage()
  640. {
  641. $url = 'http://source.example.com/h-event-featured';
  642. $response = $this->parse(['url' => $url]);
  643. $body = $response->getContent();
  644. $this->assertEquals(200, $response->getStatusCode());
  645. $data = json_decode($body, true);
  646. $this->assertEquals('mf2+html', $data['source-format']);
  647. $this->assertEquals('event', $data['data']['type']);
  648. $this->assertEquals('http://source.example.com/featured.jpg', $data['data']['featured']);
  649. }
  650. public function testMf2ReviewOfProduct()
  651. {
  652. $url = 'http://source.example.com/h-review-of-product';
  653. $response = $this->parse(['url' => $url]);
  654. $body = $response->getContent();
  655. $this->assertEquals(200, $response->getStatusCode());
  656. $data = json_decode($body, true);
  657. $this->assertEquals('mf2+html', $data['source-format']);
  658. $this->assertEquals('review', $data['data']['type']);
  659. $this->assertEquals('review', $data['data']['post-type']);
  660. $this->assertEquals('Review', $data['data']['name']);
  661. $this->assertEquals('Not great', $data['data']['summary']);
  662. $this->assertEquals('3', $data['data']['rating']);
  663. $this->assertEquals('5', $data['data']['best']);
  664. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  665. $this->assertContains('red', $data['data']['category']);
  666. $this->assertContains('blue', $data['data']['category']);
  667. $this->assertContains('http://product.example.com/', $data['data']['item']);
  668. $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
  669. $this->assertEquals('product', $data['data']['refs']['http://product.example.com/']['type']);
  670. $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
  671. $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
  672. }
  673. public function testMf2ReviewOfHCard()
  674. {
  675. $url = 'http://source.example.com/h-review-of-h-card';
  676. $response = $this->parse(['url' => $url]);
  677. $body = $response->getContent();
  678. $this->assertEquals(200, $response->getStatusCode());
  679. $data = json_decode($body, true);
  680. $this->assertEquals('mf2+html', $data['source-format']);
  681. $this->assertEquals('review', $data['data']['type']);
  682. $this->assertEquals('review', $data['data']['post-type']);
  683. $this->assertEquals('Review', $data['data']['name']);
  684. $this->assertEquals('Not great', $data['data']['summary']);
  685. $this->assertEquals('3', $data['data']['rating']);
  686. $this->assertEquals('5', $data['data']['best']);
  687. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  688. $this->assertContains('http://business.example.com/', $data['data']['item']);
  689. $this->assertArrayHasKey('http://business.example.com/', $data['data']['refs']);
  690. $this->assertEquals('card', $data['data']['refs']['http://business.example.com/']['type']);
  691. $this->assertEquals('The Reviewed Business', $data['data']['refs']['http://business.example.com/']['name']);
  692. $this->assertEquals('http://business.example.com/', $data['data']['refs']['http://business.example.com/']['url']);
  693. }
  694. public function testMf1Review()
  695. {
  696. $url = 'http://source.example.com/hReview';
  697. $response = $this->parse(['url' => $url]);
  698. $body = $response->getContent();
  699. $this->assertEquals(200, $response->getStatusCode());
  700. $data = json_decode($body, true);
  701. $this->assertEquals('mf2+html', $data['source-format']);
  702. $this->assertEquals('review', $data['data']['type']);
  703. $this->assertEquals('review', $data['data']['post-type']);
  704. $this->assertEquals('Not great', $data['data']['name']);
  705. $this->assertEquals('3', $data['data']['rating']);
  706. $this->assertEquals('5', $data['data']['best']);
  707. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  708. $this->assertContains('http://product.example.com/', $data['data']['item']);
  709. $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
  710. $this->assertEquals('item', $data['data']['refs']['http://product.example.com/']['type']);
  711. $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
  712. $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
  713. }
  714. public function testMf2Recipe()
  715. {
  716. $url = 'http://source.example.com/h-recipe';
  717. $response = $this->parse(['url' => $url]);
  718. $body = $response->getContent();
  719. $this->assertEquals(200, $response->getStatusCode());
  720. $data = json_decode($body, true);
  721. $this->assertEquals('mf2+html', $data['source-format']);
  722. $this->assertEquals('recipe', $data['data']['type']);
  723. $this->assertEquals('recipe', $data['data']['post-type']);
  724. $this->assertEquals('Cookie Recipe', $data['data']['name']);
  725. $this->assertEquals('12 Cookies', $data['data']['yield']);
  726. $this->assertEquals('PT30M', $data['data']['duration']);
  727. $this->assertEquals('The best chocolate chip cookie recipe', $data['data']['summary']);
  728. $this->assertContains('3 cups flour', $data['data']['ingredient']);
  729. $this->assertContains('chocolate chips', $data['data']['ingredient']);
  730. }
  731. public function testEntryIsAnInvitee()
  732. {
  733. $url = 'http://source.example.com/bridgy-invitee';
  734. $response = $this->parse(['url' => $url]);
  735. $body = $response->getContent();
  736. $this->assertEquals(200, $response->getStatusCode());
  737. $data = json_decode($body, true);
  738. $this->assertEquals('mf2+html', $data['source-format']);
  739. $this->assertEquals('entry', $data['data']['type']);
  740. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  741. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  742. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['data']['refs']);
  743. $this->assertEquals('Tantek Çelik', $data['data']['refs']['https://www.facebook.com/tantek.celik']['name']);
  744. }
  745. public function testEntryAtFragmentID()
  746. {
  747. $url = 'http://source.example.com/fragment-id#comment-1000';
  748. $response = $this->parse(['url' => $url]);
  749. $body = $response->getContent();
  750. $this->assertEquals(200, $response->getStatusCode());
  751. $data = json_decode($body, true);
  752. $this->assertEquals('mf2+html', $data['source-format']);
  753. $this->assertEquals('entry', $data['data']['type']);
  754. $this->assertEquals('note', $data['data']['post-type']);
  755. $this->assertEquals('Comment text', $data['data']['content']['text']);
  756. $this->assertEquals('http://source.example.com/fragment-id#comment-1000', $data['data']['url']);
  757. $this->assertTrue($data['info']['found_fragment']);
  758. }
  759. public function testEntryAtNonExistentFragmentID()
  760. {
  761. $url = 'http://source.example.com/fragment-id#comment-404';
  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->assertEquals('entry', $data['data']['type']);
  768. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  769. $this->assertFalse($data['info']['found_fragment']);
  770. }
  771. public function testCheckin()
  772. {
  773. $url = 'http://source.example.com/checkin';
  774. $response = $this->parse(['url' => $url]);
  775. $body = $response->getContent();
  776. $this->assertEquals(200, $response->getStatusCode());
  777. $data = json_decode($body, true);
  778. $this->assertEquals('mf2+html', $data['source-format']);
  779. $this->assertEquals('entry', $data['data']['type']);
  780. $venue = $data['data']['checkin'];
  781. $this->assertEquals('checkin', $data['data']['post-type']);
  782. $this->assertEquals('https://foursquare.com/v/57104d2e498ece022e169dca', $venue['url']);
  783. $this->assertEquals('DreamHost', $venue['name']);
  784. $this->assertEquals('45.518716', $venue['latitude']);
  785. $this->assertEquals('Homebrew Website Club!', $data['data']['content']['text']);
  786. $this->assertEquals('https://aaronparecki.com/2017/06/07/12/photo.jpg', $data['data']['photo'][0]);
  787. $this->assertEquals('2017-06-07T17:14:40-07:00', $data['data']['published']);
  788. $this->assertArrayNotHasKey('name', $data['data']);
  789. }
  790. public function testCheckinURLOnly()
  791. {
  792. $url = 'http://source.example.com/checkin-url';
  793. $response = $this->parse(['url' => $url]);
  794. $body = $response->getContent();
  795. $this->assertEquals(200, $response->getStatusCode());
  796. $data = json_decode($body, true);
  797. $this->assertEquals('mf2+html', $data['source-format']);
  798. $this->assertEquals('entry', $data['data']['type']);
  799. $this->assertEquals('checkin', $data['data']['post-type']);
  800. $venue = $data['data']['checkin'];
  801. $this->assertEquals('https://foursquare.com/v/57104d2e498ece022e169dca', $venue['url']);
  802. $this->assertEquals('Homebrew Website Club!', $data['data']['content']['text']);
  803. $this->assertEquals('https://aaronparecki.com/2017/06/07/12/photo.jpg', $data['data']['photo'][0]);
  804. $this->assertEquals('2017-06-07T17:14:40-07:00', $data['data']['published']);
  805. $this->assertArrayNotHasKey('name', $data['data']);
  806. }
  807. public function testXKCD()
  808. {
  809. $url = 'http://xkcd.com/1810/';
  810. $response = $this->parse(['url' => $url]);
  811. $body = $response->getContent();
  812. $this->assertEquals(200, $response->getStatusCode());
  813. $data = json_decode($body, true);
  814. $this->assertEquals(200, $data['code']);
  815. $this->assertEquals('xkcd', $data['source-format']);
  816. $this->assertEquals('entry', $data['data']['type']);
  817. $this->assertEquals('photo', $data['data']['post-type']);
  818. $this->assertEquals('http://xkcd.com/1810/', $data['data']['url']);
  819. $this->assertEquals('Chat Systems', $data['data']['name']);
  820. $this->assertContains('http://imgs.xkcd.com/comics/chat_systems_2x.png', $data['data']['photo']);
  821. }
  822. public function testEntryHasMultipleURLs()
  823. {
  824. $url = 'http://source.example.com/multiple-urls';
  825. $response = $this->parse(['url' => $url]);
  826. $body = $response->getContent();
  827. $this->assertEquals(200, $response->getStatusCode());
  828. $data = json_decode($body, true);
  829. // Should prioritize the URL on the same domain
  830. $this->assertEquals($url, $data['data']['url']);
  831. }
  832. public function testEntryHasMultipleURLsOffDomain()
  833. {
  834. $url = 'http://source.example.com/multiple-urls-off-domain';
  835. $response = $this->parse(['url' => $url]);
  836. $body = $response->getContent();
  837. $this->assertEquals(200, $response->getStatusCode());
  838. $data = json_decode($body, true);
  839. // Neither URL is on the same domain, so should use the first
  840. $this->assertEquals('http://one.example.com/test', $data['data']['url']);
  841. }
  842. public function testInputIsJSON()
  843. {
  844. $url = 'http://example.com/entry';
  845. $mf2json = ['items' => [
  846. [
  847. 'type' => ['h-entry'],
  848. 'properties' => [
  849. 'content' => [['html' => 'Hello World']]
  850. ]
  851. ]
  852. ]];
  853. $response = $this->parse(
  854. [
  855. 'body' => $mf2json,
  856. 'url' => $url,
  857. ]
  858. );
  859. $body = $response->getContent();
  860. $data = json_decode($body, true);
  861. $this->assertEquals('mf2+json', $data['source-format']);
  862. $this->assertEquals('Hello World', $data['data']['content']['text']);
  863. }
  864. public function testInputIsParsedMf2()
  865. {
  866. $html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
  867. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  868. $url = 'http://example.com/entry';
  869. $response = $this->parse(
  870. [
  871. 'url' => $url,
  872. 'body' => json_encode($mf2)
  873. ]
  874. );
  875. $body = $response->getContent();
  876. $this->assertEquals(200, $response->getStatusCode());
  877. $data = json_decode($body, true);
  878. $this->assertEquals('mf2+json', $data['source-format']);
  879. $this->assertEquals('Hello World', $data['data']['content']['text']);
  880. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  881. }
  882. public function testInputIsParsedMf2WithHTML()
  883. {
  884. $html = '<div class="h-entry"><p class="e-content p-name"><b>Hello</b> <i>World</i></p><img src="/photo.jpg"></p></div>';
  885. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  886. $url = 'http://example.com/entry';
  887. $response = $this->parse(
  888. [
  889. 'url' => $url,
  890. 'body' => json_encode($mf2)
  891. ]
  892. );
  893. $body = $response->getContent();
  894. $this->assertEquals(200, $response->getStatusCode());
  895. $data = json_decode($body, true);
  896. $this->assertEquals('mf2+json', $data['source-format']);
  897. $this->assertEquals('Hello World', $data['data']['content']['text']);
  898. $this->assertEquals('<b>Hello</b> <i>World</i>', $data['data']['content']['html']);
  899. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  900. }
  901. public function testHApp()
  902. {
  903. $url = 'http://source.example.com/h-app';
  904. $response = $this->parse(['url' => $url]);
  905. $body = $response->getContent();
  906. $this->assertEquals(200, $response->getStatusCode());
  907. $data = json_decode($body, true);
  908. $this->assertEquals('mf2+html', $data['source-format']);
  909. $this->assertEquals('app', $data['data']['type']);
  910. $this->assertEquals('http://source.example.com/images/quill.png', $data['data']['logo']);
  911. $this->assertEquals('Quill', $data['data']['name']);
  912. $this->assertEquals($url, $data['data']['url']);
  913. $this->assertEquals(['http://source.example.com/redirect1','http://source.example.com/redirect2'], $data['data']['redirect-uri']);
  914. $this->assertArrayNotHasKey('photo', $data['data']);
  915. }
  916. public function testHXApp()
  917. {
  918. $url = 'http://source.example.com/h-x-app';
  919. $response = $this->parse(['url' => $url]);
  920. $body = $response->getContent();
  921. $this->assertEquals(200, $response->getStatusCode());
  922. $data = json_decode($body);
  923. $this->assertEquals('mf2+html', $data->{'source-format'});
  924. $this->assertEquals('app', $data->data->type);
  925. $this->assertEquals('http://source.example.com/images/quill.png', $data->data->logo);
  926. $this->assertEquals('Quill', $data->data->name);
  927. $this->assertEquals($url, $data->data->url);
  928. $this->assertObjectNotHasAttribute('photo', $data->data);
  929. }
  930. public function testDuplicateReplyURLValues()
  931. {
  932. $url = 'http://source.example.com/duplicate-in-reply-to-urls';
  933. $response = $this->parse(['url' => $url]);
  934. $body = $response->getContent();
  935. $this->assertEquals(200, $response->getStatusCode());
  936. $data = json_decode($body, true);
  937. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  938. $this->assertEquals(1, count($data['data']['in-reply-to']));
  939. }
  940. public function testDuplicateLikeOfURLValues()
  941. {
  942. $url = 'http://source.example.com/duplicate-like-of-urls';
  943. $response = $this->parse(['url' => $url]);
  944. $body = $response->getContent();
  945. $this->assertEquals(200, $response->getStatusCode());
  946. $data = json_decode($body, true);
  947. $this->assertEquals('http://example.com/100', $data['data']['like-of'][0]);
  948. $this->assertEquals(1, count($data['data']['like-of']));
  949. }
  950. public function testQuotationOf()
  951. {
  952. $url = 'http://source.example.com/quotation-of';
  953. $response = $this->parse(['url' => $url]);
  954. $body = $response->getContent();
  955. $this->assertEquals(200, $response->getStatusCode());
  956. $data = json_decode($body, true);
  957. $this->assertEquals('I’m so making this into a t-shirt', $data['data']['content']['text']);
  958. $this->assertEquals('https://twitter.com/gitlost/status/1015005409726357504', $data['data']['quotation-of']);
  959. $this->assertArrayHasKey('https://twitter.com/gitlost/status/1015005409726357504', $data['data']['refs']);
  960. $q = $data['data']['refs']['https://twitter.com/gitlost/status/1015005409726357504'];
  961. $this->assertEquals("Still can't git fer shit", $q['content']['text']);
  962. $this->assertEquals('2018-07-05T22:52:02+00:00', $q['published']);
  963. }
  964. public function testHTML5Markup()
  965. {
  966. $url = 'http://source.example.com/html5-tags';
  967. $response = $this->parse(['url' => $url]);
  968. $body = $response->getContent();
  969. $this->assertEquals(200, $response->getStatusCode());
  970. $data = json_decode($body, true);
  971. $this->assertEquals('Hello World', $data['data']['name']);
  972. $this->assertEquals('The content of the blog post', $data['data']['content']['text']);
  973. }
  974. public function testRelAlternateToMf2JSON()
  975. {
  976. $url = 'http://source.example.com/rel-alternate-mf2-json';
  977. $response = $this->parse(['url' => $url]);
  978. $body = $response->getContent();
  979. $this->assertEquals(200, $response->getStatusCode());
  980. $data = json_decode($body, true);
  981. $this->assertEquals('mf2+json', $data['source-format']);
  982. $this->assertEquals('http://source.example.com/rel-alternate-mf2-json.json', $data['parsed-url']);
  983. $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']);
  984. }
  985. public function testRelAlternateToNotFoundURL()
  986. {
  987. $url = 'http://source.example.com/rel-alternate-not-found';
  988. $response = $this->parse(['url' => $url]);
  989. $body = $response->getContent();
  990. $this->assertEquals(200, $response->getStatusCode());
  991. $data = json_decode($body, true);
  992. $this->assertEquals('mf2+html', $data['source-format']);
  993. $this->assertArrayNotHasKey('parsed-url', $data);
  994. $this->assertEquals('Test content with a rel alternate link to a 404 page', $data['data']['content']['text']);
  995. }
  996. public function testRelAlternatePrioritizesJSON()
  997. {
  998. $url = 'http://source.example.com/rel-alternate-priority';
  999. $response = $this->parse(['url' => $url]);
  1000. $body = $response->getContent();
  1001. $this->assertEquals(200, $response->getStatusCode());
  1002. $data = json_decode($body, true);
  1003. $this->assertEquals('mf2+json', $data['source-format']);
  1004. $this->assertEquals('http://source.example.com/rel-alternate-priority.json', $data['parsed-url']);
  1005. $this->assertEquals('This is the content in the MF2 JSON file', $data['data']['content']['text']);
  1006. }
  1007. public function testRelAlternatePrioritizesMf2OverAS2()
  1008. {
  1009. $url = 'http://source.example.com/rel-alternate-priority-mf2-as2';
  1010. $response = $this->parse(['url' => $url]);
  1011. $body = $response->getContent();
  1012. $this->assertEquals(200, $response->getStatusCode());
  1013. $data = json_decode($body, true);
  1014. $this->assertEquals('mf2+json', $data['source-format']);
  1015. $this->assertEquals('http://source.example.com/rel-alternate-priority.json', $data['parsed-url']);
  1016. $this->assertEquals('This is the content in the MF2 JSON file', $data['data']['content']['text']);
  1017. }
  1018. public function testRelAlternateIgnoreAS2AlternateOption()
  1019. {
  1020. $url = 'http://source.example.com/rel-alternate-as2';
  1021. $response = $this->parse(['url' => $url, 'ignore-as2' => true]);
  1022. $body = $response->getContent();
  1023. $this->assertEquals(200, $response->getStatusCode());
  1024. $data = json_decode($body, true);
  1025. $this->assertEquals('mf2+html', $data['source-format']);
  1026. $this->assertArrayNotHasKey('parsed-url', $data);
  1027. $this->assertEquals('This is the content in the HTML instead of the AS2 JSON', $data['data']['content']['text']);
  1028. }
  1029. public function testRelAlternateFallsBackOnInvalidJSON()
  1030. {
  1031. $url = 'http://source.example.com/rel-alternate-fallback';
  1032. $response = $this->parse(['url' => $url]);
  1033. $body = $response->getContent();
  1034. $this->assertEquals(200, $response->getStatusCode());
  1035. $data = json_decode($body, true);
  1036. $this->assertEquals('mf2+html', $data['source-format']);
  1037. $this->assertArrayNotHasKey('parsed-url', $data);
  1038. $this->assertEquals('XRay should use this content since the JSON in the rel-alternate is invalid', $data['data']['content']['text']);
  1039. }
  1040. public function testMultipleContentTypeHeaders()
  1041. {
  1042. $url = 'http://source.example.com/multiple-content-type';
  1043. $response = $this->parse(['url' => $url]);
  1044. $body = $response->getContent();
  1045. $this->assertEquals(200, $response->getStatusCode());
  1046. $data = json_decode($body, true);
  1047. $this->assertEquals('mf2+html', $data['source-format']);
  1048. }
  1049. public function testFollowOf()
  1050. {
  1051. $url = 'http://source.example.com/bridgy-follow';
  1052. $response = $this->parse(['url' => $url]);
  1053. $body = $response->getContent();
  1054. $this->assertEquals(200, $response->getStatusCode());
  1055. $data = json_decode($body, true);
  1056. $this->assertEquals('https://realize.be/', $data['data']['follow-of']);
  1057. $this->assertEquals('follow', $data['data']['post-type']);
  1058. }
  1059. public function testFollowOfHCard()
  1060. {
  1061. $url = 'http://source.example.com/follow-of-h-card';
  1062. $response = $this->parse(['url' => $url]);
  1063. $body = $response->getContent();
  1064. $this->assertEquals(200, $response->getStatusCode());
  1065. $data = json_decode($body, true);
  1066. $this->assertEquals('https://realize.be/', $data['data']['follow-of']);
  1067. $this->assertEquals('follow', $data['data']['post-type']);
  1068. }
  1069. public function testRelCanonical()
  1070. {
  1071. $url = 'http://source.example.com/rel-canonical';
  1072. $response = $this->parse(['url' => $url]);
  1073. $body = $response->getContent();
  1074. $this->assertEquals(200, $response->getStatusCode());
  1075. $data = json_decode($body, true);
  1076. $this->assertEquals('https://aaronparecki.com/2019/12/01/10/homeautomation', $data['data']['url']);
  1077. $this->assertEquals('https://aaronparecki.com/2019/12/01/10/homeautomation', $data['data']['rels']['canonical']);
  1078. }
  1079. public function testTargetLinkOutsideHEntry()
  1080. {
  1081. $url = 'http://source.example.com/target-test-link-outside-h-entry';
  1082. $response = $this->parse(['url' => $url, 'target' => 'https://target.example.com/']);
  1083. $body = $response->getContent();
  1084. $this->assertEquals(200, $response->getStatusCode());
  1085. $data = json_decode($body, true);
  1086. $this->assertEquals('no_link_found', $data['error']);
  1087. }
  1088. public function testTargetLinkWithBadMf1()
  1089. {
  1090. $url = 'http://source.example.com/target-test-only-bad-mf1';
  1091. $response = $this->parse(['url' => $url, 'target' => 'https://target.example.com/']);
  1092. $body = $response->getContent();
  1093. $this->assertEquals(200, $response->getStatusCode());
  1094. $data = json_decode($body, true);
  1095. $this->assertEquals('unknown', $data['data']['type']);
  1096. }
  1097. public function testTargetLinkWithValidMf1()
  1098. {
  1099. $url = 'http://source.example.com/target-test-only-good-mf1';
  1100. $response = $this->parse(['url' => $url, 'target' => 'https://target.example.com/']);
  1101. $body = $response->getContent();
  1102. $this->assertEquals(200, $response->getStatusCode());
  1103. $data = json_decode($body, true);
  1104. $this->assertEquals('entry', $data['data']['type']);
  1105. $this->assertEquals('<a href="https://target.example.com/">target</a>', $data['data']['content']['html']);
  1106. }
  1107. public function testTargetLinkOutsideValidMf1()
  1108. {
  1109. $url = 'http://source.example.com/target-test-link-outside-valid-mf1';
  1110. $response = $this->parse(['url' => $url, 'target' => 'https://target.example.com/']);
  1111. $body = $response->getContent();
  1112. $this->assertEquals(200, $response->getStatusCode());
  1113. $data = json_decode($body, true);
  1114. // Since the link was found in the HTML, but not in the parsed tree, it shouldn't return the parsed document
  1115. $this->assertEquals('unknown', $data['data']['type']);
  1116. }
  1117. public function testDisableMf1Parsing()
  1118. {
  1119. $url = 'http://source.example.com/target-test-only-good-mf1';
  1120. $response = $this->parse(['url' => $url, 'include-mf1' => 'false']);
  1121. $body = $response->getContent();
  1122. $this->assertEquals(200, $response->getStatusCode());
  1123. $data = json_decode($body, true);
  1124. $this->assertEquals('unknown', $data['data']['type']);
  1125. }
  1126. public function testEnableMf1Parsing()
  1127. {
  1128. $url = 'http://source.example.com/target-test-only-good-mf1';
  1129. $response = $this->parse(['url' => $url, 'include-mf1' => 'true']);
  1130. $body = $response->getContent();
  1131. $this->assertEquals(200, $response->getStatusCode());
  1132. $data = json_decode($body, true);
  1133. $this->assertEquals('entry', $data['data']['type']);
  1134. }
  1135. public function testMissingContent() {
  1136. $url = 'http://source.example.com/bookmark-missing-content';
  1137. $response = $this->parse(['url' => $url]);
  1138. $body = $response->getContent();
  1139. $this->assertEquals(200, $response->getStatusCode());
  1140. $data = json_decode($body, true);
  1141. $this->assertArrayNotHasKey('content', $data['data']);
  1142. }
  1143. }