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.

249 lines
9.9 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. }
  10. private function parse($params) {
  11. $request = new Request($params);
  12. $response = new Response();
  13. return $this->client->parse($request, $response);
  14. }
  15. public function testMissingURL() {
  16. $response = $this->parse([]);
  17. $body = $response->getContent();
  18. $this->assertEquals(400, $response->getStatusCode());
  19. $data = json_decode($body);
  20. $this->assertObjectHasAttribute('error', $data);
  21. $this->assertEquals('missing_url', $data->error);
  22. }
  23. public function testInvalidURL() {
  24. $url = 'ftp://example.com/foo';
  25. $response = $this->parse(['url' => $url]);
  26. $body = $response->getContent();
  27. $this->assertEquals(400, $response->getStatusCode());
  28. $data = json_decode($body);
  29. $this->assertObjectHasAttribute('error', $data);
  30. $this->assertEquals('invalid_url', $data->error);
  31. }
  32. public function testTargetNotFound() {
  33. $url = 'http://source.example.com/basictest';
  34. $response = $this->parse(['url' => $url, 'target' => 'http://example.net']);
  35. $body = $response->getContent();
  36. $this->assertEquals(400, $response->getStatusCode());
  37. $data = json_decode($body);
  38. $this->assertObjectHasAttribute('error', $data);
  39. $this->assertEquals('no_link_found', $data->error);
  40. }
  41. public function testTargetFound() {
  42. $url = 'http://source.example.com/basictest';
  43. $response = $this->parse(['url' => $url, 'target' => 'http://target.example.com']);
  44. $body = $response->getContent();
  45. $this->assertEquals(200, $response->getStatusCode());
  46. $data = json_decode($body);
  47. $this->assertObjectNotHasAttribute('error', $data);
  48. $this->assertObjectNotHasAttribute('error', $data);
  49. }
  50. public function testHTMLContent() {
  51. $url = 'http://source.example.com/html-content';
  52. $response = $this->parse(['url' => $url]);
  53. $body = $response->getContent();
  54. $this->assertEquals(200, $response->getStatusCode());
  55. $data = json_decode($body);
  56. $this->assertObjectNotHasAttribute('name', $data->data);
  57. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  58. $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);
  59. }
  60. public function testTextContent() {
  61. $url = 'http://source.example.com/text-content';
  62. $response = $this->parse(['url' => $url]);
  63. $body = $response->getContent();
  64. $this->assertEquals(200, $response->getStatusCode());
  65. $data = json_decode($body);
  66. $this->assertObjectNotHasAttribute('name', $data->data);
  67. $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);
  68. }
  69. public function testContentWithPrefixedName() {
  70. $url = 'http://source.example.com/content-with-prefixed-name';
  71. $response = $this->parse(['url' => $url]);
  72. $body = $response->getContent();
  73. $this->assertEquals(200, $response->getStatusCode());
  74. $data = json_decode($body);
  75. $this->assertObjectNotHasAttribute('name', $data->data);
  76. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  77. $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);
  78. }
  79. public function testContentWithDistinctName() {
  80. $url = 'http://source.example.com/content-with-distinct-name';
  81. $response = $this->parse(['url' => $url]);
  82. $body = $response->getContent();
  83. $this->assertEquals(200, $response->getStatusCode());
  84. $data = json_decode($body);
  85. $this->assertEquals('Hello World', $data->data->name);
  86. $this->assertEquals('This page has a link to target.example.com and some formatted text.', $data->data->content->text);
  87. $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);
  88. }
  89. public function testNameWithNoContent() {
  90. $url = 'http://source.example.com/name-no-content';
  91. $response = $this->parse(['url' => $url]);
  92. $body = $response->getContent();
  93. $this->assertEquals(200, $response->getStatusCode());
  94. $data = json_decode($body);
  95. $this->assertEquals('Hello World', $data->data->name);
  96. $this->assertObjectNotHasAttribute('content', $data->data);
  97. }
  98. public function testNoHEntryMarkup() {
  99. $url = 'http://source.example.com/no-h-entry';
  100. $response = $this->parse(['url' => $url]);
  101. $body = $response->getContent();
  102. $this->assertEquals(200, $response->getStatusCode());
  103. $data = json_decode($body);
  104. $this->assertEquals('unknown', $data->data->type);
  105. }
  106. public function testReplyIsURL() {
  107. $url = 'http://source.example.com/reply-is-url';
  108. $response = $this->parse(['url' => $url]);
  109. $body = $response->getContent();
  110. $this->assertEquals(200, $response->getStatusCode());
  111. $data = json_decode($body, true);
  112. $this->assertEquals('entry', $data['data']['type']);
  113. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  114. }
  115. public function testReplyIsHCite() {
  116. $url = 'http://source.example.com/reply-is-h-cite';
  117. $response = $this->parse(['url' => $url]);
  118. $body = $response->getContent();
  119. $this->assertEquals(200, $response->getStatusCode());
  120. $data = json_decode($body, true);
  121. $this->assertEquals('entry', $data['data']['type']);
  122. $this->assertEquals('http://example.com/100', $data['data']['in-reply-to'][0]);
  123. $this->assertArrayHasKey('http://example.com/100', $data['refs']);
  124. $this->assertEquals('Example Post', $data['refs']['http://example.com/100']['name']);
  125. $this->assertEquals('http://example.com/100', $data['refs']['http://example.com/100']['url']);
  126. }
  127. public function testPersonTagIsURL() {
  128. $url = 'http://source.example.com/person-tag-is-url';
  129. $response = $this->parse(['url' => $url]);
  130. $body = $response->getContent();
  131. $this->assertEquals(200, $response->getStatusCode());
  132. $data = json_decode($body, true);
  133. $this->assertEquals('entry', $data['data']['type']);
  134. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  135. }
  136. public function testPersonTagIsHCard() {
  137. $url = 'http://source.example.com/person-tag-is-h-card';
  138. $response = $this->parse(['url' => $url]);
  139. $body = $response->getContent();
  140. $this->assertEquals(200, $response->getStatusCode());
  141. $data = json_decode($body, true);
  142. $this->assertEquals('entry', $data['data']['type']);
  143. $this->assertEquals('http://alice.example.com/', $data['data']['category'][0]);
  144. $this->assertArrayHasKey('http://alice.example.com/', $data['refs']);
  145. $this->assertEquals('card', $data['refs']['http://alice.example.com/']['type']);
  146. $this->assertEquals('http://alice.example.com/', $data['refs']['http://alice.example.com/']['url']);
  147. $this->assertEquals('Alice', $data['refs']['http://alice.example.com/']['name']);
  148. }
  149. public function testHEntryIsNotFirstObject() {
  150. $url = 'http://source.example.com/h-entry-is-not-first';
  151. $response = $this->parse(['url' => $url]);
  152. $body = $response->getContent();
  153. $this->assertEquals(200, $response->getStatusCode());
  154. $data = json_decode($body, true);
  155. $this->assertEquals('entry', $data['data']['type']);
  156. $this->assertEquals('Hello World', $data['data']['content']['text']);
  157. }
  158. public function testHEntryRSVP() {
  159. $url = 'http://source.example.com/h-entry-rsvp';
  160. $response = $this->parse(['url' => $url]);
  161. $body = $response->getContent();
  162. $this->assertEquals(200, $response->getStatusCode());
  163. $data = json_decode($body, true);
  164. $this->assertEquals('entry', $data['data']['type']);
  165. $this->assertEquals('I\'ll be there!', $data['data']['name']);
  166. $this->assertEquals('yes', $data['data']['rsvp']);
  167. }
  168. public function testMultipleHEntryOnPermalink() {
  169. $url = 'http://source.example.com/multiple-h-entry-on-permalink';
  170. $response = $this->parse(['url' => $url]);
  171. $body = $response->getContent();
  172. $this->assertEquals(200, $response->getStatusCode());
  173. $data = json_decode($body, true);
  174. $this->assertEquals('entry', $data['data']['type']);
  175. $this->assertEquals('Primary Post', $data['data']['name']);
  176. }
  177. public function testHEntryWithHCardSibling() {
  178. $url = 'http://source.example.com/h-entry-with-h-card-sibling';
  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 testHEntryRedirectWithHCardSibling() {
  187. $url = 'http://source.example.com/h-entry-redirect-with-h-card-sibling';
  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('Hello World', $data['data']['content']['text']);
  194. }
  195. public function testSingleHEntryHasNoPermalink() {
  196. $url = 'http://source.example.com/single-h-entry-has-no-permalink';
  197. $response = $this->parse(['url' => $url]);
  198. $body = $response->getContent();
  199. $this->assertEquals(200, $response->getStatusCode());
  200. $data = json_decode($body, true);
  201. $this->assertEquals('entry', $data['data']['type']);
  202. $this->assertEquals('Hello World', $data['data']['content']['text']);
  203. }
  204. }