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.

328 lines
15 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 testTextContent() {
  62. $url = 'http://source.example.com/text-content';
  63. $response = $this->parse(['url' => $url]);
  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 a link to target.example.com and some formatted text but is in a p-content element so is plaintext.', $data->data->content->text);
  69. }
  70. public function testContentWithPrefixedName() {
  71. $url = 'http://source.example.com/content-with-prefixed-name';
  72. $response = $this->parse(['url' => $url]);
  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 link to target.example.com and some formatted text.', $data->data->content->text);
  78. $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);
  79. }
  80. public function testContentWithDistinctName() {
  81. $url = 'http://source.example.com/content-with-distinct-name';
  82. $response = $this->parse(['url' => $url]);
  83. $body = $response->getContent();
  84. $this->assertEquals(200, $response->getStatusCode());
  85. $data = json_decode($body);
  86. $this->assertEquals('Hello World', $data->data->name);
  87. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  88. $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);
  89. }
  90. public function testNameWithNoContent() {
  91. $url = 'http://source.example.com/name-no-content';
  92. $response = $this->parse(['url' => $url]);
  93. $body = $response->getContent();
  94. $this->assertEquals(200, $response->getStatusCode());
  95. $data = json_decode($body);
  96. $this->assertEquals('Hello World', $data->data->name);
  97. $this->assertObjectNotHasAttribute('content', $data->data);
  98. }
  99. public function testNoHEntryMarkup() {
  100. $url = 'http://source.example.com/no-h-entry';
  101. $response = $this->parse(['url' => $url]);
  102. $body = $response->getContent();
  103. $this->assertEquals(200, $response->getStatusCode());
  104. $data = json_decode($body);
  105. $this->assertEquals('unknown', $data->data->type);
  106. }
  107. public function testReplyIsURL() {
  108. $url = 'http://source.example.com/reply-is-url';
  109. $response = $this->parse(['url' => $url]);
  110. $body = $response->getContent();
  111. $this->assertEquals(200, $response->getStatusCode());
  112. $data = json_decode($body, true);
  113. $this->assertEquals('entry', $data['data']['type']);
  114. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  115. }
  116. public function testReplyIsHCite() {
  117. $url = 'http://source.example.com/reply-is-h-cite';
  118. $response = $this->parse(['url' => $url]);
  119. $body = $response->getContent();
  120. $this->assertEquals(200, $response->getStatusCode());
  121. $data = json_decode($body, true);
  122. $this->assertEquals('entry', $data['data']['type']);
  123. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  124. $this->assertArrayHasKey('http://example.com/100', $data['refs']);
  125. $this->assertEquals('Example Post', $data['refs']['http://example.com/100']['name']);
  126. $this->assertEquals('http://example.com/100', $data['refs']['http://example.com/100']['url']);
  127. }
  128. public function testPersonTagIsURL() {
  129. $url = 'http://source.example.com/person-tag-is-url';
  130. $response = $this->parse(['url' => $url]);
  131. $body = $response->getContent();
  132. $this->assertEquals(200, $response->getStatusCode());
  133. $data = json_decode($body, true);
  134. $this->assertEquals('entry', $data['data']['type']);
  135. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  136. }
  137. public function testPersonTagIsHCard() {
  138. $url = 'http://source.example.com/person-tag-is-h-card';
  139. $response = $this->parse(['url' => $url]);
  140. $body = $response->getContent();
  141. $this->assertEquals(200, $response->getStatusCode());
  142. $data = json_decode($body, true);
  143. $this->assertEquals('entry', $data['data']['type']);
  144. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  145. $this->assertArrayHasKey('http://alice.example.com/', $data['refs']);
  146. $this->assertEquals('card', $data['refs']['http://alice.example.com/']['type']);
  147. $this->assertEquals('http://alice.example.com/', $data['refs']['http://alice.example.com/']['url']);
  148. $this->assertEquals('Alice', $data['refs']['http://alice.example.com/']['name']);
  149. }
  150. public function testHEntryIsNotFirstObject() {
  151. $url = 'http://source.example.com/h-entry-is-not-first';
  152. $response = $this->parse(['url' => $url]);
  153. $body = $response->getContent();
  154. $this->assertEquals(200, $response->getStatusCode());
  155. $data = json_decode($body, true);
  156. $this->assertEquals('entry', $data['data']['type']);
  157. $this->assertEquals('Hello World', $data['data']['content']['text']);
  158. }
  159. public function testHEntryRSVP() {
  160. $url = 'http://source.example.com/h-entry-rsvp';
  161. $response = $this->parse(['url' => $url]);
  162. $body = $response->getContent();
  163. $this->assertEquals(200, $response->getStatusCode());
  164. $data = json_decode($body, true);
  165. $this->assertEquals('entry', $data['data']['type']);
  166. $this->assertEquals('I\'ll be there!', $data['data']['name']);
  167. $this->assertEquals('yes', $data['data']['rsvp']);
  168. }
  169. public function testMultipleHEntryOnPermalink() {
  170. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  171. $response = $this->parse(['url' => $url]);
  172. $body = $response->getContent();
  173. $this->assertEquals(200, $response->getStatusCode());
  174. $data = json_decode($body, true);
  175. $this->assertEquals('entry', $data['data']['type']);
  176. $this->assertEquals('Primary Post', $data['data']['name']);
  177. }
  178. public function testHEntryWithHCardSibling() {
  179. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  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 testHEntryRedirectWithHCardSibling() {
  188. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  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 testSingleHEntryHasNoPermalink() {
  197. $url = 'http://source.example.com/single-h-entry-has-no-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('Hello World', $data['data']['content']['text']);
  204. }
  205. public function testBridgyExampleWithNoMatchingURL() {
  206. $url = 'http://source.example.com/bridgy-example';
  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. }
  213. public function testEventWithHTMLDescription() {
  214. $url = 'http://source.example.com/h-event';
  215. $response = $this->parse(['url' => $url]);
  216. $body = $response->getContent();
  217. $this->assertEquals(200, $response->getStatusCode());
  218. $data = json_decode($body, true);
  219. $this->assertEquals('event', $data['data']['type']);
  220. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  221. $this->assertEquals($url, $data['data']['url']);
  222. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  223. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  224. $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']);
  225. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  226. $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']);
  227. $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']);
  228. }
  229. public function testEventWithTextDescription() {
  230. $url = 'http://source.example.com/h-event-text-description';
  231. $response = $this->parse(['url' => $url]);
  232. $body = $response->getContent();
  233. $this->assertEquals(200, $response->getStatusCode());
  234. $data = json_decode($body, true);
  235. $this->assertEquals('event', $data['data']['type']);
  236. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  237. $this->assertEquals($url, $data['data']['url']);
  238. $this->assertEquals('2016-03-09T18:30', $data['data']['start']);
  239. $this->assertEquals('2016-03-09T19:30', $data['data']['end']);
  240. $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']);
  241. $this->assertStringEndsWith("See the Homebrew Website Club Newsletter Volume 1 Issue 1 for a description of the first meeting.", $data['data']['description']['text']);
  242. $this->assertArrayNotHasKey('html', $data['data']['description']);
  243. }
  244. public function testEventWithHCardLocation() {
  245. $url = 'http://source.example.com/h-event-with-h-card-location';
  246. $response = $this->parse(['url' => $url]);
  247. $body = $response->getContent();
  248. $this->assertEquals(200, $response->getStatusCode());
  249. $data = json_decode($body, true);
  250. $this->assertEquals('event', $data['data']['type']);
  251. $this->assertEquals('Homebrew Website Club', $data['data']['name']);
  252. $this->assertEquals($url, $data['data']['url']);
  253. $this->assertEquals('2016-02-09T18:30', $data['data']['start']);
  254. $this->assertEquals('2016-02-09T19:30', $data['data']['end']);
  255. $this->assertArrayHasKey('http://source.example.com/venue', $data['refs']);
  256. $this->assertEquals('card', $data['refs']['http://source.example.com/venue']['type']);
  257. $this->assertEquals('http://source.example.com/venue', $data['refs']['http://source.example.com/venue']['url']);
  258. $this->assertEquals('Venue', $data['refs']['http://source.example.com/venue']['name']);
  259. }
  260. public function testEntryIsAnInvitee() {
  261. $url = 'http://source.example.com/bridgy-invitee';
  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. $this->assertEquals('https://www.facebook.com/555707837940351#tantek', $data['data']['url']);
  268. $this->assertContains('https://www.facebook.com/tantek.celik', $data['data']['invitee']);
  269. $this->assertArrayHasKey('https://www.facebook.com/tantek.celik', $data['refs']);
  270. $this->assertEquals('Tantek Çelik', $data['refs']['https://www.facebook.com/tantek.celik']['name']);
  271. }
  272. }