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.

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