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.

504 lines
23 KiB

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\HTTPTest(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 testFindTargetLinkIsImage() {
  63. $url = 'http://source.example.com/link-is-img';
  64. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/photo.jpg']);
  65. $body = $response->getContent();
  66. $this->assertEquals(200, $response->getStatusCode());
  67. $data = json_decode($body);
  68. $this->assertObjectNotHasAttribute('name', $data->data);
  69. $this->assertEquals('This page has an img tag with the target URL.', $data->data->content->text);
  70. }
  71. public function testFindTargetLinkIsVideo() {
  72. $url = 'http://source.example.com/link-is-video';
  73. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/movie.mp4']);
  74. $body = $response->getContent();
  75. $this->assertEquals(200, $response->getStatusCode());
  76. $data = json_decode($body);
  77. $this->assertObjectNotHasAttribute('name', $data->data);
  78. $this->assertEquals('This page has a video tag with the target URL.', $data->data->content->text);
  79. }
  80. public function testFindTargetLinkIsAudio() {
  81. $url = 'http://source.example.com/link-is-audio';
  82. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/media.mp3']);
  83. $body = $response->getContent();
  84. $this->assertEquals(200, $response->getStatusCode());
  85. $data = json_decode($body);
  86. $this->assertObjectNotHasAttribute('name', $data->data);
  87. $this->assertEquals('This page has an audio tag with the target URL.', $data->data->content->text);
  88. }
  89. public function testTextContent() {
  90. $url = 'http://source.example.com/text-content';
  91. $response = $this->parse(['url' => $url]);
  92. $body = $response->getContent();
  93. $this->assertEquals(200, $response->getStatusCode());
  94. $data = json_decode($body);
  95. $this->assertObjectNotHasAttribute('name', $data->data);
  96. $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);
  97. }
  98. public function testContentWithPrefixedName() {
  99. $url = 'http://source.example.com/content-with-prefixed-name';
  100. $response = $this->parse(['url' => $url]);
  101. $body = $response->getContent();
  102. $this->assertEquals(200, $response->getStatusCode());
  103. $data = json_decode($body);
  104. $this->assertObjectNotHasAttribute('name', $data->data);
  105. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  106. $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);
  107. }
  108. public function testContentWithDistinctName() {
  109. $url = 'http://source.example.com/content-with-distinct-name';
  110. $response = $this->parse(['url' => $url]);
  111. $body = $response->getContent();
  112. $this->assertEquals(200, $response->getStatusCode());
  113. $data = json_decode($body);
  114. $this->assertEquals('Hello World', $data->data->name);
  115. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  116. $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);
  117. }
  118. public function testNameWithNoContent() {
  119. $url = 'http://source.example.com/name-no-content';
  120. $response = $this->parse(['url' => $url]);
  121. $body = $response->getContent();
  122. $this->assertEquals(200, $response->getStatusCode());
  123. $data = json_decode($body);
  124. $this->assertEquals('Hello World', $data->data->name);
  125. $this->assertObjectNotHasAttribute('content', $data->data);
  126. }
  127. public function testEntryWithDuplicateCategories() {
  128. $url = 'http://source.example.com/h-entry-duplicate-categories';
  129. $response = $this->parse(['url' => $url]);
  130. $body = $response->getContent();
  131. $this->assertEquals(200, $response->getStatusCode());
  132. $data = json_decode($body);
  133. $this->assertEquals(['indieweb'], $data->data->category);
  134. }
  135. public function testEntryStripHashtagWithDuplicateCategories() {
  136. $url = 'http://source.example.com/h-entry-strip-hashtag-from-categories';
  137. $response = $this->parse(['url' => $url]);
  138. $body = $response->getContent();
  139. $this->assertEquals(200, $response->getStatusCode());
  140. $data = json_decode($body);
  141. $this->assertContains('indieweb', $data->data->category);
  142. $this->assertContains('xray', $data->data->category);
  143. $this->assertEquals(2, count($data->data->category));
  144. }
  145. public function testNoHEntryMarkup() {
  146. $url = 'http://source.example.com/no-h-entry';
  147. $response = $this->parse(['url' => $url]);
  148. $body = $response->getContent();
  149. $this->assertEquals(200, $response->getStatusCode());
  150. $data = json_decode($body);
  151. $this->assertEquals('unknown', $data->data->type);
  152. }
  153. public function testReplyIsURL() {
  154. $url = 'http://source.example.com/reply-is-url';
  155. $response = $this->parse(['url' => $url]);
  156. $body = $response->getContent();
  157. $this->assertEquals(200, $response->getStatusCode());
  158. $data = json_decode($body, true);
  159. $this->assertEquals('entry', $data['data']['type']);
  160. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  161. }
  162. public function testReplyIsHCite() {
  163. $url = 'http://source.example.com/reply-is-h-cite';
  164. $response = $this->parse(['url' => $url]);
  165. $body = $response->getContent();
  166. $this->assertEquals(200, $response->getStatusCode());
  167. $data = json_decode($body, true);
  168. $this->assertEquals('entry', $data['data']['type']);
  169. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  170. $this->assertArrayHasKey('http://example.com/100', $data['refs']);
  171. $this->assertEquals('Example Post', $data['refs']['http://example.com/100']['name']);
  172. $this->assertEquals('http://example.com/100', $data['refs']['http://example.com/100']['url']);
  173. }
  174. public function testPersonTagIsURL() {
  175. $url = 'http://source.example.com/person-tag-is-url';
  176. $response = $this->parse(['url' => $url]);
  177. $body = $response->getContent();
  178. $this->assertEquals(200, $response->getStatusCode());
  179. $data = json_decode($body, true);
  180. $this->assertEquals('entry', $data['data']['type']);
  181. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  182. }
  183. public function testPersonTagIsHCard() {
  184. $url = 'http://source.example.com/person-tag-is-h-card';
  185. $response = $this->parse(['url' => $url]);
  186. $body = $response->getContent();
  187. $this->assertEquals(200, $response->getStatusCode());
  188. $data = json_decode($body, true);
  189. $this->assertEquals('entry', $data['data']['type']);
  190. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  191. $this->assertArrayHasKey('http://alice.example.com/', $data['refs']);
  192. $this->assertEquals('card', $data['refs']['http://alice.example.com/']['type']);
  193. $this->assertEquals('http://alice.example.com/', $data['refs']['http://alice.example.com/']['url']);
  194. $this->assertEquals('Alice', $data['refs']['http://alice.example.com/']['name']);
  195. }
  196. public function testSyndicationIsURL() {
  197. $url = 'http://source.example.com/has-syndication';
  198. $response = $this->parse(['url' => $url]);
  199. $body = $response->getContent();
  200. $this->assertEquals(200, $response->getStatusCode());
  201. $data = json_decode($body, true);
  202. $this->assertEquals('entry', $data['data']['type']);
  203. $this->assertEquals('http://syndicated.example/', $data['data']['syndication'][0]);
  204. }
  205. public function testHEntryIsNotFirstObject() {
  206. $url = 'http://source.example.com/h-entry-is-not-first';
  207. $response = $this->parse(['url' => $url]);
  208. $body = $response->getContent();
  209. $this->assertEquals(200, $response->getStatusCode());
  210. $data = json_decode($body, true);
  211. $this->assertEquals('entry', $data['data']['type']);
  212. $this->assertEquals('Hello World', $data['data']['content']['text']);
  213. }
  214. public function testHEntryRSVP() {
  215. $url = 'http://source.example.com/h-entry-rsvp';
  216. $response = $this->parse(['url' => $url]);
  217. $body = $response->getContent();
  218. $this->assertEquals(200, $response->getStatusCode());
  219. $data = json_decode($body, true);
  220. $this->assertEquals('entry', $data['data']['type']);
  221. $this->assertEquals('I\'ll be there!', $data['data']['name']);
  222. $this->assertEquals('yes', $data['data']['rsvp']);
  223. }
  224. public function testMultipleHEntryOnPermalink() {
  225. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  226. $response = $this->parse(['url' => $url]);
  227. $body = $response->getContent();
  228. $this->assertEquals(200, $response->getStatusCode());
  229. $data = json_decode($body, true);
  230. $this->assertEquals('entry', $data['data']['type']);
  231. $this->assertEquals('Primary Post', $data['data']['name']);
  232. }
  233. public function testHEntryWithHCardSibling() {
  234. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  235. $response = $this->parse(['url' => $url]);
  236. $body = $response->getContent();
  237. $this->assertEquals(200, $response->getStatusCode());
  238. $data = json_decode($body, true);
  239. $this->assertEquals('entry', $data['data']['type']);
  240. $this->assertEquals('Hello World', $data['data']['content']['text']);
  241. }
  242. public function testHEntryRedirectWithHCardSibling() {
  243. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  244. $response = $this->parse(['url' => $url]);
  245. $body = $response->getContent();
  246. $this->assertEquals(200, $response->getStatusCode());
  247. $data = json_decode($body, true);
  248. $this->assertEquals('entry', $data['data']['type']);
  249. $this->assertEquals('Hello World', $data['data']['content']['text']);
  250. }
  251. public function testSingleHEntryHasNoPermalink() {
  252. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  253. $response = $this->parse(['url' => $url]);
  254. $body = $response->getContent();
  255. $this->assertEquals(200, $response->getStatusCode());
  256. $data = json_decode($body, true);
  257. $this->assertEquals('entry', $data['data']['type']);
  258. $this->assertEquals('Hello World', $data['data']['content']['text']);
  259. }
  260. public function testBridgyExampleWithNoMatchingURL() {
  261. $url = 'http://source.example.com/bridgy-example';
  262. $response = $this->parse(['url' => $url]);
  263. $body = $response->getContent();
  264. $this->assertEquals(200, $response->getStatusCode());
  265. $data = json_decode($body, true);
  266. $this->assertEquals('entry', $data['data']['type']);
  267. }
  268. public function testEventWithHTMLDescription() {
  269. $url = 'http://source.example.com/h-event';
  270. $response = $this->parse(['url' => $url]);
  271. $body = $response->getContent();
  272. $this->assertEquals(200, $response->getStatusCode());
  273. $data = json_decode($body, true);
  274. $this->assertEquals('event', $data['data']['type']);
  275. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  276. $this->assertEquals($url, $data['data']['url']);
  277. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  278. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  279. $this->assertStringStartsWith("Are you building your own website? Indie reader? Personal publishing web app? Or some other digital magic-cloud proxy? If so, come on by and join a gathering of people with likeminded interests. Bring your friends that want to start a personal web site. Exchange information, swap ideas, talk shop, help work on a project...", $data['data']['description']['text']);
  280. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  281. $this->assertStringStartsWith("<p>Are you building your own website? Indie reader? Personal publishing web app? Or some other digital magic-cloud proxy? If so, come on by and join a gathering of people with likeminded interests. Bring your friends that want to start a personal web site. Exchange information, swap ideas, talk shop, help work on a project...</p>", $data['data']['description']['html']);
  282. $this->assertStringEndsWith('<p>See the <a href="http://tantek.com/2013/332/b1/homebrew-website-club-newsletter">Homebrew Website Club Newsletter Volume 1 Issue 1</a> for a description of the first meeting.</p>', $data['data']['description']['html']);
  283. }
  284. public function testEventWithTextDescription() {
  285. $url = 'http://source.example.com/h-event-text-description';
  286. $response = $this->parse(['url' => $url]);
  287. $body = $response->getContent();
  288. $this->assertEquals(200, $response->getStatusCode());
  289. $data = json_decode($body, true);
  290. $this->assertEquals('event', $data['data']['type']);
  291. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  292. $this->assertEquals($url, $data['data']['url']);
  293. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  294. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  295. $this->assertStringStartsWith("Are you building your own website? Indie reader? Personal publishing web app? Or some other digital magic-cloud proxy? If so, come on by and join a gathering of people with likeminded interests. Bring your friends that want to start a personal web site. Exchange information, swap ideas, talk shop, help work on a project...", $data['data']['description']['text']);
  296. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  297. $this->assertArrayNotHasKey('html', $data['data']['description']);
  298. }
  299. public function testEventWithHCardLocation() {
  300. $url = 'http://source.example.com/h-event-with-h-card-location';
  301. $response = $this->parse(['url' => $url]);
  302. $body = $response->getContent();
  303. $this->assertEquals(200, $response->getStatusCode());
  304. $data = json_decode($body, true);
  305. $this->assertEquals('event', $data['data']['type']);
  306. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  307. $this->assertEquals($url, $data['data']['url']);
  308. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  309. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  310. $this->assertArrayHasKey('http://source.example.com/venue', $data['refs']);
  311. $this->assertEquals('card', $data['refs']['http://source.example.com/venue']['type']);
  312. $this->assertEquals('http://source.example.com/venue', $data['refs']['http://source.example.com/venue']['url']);
  313. $this->assertEquals('Venue', $data['refs']['http://source.example.com/venue']['name']);
  314. }
  315. public function testMf2ReviewOfProduct() {
  316. $url = 'http://source.example.com/h-review-of-product';
  317. $response = $this->parse(['url' => $url]);
  318. $body = $response->getContent();
  319. $this->assertEquals(200, $response->getStatusCode());
  320. $data = json_decode($body, true);
  321. $this->assertEquals('review', $data['data']['type']);
  322. $this->assertEquals('Review', $data['data']['name']);
  323. $this->assertEquals('Not great', $data['data']['summary']);
  324. $this->assertEquals('3', $data['data']['rating']);
  325. $this->assertEquals('5', $data['data']['best']);
  326. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  327. $this->assertContains('red', $data['data']['category']);
  328. $this->assertContains('blue', $data['data']['category']);
  329. $this->assertContains('http://product.example.com/', $data['data']['item']);
  330. $this->assertArrayHasKey('http://product.example.com/', $data['refs']);
  331. $this->assertEquals('product', $data['refs']['http://product.example.com/']['type']);
  332. $this->assertEquals('The Reviewed Product', $data['refs']['http://product.example.com/']['name']);
  333. $this->assertEquals('http://product.example.com/', $data['refs']['http://product.example.com/']['url']);
  334. }
  335. public function testMf2ReviewOfHCard() {
  336. $url = 'http://source.example.com/h-review-of-h-card';
  337. $response = $this->parse(['url' => $url]);
  338. $body = $response->getContent();
  339. $this->assertEquals(200, $response->getStatusCode());
  340. $data = json_decode($body, true);
  341. $this->assertEquals('review', $data['data']['type']);
  342. $this->assertEquals('Review', $data['data']['name']);
  343. $this->assertEquals('Not great', $data['data']['summary']);
  344. $this->assertEquals('3', $data['data']['rating']);
  345. $this->assertEquals('5', $data['data']['best']);
  346. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  347. $this->assertContains('http://business.example.com/', $data['data']['item']);
  348. $this->assertArrayHasKey('http://business.example.com/', $data['refs']);
  349. $this->assertEquals('card', $data['refs']['http://business.example.com/']['type']);
  350. $this->assertEquals('The Reviewed Business', $data['refs']['http://business.example.com/']['name']);
  351. $this->assertEquals('http://business.example.com/', $data['refs']['http://business.example.com/']['url']);
  352. }
  353. public function testMf1Review() {
  354. $url = 'http://source.example.com/hReview';
  355. $response = $this->parse(['url' => $url]);
  356. $body = $response->getContent();
  357. $this->assertEquals(200, $response->getStatusCode());
  358. $data = json_decode($body, true);
  359. $this->assertEquals('review', $data['data']['type']);
  360. $this->assertEquals('Review', $data['data']['name']);
  361. # TODO: backcompat of mf1 parser is kind of messed up right now
  362. #$this->assertEquals('Not great', $data['data']['summary']);
  363. $this->assertEquals('3', $data['data']['rating']);
  364. $this->assertEquals('5', $data['data']['best']);
  365. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  366. // $this->assertContains('http://product.example.com/', $data['data']['item']);
  367. // $this->assertArrayHasKey('http://product.example.com/', $data['refs']);
  368. // $this->assertEquals('product', $data['refs']['http://product.example.com/']['type']);
  369. // $this->assertEquals('The Reviewed Product', $data['refs']['http://product.example.com/']['name']);
  370. // $this->assertEquals('http://product.example.com/', $data['refs']['http://product.example.com/']['url']);
  371. }
  372. public function testMf2Recipe() {
  373. $url = 'http://source.example.com/h-recipe';
  374. $response = $this->parse(['url' => $url]);
  375. $body = $response->getContent();
  376. $this->assertEquals(200, $response->getStatusCode());
  377. $data = json_decode($body, true);
  378. $this->assertEquals('recipe', $data['data']['type']);
  379. $this->assertEquals('Cookie Recipe', $data['data']['name']);
  380. $this->assertEquals('12 Cookies', $data['data']['yield']);
  381. $this->assertEquals('PT30M', $data['data']['duration']);
  382. $this->assertEquals('The best chocolate chip cookie recipe', $data['data']['summary']);
  383. $this->assertContains('3 cups flour', $data['data']['ingredient']);
  384. $this->assertContains('chocolate chips', $data['data']['ingredient']);
  385. }
  386. public function testEntryIsAnInvitee() {
  387. $url = 'http://source.example.com/bridgy-invitee';
  388. $response = $this->parse(['url' => $url]);
  389. $body = $response->getContent();
  390. $this->assertEquals(200, $response->getStatusCode());
  391. $data = json_decode($body, true);
  392. $this->assertEquals('entry', $data['data']['type']);
  393. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  394. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  395. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['refs']);
  396. $this->assertEquals('Tantek Çelik', $data['refs']['https://www.facebook.com/tantek.celik']['name']);
  397. }
  398. public function testEntryAtFragmentID() {
  399. $url = 'http://source.example.com/fragment-id#comment-1000';
  400. $response = $this->parse(['url' => $url]);
  401. $body = $response->getContent();
  402. $this->assertEquals(200, $response->getStatusCode());
  403. $data = json_decode($body, true);
  404. $this->assertEquals('entry', $data['data']['type']);
  405. $this->assertEquals('http://source.example.com/fragment-id#comment-1000', $data['data']['url']);
  406. $this->assertTrue($data['info']['found_fragment']);
  407. }
  408. public function testEntryAtNonExistentFragmentID() {
  409. $url = 'http://source.example.com/fragment-id#comment-404';
  410. $response = $this->parse(['url' => $url]);
  411. $body = $response->getContent();
  412. $this->assertEquals(200, $response->getStatusCode());
  413. $data = json_decode($body, true);
  414. $this->assertEquals('entry', $data['data']['type']);
  415. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  416. $this->assertFalse($data['info']['found_fragment']);
  417. }
  418. }