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.

1212 lines
52 KiB

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