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.

246 lines
12 KiB

  1. <?php
  2. use Symfony\Component\HttpFoundation\Request;
  3. use Symfony\Component\HttpFoundation\Response;
  4. class ActivityStreamsTest extends PHPUnit\Framework\TestCase
  5. {
  6. private $http;
  7. public function setUp(): void
  8. {
  9. $this->client = new Parse();
  10. $this->client->http = new p3k\HTTP\Test(dirname(__FILE__).'/data/');
  11. $this->client->mc = null;
  12. }
  13. private function parse($params)
  14. {
  15. $request = new Request($params);
  16. $response = new Response();
  17. return $this->client->parse($request, $response);
  18. }
  19. public function testAuthorProfile()
  20. {
  21. $url = 'http://activitystreams.example/aaronpk';
  22. $response = $this->parse(['url' => $url]);
  23. $body = $response->getContent();
  24. $this->assertEquals(200, $response->getStatusCode());
  25. $data = json_decode($body, true);
  26. $this->assertEquals('activity+json', $data['source-format']);
  27. $this->assertEquals('card', $data['data']['type']);
  28. $this->assertEquals('aaronpk', $data['data']['name']);
  29. $this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['photo']);
  30. $this->assertEquals('https://aaronparecki.com/', $data['data']['url']);
  31. }
  32. public function testNoteWithTags()
  33. {
  34. $url = 'http://activitystreams.example/note.json';
  35. $response = $this->parse(['url' => $url]);
  36. $body = $response->getContent();
  37. $this->assertEquals(200, $response->getStatusCode());
  38. $data = json_decode($body, true);
  39. $this->assertEquals('activity+json', $data['source-format']);
  40. $this->assertEquals('note', $data['data']['post-type']);
  41. $this->assertEquals($url, $data['data']['url']);
  42. $this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
  43. $this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['content']['text']);
  44. $this->assertArrayNotHasKey('html', $data['data']['content']);
  45. $this->assertSame(['activitystreams'], $data['data']['category']);
  46. $this->assertEquals('aaronpk', $data['data']['author']['name']);
  47. $this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['author']['photo']);
  48. $this->assertEquals('https://aaronparecki.com/', $data['data']['author']['url']);
  49. }
  50. public function testArticle()
  51. {
  52. $url = 'http://activitystreams.example/article.json';
  53. $response = $this->parse(['url' => $url]);
  54. $body = $response->getContent();
  55. $this->assertEquals(200, $response->getStatusCode());
  56. $data = json_decode($body, true);
  57. $this->assertEquals('activity+json', $data['source-format']);
  58. $this->assertEquals('article', $data['data']['post-type']);
  59. $this->assertEquals($url, $data['data']['url']);
  60. $this->assertEquals('An Article', $data['data']['name']);
  61. $this->assertEquals('This is the content of an ActivityStreams article', $data['data']['content']['text']);
  62. $this->assertEquals('<p>This is the content of an <b>ActivityStreams</b> article</p>', $data['data']['content']['html']);
  63. $this->assertEquals('aaronpk', $data['data']['author']['name']);
  64. $this->assertEquals('https://aaronparecki.com/images/profile.jpg', $data['data']['author']['photo']);
  65. $this->assertEquals('https://aaronparecki.com/', $data['data']['author']['url']);
  66. }
  67. public function testPhoto()
  68. {
  69. $url = 'http://activitystreams.example/photo.json';
  70. $response = $this->parse(['url' => $url]);
  71. $body = $response->getContent();
  72. $this->assertEquals(200, $response->getStatusCode());
  73. $data = json_decode($body, true);
  74. $this->assertEquals('activity+json', $data['source-format']);
  75. $this->assertEquals($url, $data['data']['url']);
  76. $this->assertEquals('photo', $data['data']['post-type']);
  77. $this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
  78. $this->assertEquals('This is the text content of an ActivityStreams photo', $data['data']['content']['text']);
  79. $this->assertArrayNotHasKey('html', $data['data']['content']);
  80. $this->assertSame(['activitystreams'], $data['data']['category']);
  81. $this->assertSame(['https://aaronparecki.com/2018/06/28/26/photo.jpg'], $data['data']['photo']);
  82. }
  83. public function testVideo()
  84. {
  85. $url = 'http://activitystreams.example/video.json';
  86. $response = $this->parse(['url' => $url]);
  87. $body = $response->getContent();
  88. $this->assertEquals(200, $response->getStatusCode());
  89. $data = json_decode($body, true);
  90. $this->assertEquals('activity+json', $data['source-format']);
  91. $this->assertEquals('video', $data['data']['post-type']);
  92. $this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
  93. $this->assertSame(['https://aaronparecki.com/2018/07/21/19/video.mp4'], $data['data']['video']);
  94. }
  95. public function testReply()
  96. {
  97. $url = 'http://activitystreams.example/reply.json';
  98. $response = $this->parse(['url' => $url]);
  99. $body = $response->getContent();
  100. $this->assertEquals(200, $response->getStatusCode());
  101. $data = json_decode($body, true);
  102. $this->assertEquals('activity+json', $data['source-format']);
  103. $this->assertEquals('reply', $data['data']['post-type']);
  104. $this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
  105. $this->assertArrayNotHasKey('category', $data['data']); // should not include the person-tag
  106. // For now, don't fetch the reply context
  107. $this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['in-reply-to']);
  108. }
  109. public function testCustomEmoji()
  110. {
  111. $url = 'http://activitystreams.example/custom-emoji.json';
  112. $response = $this->parse(['url' => $url]);
  113. $body = $response->getContent();
  114. $this->assertEquals(200, $response->getStatusCode());
  115. $data = json_decode($body, true);
  116. $this->assertEquals('activity+json', $data['source-format']);
  117. $this->assertEquals('note', $data['data']['post-type']);
  118. $this->assertEquals("https://mastodon.social/@Gargron/100465999501820229", $data['data']['url']);
  119. $this->assertEquals('2018-07-30T22:24:54+00:00', $data['data']['published']);
  120. $this->assertEquals(':yikes:', $data['data']['content']['text']);
  121. $this->assertEquals('<p><img src="https://files.mastodon.social/custom_emojis/images/000/031/275/original/yikes.png" alt=":yikes:" title=":yikes:" width="24" class="xray-emoji"></p>', $data['data']['content']['html']);
  122. $this->assertEquals('Eugen', $data['data']['author']['name']);
  123. $this->assertEquals('Gargron', $data['data']['author']['nickname']);
  124. $this->assertEquals('https://files.mastodon.social/accounts/avatars/000/000/001/original/eb9e00274b135808.png', $data['data']['author']['photo']);
  125. $this->assertEquals('https://mastodon.social/@Gargron', $data['data']['author']['url']);
  126. }
  127. public function testRelAlternatePriority()
  128. {
  129. $url = 'http://source.example.com/rel-alternate-as2';
  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('activity+json', $data['source-format']);
  135. $this->assertEquals('http://activitystreams.example/note.json', $data['parsed-url']);
  136. $this->assertEquals('http://source.example.com/rel-alternate-as2', $data['url']);
  137. $this->assertEquals('note', $data['data']['post-type']);
  138. $this->assertEquals('2018-07-12T13:02:04-07:00', $data['data']['published']);
  139. $this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['content']['text']);
  140. $this->assertArrayNotHasKey('html', $data['data']['content']);
  141. $this->assertSame(['activitystreams'], $data['data']['category']);
  142. }
  143. public function testSensitiveContent()
  144. {
  145. $url = 'http://activitystreams.example/sensitive.json';
  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('activity+json', $data['source-format']);
  151. $this->assertEquals('note', $data['data']['post-type']);
  152. $this->assertEquals('sensitive topic', $data['data']['summary']);
  153. $this->assertEquals('This is the text content of a sensitive ActivityStreams note', $data['data']['content']['text']);
  154. $this->assertArrayNotHasKey('name', $data['data']);
  155. }
  156. public function testRepost()
  157. {
  158. $url = 'http://activitystreams.example/repost.json';
  159. $response = $this->parse(['url' => $url]);
  160. $body = $response->getContent();
  161. $this->assertEquals(200, $response->getStatusCode());
  162. $data = json_decode($body, true);
  163. $this->assertEquals('activity+json', $data['source-format']);
  164. $this->assertEquals('repost', $data['data']['post-type']);
  165. $this->assertArrayNotHasKey('content', $data['data']);
  166. $this->assertArrayNotHasKey('name', $data['data']);
  167. $this->assertEquals('Gargron', $data['data']['author']['nickname']);
  168. $this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['repost-of']);
  169. $this->assertArrayHasKey('http://activitystreams.example/note.json', $data['data']['refs']);
  170. $this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['refs']['http://activitystreams.example/note.json']['content']['text']);
  171. }
  172. public function testLike()
  173. {
  174. $url = 'http://activitystreams.example/like.json';
  175. $response = $this->parse(['url' => $url]);
  176. $body = $response->getContent();
  177. $this->assertEquals(200, $response->getStatusCode());
  178. $data = json_decode($body, true);
  179. $this->assertEquals('activity+json', $data['source-format']);
  180. $this->assertEquals('like', $data['data']['post-type']);
  181. $this->assertArrayNotHasKey('content', $data['data']);
  182. $this->assertArrayNotHasKey('name', $data['data']);
  183. $this->assertEquals('Gargron', $data['data']['author']['nickname']);
  184. $this->assertEquals(['http://activitystreams.example/note.json'], $data['data']['like-of']);
  185. $this->assertArrayHasKey('http://activitystreams.example/note.json', $data['data']['refs']);
  186. $this->assertEquals('This is the text content of an ActivityStreams note', $data['data']['refs']['http://activitystreams.example/note.json']['content']['text']);
  187. }
  188. public function testNoteWrappedInCreate()
  189. {
  190. $url = 'http://activitystreams.example/create.json';
  191. $response = $this->parse(['url' => $url]);
  192. $body = $response->getContent();
  193. $this->assertEquals(200, $response->getStatusCode());
  194. $data = json_decode($body, true);
  195. $this->assertEquals('activity+json', $data['source-format']);
  196. $this->assertEquals('reply', $data['data']['post-type']);
  197. $this->assertEquals('https://toot.cat/@jamey/100471682482196371', $data['data']['url']);
  198. $this->assertEquals('2018-07-31T22:30:09+00:00', $data['data']['published']);
  199. $this->assertEquals('@darius Huh, I just have never encountered anyone using the phrase generically like that.But you might consider writing IndieWeb.org-style bots (Atom+WebSub, and optionally WebMention if you want them to be interactive), and then using https://fed.brid.gy/ as an alternative to implementing ActivityPub yourself...', $data['data']['content']['text']);
  200. $this->assertEquals('https://social.tinysubversions.com/users/darius/statuses/100471614681787834', $data['data']['in-reply-to'][0]);
  201. $this->assertEquals('Jamey Sharp', $data['data']['author']['name']);
  202. $this->assertEquals('https://s3-us-west-2.amazonaws.com/tootcatapril2017/accounts/avatars/000/013/259/original/c904452a8411e4f5.jpg', $data['data']['author']['photo']);
  203. $this->assertEquals('https://toot.cat/@jamey', $data['data']['author']['url']);
  204. }
  205. }