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.

214 lines
10 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class TwitterTest extends PHPUnit_Framework_TestCase {
  5. public function setUp() {
  6. $this->client = new Parse();
  7. $this->client->mc = null;
  8. }
  9. private function parse($params) {
  10. $request = new Request($params);
  11. $response = new Response();
  12. $result = $this->client->parse($request, $response);
  13. $body = $result->getContent();
  14. $this->assertEquals(200, $result->getStatusCode());
  15. return json_decode($body, true);
  16. }
  17. private function loadTweet($id) {
  18. $url = 'https://twitter.com/_/status/'.$id;
  19. $json = file_get_contents(dirname(__FILE__).'/data/api.twitter.com/'.$id.'.json');
  20. $parsed = json_decode($json);
  21. $url = 'https://twitter.com/'.$parsed->user->screen_name.'/status/'.$id;
  22. return [$url, $json];
  23. }
  24. public function testBasicProfileInfo() {
  25. list($url, $json) = $this->loadTweet('818912506496229376');
  26. $data = $this->parse(['url' => $url, 'body' => $json]);
  27. $this->assertEquals('entry', $data['data']['type']);
  28. $this->assertEquals('aaronpk dev', $data['data']['author']['name']);
  29. $this->assertEquals('pkdev', $data['data']['author']['nickname']);
  30. $this->assertEquals('https://aaronparecki.com/', $data['data']['author']['url']);
  31. $this->assertEquals('Portland, OR', $data['data']['author']['location']);
  32. $this->assertEquals('Dev account for testing Twitter things. Follow me here: https://twitter.com/aaronpk', $data['data']['author']['bio']);
  33. $this->assertEquals('https://pbs.twimg.com/profile_images/638125135904436224/qd_d94Qn_normal.jpg', $data['data']['author']['photo']);
  34. }
  35. public function testProfileWithNonExpandedURL() {
  36. list($url, $json) = $this->loadTweet('791704641046052864');
  37. $data = $this->parse(['url' => $url, 'body' => $json]);
  38. $this->assertEquals('http://agiletortoise.com', $data['data']['author']['url']);
  39. }
  40. public function testBasicTestStuff() {
  41. list($url, $json) = $this->loadTweet('818913630569664512');
  42. $data = $this->parse(['url' => $url, 'body' => $json]);
  43. $this->assertEquals(null, $data['code']); // no code is expected if we pass in the body
  44. $this->assertEquals('https://twitter.com/pkdev/status/818913630569664512', $data['url']);
  45. $this->assertEquals('entry', $data['data']['type']);
  46. $this->assertEquals('A tweet with a URL https://indieweb.org/ #and #some #hashtags', $data['data']['content']['text']);
  47. $this->assertContains('and', $data['data']['category']);
  48. $this->assertContains('some', $data['data']['category']);
  49. $this->assertContains('hashtags', $data['data']['category']);
  50. // Published date should be set to the timezone of the user
  51. $this->assertEquals('2017-01-10T12:13:18-08:00', $data['data']['published']);
  52. }
  53. public function testPositiveTimezone() {
  54. list($url, $json) = $this->loadTweet('719914707566649344');
  55. $data = $this->parse(['url' => $url, 'body' => $json]);
  56. $this->assertEquals("2016-04-12T16:46:56+01:00", $data['data']['published']);
  57. }
  58. public function testTweetWithEmoji() {
  59. list($url, $json) = $this->loadTweet('818943244553699328');
  60. $data = $this->parse(['url' => $url, 'body' => $json]);
  61. $this->assertEquals('entry', $data['data']['type']);
  62. $this->assertEquals('Here 🎉 have an emoji', $data['data']['content']['text']);
  63. }
  64. public function testHTMLEscaping() {
  65. list($url, $json) = $this->loadTweet('818928092383166465');
  66. $data = $this->parse(['url' => $url, 'body' => $json]);
  67. $this->assertEquals('entry', $data['data']['type']);
  68. $this->assertEquals('Double escaping &amp; & amp', $data['data']['content']['text']);
  69. }
  70. public function testTweetWithPhoto() {
  71. list($url, $json) = $this->loadTweet('818912506496229376');
  72. $data = $this->parse(['url' => $url, 'body' => $json]);
  73. $this->assertEquals('entry', $data['data']['type']);
  74. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  75. $this->assertEquals('https://pbs.twimg.com/media/C11cfRJUoAI26h9.jpg', $data['data']['photo'][0]);
  76. }
  77. public function testTweetWithTwoPhotos() {
  78. list($url, $json) = $this->loadTweet('818935308813103104');
  79. $data = $this->parse(['url' => $url, 'body' => $json]);
  80. $this->assertEquals('entry', $data['data']['type']);
  81. $this->assertEquals('Two photos', $data['data']['content']['text']);
  82. $this->assertContains('https://pbs.twimg.com/media/C11xS1wUcAAeaKF.jpg', $data['data']['photo']);
  83. $this->assertContains('https://pbs.twimg.com/media/C11wtndUoAE1WfE.jpg', $data['data']['photo']);
  84. }
  85. public function testTweetWithVideo() {
  86. list($url, $json) = $this->loadTweet('818913178260160512');
  87. $data = $this->parse(['url' => $url, 'body' => $json]);
  88. $this->assertEquals('entry', $data['data']['type']);
  89. $this->assertEquals('Tweet with a video', $data['data']['content']['text']);
  90. $this->assertEquals('https://video.twimg.com/ext_tw_video/818913089248595970/pr/vid/1280x720/qP-sDx-Q0Hs-ckVv.mp4', $data['data']['video'][0]);
  91. }
  92. public function testTweetWithLocation() {
  93. list($url, $json) = $this->loadTweet('818912506496229376');
  94. $data = $this->parse(['url' => $url, 'body' => $json]);
  95. $this->assertEquals('entry', $data['data']['type']);
  96. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  97. $this->assertEquals('https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json', $data['data']['location']);
  98. $location = $data['data']['refs']['https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json'];
  99. $this->assertEquals('adr', $location['type']);
  100. $this->assertEquals('Portland', $location['locality']);
  101. $this->assertEquals('United States', $location['country-name']);
  102. $this->assertEquals('Portland, OR', $location['name']);
  103. }
  104. public function testRetweet() {
  105. list($url, $json) = $this->loadTweet('818913351623245824');
  106. $data = $this->parse(['url' => $url, 'body' => $json]);
  107. $this->assertEquals('entry', $data['data']['type']);
  108. $this->assertArrayNotHasKey('content', $data['data']);
  109. $repostOf = 'https://twitter.com/aaronpk/status/817414679131660288';
  110. $this->assertEquals($repostOf, $data['data']['repost-of']);
  111. $tweet = $data['data']['refs'][$repostOf];
  112. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  113. }
  114. public function testRetweetWithPhoto() {
  115. list($url, $json) = $this->loadTweet('820039442773798912');
  116. $data = $this->parse(['url' => $url, 'body' => $json]);
  117. $this->assertEquals('entry', $data['data']['type']);
  118. $this->assertArrayNotHasKey('content', $data['data']);
  119. $this->assertArrayNotHasKey('photo', $data['data']);
  120. $repostOf = 'https://twitter.com/phlaimeaux/status/819943954724556800';
  121. $this->assertEquals($repostOf, $data['data']['repost-of']);
  122. $tweet = $data['data']['refs'][$repostOf];
  123. $this->assertEquals('this headline is such a rollercoaster', $tweet['content']['text']);
  124. }
  125. public function testQuotedTweet() {
  126. list($url, $json) = $this->loadTweet('818913488609251331');
  127. $data = $this->parse(['url' => $url, 'body' => $json]);
  128. $this->assertEquals('entry', $data['data']['type']);
  129. $this->assertEquals('Quoted tweet with a #hashtag https://twitter.com/aaronpk/status/817414679131660288', $data['data']['content']['text']);
  130. $this->assertEquals('https://twitter.com/aaronpk/status/817414679131660288', $data['data']['quotation-of']);
  131. $tweet = $data['data']['refs']['https://twitter.com/aaronpk/status/817414679131660288'];
  132. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  133. }
  134. public function testTruncatedQuotedTweet() {
  135. list($url, $json) = $this->loadTweet('tweet-with-truncated-quoted-tweet');
  136. $data = $this->parse(['url' => $url, 'body' => $json]);
  137. $this->assertEquals('entry', $data['data']['type']);
  138. $this->assertEquals('.@stream_pdx is a real treasure of our city.', $data['data']['content']['text']);
  139. $this->assertEquals('https://twitter.com/PDXStephenG/status/964598574322339841', $data['data']['quotation-of']);
  140. $tweet = $data['data']['refs']['https://twitter.com/PDXStephenG/status/964598574322339841'];
  141. $this->assertEquals('Hey @OregonGovBrown @tedwheeler day 16 of #BHM is for @stream_pdx. An amazing podcast trailer run by @tyeshasnow helping to democratize story telling in #PDX. Folks can get training in the production of podcasts. @siliconflorist #SupportBlackBusiness', $tweet['content']['text']);
  142. $this->assertEquals("Hey <a href=\"https://twitter.com/OregonGovBrown\">@OregonGovBrown</a> <a href=\"https://twitter.com/tedwheeler\">@tedwheeler</a> day 16 of #BHM is for <a href=\"https://twitter.com/stream_pdx\">@stream_pdx</a>. An amazing podcast trailer run by <a href=\"https://twitter.com/tyeshasnow\">@tyeshasnow</a> helping to democratize story telling in #PDX. Folks can get training in the production of podcasts. <a href=\"https://twitter.com/siliconflorist\">@siliconflorist</a> #SupportBlackBusiness", $tweet['content']['html']);
  143. }
  144. public function testStreamingTweetWithLink() {
  145. list($url, $json) = $this->loadTweet('streaming-tweet-with-link');
  146. $data = $this->parse(['url' => $url, 'body' => $json]);
  147. $this->assertEquals('what happens if i include a link like https://kmikeym.com', $data['data']['content']['text']);
  148. $this->assertEquals('what happens if i include a link like <a href="https://kmikeym.com">https://kmikeym.com</a>', $data['data']['content']['html']);
  149. }
  150. public function testStreamingTweetWithMentions() {
  151. list($url, $json) = $this->loadTweet('streaming-tweet-with-mentions');
  152. $data = $this->parse(['url' => $url, 'body' => $json]);
  153. $this->assertEquals('Offer accepted! @aaronpk bought 1 shares from @coledrobison at $6.73 https://kmikeym.com/trades', $data['data']['content']['text']);
  154. $this->assertEquals('Offer accepted! <a href="https://twitter.com/aaronpk">@aaronpk</a> bought 1 shares from <a href="https://twitter.com/coledrobison">@coledrobison</a> at $6.73 <a href="https://kmikeym.com/trades">https://kmikeym.com/trades</a>', $data['data']['content']['html']);
  155. }
  156. public function testStreamingTweetTruncated() {
  157. list($url, $json) = $this->loadTweet('streaming-tweet-truncated');
  158. $data = $this->parse(['url' => $url, 'body' => $json]);
  159. $this->assertEquals("#indieweb community. Really would like to see a Micropub client for Gratitude logging and also a Mastodon poster similar to the twitter one.\nFeel like I could (maybe) rewrite previous open code to do some of this :)", $data['data']['content']['text']);
  160. $this->assertArrayNotHasKey('html', $data['data']['content']);
  161. }
  162. }