ActivityGenerationTests.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. namespace Tests\Unit;
  17. if (!defined('INSTALLDIR')) {
  18. define('INSTALLDIR', dirname(dirname(__DIR__)));
  19. }
  20. if (!defined('PUBLICDIR')) {
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. }
  23. if (!defined('GNUSOCIAL')) {
  24. define('GNUSOCIAL', true);
  25. }
  26. if (!defined('STATUSNET')) { // Compatibility
  27. define('STATUSNET', true);
  28. }
  29. use Activity;
  30. use ActivityObject;
  31. use ActivityUtils;
  32. use ActivityVerb;
  33. use Conversation;
  34. use DOMDocument;
  35. use Exception;
  36. use Notice;
  37. use PHPUnit\Framework\TestCase;
  38. use User;
  39. use User_group;
  40. require_once INSTALLDIR . '/lib/util/common.php';
  41. final class ActivityGenerationTests extends TestCase
  42. {
  43. static $author1 = null;
  44. static $author2 = null;
  45. static $targetUser1 = null;
  46. static $targetUser2 = null;
  47. static $targetGroup1 = null;
  48. static $targetGroup2 = null;
  49. public static function setUpBeforeClass(): void
  50. {
  51. $authorNick1 = 'activitygenerationtestsuser' . common_random_hexstr(4);
  52. $authorNick2 = 'activitygenerationtestsuser' . common_random_hexstr(4);
  53. $targetNick1 = 'activitygenerationteststarget' . common_random_hexstr(4);
  54. $targetNick2 = 'activitygenerationteststarget' . common_random_hexstr(4);
  55. $groupNick1 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
  56. $groupNick2 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
  57. try {
  58. self::$author1 = User::register(array('nickname' => $authorNick1,
  59. 'email' => $authorNick1 . '@example.net',
  60. 'email_confirmed' => true));
  61. self::$author2 = User::register(array('nickname' => $authorNick2,
  62. 'email' => $authorNick2 . '@example.net',
  63. 'email_confirmed' => true));
  64. self::$targetUser1 = User::register(array('nickname' => $targetNick1,
  65. 'email' => $targetNick1 . '@example.net',
  66. 'email_confirmed' => true));
  67. self::$targetUser2 = User::register(array('nickname' => $targetNick2,
  68. 'email' => $targetNick2 . '@example.net',
  69. 'email_confirmed' => true));
  70. self::$targetGroup1 = User_group::register(array('nickname' => $groupNick1,
  71. 'userid' => self::$author1->id,
  72. 'aliases' => array(),
  73. 'local' => true,
  74. 'location' => null,
  75. 'description' => null,
  76. 'fullname' => null,
  77. 'homepage' => null,
  78. 'mainpage' => null));
  79. self::$targetGroup2 = User_group::register(array('nickname' => $groupNick2,
  80. 'userid' => self::$author1->id,
  81. 'aliases' => array(),
  82. 'local' => true,
  83. 'location' => null,
  84. 'description' => null,
  85. 'fullname' => null,
  86. 'homepage' => null,
  87. 'mainpage' => null));
  88. } catch (Exception $e) {
  89. self::tearDownAfterClass();
  90. throw $e;
  91. }
  92. }
  93. public function testBasicNoticeActivity()
  94. {
  95. $notice = $this->_fakeNotice();
  96. $entry = $notice->asAtomEntry(true);
  97. $element = $this->_entryToElement($entry, false);
  98. $this->assertEquals($notice->getUri(), ActivityUtils::childContent($element, 'id'));
  99. $this->assertEquals('New note by ' . self::$author1->nickname, ActivityUtils::childContent($element, 'title'));
  100. $this->assertEquals($notice->rendered, ActivityUtils::childContent($element, 'content'));
  101. $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'published')));
  102. $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'updated')));
  103. $this->assertEquals(ActivityVerb::POST, ActivityUtils::childContent($element, 'verb', Activity::SPEC));
  104. $this->assertEquals(ActivityObject::NOTE, ActivityUtils::childContent($element, 'object-type', Activity::SPEC));
  105. }
  106. public function testNamespaceFlag()
  107. {
  108. $notice = $this->_fakeNotice();
  109. $entry = $notice->asAtomEntry(true);
  110. $element = $this->_entryToElement($entry, false);
  111. $this->assertTrue($element->hasAttribute('xmlns'));
  112. $this->assertTrue($element->hasAttribute('xmlns:thr'));
  113. $this->assertTrue($element->hasAttribute('xmlns:georss'));
  114. $this->assertTrue($element->hasAttribute('xmlns:activity'));
  115. $this->assertTrue($element->hasAttribute('xmlns:media'));
  116. $this->assertTrue($element->hasAttribute('xmlns:poco'));
  117. $this->assertTrue($element->hasAttribute('xmlns:ostatus'));
  118. $this->assertTrue($element->hasAttribute('xmlns:statusnet'));
  119. $entry = $notice->asAtomEntry(false);
  120. $element = $this->_entryToElement($entry, true);
  121. $this->assertFalse($element->hasAttribute('xmlns'));
  122. $this->assertFalse($element->hasAttribute('xmlns:thr'));
  123. $this->assertFalse($element->hasAttribute('xmlns:georss'));
  124. $this->assertFalse($element->hasAttribute('xmlns:activity'));
  125. $this->assertFalse($element->hasAttribute('xmlns:media'));
  126. $this->assertFalse($element->hasAttribute('xmlns:poco'));
  127. $this->assertFalse($element->hasAttribute('xmlns:ostatus'));
  128. $this->assertFalse($element->hasAttribute('xmlns:statusnet'));
  129. }
  130. public function testSourceFlag()
  131. {
  132. $notice = $this->_fakeNotice();
  133. // Test with no source
  134. $entry = $notice->asAtomEntry(false, false);
  135. $element = $this->_entryToElement($entry, true);
  136. $source = ActivityUtils::child($element, 'source');
  137. $this->assertNull($source);
  138. // Test with source
  139. $entry = $notice->asAtomEntry(false, true);
  140. $element = $this->_entryToElement($entry, true);
  141. $source = ActivityUtils::child($element, 'source');
  142. $this->assertNotNull($source);
  143. }
  144. public function testSourceContent()
  145. {
  146. $notice = $this->_fakeNotice();
  147. // make a time difference!
  148. sleep(2);
  149. $notice2 = $this->_fakeNotice();
  150. $entry = $notice->asAtomEntry(false, true);
  151. $element = $this->_entryToElement($entry, true);
  152. $source = ActivityUtils::child($element, 'source');
  153. $atomUrl = common_local_url('ApiTimelineUser', array('id' => self::$author1->id, 'format' => 'atom'));
  154. $profile = self::$author1->getProfile();
  155. $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id'));
  156. $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml'));
  157. $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source));
  158. $this->assertEquals(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated')));
  159. // XXX: do we care here?
  160. $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title')));
  161. $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license'));
  162. }
  163. public function testAuthorFlag()
  164. {
  165. $notice = $this->_fakeNotice();
  166. // Test with no author
  167. $entry = $notice->asAtomEntry(false, false, false);
  168. $element = $this->_entryToElement($entry, true);
  169. $this->assertNull(ActivityUtils::child($element, 'author'));
  170. $this->assertNull(ActivityUtils::child($element, 'actor', Activity::SPEC));
  171. // Test with source
  172. $entry = $notice->asAtomEntry(false, false, true);
  173. $element = $this->_entryToElement($entry, true);
  174. $author = ActivityUtils::child($element, 'author');
  175. $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
  176. $this->assertFalse(is_null($author));
  177. $this->assertTrue(is_null($actor)); // <activity:actor> is obsolete, no longer added
  178. }
  179. public function testAuthorContent()
  180. {
  181. $notice = $this->_fakeNotice();
  182. // Test with author
  183. $entry = $notice->asAtomEntry(false, false, true);
  184. $element = $this->_entryToElement($entry, true);
  185. $author = ActivityUtils::child($element, 'author');
  186. $this->assertEquals(self::$author1->getNickname(), ActivityUtils::childContent($author, 'name'));
  187. $this->assertEquals(self::$author1->getUri(), ActivityUtils::childContent($author, 'uri'));
  188. }
  189. /**
  190. * We no longer create <activity:actor> entries, they have merged to <atom:author>
  191. */
  192. public function testActorContent()
  193. {
  194. $notice = $this->_fakeNotice();
  195. // Test with author
  196. $entry = $notice->asAtomEntry(false, false, true);
  197. $element = $this->_entryToElement($entry, true);
  198. $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
  199. $this->assertEquals($actor, null);
  200. }
  201. public function testReplyLink()
  202. {
  203. $orig = $this->_fakeNotice(self::$targetUser1);
  204. $text = "@" . self::$targetUser1->nickname . " reply text " . common_random_hexstr(4);
  205. $reply = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
  206. $entry = $reply->asAtomEntry();
  207. $element = $this->_entryToElement($entry, true);
  208. $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0');
  209. $this->assertNotNull($irt);
  210. $this->assertEquals($orig->getUri(), $irt->getAttribute('ref'));
  211. $this->assertEquals($orig->getUrl(), $irt->getAttribute('href'));
  212. }
  213. public function testReplyAttention()
  214. {
  215. $orig = $this->_fakeNotice(self::$targetUser1);
  216. $text = "@" . self::$targetUser1->nickname . " reply text " . common_random_hexstr(4);
  217. $reply = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
  218. $entry = $reply->asAtomEntry();
  219. $element = $this->_entryToElement($entry, true);
  220. $this->assertEquals(self::$targetUser1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
  221. }
  222. public function testMultipleReplyAttention()
  223. {
  224. $orig = $this->_fakeNotice(self::$targetUser1);
  225. $text = "@" . self::$targetUser1->nickname . " reply text " . common_random_hexstr(4);
  226. $reply = Notice::saveNew(self::$targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
  227. $text = "@" . self::$targetUser1->nickname . " @" . self::$targetUser2->nickname . " reply text " . common_random_hexstr(4);
  228. $reply2 = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id));
  229. $entry = $reply2->asAtomEntry();
  230. $element = $this->_entryToElement($entry, true);
  231. $links = ActivityUtils::getLinks($element, 'mentioned');
  232. $hrefs = array();
  233. foreach ($links as $link) {
  234. $hrefs[] = $link->getAttribute('href');
  235. }
  236. $this->assertTrue(in_array(self::$targetUser1->getUri(), $hrefs));
  237. $this->assertTrue(in_array(self::$targetUser2->getUri(), $hrefs));
  238. }
  239. public function testGroupPostAttention()
  240. {
  241. $text = "!" . self::$targetGroup1->nickname . " reply text " . common_random_hexstr(4);
  242. $notice = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null));
  243. $entry = $notice->asAtomEntry();
  244. $element = $this->_entryToElement($entry, true);
  245. $this->assertEquals(self::$targetGroup1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
  246. }
  247. public function testMultipleGroupPostAttention()
  248. {
  249. $text = "!" . self::$targetGroup1->nickname . " !" . self::$targetGroup2->nickname . " reply text " . common_random_hexstr(4);
  250. $notice = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null));
  251. $entry = $notice->asAtomEntry();
  252. $element = $this->_entryToElement($entry, true);
  253. $links = ActivityUtils::getLinks($element, 'mentioned');
  254. $hrefs = array();
  255. foreach ($links as $link) {
  256. $hrefs[] = $link->getAttribute('href');
  257. }
  258. $this->assertTrue(in_array(self::$targetGroup1->getUri(), $hrefs));
  259. $this->assertTrue(in_array(self::$targetGroup2->getUri(), $hrefs));
  260. }
  261. public function testRepeatLink()
  262. {
  263. $notice = $this->_fakeNotice(self::$author1);
  264. $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
  265. $entry = $repeat->asAtomEntry();
  266. $element = $this->_entryToElement($entry, true);
  267. $noticeInfo = ActivityUtils::child($element, 'notice_info', 'http://status.net/schema/api/1/');
  268. $this->assertNotNull($noticeInfo);
  269. $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
  270. $this->assertEquals($repeat->id, $noticeInfo->getAttribute('local_id'));
  271. }
  272. public function testTag()
  273. {
  274. $tag1 = common_random_hexstr(4);
  275. $notice = $this->_fakeNotice(self::$author1, '#' . $tag1);
  276. $entry = $notice->asAtomEntry();
  277. $element = $this->_entryToElement($entry, true);
  278. $category = ActivityUtils::child($element, 'category');
  279. $this->assertNotNull($category);
  280. $this->assertEquals($tag1, $category->getAttribute('term'));
  281. }
  282. public function testMultiTag()
  283. {
  284. $tag1 = common_random_hexstr(4);
  285. $tag2 = common_random_hexstr(4);
  286. $notice = $this->_fakeNotice(self::$author1, '#' . $tag1 . ' #' . $tag2);
  287. $entry = $notice->asAtomEntry();
  288. $element = $this->_entryToElement($entry, true);
  289. $categories = $element->getElementsByTagName('category');
  290. $this->assertNotNull($categories);
  291. $this->assertEquals(2, $categories->length);
  292. $terms = array();
  293. for ($i = 0; $i < $categories->length; $i++) {
  294. $cat = $categories->item($i);
  295. $terms[] = $cat->getAttribute('term');
  296. }
  297. $this->assertTrue(in_array($tag1, $terms));
  298. $this->assertTrue(in_array($tag2, $terms));
  299. }
  300. public function testGeotaggedActivity()
  301. {
  302. $notice = Notice::saveNew(self::$author1->id, common_random_hexstr(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
  303. $entry = $notice->asAtomEntry();
  304. $element = $this->_entryToElement($entry, true);
  305. $this->assertEquals('45.5000000 -73.6000000', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
  306. }
  307. public function testNoticeInfo()
  308. {
  309. $notice = $this->_fakeNotice();
  310. $entry = $notice->asAtomEntry();
  311. $element = $this->_entryToElement($entry, true);
  312. $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
  313. $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
  314. $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
  315. $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
  316. $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
  317. // $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
  318. $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
  319. }
  320. public function testNoticeInfoRepeatOf()
  321. {
  322. $notice = $this->_fakeNotice();
  323. $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
  324. $entry = $repeat->asAtomEntry();
  325. $element = $this->_entryToElement($entry, true);
  326. $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
  327. $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
  328. }
  329. public function testNoticeInfoRepeated()
  330. {
  331. $notice = $this->_fakeNotice();
  332. $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
  333. $entry = $notice->asAtomEntry(false, false, false, self::$author2->getProfile());
  334. $element = $this->_entryToElement($entry, true);
  335. $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
  336. $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));
  337. $entry = $notice->asAtomEntry(false, false, false, self::$targetUser1->getProfile());
  338. $element = $this->_entryToElement($entry, true);
  339. $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
  340. $this->assertEquals('false', $noticeInfo->getAttribute('repeated'));
  341. }
  342. /* public function testNoticeInfoFave()
  343. {
  344. $notice = $this->_fakeNotice();
  345. $fave = Fave::addNew(self::$author2->getProfile(), $notice);
  346. // Should be set if user has faved
  347. $entry = $notice->asAtomEntry(false, false, false, self::$author2);
  348. $element = $this->_entryToElement($entry, true);
  349. $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
  350. $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));
  351. // Shouldn't be set if user has not faved
  352. $entry = $notice->asAtomEntry(false, false, false, self::$targetUser1);
  353. $element = $this->_entryToElement($entry, true);
  354. $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
  355. $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
  356. }*/
  357. public function testConversationLink()
  358. {
  359. $orig = $this->_fakeNotice(self::$targetUser1);
  360. $text = "@" . self::$targetUser1->nickname . " reply text " . common_random_hexstr(4);
  361. $reply = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
  362. $conv = Conversation::getKV('id', $reply->conversation);
  363. $entry = $reply->asAtomEntry();
  364. $element = $this->_entryToElement($entry, true);
  365. $this->assertEquals($conv->getUrl(), ActivityUtils::getLink($element, 'ostatus:conversation'));
  366. }
  367. public static function tearDownAfterClass(): void
  368. {
  369. if (!is_null(self::$author1)) {
  370. self::$author1->getProfile()->delete();
  371. }
  372. if (!is_null(self::$author2)) {
  373. self::$author2->getProfile()->delete();
  374. }
  375. if (!is_null(self::$targetUser1)) {
  376. self::$targetUser1->getProfile()->delete();
  377. }
  378. if (!is_null(self::$targetUser2)) {
  379. self::$targetUser2->getProfile()->delete();
  380. }
  381. if (!is_null(self::$targetGroup1)) {
  382. self::$targetGroup1->delete();
  383. }
  384. if (!is_null(self::$targetGroup2)) {
  385. self::$targetGroup2->delete();
  386. }
  387. }
  388. private function _fakeNotice($user = null, $text = null)
  389. {
  390. if (empty($user)) {
  391. $user = self::$author1;
  392. }
  393. if (empty($text)) {
  394. $text = "fake-o text-o " . common_random_hexstr(32);
  395. }
  396. return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
  397. }
  398. private function _entryToElement($entry, $namespace = false)
  399. {
  400. $xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n\n";
  401. $xml .= '<feed';
  402. if ($namespace) {
  403. $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
  404. $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
  405. $xml .= ' xmlns:georss="http://www.georss.org/georss"';
  406. $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
  407. $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
  408. $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
  409. $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
  410. $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
  411. }
  412. $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
  413. $doc = new DOMDocument();
  414. $doc->loadXML($xml);
  415. $feed = $doc->documentElement;
  416. $entries = $feed->getElementsByTagName('entry');
  417. return $entries->item(0);
  418. }
  419. }