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.

1324 lines
56 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 testArticleWithFeaturedImage()
  281. {
  282. $url = 'http://source.example.com/article-with-featured-image';
  283. $response = $this->parse(['url' => $url]);
  284. $body = $response->getContent();
  285. $this->assertEquals(200, $response->getStatusCode());
  286. $data = json_decode($body);
  287. $this->assertEquals('mf2+html', $data->{'source-format'});
  288. $this->assertEquals('article', $data->data->{'post-type'});
  289. $this->assertEquals('Post Title', $data->data->name);
  290. $this->assertEquals('This is a blog post.', $data->data->content->text);
  291. $this->assertEquals('http://source.example.com/featured.jpg', $data->data->featured);
  292. }
  293. public function testContentWithPrefixedName()
  294. {
  295. $url = 'http://source.example.com/content-with-prefixed-name';
  296. $response = $this->parse(['url' => $url]);
  297. $body = $response->getContent();
  298. $this->assertEquals(200, $response->getStatusCode());
  299. $data = json_decode($body);
  300. $this->assertEquals('mf2+html', $data->{'source-format'});
  301. $this->assertObjectNotHasAttribute('name', $data->data);
  302. $this->assertEquals('note', $data->data->{'post-type'});
  303. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  304. $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);
  305. }
  306. public function testContentWithDistinctName()
  307. {
  308. $url = 'http://source.example.com/content-with-distinct-name';
  309. $response = $this->parse(['url' => $url]);
  310. $body = $response->getContent();
  311. $this->assertEquals(200, $response->getStatusCode());
  312. $data = json_decode($body);
  313. $this->assertEquals('mf2+html', $data->{'source-format'});
  314. $this->assertEquals('Hello World', $data->data->name);
  315. $this->assertEquals('article', $data->data->{'post-type'});
  316. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  317. $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);
  318. }
  319. public function testNameWithNoContent()
  320. {
  321. $url = 'http://source.example.com/name-no-content';
  322. $response = $this->parse(['url' => $url]);
  323. $body = $response->getContent();
  324. $this->assertEquals(200, $response->getStatusCode());
  325. $data = json_decode($body);
  326. $this->assertEquals('mf2+html', $data->{'source-format'});
  327. $this->assertEquals('Hello World', $data->data->name);
  328. $this->assertEquals('article', $data->data->{'post-type'});
  329. $this->assertObjectNotHasAttribute('content', $data->data);
  330. }
  331. public function testEntryWithDuplicateCategories()
  332. {
  333. $url = 'http://source.example.com/h-entry-duplicate-categories';
  334. $response = $this->parse(['url' => $url]);
  335. $body = $response->getContent();
  336. $this->assertEquals(200, $response->getStatusCode());
  337. $data = json_decode($body);
  338. $this->assertEquals('mf2+html', $data->{'source-format'});
  339. $this->assertEquals(['indieweb'], $data->data->category);
  340. }
  341. public function testEntryStripHashtagWithDuplicateCategories()
  342. {
  343. $url = 'http://source.example.com/h-entry-strip-hashtag-from-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->assertContains('indieweb', $data->data->category);
  350. $this->assertContains('xray', $data->data->category);
  351. $this->assertEquals(2, count($data->data->category));
  352. }
  353. public function testNoHEntryMarkup()
  354. {
  355. $url = 'http://source.example.com/no-h-entry';
  356. $response = $this->parse(['url' => $url]);
  357. $body = $response->getContent();
  358. $this->assertEquals(200, $response->getStatusCode());
  359. $data = json_decode($body);
  360. $this->assertEquals('unknown', $data->data->type);
  361. $this->assertObjectNotHasAttribute('html', $data);
  362. }
  363. public function testFindTargetInNoParsedResult()
  364. {
  365. $url = 'http://source.example.com/no-h-entry';
  366. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com']);
  367. $body = $response->getContent();
  368. $this->assertEquals(200, $response->getStatusCode());
  369. $data = json_decode($body);
  370. $this->assertObjectNotHasAttribute('error', $data);
  371. $this->assertEquals('unknown', $data->data->type);
  372. }
  373. public function testReplyIsURL()
  374. {
  375. $url = 'http://source.example.com/reply-is-url';
  376. $response = $this->parse(['url' => $url]);
  377. $body = $response->getContent();
  378. $this->assertEquals(200, $response->getStatusCode());
  379. $data = json_decode($body, true);
  380. $this->assertEquals('mf2+html', $data['source-format']);
  381. $this->assertEquals('entry', $data['data']['type']);
  382. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  383. }
  384. public function testReplyIsHCite()
  385. {
  386. $url = 'http://source.example.com/reply-is-h-cite';
  387. $response = $this->parse(['url' => $url]);
  388. $body = $response->getContent();
  389. $this->assertEquals(200, $response->getStatusCode());
  390. $data = json_decode($body, true);
  391. $this->assertEquals('mf2+html', $data['source-format']);
  392. $this->assertEquals('entry', $data['data']['type']);
  393. $this->assertEquals('reply', $data['data']['post-type']);
  394. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  395. $this->assertArrayHasKey('http://example.com/100', $data['data']['refs']);
  396. $this->assertEquals('Example Post', $data['data']['refs']['http://example.com/100']['name']);
  397. $this->assertEquals('http://example.com/100', $data['data']['refs']['http://example.com/100']['url']);
  398. }
  399. public function testPersonTagIsURL()
  400. {
  401. $url = 'http://source.example.com/person-tag-is-url';
  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('http://alice.example.com/', $data['data']['category'][0]);
  409. }
  410. public function testPersonTagIsHCard()
  411. {
  412. $url = 'http://source.example.com/person-tag-is-h-card';
  413. $response = $this->parse(['url' => $url]);
  414. $body = $response->getContent();
  415. $this->assertEquals(200, $response->getStatusCode());
  416. $data = json_decode($body, true);
  417. $this->assertEquals('mf2+html', $data['source-format']);
  418. $this->assertEquals('entry', $data['data']['type']);
  419. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  420. $this->assertArrayHasKey('http://alice.example.com/', $data['data']['refs']);
  421. $this->assertEquals('card', $data['data']['refs']['http://alice.example.com/']['type']);
  422. $this->assertEquals('http://alice.example.com/', $data['data']['refs']['http://alice.example.com/']['url']);
  423. $this->assertEquals('Alice', $data['data']['refs']['http://alice.example.com/']['name']);
  424. }
  425. public function testSyndicationIsURL()
  426. {
  427. $url = 'http://source.example.com/has-syndication';
  428. $response = $this->parse(['url' => $url]);
  429. $body = $response->getContent();
  430. $this->assertEquals(200, $response->getStatusCode());
  431. $data = json_decode($body, true);
  432. $this->assertEquals('mf2+html', $data['source-format']);
  433. $this->assertEquals('entry', $data['data']['type']);
  434. $this->assertEquals('http://syndicated.example/', $data['data']['syndication'][0]);
  435. }
  436. public function testHEntryNoContent()
  437. {
  438. $url = 'http://source.example.com/h-entry-no-content';
  439. $response = $this->parse(['url' => $url]);
  440. $body = $response->getContent();
  441. $this->assertEquals(200, $response->getStatusCode());
  442. $data = json_decode($body);
  443. $this->assertEquals('mf2+html', $data->{'source-format'});
  444. $this->assertObjectNotHasAttribute('content', $data->data);
  445. $this->assertEquals('This is a Post', $data->data->name);
  446. }
  447. public function testHEntryIsNotFirstObject()
  448. {
  449. $url = 'http://source.example.com/h-entry-is-not-first';
  450. $response = $this->parse(['url' => $url]);
  451. $body = $response->getContent();
  452. $this->assertEquals(200, $response->getStatusCode());
  453. $data = json_decode($body, true);
  454. $this->assertEquals('mf2+html', $data['source-format']);
  455. $this->assertEquals('entry', $data['data']['type']);
  456. $this->assertEquals('Hello World', $data['data']['content']['text']);
  457. }
  458. public function testHEntryRSVP()
  459. {
  460. $url = 'http://source.example.com/h-entry-rsvp';
  461. $response = $this->parse(['url' => $url]);
  462. $body = $response->getContent();
  463. $this->assertEquals(200, $response->getStatusCode());
  464. $data = json_decode($body, true);
  465. $this->assertEquals('mf2+html', $data['source-format']);
  466. $this->assertEquals('entry', $data['data']['type']);
  467. $this->assertEquals('rsvp', $data['data']['post-type']);
  468. $this->assertEquals('I\'ll be there!', $data['data']['content']['text']);
  469. $this->assertEquals('yes', $data['data']['rsvp']);
  470. }
  471. public function testMultipleHEntryOnPermalink()
  472. {
  473. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  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('entry', $data['data']['type']);
  480. $this->assertEquals('Primary Post', $data['data']['name']);
  481. }
  482. public function testHEntryWithHCardBeforeIt()
  483. {
  484. $url = 'http://source.example.com/h-entry-with-h-card-before-it';
  485. $response = $this->parse(['url' => $url]);
  486. $body = $response->getContent();
  487. $this->assertEquals(200, $response->getStatusCode());
  488. $data = json_decode($body, true);
  489. $this->assertEquals('mf2+html', $data['source-format']);
  490. $this->assertEquals('entry', $data['data']['type']);
  491. $this->assertEquals('Hello World', $data['data']['content']['text']);
  492. }
  493. public function testHEntryWithHCardSibling()
  494. {
  495. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  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('entry', $data['data']['type']);
  502. $this->assertEquals('Hello World', $data['data']['content']['text']);
  503. }
  504. public function testHEntryWithTwoHCardsBeforeIt()
  505. {
  506. $url = 'http://source.example.com/h-entry-with-two-h-cards-before-it';
  507. $response = $this->parse(['url' => $url]);
  508. $body = $response->getContent();
  509. $this->assertEquals(200, $response->getStatusCode());
  510. $data = json_decode($body, true);
  511. $this->assertEquals('mf2+html', $data['source-format']);
  512. $this->assertEquals('entry', $data['data']['type']);
  513. $this->assertEquals('Hello World', $data['data']['content']['text']);
  514. }
  515. public function testHEntryRedirectWithHCardSibling()
  516. {
  517. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  518. $response = $this->parse(['url' => $url]);
  519. $body = $response->getContent();
  520. $this->assertEquals(200, $response->getStatusCode());
  521. $data = json_decode($body, true);
  522. $this->assertEquals('mf2+html', $data['source-format']);
  523. $this->assertEquals('entry', $data['data']['type']);
  524. $this->assertEquals('Hello World', $data['data']['content']['text']);
  525. }
  526. public function testSingleHEntryHasNoPermalink()
  527. {
  528. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  529. $response = $this->parse(['url' => $url]);
  530. $body = $response->getContent();
  531. $this->assertEquals(200, $response->getStatusCode());
  532. $data = json_decode($body, true);
  533. $this->assertEquals('mf2+html', $data['source-format']);
  534. $this->assertEquals('entry', $data['data']['type']);
  535. $this->assertEquals('Hello World', $data['data']['content']['text']);
  536. }
  537. public function testBridgyExampleWithNoMatchingURL()
  538. {
  539. $url = 'http://source.example.com/bridgy-example';
  540. $response = $this->parse(['url' => $url]);
  541. $body = $response->getContent();
  542. $this->assertEquals(200, $response->getStatusCode());
  543. $data = json_decode($body, true);
  544. $this->assertEquals('mf2+html', $data['source-format']);
  545. $this->assertEquals('entry', $data['data']['type']);
  546. }
  547. public function testEventWithHTMLDescription()
  548. {
  549. $url = 'http://source.example.com/h-event';
  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('event', $data['data']['type']);
  556. $this->assertEquals('event', $data['data']['post-type']);
  557. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  558. $this->assertEquals($url, $data['data']['url']);
  559. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  560. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  561. $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']);
  562. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['content']['text']);
  563. $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']);
  564. $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']);
  565. }
  566. public function testEventWithTextDescription()
  567. {
  568. $url = 'http://source.example.com/h-event-text-description';
  569. $response = $this->parse(['url' => $url]);
  570. $body = $response->getContent();
  571. $this->assertEquals(200, $response->getStatusCode());
  572. $data = json_decode($body, true);
  573. $this->assertEquals('mf2+html', $data['source-format']);
  574. $this->assertEquals('event', $data['data']['type']);
  575. $this->assertEquals('event', $data['data']['post-type']);
  576. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  577. $this->assertEquals($url, $data['data']['url']);
  578. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  579. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  580. $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']);
  581. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['content']['text']);
  582. $this->assertArrayNotHasKey('html', $data['data']['content']);
  583. }
  584. public function testEventWithTextContent()
  585. {
  586. $url = 'http://source.example.com/h-event-text-content';
  587. $response = $this->parse(['url' => $url]);
  588. $body = $response->getContent();
  589. $this->assertEquals(200, $response->getStatusCode());
  590. $data = json_decode($body, true);
  591. $this->assertEquals('mf2+html', $data['source-format']);
  592. $this->assertEquals('event', $data['data']['type']);
  593. $this->assertEquals('event', $data['data']['post-type']);
  594. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  595. $this->assertEquals($url, $data['data']['url']);
  596. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  597. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  598. $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']);
  599. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['content']['text']);
  600. $this->assertArrayNotHasKey('html', $data['data']['content']);
  601. $this->assertEquals('card', $data['data']['author']['type']);
  602. $this->assertEquals('Event Author', $data['data']['author']['name']);
  603. $this->assertEquals('http://source.example.com/', $data['data']['author']['url']);
  604. }
  605. public function testEventWithHCardLocation()
  606. {
  607. $url = 'http://source.example.com/h-event-with-h-card-location';
  608. $response = $this->parse(['url' => $url]);
  609. $body = $response->getContent();
  610. $this->assertEquals(200, $response->getStatusCode());
  611. $data = json_decode($body, true);
  612. $this->assertEquals('mf2+html', $data['source-format']);
  613. $this->assertEquals('event', $data['data']['type']);
  614. $this->assertEquals('event', $data['data']['post-type']);
  615. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  616. $this->assertEquals($url, $data['data']['url']);
  617. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  618. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  619. $this->assertEquals('card', $data['data']['location']['type']);
  620. $this->assertEquals('http://source.example.com/venue', $data['data']['location']['url']);
  621. $this->assertEquals('Venue', $data['data']['location']['name']);
  622. $this->assertEquals('45.5', $data['data']['location']['latitude']);
  623. $this->assertEquals('-122.6', $data['data']['location']['longitude']);
  624. $this->assertEquals('1234 Main St', $data['data']['location']['street-address']);
  625. $this->assertEquals('Portland', $data['data']['location']['locality']);
  626. $this->assertEquals('Oregon', $data['data']['location']['region']);
  627. $this->assertEquals('USA', $data['data']['location']['country-name']);
  628. }
  629. public function testEventWithFeaturedImage()
  630. {
  631. $url = 'http://source.example.com/h-event-featured';
  632. $response = $this->parse(['url' => $url]);
  633. $body = $response->getContent();
  634. $this->assertEquals(200, $response->getStatusCode());
  635. $data = json_decode($body, true);
  636. $this->assertEquals('mf2+html', $data['source-format']);
  637. $this->assertEquals('event', $data['data']['type']);
  638. $this->assertEquals('http://source.example.com/featured.jpg', $data['data']['featured']);
  639. }
  640. public function testMf2ReviewOfProduct()
  641. {
  642. $url = 'http://source.example.com/h-review-of-product';
  643. $response = $this->parse(['url' => $url]);
  644. $body = $response->getContent();
  645. $this->assertEquals(200, $response->getStatusCode());
  646. $data = json_decode($body, true);
  647. $this->assertEquals('mf2+html', $data['source-format']);
  648. $this->assertEquals('review', $data['data']['type']);
  649. $this->assertEquals('review', $data['data']['post-type']);
  650. $this->assertEquals('Review', $data['data']['name']);
  651. $this->assertEquals('Not great', $data['data']['summary']);
  652. $this->assertEquals('3', $data['data']['rating']);
  653. $this->assertEquals('5', $data['data']['best']);
  654. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  655. $this->assertContains('red', $data['data']['category']);
  656. $this->assertContains('blue', $data['data']['category']);
  657. $this->assertContains('http://product.example.com/', $data['data']['item']);
  658. $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
  659. $this->assertEquals('product', $data['data']['refs']['http://product.example.com/']['type']);
  660. $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
  661. $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
  662. }
  663. public function testMf2ReviewOfHCard()
  664. {
  665. $url = 'http://source.example.com/h-review-of-h-card';
  666. $response = $this->parse(['url' => $url]);
  667. $body = $response->getContent();
  668. $this->assertEquals(200, $response->getStatusCode());
  669. $data = json_decode($body, true);
  670. $this->assertEquals('mf2+html', $data['source-format']);
  671. $this->assertEquals('review', $data['data']['type']);
  672. $this->assertEquals('review', $data['data']['post-type']);
  673. $this->assertEquals('Review', $data['data']['name']);
  674. $this->assertEquals('Not great', $data['data']['summary']);
  675. $this->assertEquals('3', $data['data']['rating']);
  676. $this->assertEquals('5', $data['data']['best']);
  677. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  678. $this->assertContains('http://business.example.com/', $data['data']['item']);
  679. $this->assertArrayHasKey('http://business.example.com/', $data['data']['refs']);
  680. $this->assertEquals('card', $data['data']['refs']['http://business.example.com/']['type']);
  681. $this->assertEquals('The Reviewed Business', $data['data']['refs']['http://business.example.com/']['name']);
  682. $this->assertEquals('http://business.example.com/', $data['data']['refs']['http://business.example.com/']['url']);
  683. }
  684. public function testMf1Review()
  685. {
  686. $url = 'http://source.example.com/hReview';
  687. $response = $this->parse(['url' => $url]);
  688. $body = $response->getContent();
  689. $this->assertEquals(200, $response->getStatusCode());
  690. $data = json_decode($body, true);
  691. $this->assertEquals('mf2+html', $data['source-format']);
  692. $this->assertEquals('review', $data['data']['type']);
  693. $this->assertEquals('review', $data['data']['post-type']);
  694. $this->assertEquals('Not great', $data['data']['name']);
  695. $this->assertEquals('3', $data['data']['rating']);
  696. $this->assertEquals('5', $data['data']['best']);
  697. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  698. $this->assertContains('http://product.example.com/', $data['data']['item']);
  699. $this->assertArrayHasKey('http://product.example.com/', $data['data']['refs']);
  700. $this->assertEquals('item', $data['data']['refs']['http://product.example.com/']['type']);
  701. $this->assertEquals('The Reviewed Product', $data['data']['refs']['http://product.example.com/']['name']);
  702. $this->assertEquals('http://product.example.com/', $data['data']['refs']['http://product.example.com/']['url']);
  703. }
  704. public function testMf2Recipe()
  705. {
  706. $url = 'http://source.example.com/h-recipe';
  707. $response = $this->parse(['url' => $url]);
  708. $body = $response->getContent();
  709. $this->assertEquals(200, $response->getStatusCode());
  710. $data = json_decode($body, true);
  711. $this->assertEquals('mf2+html', $data['source-format']);
  712. $this->assertEquals('recipe', $data['data']['type']);
  713. $this->assertEquals('recipe', $data['data']['post-type']);
  714. $this->assertEquals('Cookie Recipe', $data['data']['name']);
  715. $this->assertEquals('12 Cookies', $data['data']['yield']);
  716. $this->assertEquals('PT30M', $data['data']['duration']);
  717. $this->assertEquals('The best chocolate chip cookie recipe', $data['data']['summary']);
  718. $this->assertContains('3 cups flour', $data['data']['ingredient']);
  719. $this->assertContains('chocolate chips', $data['data']['ingredient']);
  720. }
  721. public function testEntryIsAnInvitee()
  722. {
  723. $url = 'http://source.example.com/bridgy-invitee';
  724. $response = $this->parse(['url' => $url]);
  725. $body = $response->getContent();
  726. $this->assertEquals(200, $response->getStatusCode());
  727. $data = json_decode($body, true);
  728. $this->assertEquals('mf2+html', $data['source-format']);
  729. $this->assertEquals('entry', $data['data']['type']);
  730. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  731. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  732. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['data']['refs']);
  733. $this->assertEquals('Tantek Çelik', $data['data']['refs']['https://www.facebook.com/tantek.celik']['name']);
  734. }
  735. public function testEntryAtFragmentID()
  736. {
  737. $url = 'http://source.example.com/fragment-id#comment-1000';
  738. $response = $this->parse(['url' => $url]);
  739. $body = $response->getContent();
  740. $this->assertEquals(200, $response->getStatusCode());
  741. $data = json_decode($body, true);
  742. $this->assertEquals('mf2+html', $data['source-format']);
  743. $this->assertEquals('entry', $data['data']['type']);
  744. $this->assertEquals('note', $data['data']['post-type']);
  745. $this->assertEquals('Comment text', $data['data']['content']['text']);
  746. $this->assertEquals('http://source.example.com/fragment-id#comment-1000', $data['data']['url']);
  747. $this->assertTrue($data['info']['found_fragment']);
  748. }
  749. public function testEntryAtNonExistentFragmentID()
  750. {
  751. $url = 'http://source.example.com/fragment-id#comment-404';
  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+html', $data['source-format']);
  757. $this->assertEquals('entry', $data['data']['type']);
  758. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  759. $this->assertFalse($data['info']['found_fragment']);
  760. }
  761. public function testCheckin()
  762. {
  763. $url = 'http://source.example.com/checkin';
  764. $response = $this->parse(['url' => $url]);
  765. $body = $response->getContent();
  766. $this->assertEquals(200, $response->getStatusCode());
  767. $data = json_decode($body, true);
  768. $this->assertEquals('mf2+html', $data['source-format']);
  769. $this->assertEquals('entry', $data['data']['type']);
  770. $venue = $data['data']['checkin'];
  771. $this->assertEquals('checkin', $data['data']['post-type']);
  772. $this->assertEquals('https://foursquare.com/v/57104d2e498ece022e169dca', $venue['url']);
  773. $this->assertEquals('DreamHost', $venue['name']);
  774. $this->assertEquals('45.518716', $venue['latitude']);
  775. $this->assertEquals('Homebrew Website Club!', $data['data']['content']['text']);
  776. $this->assertEquals('https://aaronparecki.com/2017/06/07/12/photo.jpg', $data['data']['photo'][0]);
  777. $this->assertEquals('2017-06-07T17:14:40-07:00', $data['data']['published']);
  778. $this->assertArrayNotHasKey('name', $data['data']);
  779. }
  780. public function testCheckinURLOnly()
  781. {
  782. $url = 'http://source.example.com/checkin-url';
  783. $response = $this->parse(['url' => $url]);
  784. $body = $response->getContent();
  785. $this->assertEquals(200, $response->getStatusCode());
  786. $data = json_decode($body, true);
  787. $this->assertEquals('mf2+html', $data['source-format']);
  788. $this->assertEquals('entry', $data['data']['type']);
  789. $this->assertEquals('checkin', $data['data']['post-type']);
  790. $venue = $data['data']['checkin'];
  791. $this->assertEquals('https://foursquare.com/v/57104d2e498ece022e169dca', $venue['url']);
  792. $this->assertEquals('Homebrew Website Club!', $data['data']['content']['text']);
  793. $this->assertEquals('https://aaronparecki.com/2017/06/07/12/photo.jpg', $data['data']['photo'][0]);
  794. $this->assertEquals('2017-06-07T17:14:40-07:00', $data['data']['published']);
  795. $this->assertArrayNotHasKey('name', $data['data']);
  796. }
  797. public function testXKCD()
  798. {
  799. $url = 'http://xkcd.com/1810/';
  800. $response = $this->parse(['url' => $url]);
  801. $body = $response->getContent();
  802. $this->assertEquals(200, $response->getStatusCode());
  803. $data = json_decode($body, true);
  804. $this->assertEquals(200, $data['code']);
  805. $this->assertEquals('xkcd', $data['source-format']);
  806. $this->assertEquals('entry', $data['data']['type']);
  807. $this->assertEquals('photo', $data['data']['post-type']);
  808. $this->assertEquals('http://xkcd.com/1810/', $data['data']['url']);
  809. $this->assertEquals('Chat Systems', $data['data']['name']);
  810. $this->assertContains('http://imgs.xkcd.com/comics/chat_systems_2x.png', $data['data']['photo']);
  811. }
  812. public function testEntryHasMultipleURLs()
  813. {
  814. $url = 'http://source.example.com/multiple-urls';
  815. $response = $this->parse(['url' => $url]);
  816. $body = $response->getContent();
  817. $this->assertEquals(200, $response->getStatusCode());
  818. $data = json_decode($body, true);
  819. // Should prioritize the URL on the same domain
  820. $this->assertEquals($url, $data['data']['url']);
  821. }
  822. public function testEntryHasMultipleURLsOffDomain()
  823. {
  824. $url = 'http://source.example.com/multiple-urls-off-domain';
  825. $response = $this->parse(['url' => $url]);
  826. $body = $response->getContent();
  827. $this->assertEquals(200, $response->getStatusCode());
  828. $data = json_decode($body, true);
  829. // Neither URL is on the same domain, so should use the first
  830. $this->assertEquals('http://one.example.com/test', $data['data']['url']);
  831. }
  832. public function testInputIsJSON()
  833. {
  834. $url = 'http://example.com/entry';
  835. $mf2json = ['items' => [
  836. [
  837. 'type' => ['h-entry'],
  838. 'properties' => [
  839. 'content' => [['html' => 'Hello World']]
  840. ]
  841. ]
  842. ]];
  843. $response = $this->parse(
  844. [
  845. 'body' => $mf2json,
  846. 'url' => $url,
  847. ]
  848. );
  849. $body = $response->getContent();
  850. $data = json_decode($body, true);
  851. $this->assertEquals('mf2+json', $data['source-format']);
  852. $this->assertEquals('Hello World', $data['data']['content']['text']);
  853. }
  854. public function testInputIsParsedMf2()
  855. {
  856. $html = '<div class="h-entry"><p class="p-content p-name">Hello World</p><img src="/photo.jpg"></p></div>';
  857. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  858. $url = 'http://example.com/entry';
  859. $response = $this->parse(
  860. [
  861. 'url' => $url,
  862. 'body' => json_encode($mf2)
  863. ]
  864. );
  865. $body = $response->getContent();
  866. $this->assertEquals(200, $response->getStatusCode());
  867. $data = json_decode($body, true);
  868. $this->assertEquals('mf2+json', $data['source-format']);
  869. $this->assertEquals('Hello World', $data['data']['content']['text']);
  870. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  871. }
  872. public function testInputIsParsedMf2WithHTML()
  873. {
  874. $html = '<div class="h-entry"><p class="e-content p-name"><b>Hello</b> <i>World</i></p><img src="/photo.jpg"></p></div>';
  875. $mf2 = Mf2\parse($html, 'http://example.com/entry');
  876. $url = 'http://example.com/entry';
  877. $response = $this->parse(
  878. [
  879. 'url' => $url,
  880. 'body' => json_encode($mf2)
  881. ]
  882. );
  883. $body = $response->getContent();
  884. $this->assertEquals(200, $response->getStatusCode());
  885. $data = json_decode($body, true);
  886. $this->assertEquals('mf2+json', $data['source-format']);
  887. $this->assertEquals('Hello World', $data['data']['content']['text']);
  888. $this->assertEquals('<b>Hello</b> <i>World</i>', $data['data']['content']['html']);
  889. $this->assertEquals('http://example.com/photo.jpg', $data['data']['photo'][0]);
  890. }
  891. public function testHApp()
  892. {
  893. $url = 'http://source.example.com/h-app';
  894. $response = $this->parse(['url' => $url]);
  895. $body = $response->getContent();
  896. $this->assertEquals(200, $response->getStatusCode());
  897. $data = json_decode($body, true);
  898. $this->assertEquals('mf2+html', $data['source-format']);
  899. $this->assertEquals('app', $data['data']['type']);
  900. $this->assertEquals('http://source.example.com/images/quill.png', $data['data']['logo']);
  901. $this->assertEquals('Quill', $data['data']['name']);
  902. $this->assertEquals($url, $data['data']['url']);
  903. $this->assertEquals(['http://source.example.com/redirect1','http://source.example.com/redirect2'], $data['data']['redirect-uri']);
  904. $this->assertArrayNotHasKey('photo', $data['data']);
  905. }
  906. public function testHXApp()
  907. {
  908. $url = 'http://source.example.com/h-x-app';
  909. $response = $this->parse(['url' => $url]);
  910. $body = $response->getContent();
  911. $this->assertEquals(200, $response->getStatusCode());
  912. $data = json_decode($body);
  913. $this->assertEquals('mf2+html', $data->{'source-format'});
  914. $this->assertEquals('app', $data->data->type);
  915. $this->assertEquals('http://source.example.com/images/quill.png', $data->data->logo);
  916. $this->assertEquals('Quill', $data->data->name);
  917. $this->assertEquals($url, $data->data->url);
  918. $this->assertObjectNotHasAttribute('photo', $data->data);
  919. }
  920. public function testDuplicateReplyURLValues()
  921. {
  922. $url = 'http://source.example.com/duplicate-in-reply-to-urls';
  923. $response = $this->parse(['url' => $url]);
  924. $body = $response->getContent();
  925. $this->assertEquals(200, $response->getStatusCode());
  926. $data = json_decode($body, true);
  927. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  928. $this->assertEquals(1, count($data['data']['in-reply-to']));
  929. }
  930. public function testDuplicateLikeOfURLValues()
  931. {
  932. $url = 'http://source.example.com/duplicate-like-of-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']['like-of'][0]);
  938. $this->assertEquals(1, count($data['data']['like-of']));
  939. }
  940. public function testQuotationOf()
  941. {
  942. $url = 'http://source.example.com/quotation-of';
  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('I’m so making this into a t-shirt', $data['data']['content']['text']);
  948. $this->assertEquals('https://twitter.com/gitlost/status/1015005409726357504', $data['data']['quotation-of']);
  949. $this->assertArrayHasKey('https://twitter.com/gitlost/status/1015005409726357504', $data['data']['refs']);
  950. $q = $data['data']['refs']['https://twitter.com/gitlost/status/1015005409726357504'];
  951. $this->assertEquals("Still can't git fer shit", $q['content']['text']);
  952. $this->assertEquals('2018-07-05T22:52:02+00:00', $q['published']);
  953. }
  954. public function testHTML5Markup()
  955. {
  956. $url = 'http://source.example.com/html5-tags';
  957. $response = $this->parse(['url' => $url]);
  958. $body = $response->getContent();
  959. $this->assertEquals(200, $response->getStatusCode());
  960. $data = json_decode($body, true);
  961. $this->assertEquals('Hello World', $data['data']['name']);
  962. $this->assertEquals('The content of the blog post', $data['data']['content']['text']);
  963. }
  964. public function testRelAlternateToMf2JSON()
  965. {
  966. $url = 'http://source.example.com/rel-alternate-mf2-json';
  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('mf2+json', $data['source-format']);
  972. $this->assertEquals('http://source.example.com/rel-alternate-mf2-json.json', $data['parsed-url']);
  973. $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']);
  974. }
  975. public function testRelAlternateToNotFoundURL()
  976. {
  977. $url = 'http://source.example.com/rel-alternate-not-found';
  978. $response = $this->parse(['url' => $url]);
  979. $body = $response->getContent();
  980. $this->assertEquals(200, $response->getStatusCode());
  981. $data = json_decode($body, true);
  982. $this->assertEquals('mf2+html', $data['source-format']);
  983. $this->assertArrayNotHasKey('parsed-url', $data);
  984. $this->assertEquals('Test content with a rel alternate link to a 404 page', $data['data']['content']['text']);
  985. }
  986. public function testRelAlternatePrioritizesJSON()
  987. {
  988. $url = 'http://source.example.com/rel-alternate-priority';
  989. $response = $this->parse(['url' => $url]);
  990. $body = $response->getContent();
  991. $this->assertEquals(200, $response->getStatusCode());
  992. $data = json_decode($body, true);
  993. $this->assertEquals('mf2+json', $data['source-format']);
  994. $this->assertEquals('http://source.example.com/rel-alternate-priority.json', $data['parsed-url']);
  995. $this->assertEquals('This should be the content from XRay', $data['data']['content']['text']);
  996. }
  997. public function testRelAlternatePrioritizesMf2OverAS2()
  998. {
  999. $url = 'http://source.example.com/rel-alternate-priority-mf2-as2';
  1000. $response = $this->parse(['url' => $url]);
  1001. $body = $response->getContent();
  1002. $this->assertEquals(200, $response->getStatusCode());
  1003. $data = json_decode($body, true);
  1004. $this->assertEquals('mf2+json', $data['source-format']);
  1005. $this->assertEquals('http://source.example.com/rel-alternate-priority.json', $data['parsed-url']);
  1006. $this->assertEquals('This should be the content from XRay', $data['data']['content']['text']);
  1007. }
  1008. public function testRelAlternateFallsBackOnInvalidJSON()
  1009. {
  1010. $url = 'http://source.example.com/rel-alternate-fallback';
  1011. $response = $this->parse(['url' => $url]);
  1012. $body = $response->getContent();
  1013. $this->assertEquals(200, $response->getStatusCode());
  1014. $data = json_decode($body, true);
  1015. $this->assertEquals('mf2+html', $data['source-format']);
  1016. $this->assertArrayNotHasKey('parsed-url', $data);
  1017. $this->assertEquals('XRay should use this content since the JSON in the rel-alternate is invalid', $data['data']['content']['text']);
  1018. }
  1019. public function testMultipleContentTypeHeaders()
  1020. {
  1021. $url = 'http://source.example.com/multiple-content-type';
  1022. $response = $this->parse(['url' => $url]);
  1023. $body = $response->getContent();
  1024. $this->assertEquals(200, $response->getStatusCode());
  1025. $data = json_decode($body, true);
  1026. $this->assertEquals('mf2+html', $data['source-format']);
  1027. }
  1028. public function testFollowOf()
  1029. {
  1030. $url = 'http://source.example.com/bridgy-follow';
  1031. $response = $this->parse(['url' => $url]);
  1032. $body = $response->getContent();
  1033. $this->assertEquals(200, $response->getStatusCode());
  1034. $data = json_decode($body, true);
  1035. $this->assertEquals('https://realize.be/', $data['data']['follow-of']);
  1036. $this->assertEquals('follow', $data['data']['post-type']);
  1037. }
  1038. public function testFollowOfHCard()
  1039. {
  1040. $url = 'http://source.example.com/follow-of-h-card';
  1041. $response = $this->parse(['url' => $url]);
  1042. $body = $response->getContent();
  1043. $this->assertEquals(200, $response->getStatusCode());
  1044. $data = json_decode($body, true);
  1045. $this->assertEquals('https://realize.be/', $data['data']['follow-of']);
  1046. $this->assertEquals('follow', $data['data']['post-type']);
  1047. }
  1048. public function testRelCanonical()
  1049. {
  1050. $url = 'http://source.example.com/rel-canonical';
  1051. $response = $this->parse(['url' => $url]);
  1052. $body = $response->getContent();
  1053. $this->assertEquals(200, $response->getStatusCode());
  1054. $data = json_decode($body, true);
  1055. $this->assertEquals('https://aaronparecki.com/2019/12/01/10/homeautomation', $data['data']['url']);
  1056. $this->assertEquals('https://aaronparecki.com/2019/12/01/10/homeautomation', $data['data']['rels']['canonical']);
  1057. }
  1058. public function testTargetLinkOutsideHEntry()
  1059. {
  1060. $url = 'http://source.example.com/target-test-link-outside-h-entry';
  1061. $response = $this->parse(['url' => $url, 'target' => 'https://target.example.com/']);
  1062. $body = $response->getContent();
  1063. $this->assertEquals(200, $response->getStatusCode());
  1064. $data = json_decode($body, true);
  1065. $this->assertEquals('no_link_found', $data['error']);
  1066. }
  1067. public function testTargetLinkWithBadMf1()
  1068. {
  1069. $url = 'http://source.example.com/target-test-only-bad-mf1';
  1070. $response = $this->parse(['url' => $url, 'target' => 'https://target.example.com/']);
  1071. $body = $response->getContent();
  1072. $this->assertEquals(200, $response->getStatusCode());
  1073. $data = json_decode($body, true);
  1074. $this->assertEquals('unknown', $data['data']['type']);
  1075. }
  1076. public function testTargetLinkWithValidMf1()
  1077. {
  1078. $url = 'http://source.example.com/target-test-only-good-mf1';
  1079. $response = $this->parse(['url' => $url, 'target' => 'https://target.example.com/']);
  1080. $body = $response->getContent();
  1081. $this->assertEquals(200, $response->getStatusCode());
  1082. $data = json_decode($body, true);
  1083. $this->assertEquals('entry', $data['data']['type']);
  1084. $this->assertEquals('<a href="https://target.example.com/">target</a>', $data['data']['content']['html']);
  1085. }
  1086. public function testTargetLinkOutsideValidMf1()
  1087. {
  1088. $url = 'http://source.example.com/target-test-link-outside-valid-mf1';
  1089. $response = $this->parse(['url' => $url, 'target' => 'https://target.example.com/']);
  1090. $body = $response->getContent();
  1091. $this->assertEquals(200, $response->getStatusCode());
  1092. $data = json_decode($body, true);
  1093. // Since the link was found in the HTML, but not in the parsed tree, it shouldn't return the parsed document
  1094. $this->assertEquals('unknown', $data['data']['type']);
  1095. }
  1096. public function testDisableMf1Parsing()
  1097. {
  1098. $url = 'http://source.example.com/target-test-only-good-mf1';
  1099. $response = $this->parse(['url' => $url, 'include-mf1' => 'false']);
  1100. $body = $response->getContent();
  1101. $this->assertEquals(200, $response->getStatusCode());
  1102. $data = json_decode($body, true);
  1103. $this->assertEquals('unknown', $data['data']['type']);
  1104. }
  1105. public function testEnableMf1Parsing()
  1106. {
  1107. $url = 'http://source.example.com/target-test-only-good-mf1';
  1108. $response = $this->parse(['url' => $url, 'include-mf1' => 'true']);
  1109. $body = $response->getContent();
  1110. $this->assertEquals(200, $response->getStatusCode());
  1111. $data = json_decode($body, true);
  1112. $this->assertEquals('entry', $data['data']['type']);
  1113. }
  1114. public function testMissingContent() {
  1115. $url = 'http://source.example.com/bookmark-missing-content';
  1116. $response = $this->parse(['url' => $url]);
  1117. $body = $response->getContent();
  1118. $this->assertEquals(200, $response->getStatusCode());
  1119. $data = json_decode($body, true);
  1120. $this->assertArrayNotHasKey('content', $data['data']);
  1121. }
  1122. }