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.

465 lines
21 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 testNoHEntryMarkup() {
  128. $url = 'http://source.example.com/no-h-entry';
  129. $response = $this->parse(['url' => $url]);
  130. $body = $response->getContent();
  131. $this->assertEquals(200, $response->getStatusCode());
  132. $data = json_decode($body);
  133. $this->assertEquals('unknown', $data->data->type);
  134. }
  135. public function testReplyIsURL() {
  136. $url = 'http://source.example.com/reply-is-url';
  137. $response = $this->parse(['url' => $url]);
  138. $body = $response->getContent();
  139. $this->assertEquals(200, $response->getStatusCode());
  140. $data = json_decode($body, true);
  141. $this->assertEquals('entry', $data['data']['type']);
  142. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  143. }
  144. public function testReplyIsHCite() {
  145. $url = 'http://source.example.com/reply-is-h-cite';
  146. $response = $this->parse(['url' => $url]);
  147. $body = $response->getContent();
  148. $this->assertEquals(200, $response->getStatusCode());
  149. $data = json_decode($body, true);
  150. $this->assertEquals('entry', $data['data']['type']);
  151. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  152. $this->assertArrayHasKey('http://example.com/100', $data['refs']);
  153. $this->assertEquals('Example Post', $data['refs']['http://example.com/100']['name']);
  154. $this->assertEquals('http://example.com/100', $data['refs']['http://example.com/100']['url']);
  155. }
  156. public function testPersonTagIsURL() {
  157. $url = 'http://source.example.com/person-tag-is-url';
  158. $response = $this->parse(['url' => $url]);
  159. $body = $response->getContent();
  160. $this->assertEquals(200, $response->getStatusCode());
  161. $data = json_decode($body, true);
  162. $this->assertEquals('entry', $data['data']['type']);
  163. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  164. }
  165. public function testPersonTagIsHCard() {
  166. $url = 'http://source.example.com/person-tag-is-h-card';
  167. $response = $this->parse(['url' => $url]);
  168. $body = $response->getContent();
  169. $this->assertEquals(200, $response->getStatusCode());
  170. $data = json_decode($body, true);
  171. $this->assertEquals('entry', $data['data']['type']);
  172. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  173. $this->assertArrayHasKey('http://alice.example.com/', $data['refs']);
  174. $this->assertEquals('card', $data['refs']['http://alice.example.com/']['type']);
  175. $this->assertEquals('http://alice.example.com/', $data['refs']['http://alice.example.com/']['url']);
  176. $this->assertEquals('Alice', $data['refs']['http://alice.example.com/']['name']);
  177. }
  178. public function testSyndicationIsURL() {
  179. $url = 'http://source.example.com/has-syndication';
  180. $response = $this->parse(['url' => $url]);
  181. $body = $response->getContent();
  182. $this->assertEquals(200, $response->getStatusCode());
  183. $data = json_decode($body, true);
  184. $this->assertEquals('entry', $data['data']['type']);
  185. $this->assertEquals('http://syndicated.example/', $data['data']['syndication'][0]);
  186. }
  187. public function testHEntryIsNotFirstObject() {
  188. $url = 'http://source.example.com/h-entry-is-not-first';
  189. $response = $this->parse(['url' => $url]);
  190. $body = $response->getContent();
  191. $this->assertEquals(200, $response->getStatusCode());
  192. $data = json_decode($body, true);
  193. $this->assertEquals('entry', $data['data']['type']);
  194. $this->assertEquals('Hello World', $data['data']['content']['text']);
  195. }
  196. public function testHEntryRSVP() {
  197. $url = 'http://source.example.com/h-entry-rsvp';
  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('I\'ll be there!', $data['data']['name']);
  204. $this->assertEquals('yes', $data['data']['rsvp']);
  205. }
  206. public function testMultipleHEntryOnPermalink() {
  207. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  208. $response = $this->parse(['url' => $url]);
  209. $body = $response->getContent();
  210. $this->assertEquals(200, $response->getStatusCode());
  211. $data = json_decode($body, true);
  212. $this->assertEquals('entry', $data['data']['type']);
  213. $this->assertEquals('Primary Post', $data['data']['name']);
  214. }
  215. public function testHEntryWithHCardSibling() {
  216. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  217. $response = $this->parse(['url' => $url]);
  218. $body = $response->getContent();
  219. $this->assertEquals(200, $response->getStatusCode());
  220. $data = json_decode($body, true);
  221. $this->assertEquals('entry', $data['data']['type']);
  222. $this->assertEquals('Hello World', $data['data']['content']['text']);
  223. }
  224. public function testHEntryRedirectWithHCardSibling() {
  225. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  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('Hello World', $data['data']['content']['text']);
  232. }
  233. public function testSingleHEntryHasNoPermalink() {
  234. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  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 testBridgyExampleWithNoMatchingURL() {
  243. $url = 'http://source.example.com/bridgy-example';
  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. }
  250. public function testEventWithHTMLDescription() {
  251. $url = 'http://source.example.com/h-event';
  252. $response = $this->parse(['url' => $url]);
  253. $body = $response->getContent();
  254. $this->assertEquals(200, $response->getStatusCode());
  255. $data = json_decode($body, true);
  256. $this->assertEquals('event', $data['data']['type']);
  257. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  258. $this->assertEquals($url, $data['data']['url']);
  259. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  260. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  261. $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']);
  262. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  263. $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']);
  264. $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']);
  265. }
  266. public function testEventWithTextDescription() {
  267. $url = 'http://source.example.com/h-event-text-description';
  268. $response = $this->parse(['url' => $url]);
  269. $body = $response->getContent();
  270. $this->assertEquals(200, $response->getStatusCode());
  271. $data = json_decode($body, true);
  272. $this->assertEquals('event', $data['data']['type']);
  273. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  274. $this->assertEquals($url, $data['data']['url']);
  275. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  276. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  277. $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']);
  278. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  279. $this->assertArrayNotHasKey('html', $data['data']['description']);
  280. }
  281. public function testEventWithHCardLocation() {
  282. $url = 'http://source.example.com/h-event-with-h-card-location';
  283. $response = $this->parse(['url' => $url]);
  284. $body = $response->getContent();
  285. $this->assertEquals(200, $response->getStatusCode());
  286. $data = json_decode($body, true);
  287. $this->assertEquals('event', $data['data']['type']);
  288. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  289. $this->assertEquals($url, $data['data']['url']);
  290. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  291. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  292. $this->assertArrayHasKey('http://source.example.com/venue', $data['refs']);
  293. $this->assertEquals('card', $data['refs']['http://source.example.com/venue']['type']);
  294. $this->assertEquals('http://source.example.com/venue', $data['refs']['http://source.example.com/venue']['url']);
  295. $this->assertEquals('Venue', $data['refs']['http://source.example.com/venue']['name']);
  296. }
  297. public function testMf2ReviewOfProduct() {
  298. $url = 'http://source.example.com/h-review-of-product';
  299. $response = $this->parse(['url' => $url]);
  300. $body = $response->getContent();
  301. $this->assertEquals(200, $response->getStatusCode());
  302. $data = json_decode($body, true);
  303. $this->assertEquals('review', $data['data']['type']);
  304. $this->assertEquals('Review', $data['data']['name']);
  305. $this->assertEquals('Not great', $data['data']['summary']);
  306. $this->assertEquals('3', $data['data']['rating']);
  307. $this->assertEquals('5', $data['data']['best']);
  308. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  309. $this->assertContains('red', $data['data']['category']);
  310. $this->assertContains('blue', $data['data']['category']);
  311. $this->assertContains('http://product.example.com/', $data['data']['item']);
  312. $this->assertArrayHasKey('http://product.example.com/', $data['refs']);
  313. $this->assertEquals('product', $data['refs']['http://product.example.com/']['type']);
  314. $this->assertEquals('The Reviewed Product', $data['refs']['http://product.example.com/']['name']);
  315. $this->assertEquals('http://product.example.com/', $data['refs']['http://product.example.com/']['url']);
  316. }
  317. public function testMf2ReviewOfHCard() {
  318. $url = 'http://source.example.com/h-review-of-h-card';
  319. $response = $this->parse(['url' => $url]);
  320. $body = $response->getContent();
  321. $this->assertEquals(200, $response->getStatusCode());
  322. $data = json_decode($body, true);
  323. $this->assertEquals('review', $data['data']['type']);
  324. $this->assertEquals('Review', $data['data']['name']);
  325. $this->assertEquals('Not great', $data['data']['summary']);
  326. $this->assertEquals('3', $data['data']['rating']);
  327. $this->assertEquals('5', $data['data']['best']);
  328. $this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  329. $this->assertContains('http://business.example.com/', $data['data']['item']);
  330. $this->assertArrayHasKey('http://business.example.com/', $data['refs']);
  331. $this->assertEquals('card', $data['refs']['http://business.example.com/']['type']);
  332. $this->assertEquals('The Reviewed Business', $data['refs']['http://business.example.com/']['name']);
  333. $this->assertEquals('http://business.example.com/', $data['refs']['http://business.example.com/']['url']);
  334. }
  335. public function testMf1Review() {
  336. $url = 'http://source.example.com/hReview';
  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. # TODO: backcompat of mf1 parser is kind of messed up right now
  344. #$this->assertEquals('Not great', $data['data']['summary']);
  345. $this->assertEquals('3', $data['data']['rating']);
  346. $this->assertEquals('5', $data['data']['best']);
  347. #$this->assertEquals('This is the full text of the review', $data['data']['content']['text']);
  348. // $this->assertContains('http://product.example.com/', $data['data']['item']);
  349. // $this->assertArrayHasKey('http://product.example.com/', $data['refs']);
  350. // $this->assertEquals('product', $data['refs']['http://product.example.com/']['type']);
  351. // $this->assertEquals('The Reviewed Product', $data['refs']['http://product.example.com/']['name']);
  352. // $this->assertEquals('http://product.example.com/', $data['refs']['http://product.example.com/']['url']);
  353. }
  354. public function testEntryIsAnInvitee() {
  355. $url = 'http://source.example.com/bridgy-invitee';
  356. $response = $this->parse(['url' => $url]);
  357. $body = $response->getContent();
  358. $this->assertEquals(200, $response->getStatusCode());
  359. $data = json_decode($body, true);
  360. $this->assertEquals('entry', $data['data']['type']);
  361. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  362. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  363. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['refs']);
  364. $this->assertEquals('Tantek Çelik', $data['refs']['https://www.facebook.com/tantek.celik']['name']);
  365. }
  366. public function testEntryAtFragmentID() {
  367. $url = 'http://source.example.com/fragment-id#comment-1000';
  368. $response = $this->parse(['url' => $url]);
  369. $body = $response->getContent();
  370. $this->assertEquals(200, $response->getStatusCode());
  371. $data = json_decode($body, true);
  372. $this->assertEquals('entry', $data['data']['type']);
  373. $this->assertEquals('http://source.example.com/fragment-id#comment-1000', $data['data']['url']);
  374. $this->assertTrue($data['info']['found_fragment']);
  375. }
  376. public function testEntryAtNonExistentFragmentID() {
  377. $url = 'http://source.example.com/fragment-id#comment-404';
  378. $response = $this->parse(['url' => $url]);
  379. $body = $response->getContent();
  380. $this->assertEquals(200, $response->getStatusCode());
  381. $data = json_decode($body, true);
  382. $this->assertEquals('entry', $data['data']['type']);
  383. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  384. $this->assertFalse($data['info']['found_fragment']);
  385. }
  386. }