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.

387 lines
17 KiB

6 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 testHEntryIsNotFirstObject() {
  179. $url = 'http://source.example.com/h-entry-is-not-first';
  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('Hello World', $data['data']['content']['text']);
  186. }
  187. public function testHEntryRSVP() {
  188. $url = 'http://source.example.com/h-entry-rsvp';
  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('I\'ll be there!', $data['data']['name']);
  195. $this->assertEquals('yes', $data['data']['rsvp']);
  196. }
  197. public function testMultipleHEntryOnPermalink() {
  198. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  199. $response = $this->parse(['url' => $url]);
  200. $body = $response->getContent();
  201. $this->assertEquals(200, $response->getStatusCode());
  202. $data = json_decode($body, true);
  203. $this->assertEquals('entry', $data['data']['type']);
  204. $this->assertEquals('Primary Post', $data['data']['name']);
  205. }
  206. public function testHEntryWithHCardSibling() {
  207. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  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('Hello World', $data['data']['content']['text']);
  214. }
  215. public function testHEntryRedirectWithHCardSibling() {
  216. $url = 'http://source.example.com/h-entry-redirect-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 testSingleHEntryHasNoPermalink() {
  225. $url = 'http://source.example.com/single-h-entry-has-no-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('Hello World', $data['data']['content']['text']);
  232. }
  233. public function testBridgyExampleWithNoMatchingURL() {
  234. $url = 'http://source.example.com/bridgy-example';
  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. }
  241. public function testEventWithHTMLDescription() {
  242. $url = 'http://source.example.com/h-event';
  243. $response = $this->parse(['url' => $url]);
  244. $body = $response->getContent();
  245. $this->assertEquals(200, $response->getStatusCode());
  246. $data = json_decode($body, true);
  247. $this->assertEquals('event', $data['data']['type']);
  248. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  249. $this->assertEquals($url, $data['data']['url']);
  250. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  251. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  252. $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']);
  253. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  254. $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']);
  255. $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']);
  256. }
  257. public function testEventWithTextDescription() {
  258. $url = 'http://source.example.com/h-event-text-description';
  259. $response = $this->parse(['url' => $url]);
  260. $body = $response->getContent();
  261. $this->assertEquals(200, $response->getStatusCode());
  262. $data = json_decode($body, true);
  263. $this->assertEquals('event', $data['data']['type']);
  264. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  265. $this->assertEquals($url, $data['data']['url']);
  266. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  267. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  268. $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']);
  269. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  270. $this->assertArrayNotHasKey('html', $data['data']['description']);
  271. }
  272. public function testEventWithHCardLocation() {
  273. $url = 'http://source.example.com/h-event-with-h-card-location';
  274. $response = $this->parse(['url' => $url]);
  275. $body = $response->getContent();
  276. $this->assertEquals(200, $response->getStatusCode());
  277. $data = json_decode($body, true);
  278. $this->assertEquals('event', $data['data']['type']);
  279. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  280. $this->assertEquals($url, $data['data']['url']);
  281. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  282. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  283. $this->assertArrayHasKey('http://source.example.com/venue', $data['refs']);
  284. $this->assertEquals('card', $data['refs']['http://source.example.com/venue']['type']);
  285. $this->assertEquals('http://source.example.com/venue', $data['refs']['http://source.example.com/venue']['url']);
  286. $this->assertEquals('Venue', $data['refs']['http://source.example.com/venue']['name']);
  287. }
  288. public function testEntryIsAnInvitee() {
  289. $url = 'http://source.example.com/bridgy-invitee';
  290. $response = $this->parse(['url' => $url]);
  291. $body = $response->getContent();
  292. $this->assertEquals(200, $response->getStatusCode());
  293. $data = json_decode($body, true);
  294. $this->assertEquals('entry', $data['data']['type']);
  295. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  296. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  297. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['refs']);
  298. $this->assertEquals('Tantek Çelik', $data['refs']['https://www.facebook.com/tantek.celik']['name']);
  299. }
  300. public function testEntryAtFragmentID() {
  301. $url = 'http://source.example.com/fragment-id#comment-1000';
  302. $response = $this->parse(['url' => $url]);
  303. $body = $response->getContent();
  304. $this->assertEquals(200, $response->getStatusCode());
  305. $data = json_decode($body, true);
  306. $this->assertEquals('entry', $data['data']['type']);
  307. $this->assertEquals('http://source.example.com/fragment-id#comment-1000', $data['data']['url']);
  308. $this->assertTrue($data['info']['found_fragment']);
  309. }
  310. public function testEntryAtNonExistentFragmentID() {
  311. $url = 'http://source.example.com/fragment-id#comment-404';
  312. $response = $this->parse(['url' => $url]);
  313. $body = $response->getContent();
  314. $this->assertEquals(200, $response->getStatusCode());
  315. $data = json_decode($body, true);
  316. $this->assertEquals('entry', $data['data']['type']);
  317. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  318. $this->assertFalse($data['info']['found_fragment']);
  319. }
  320. }