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.

231 lines
11 KiB

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