ActivityGenerationTests.php 21 KB

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