ActivityGenerationTests.php 20 KB

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