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.

399 lines
18 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. print_r($data['data']);
  186. $this->assertEquals('http://syndicated.example/', $data['data']['syndication'][0]);
  187. }
  188. public function testHEntryIsNotFirstObject() {
  189. $url = 'http://source.example.com/h-entry-is-not-first';
  190. $response = $this->parse(['url' => $url]);
  191. $body = $response->getContent();
  192. $this->assertEquals(200, $response->getStatusCode());
  193. $data = json_decode($body, true);
  194. $this->assertEquals('entry', $data['data']['type']);
  195. $this->assertEquals('Hello World', $data['data']['content']['text']);
  196. }
  197. public function testHEntryRSVP() {
  198. $url = 'http://source.example.com/h-entry-rsvp';
  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('I\'ll be there!', $data['data']['name']);
  205. $this->assertEquals('yes', $data['data']['rsvp']);
  206. }
  207. public function testMultipleHEntryOnPermalink() {
  208. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  209. $response = $this->parse(['url' => $url]);
  210. $body = $response->getContent();
  211. $this->assertEquals(200, $response->getStatusCode());
  212. $data = json_decode($body, true);
  213. $this->assertEquals('entry', $data['data']['type']);
  214. $this->assertEquals('Primary Post', $data['data']['name']);
  215. }
  216. public function testHEntryWithHCardSibling() {
  217. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  218. $response = $this->parse(['url' => $url]);
  219. $body = $response->getContent();
  220. $this->assertEquals(200, $response->getStatusCode());
  221. $data = json_decode($body, true);
  222. $this->assertEquals('entry', $data['data']['type']);
  223. $this->assertEquals('Hello World', $data['data']['content']['text']);
  224. }
  225. public function testHEntryRedirectWithHCardSibling() {
  226. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  227. $response = $this->parse(['url' => $url]);
  228. $body = $response->getContent();
  229. $this->assertEquals(200, $response->getStatusCode());
  230. $data = json_decode($body, true);
  231. $this->assertEquals('entry', $data['data']['type']);
  232. $this->assertEquals('Hello World', $data['data']['content']['text']);
  233. }
  234. public function testSingleHEntryHasNoPermalink() {
  235. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  236. $response = $this->parse(['url' => $url]);
  237. $body = $response->getContent();
  238. $this->assertEquals(200, $response->getStatusCode());
  239. $data = json_decode($body, true);
  240. $this->assertEquals('entry', $data['data']['type']);
  241. $this->assertEquals('Hello World', $data['data']['content']['text']);
  242. }
  243. public function testBridgyExampleWithNoMatchingURL() {
  244. $url = 'http://source.example.com/bridgy-example';
  245. $response = $this->parse(['url' => $url]);
  246. $body = $response->getContent();
  247. $this->assertEquals(200, $response->getStatusCode());
  248. $data = json_decode($body, true);
  249. $this->assertEquals('entry', $data['data']['type']);
  250. }
  251. public function testEventWithHTMLDescription() {
  252. $url = 'http://source.example.com/h-event';
  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('event', $data['data']['type']);
  258. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  259. $this->assertEquals($url, $data['data']['url']);
  260. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  261. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  262. $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']);
  263. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  264. $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']);
  265. $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']);
  266. }
  267. public function testEventWithTextDescription() {
  268. $url = 'http://source.example.com/h-event-text-description';
  269. $response = $this->parse(['url' => $url]);
  270. $body = $response->getContent();
  271. $this->assertEquals(200, $response->getStatusCode());
  272. $data = json_decode($body, true);
  273. $this->assertEquals('event', $data['data']['type']);
  274. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  275. $this->assertEquals($url, $data['data']['url']);
  276. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  277. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  278. $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']);
  279. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  280. $this->assertArrayNotHasKey('html', $data['data']['description']);
  281. }
  282. public function testEventWithHCardLocation() {
  283. $url = 'http://source.example.com/h-event-with-h-card-location';
  284. $response = $this->parse(['url' => $url]);
  285. $body = $response->getContent();
  286. $this->assertEquals(200, $response->getStatusCode());
  287. $data = json_decode($body, true);
  288. $this->assertEquals('event', $data['data']['type']);
  289. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  290. $this->assertEquals($url, $data['data']['url']);
  291. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  292. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  293. $this->assertArrayHasKey('http://source.example.com/venue', $data['refs']);
  294. $this->assertEquals('card', $data['refs']['http://source.example.com/venue']['type']);
  295. $this->assertEquals('http://source.example.com/venue', $data['refs']['http://source.example.com/venue']['url']);
  296. $this->assertEquals('Venue', $data['refs']['http://source.example.com/venue']['name']);
  297. }
  298. public function testEntryIsAnInvitee() {
  299. $url = 'http://source.example.com/bridgy-invitee';
  300. $response = $this->parse(['url' => $url]);
  301. $body = $response->getContent();
  302. $this->assertEquals(200, $response->getStatusCode());
  303. $data = json_decode($body, true);
  304. $this->assertEquals('entry', $data['data']['type']);
  305. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  306. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  307. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['refs']);
  308. $this->assertEquals('Tantek Çelik', $data['refs']['https://www.facebook.com/tantek.celik']['name']);
  309. }
  310. public function testEntryAtFragmentID() {
  311. $url = 'http://source.example.com/fragment-id#comment-1000';
  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#comment-1000', $data['data']['url']);
  318. $this->assertTrue($data['info']['found_fragment']);
  319. }
  320. public function testEntryAtNonExistentFragmentID() {
  321. $url = 'http://source.example.com/fragment-id#comment-404';
  322. $response = $this->parse(['url' => $url]);
  323. $body = $response->getContent();
  324. $this->assertEquals(200, $response->getStatusCode());
  325. $data = json_decode($body, true);
  326. $this->assertEquals('entry', $data['data']['type']);
  327. $this->assertEquals('http://source.example.com/fragment-id', $data['data']['url']);
  328. $this->assertFalse($data['info']['found_fragment']);
  329. }
  330. }