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.

385 lines
17 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. }
  42. public function testTargetFound() {
  43. $url = 'http://source.example.com/basictest';
  44. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com']);
  45. $body = $response->getContent();
  46. $this->assertEquals(200, $response->getStatusCode());
  47. $data = json_decode($body);
  48. $this->assertObjectNotHasAttribute('error', $data);
  49. $this->assertObjectNotHasAttribute('error', $data);
  50. }
  51. public function testHTMLContent() {
  52. $url = 'http://source.example.com/html-content';
  53. $response = $this->parse(['url' => $url]);
  54. $body = $response->getContent();
  55. $this->assertEquals(200, $response->getStatusCode());
  56. $data = json_decode($body);
  57. $this->assertObjectNotHasAttribute('name', $data->data);
  58. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  59. $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);
  60. }
  61. public function testFindTargetLinkIsImage() {
  62. $url = 'http://source.example.com/link-is-img';
  63. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/photo.jpg']);
  64. $body = $response->getContent();
  65. $this->assertEquals(200, $response->getStatusCode());
  66. $data = json_decode($body);
  67. $this->assertObjectNotHasAttribute('name', $data->data);
  68. $this->assertEquals('This page has an img tag with the target URL.', $data->data->content->text);
  69. }
  70. public function testFindTargetLinkIsVideo() {
  71. $url = 'http://source.example.com/link-is-video';
  72. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/movie.mp4']);
  73. $body = $response->getContent();
  74. $this->assertEquals(200, $response->getStatusCode());
  75. $data = json_decode($body);
  76. $this->assertObjectNotHasAttribute('name', $data->data);
  77. $this->assertEquals('This page has a video tag with the target URL.', $data->data->content->text);
  78. }
  79. public function testFindTargetLinkIsAudio() {
  80. $url = 'http://source.example.com/link-is-audio';
  81. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com/media.mp3']);
  82. $body = $response->getContent();
  83. $this->assertEquals(200, $response->getStatusCode());
  84. $data = json_decode($body);
  85. $this->assertObjectNotHasAttribute('name', $data->data);
  86. $this->assertEquals('This page has an audio tag with the target URL.', $data->data->content->text);
  87. }
  88. public function testTextContent() {
  89. $url = 'http://source.example.com/text-content';
  90. $response = $this->parse(['url' => $url]);
  91. $body = $response->getContent();
  92. $this->assertEquals(200, $response->getStatusCode());
  93. $data = json_decode($body);
  94. $this->assertObjectNotHasAttribute('name', $data->data);
  95. $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);
  96. }
  97. public function testContentWithPrefixedName() {
  98. $url = 'http://source.example.com/content-with-prefixed-name';
  99. $response = $this->parse(['url' => $url]);
  100. $body = $response->getContent();
  101. $this->assertEquals(200, $response->getStatusCode());
  102. $data = json_decode($body);
  103. $this->assertObjectNotHasAttribute('name', $data->data);
  104. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  105. $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);
  106. }
  107. public function testContentWithDistinctName() {
  108. $url = 'http://source.example.com/content-with-distinct-name';
  109. $response = $this->parse(['url' => $url]);
  110. $body = $response->getContent();
  111. $this->assertEquals(200, $response->getStatusCode());
  112. $data = json_decode($body);
  113. $this->assertEquals('Hello World', $data->data->name);
  114. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  115. $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);
  116. }
  117. public function testNameWithNoContent() {
  118. $url = 'http://source.example.com/name-no-content';
  119. $response = $this->parse(['url' => $url]);
  120. $body = $response->getContent();
  121. $this->assertEquals(200, $response->getStatusCode());
  122. $data = json_decode($body);
  123. $this->assertEquals('Hello World', $data->data->name);
  124. $this->assertObjectNotHasAttribute('content', $data->data);
  125. }
  126. public function testNoHEntryMarkup() {
  127. $url = 'http://source.example.com/no-h-entry';
  128. $response = $this->parse(['url' => $url]);
  129. $body = $response->getContent();
  130. $this->assertEquals(200, $response->getStatusCode());
  131. $data = json_decode($body);
  132. $this->assertEquals('unknown', $data->data->type);
  133. }
  134. public function testReplyIsURL() {
  135. $url = 'http://source.example.com/reply-is-url';
  136. $response = $this->parse(['url' => $url]);
  137. $body = $response->getContent();
  138. $this->assertEquals(200, $response->getStatusCode());
  139. $data = json_decode($body, true);
  140. $this->assertEquals('entry', $data['data']['type']);
  141. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  142. }
  143. public function testReplyIsHCite() {
  144. $url = 'http://source.example.com/reply-is-h-cite';
  145. $response = $this->parse(['url' => $url]);
  146. $body = $response->getContent();
  147. $this->assertEquals(200, $response->getStatusCode());
  148. $data = json_decode($body, true);
  149. $this->assertEquals('entry', $data['data']['type']);
  150. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  151. $this->assertArrayHasKey('http://example.com/100', $data['refs']);
  152. $this->assertEquals('Example Post', $data['refs']['http://example.com/100']['name']);
  153. $this->assertEquals('http://example.com/100', $data['refs']['http://example.com/100']['url']);
  154. }
  155. public function testPersonTagIsURL() {
  156. $url = 'http://source.example.com/person-tag-is-url';
  157. $response = $this->parse(['url' => $url]);
  158. $body = $response->getContent();
  159. $this->assertEquals(200, $response->getStatusCode());
  160. $data = json_decode($body, true);
  161. $this->assertEquals('entry', $data['data']['type']);
  162. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  163. }
  164. public function testPersonTagIsHCard() {
  165. $url = 'http://source.example.com/person-tag-is-h-card';
  166. $response = $this->parse(['url' => $url]);
  167. $body = $response->getContent();
  168. $this->assertEquals(200, $response->getStatusCode());
  169. $data = json_decode($body, true);
  170. $this->assertEquals('entry', $data['data']['type']);
  171. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  172. $this->assertArrayHasKey('http://alice.example.com/', $data['refs']);
  173. $this->assertEquals('card', $data['refs']['http://alice.example.com/']['type']);
  174. $this->assertEquals('http://alice.example.com/', $data['refs']['http://alice.example.com/']['url']);
  175. $this->assertEquals('Alice', $data['refs']['http://alice.example.com/']['name']);
  176. }
  177. public function testHEntryIsNotFirstObject() {
  178. $url = 'http://source.example.com/h-entry-is-not-first';
  179. $response = $this->parse(['url' => $url]);
  180. $body = $response->getContent();
  181. $this->assertEquals(200, $response->getStatusCode());
  182. $data = json_decode($body, true);
  183. $this->assertEquals('entry', $data['data']['type']);
  184. $this->assertEquals('Hello World', $data['data']['content']['text']);
  185. }
  186. public function testHEntryRSVP() {
  187. $url = 'http://source.example.com/h-entry-rsvp';
  188. $response = $this->parse(['url' => $url]);
  189. $body = $response->getContent();
  190. $this->assertEquals(200, $response->getStatusCode());
  191. $data = json_decode($body, true);
  192. $this->assertEquals('entry', $data['data']['type']);
  193. $this->assertEquals('I\'ll be there!', $data['data']['name']);
  194. $this->assertEquals('yes', $data['data']['rsvp']);
  195. }
  196. public function testMultipleHEntryOnPermalink() {
  197. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  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('Primary Post', $data['data']['name']);
  204. }
  205. public function testHEntryWithHCardSibling() {
  206. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  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 testHEntryRedirectWithHCardSibling() {
  215. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  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('Hello World', $data['data']['content']['text']);
  222. }
  223. public function testSingleHEntryHasNoPermalink() {
  224. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  225. $response = $this->parse(['url' => $url]);
  226. $body = $response->getContent();
  227. $this->assertEquals(200, $response->getStatusCode());
  228. $data = json_decode($body, true);
  229. $this->assertEquals('entry', $data['data']['type']);
  230. $this->assertEquals('Hello World', $data['data']['content']['text']);
  231. }
  232. public function testBridgyExampleWithNoMatchingURL() {
  233. $url = 'http://source.example.com/bridgy-example';
  234. $response = $this->parse(['url' => $url]);
  235. $body = $response->getContent();
  236. $this->assertEquals(200, $response->getStatusCode());
  237. $data = json_decode($body, true);
  238. $this->assertEquals('entry', $data['data']['type']);
  239. }
  240. public function testEventWithHTMLDescription() {
  241. $url = 'http://source.example.com/h-event';
  242. $response = $this->parse(['url' => $url]);
  243. $body = $response->getContent();
  244. $this->assertEquals(200, $response->getStatusCode());
  245. $data = json_decode($body, true);
  246. $this->assertEquals('event', $data['data']['type']);
  247. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  248. $this->assertEquals($url, $data['data']['url']);
  249. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  250. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  251. $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']);
  252. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  253. $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']);
  254. $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']);
  255. }
  256. public function testEventWithTextDescription() {
  257. $url = 'http://source.example.com/h-event-text-description';
  258. $response = $this->parse(['url' => $url]);
  259. $body = $response->getContent();
  260. $this->assertEquals(200, $response->getStatusCode());
  261. $data = json_decode($body, true);
  262. $this->assertEquals('event', $data['data']['type']);
  263. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  264. $this->assertEquals($url, $data['data']['url']);
  265. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  266. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  267. $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']);
  268. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  269. $this->assertArrayNotHasKey('html', $data['data']['description']);
  270. }
  271. public function testEventWithHCardLocation() {
  272. $url = 'http://source.example.com/h-event-with-h-card-location';
  273. $response = $this->parse(['url' => $url]);
  274. $body = $response->getContent();
  275. $this->assertEquals(200, $response->getStatusCode());
  276. $data = json_decode($body, true);
  277. $this->assertEquals('event', $data['data']['type']);
  278. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  279. $this->assertEquals($url, $data['data']['url']);
  280. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  281. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  282. $this->assertArrayHasKey('http://source.example.com/venue', $data['refs']);
  283. $this->assertEquals('card', $data['refs']['http://source.example.com/venue']['type']);
  284. $this->assertEquals('http://source.example.com/venue', $data['refs']['http://source.example.com/venue']['url']);
  285. $this->assertEquals('Venue', $data['refs']['http://source.example.com/venue']['name']);
  286. }
  287. public function testEntryIsAnInvitee() {
  288. $url = 'http://source.example.com/bridgy-invitee';
  289. $response = $this->parse(['url' => $url]);
  290. $body = $response->getContent();
  291. $this->assertEquals(200, $response->getStatusCode());
  292. $data = json_decode($body, true);
  293. $this->assertEquals('entry', $data['data']['type']);
  294. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  295. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  296. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['refs']);
  297. $this->assertEquals('Tantek Çelik', $data['refs']['https://www.facebook.com/tantek.celik']['name']);
  298. }
  299. public function testEntryAtFragmentID() {
  300. $url = 'http://source.example.com/fragment-id#comment-1000';
  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('entry', $data['data']['type']);
  306. $this->assertEquals('http://source.example.com/fragment-id#comment-1000', $data['data']['url']);
  307. $this->assertTrue($data['info']['found_fragment']);
  308. }
  309. public function testEntryAtNonExistentFragmentID() {
  310. $url = 'http://source.example.com/fragment-id#comment-404';
  311. $response = $this->parse(['url' => $url]);
  312. $body = $response->getContent();
  313. $this->assertEquals(200, $response->getStatusCode());
  314. $data = json_decode($body, true);
  315. $this->assertEquals('entry', $data['data']['type']);
  316. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  317. $this->assertFalse($data['info']['found_fragment']);
  318. }
  319. }