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.

291 lines
15 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('twitter', $data['source-format']);
  28. $this->assertEquals('entry', $data['data']['type']);
  29. $this->assertEquals('aaronpk dev', $data['data']['author']['name']);
  30. $this->assertEquals('pkdev', $data['data']['author']['nickname']);
  31. $this->assertEquals('https://twitter.com/pkdev', $data['data']['author']['url']);
  32. $this->assertEquals('Portland, OR', $data['data']['author']['location']);
  33. $this->assertEquals('Dev account for testing Twitter things. Follow me here: https://twitter.com/aaronpk', $data['data']['author']['bio']);
  34. $this->assertEquals('https://pbs.twimg.com/profile_images/638125135904436224/qd_d94Qn_normal.jpg', $data['data']['author']['photo']);
  35. }
  36. public function testProfileWithNonExpandedURL() {
  37. list($url, $json) = $this->loadTweet('791704641046052864');
  38. $data = $this->parse(['url' => $url, 'body' => $json]);
  39. $this->assertEquals('https://twitter.com/agiletortoise', $data['data']['author']['url']);
  40. }
  41. public function testBasicTestStuff() {
  42. list($url, $json) = $this->loadTweet('818913630569664512');
  43. $data = $this->parse(['url' => $url, 'body' => $json]);
  44. $this->assertEquals(null, $data['code']); // no code is expected if we pass in the body
  45. $this->assertEquals('twitter', $data['source-format']);
  46. $this->assertEquals('https://twitter.com/pkdev/status/818913630569664512', $data['url']);
  47. $this->assertEquals('entry', $data['data']['type']);
  48. $this->assertEquals('note', $data['data']['post-type']);
  49. $this->assertEquals('A tweet with a URL https://indieweb.org/ #and #some #hashtags', $data['data']['content']['text']);
  50. $this->assertContains('and', $data['data']['category']);
  51. $this->assertContains('some', $data['data']['category']);
  52. $this->assertContains('hashtags', $data['data']['category']);
  53. // Published date should be set to the timezone of the user
  54. $this->assertEquals('2017-01-10T12:13:18-08:00', $data['data']['published']);
  55. }
  56. public function testPositiveTimezone() {
  57. list($url, $json) = $this->loadTweet('719914707566649344');
  58. $data = $this->parse(['url' => $url, 'body' => $json]);
  59. $this->assertEquals("2016-04-12T16:46:56+01:00", $data['data']['published']);
  60. }
  61. public function testTweetWithEmoji() {
  62. list($url, $json) = $this->loadTweet('818943244553699328');
  63. $data = $this->parse(['url' => $url, 'body' => $json]);
  64. $this->assertEquals('twitter', $data['source-format']);
  65. $this->assertEquals('entry', $data['data']['type']);
  66. $this->assertEquals('Here 🎉 have an emoji', $data['data']['content']['text']);
  67. }
  68. public function testHTMLEscaping() {
  69. list($url, $json) = $this->loadTweet('818928092383166465');
  70. $data = $this->parse(['url' => $url, 'body' => $json]);
  71. $this->assertEquals('entry', $data['data']['type']);
  72. $this->assertEquals('Double escaping &amp; & amp', $data['data']['content']['text']);
  73. }
  74. public function testTweetWithPhoto() {
  75. list($url, $json) = $this->loadTweet('818912506496229376');
  76. $data = $this->parse(['url' => $url, 'body' => $json]);
  77. $this->assertEquals('entry', $data['data']['type']);
  78. $this->assertEquals('photo', $data['data']['post-type']);
  79. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  80. $this->assertEquals('https://pbs.twimg.com/media/C11cfRJUoAI26h9.jpg', $data['data']['photo'][0]);
  81. }
  82. public function testTweetWithTwoPhotos() {
  83. list($url, $json) = $this->loadTweet('818935308813103104');
  84. $data = $this->parse(['url' => $url, 'body' => $json]);
  85. $this->assertEquals('entry', $data['data']['type']);
  86. $this->assertEquals('photo', $data['data']['post-type']);
  87. $this->assertEquals('Two photos', $data['data']['content']['text']);
  88. $this->assertContains('https://pbs.twimg.com/media/C11xS1wUcAAeaKF.jpg', $data['data']['photo']);
  89. $this->assertContains('https://pbs.twimg.com/media/C11wtndUoAE1WfE.jpg', $data['data']['photo']);
  90. }
  91. public function testTweetWithVideo() {
  92. list($url, $json) = $this->loadTweet('818913178260160512');
  93. $data = $this->parse(['url' => $url, 'body' => $json]);
  94. $this->assertEquals('entry', $data['data']['type']);
  95. $this->assertEquals('video', $data['data']['post-type']);
  96. $this->assertEquals('Tweet with a video', $data['data']['content']['text']);
  97. $this->assertEquals('https://pbs.twimg.com/ext_tw_video_thumb/818913089248595970/pr/img/qVoEjF03Y41SKpNt.jpg', $data['data']['photo'][0]);
  98. $this->assertEquals('https://video.twimg.com/ext_tw_video/818913089248595970/pr/vid/1280x720/qP-sDx-Q0Hs-ckVv.mp4', $data['data']['video'][0]);
  99. }
  100. public function testTweetWithGif() {
  101. list($url, $json) = $this->loadTweet('tweet-with-gif');
  102. $data = $this->parse(['url' => $url, 'body' => $json]);
  103. $this->assertEquals('entry', $data['data']['type']);
  104. $this->assertEquals('reply', $data['data']['post-type']);
  105. $this->assertEquals('https://twitter.com/SwiftOnSecurity/status/1018178408398966784', $data['data']['in-reply-to'][0]);
  106. $this->assertEquals('Look! A distraction 🐁', $data['data']['content']['text']);
  107. $this->assertEquals('https://video.twimg.com/tweet_video/DiFOUuYV4AAUsgL.mp4', $data['data']['video'][0]);
  108. $this->assertEquals('https://pbs.twimg.com/tweet_video_thumb/DiFOUuYV4AAUsgL.jpg', $data['data']['photo'][0]);
  109. }
  110. public function testTweetWithLocation() {
  111. list($url, $json) = $this->loadTweet('818912506496229376');
  112. $data = $this->parse(['url' => $url, 'body' => $json]);
  113. $this->assertEquals('entry', $data['data']['type']);
  114. $this->assertEquals('Tweet with a photo and a location', $data['data']['content']['text']);
  115. $this->assertEquals('https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json', $data['data']['location']);
  116. $location = $data['data']['refs']['https://api.twitter.com/1.1/geo/id/ac88a4f17a51c7fc.json'];
  117. $this->assertEquals('adr', $location['type']);
  118. $this->assertEquals('Portland', $location['locality']);
  119. $this->assertEquals('United States', $location['country-name']);
  120. $this->assertEquals('Portland, OR', $location['name']);
  121. }
  122. public function testRetweet() {
  123. list($url, $json) = $this->loadTweet('818913351623245824');
  124. $data = $this->parse(['url' => $url, 'body' => $json]);
  125. $this->assertEquals('twitter', $data['source-format']);
  126. $this->assertEquals('entry', $data['data']['type']);
  127. $this->assertEquals('repost', $data['data']['post-type']);
  128. $this->assertArrayNotHasKey('content', $data['data']);
  129. $repostOf = 'https://twitter.com/aaronpk/status/817414679131660288';
  130. $this->assertEquals($repostOf, $data['data']['repost-of']);
  131. $tweet = $data['data']['refs'][$repostOf];
  132. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  133. }
  134. public function testRetweetWithPhoto() {
  135. list($url, $json) = $this->loadTweet('820039442773798912');
  136. $data = $this->parse(['url' => $url, 'body' => $json]);
  137. $this->assertEquals('entry', $data['data']['type']);
  138. $this->assertEquals('repost', $data['data']['post-type']);
  139. $this->assertArrayNotHasKey('content', $data['data']);
  140. $this->assertArrayNotHasKey('photo', $data['data']);
  141. $repostOf = 'https://twitter.com/phlaimeaux/status/819943954724556800';
  142. $this->assertEquals($repostOf, $data['data']['repost-of']);
  143. $tweet = $data['data']['refs'][$repostOf];
  144. $this->assertEquals('this headline is such a rollercoaster', $tweet['content']['text']);
  145. }
  146. public function testQuotedTweet() {
  147. list($url, $json) = $this->loadTweet('818913488609251331');
  148. $data = $this->parse(['url' => $url, 'body' => $json]);
  149. $this->assertEquals('entry', $data['data']['type']);
  150. $this->assertEquals('note', $data['data']['post-type']);
  151. $this->assertEquals('Quoted tweet with a #hashtag https://twitter.com/aaronpk/status/817414679131660288', $data['data']['content']['text']);
  152. $this->assertEquals('https://twitter.com/aaronpk/status/817414679131660288', $data['data']['quotation-of']);
  153. $tweet = $data['data']['refs']['https://twitter.com/aaronpk/status/817414679131660288'];
  154. $this->assertEquals('Yeah that\'s me http://xkcd.com/1782/', $tweet['content']['text']);
  155. }
  156. public function testTruncatedQuotedTweet() {
  157. list($url, $json) = $this->loadTweet('tweet-with-truncated-quoted-tweet');
  158. $data = $this->parse(['url' => $url, 'body' => $json]);
  159. $this->assertEquals('entry', $data['data']['type']);
  160. $this->assertEquals('.@stream_pdx is a real treasure of our city.', $data['data']['content']['text']);
  161. $this->assertEquals('https://twitter.com/PDXStephenG/status/964598574322339841', $data['data']['quotation-of']);
  162. $tweet = $data['data']['refs']['https://twitter.com/PDXStephenG/status/964598574322339841'];
  163. $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']);
  164. $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']);
  165. }
  166. public function testStreamingTweetWithLink() {
  167. list($url, $json) = $this->loadTweet('streaming-tweet-with-link');
  168. $data = $this->parse(['url' => $url, 'body' => $json]);
  169. $this->assertEquals('twitter', $data['source-format']);
  170. $this->assertEquals('what happens if i include a link like https://kmikeym.com', $data['data']['content']['text']);
  171. $this->assertEquals('what happens if i include a link like <a href="https://kmikeym.com">https://kmikeym.com</a>', $data['data']['content']['html']);
  172. }
  173. public function testStreamingTweetWithMentions() {
  174. list($url, $json) = $this->loadTweet('streaming-tweet-with-mentions');
  175. $data = $this->parse(['url' => $url, 'body' => $json]);
  176. $this->assertEquals('Offer accepted! @aaronpk bought 1 shares from @coledrobison at $6.73 https://kmikeym.com/trades', $data['data']['content']['text']);
  177. $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']);
  178. }
  179. public function testStreamingTweetTruncated() {
  180. list($url, $json) = $this->loadTweet('streaming-tweet-truncated');
  181. $data = $this->parse(['url' => $url, 'body' => $json]);
  182. $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']);
  183. $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.<br>
  184. Feel like I could (maybe) rewrite previous open code to do some of this :)', $data['data']['content']['html']);
  185. }
  186. public function testStreamingTweetTruncatedWithPhoto() {
  187. list($url, $json) = $this->loadTweet('streaming-tweet-truncated-with-photo');
  188. $data = $this->parse(['url' => $url, 'body' => $json]);
  189. $this->assertEquals("#MicrosoftFlow ninja-tip.\nI'm getting better at custom-connector and auth. Thanks @skillriver \nThis is OAuth2 with MSA/Live (not AzureAD) which I need to do MVP timesheets.\nStill dislike Swagger so I don't know why I bother with this. I'm just that lazy doing this manually", $data['data']['content']['text']);
  190. $this->assertEquals(4, count($data['data']['photo']));
  191. $this->assertEquals('https://pbs.twimg.com/media/DWZ-5UPVAAAQOWY.jpg', $data['data']['photo'][0]);
  192. $this->assertEquals('https://pbs.twimg.com/media/DWaAhZ2UQAAIEoS.jpg', $data['data']['photo'][3]);
  193. }
  194. public function testStreamingTweetTruncatedWithVideo() {
  195. list($url, $json) = $this->loadTweet('streaming-tweet-truncated-with-video');
  196. $data = $this->parse(['url' => $url, 'body' => $json]);
  197. $this->assertEquals("hi @aaronpk Ends was a great job I was just talking to her about the house I think she is just talking to you about that stuff like that you don't have any idea of how to make to your job so you don't want me going back on your own to make it happen", $data['data']['content']['text']);
  198. $this->assertEquals(1, count($data['data']['video']));
  199. $this->assertEquals('https://video.twimg.com/ext_tw_video/965608338917548032/pu/vid/720x720/kreAfCMf-B1dLqBH.mp4', $data['data']['video'][0]);
  200. }
  201. public function testTweetWithNewlines() {
  202. list($url, $json) = $this->loadTweet('tweet-with-newlines');
  203. $data = $this->parse(['url' => $url, 'body' => $json]);
  204. $this->assertEquals(4, substr_count($data['data']['content']['text'], "\n"));
  205. $this->assertEquals(4, substr_count($data['data']['content']['html'], "<br>\n"));
  206. $this->assertEquals("🌈🌈 I’ve watched the sun rise at Corona Heights countless times, but never before have I seen a #rainbow at #sunrise.
  207. #CoronaHeights #SanFrancisco #SF #wakeupthesun #fromwhereirun #nofilter
  208. Woke up this morning feeling compelled to run to Corona… http://tantek.com/2018/049/t3/rainbow-at-sunrise", $data['data']['content']['text']);
  209. }
  210. public function testStreamingTweetReply() {
  211. list($url, $json) = $this->loadTweet('streaming-tweet-reply');
  212. $data = $this->parse(['url' => $url, 'body' => $json]);
  213. $this->assertEquals('twitter', $data['source-format']);
  214. $this->assertEquals('https://twitter.com/anomalily/status/967024586423386112', $data['data']['in-reply-to'][0]);
  215. }
  216. public function testTweetReply() {
  217. list($url, $json) = $this->loadTweet('967046438822674432');
  218. $data = $this->parse(['url' => $url, 'body' => $json]);
  219. $this->assertEquals('twitter', $data['source-format']);
  220. $this->assertEquals('https://twitter.com/anomalily/status/967024586423386112', $data['data']['in-reply-to'][0]);
  221. }
  222. }