3 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
4 print "This script must be run from the command line\n";
8 // XXX: we should probably have some common source for this stuff
10 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
11 define('GNUSOCIAL', true);
12 define('STATUSNET', true); // compatibility
14 require_once INSTALLDIR . '/lib/common.php';
16 class ActivityGenerationTests extends PHPUnit_Framework_TestCase
21 var $targetUser1 = null;
22 var $targetUser2 = null;
24 var $targetGroup1 = null;
25 var $targetGroup2 = null;
27 function __construct()
29 parent::__construct();
31 $authorNick1 = 'activitygenerationtestsuser' . common_random_hexstr(4);
32 $authorNick2 = 'activitygenerationtestsuser' . common_random_hexstr(4);
34 $targetNick1 = 'activitygenerationteststarget' . common_random_hexstr(4);
35 $targetNick2 = 'activitygenerationteststarget' . common_random_hexstr(4);
37 $groupNick1 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
38 $groupNick2 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
40 $this->author1 = User::register(array('nickname' => $authorNick1,
41 'email' => $authorNick1 . '@example.net',
42 'email_confirmed' => true));
44 $this->author2 = User::register(array('nickname' => $authorNick2,
45 'email' => $authorNick2 . '@example.net',
46 'email_confirmed' => true));
48 $this->targetUser1 = User::register(array('nickname' => $targetNick1,
49 'email' => $targetNick1 . '@example.net',
50 'email_confirmed' => true));
52 $this->targetUser2 = User::register(array('nickname' => $targetNick2,
53 'email' => $targetNick2 . '@example.net',
54 'email_confirmed' => true));
56 $this->targetGroup1 = User_group::register(array('nickname' => $groupNick1,
57 'userid' => $this->author1->id,
61 'description' => null,
65 $this->targetGroup2 = User_group::register(array('nickname' => $groupNick2,
66 'userid' => $this->author1->id,
70 'description' => null,
76 public function testBasicNoticeActivity()
78 $notice = $this->_fakeNotice();
80 $entry = $notice->asAtomEntry(true);
82 $element = $this->_entryToElement($entry, false);
84 $this->assertEquals($notice->getUri(), ActivityUtils::childContent($element, 'id'));
85 $this->assertEquals($notice->content, ActivityUtils::childContent($element, 'title'));
86 $this->assertEquals($notice->rendered, ActivityUtils::childContent($element, 'content'));
87 $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'published')));
88 $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'updated')));
89 $this->assertEquals(ActivityVerb::POST, ActivityUtils::childContent($element, 'verb', Activity::SPEC));
90 $this->assertEquals(ActivityObject::NOTE, ActivityUtils::childContent($element, 'object-type', Activity::SPEC));
93 public function testNamespaceFlag()
95 $notice = $this->_fakeNotice();
97 $entry = $notice->asAtomEntry(true);
99 $element = $this->_entryToElement($entry, false);
101 $this->assertTrue($element->hasAttribute('xmlns'));
102 $this->assertTrue($element->hasAttribute('xmlns:thr'));
103 $this->assertTrue($element->hasAttribute('xmlns:georss'));
104 $this->assertTrue($element->hasAttribute('xmlns:activity'));
105 $this->assertTrue($element->hasAttribute('xmlns:media'));
106 $this->assertTrue($element->hasAttribute('xmlns:poco'));
107 $this->assertTrue($element->hasAttribute('xmlns:ostatus'));
108 $this->assertTrue($element->hasAttribute('xmlns:statusnet'));
110 $entry = $notice->asAtomEntry(false);
112 $element = $this->_entryToElement($entry, true);
114 $this->assertFalse($element->hasAttribute('xmlns'));
115 $this->assertFalse($element->hasAttribute('xmlns:thr'));
116 $this->assertFalse($element->hasAttribute('xmlns:georss'));
117 $this->assertFalse($element->hasAttribute('xmlns:activity'));
118 $this->assertFalse($element->hasAttribute('xmlns:media'));
119 $this->assertFalse($element->hasAttribute('xmlns:poco'));
120 $this->assertFalse($element->hasAttribute('xmlns:ostatus'));
121 $this->assertFalse($element->hasAttribute('xmlns:statusnet'));
124 public function testSourceFlag()
126 $notice = $this->_fakeNotice();
128 // Test with no source
130 $entry = $notice->asAtomEntry(false, false);
132 $element = $this->_entryToElement($entry, true);
134 $source = ActivityUtils::child($element, 'source');
136 $this->assertNull($source);
140 $entry = $notice->asAtomEntry(false, true);
142 $element = $this->_entryToElement($entry, true);
144 $source = ActivityUtils::child($element, 'source');
146 $this->assertNotNull($source);
149 public function testSourceContent()
151 $notice = $this->_fakeNotice();
152 // make a time difference!
154 $notice2 = $this->_fakeNotice();
156 $entry = $notice->asAtomEntry(false, true);
158 $element = $this->_entryToElement($entry, true);
160 $source = ActivityUtils::child($element, 'source');
162 $atomUrl = common_local_url('ApiTimelineUser', array('id' => $this->author1->id, 'format' => 'atom'));
164 $profile = $this->author1->getProfile();
166 $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id'));
167 $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml'));
168 $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source));
169 $this->assertEquals(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated')));
170 // XXX: do we care here?
171 $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title')));
172 $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license'));
175 public function testAuthorFlag()
177 $notice = $this->_fakeNotice();
179 // Test with no author
181 $entry = $notice->asAtomEntry(false, false, false);
183 $element = $this->_entryToElement($entry, true);
185 $this->assertNull(ActivityUtils::child($element, 'author'));
186 $this->assertNull(ActivityUtils::child($element, 'actor', Activity::SPEC));
190 $entry = $notice->asAtomEntry(false, false, true);
192 $element = $this->_entryToElement($entry, true);
194 $author = ActivityUtils::child($element, 'author');
195 $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
197 $this->assertFalse(is_null($author));
198 $this->assertTrue(is_null($actor)); // <activity:actor> is obsolete, no longer added
201 public function testAuthorContent()
203 $notice = $this->_fakeNotice();
207 $entry = $notice->asAtomEntry(false, false, true);
209 $element = $this->_entryToElement($entry, true);
211 $author = ActivityUtils::child($element, 'author');
213 $this->assertEquals($this->author1->getNickname(), ActivityUtils::childContent($author, 'name'));
214 $this->assertEquals($this->author1->getUri(), ActivityUtils::childContent($author, 'uri'));
218 * We no longer create <activity:actor> entries, they have merged to <atom:author>
220 public function testActorContent()
222 $notice = $this->_fakeNotice();
226 $entry = $notice->asAtomEntry(false, false, true);
228 $element = $this->_entryToElement($entry, true);
230 $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
232 $this->assertEquals($actor, null);
235 public function testReplyLink()
237 $orig = $this->_fakeNotice($this->targetUser1);
239 $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
241 $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
243 $entry = $reply->asAtomEntry();
245 $element = $this->_entryToElement($entry, true);
247 $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0');
249 $this->assertNotNull($irt);
250 $this->assertEquals($orig->getUri(), $irt->getAttribute('ref'));
251 $this->assertEquals($orig->getUrl(), $irt->getAttribute('href'));
254 public function testReplyAttention()
256 $orig = $this->_fakeNotice($this->targetUser1);
258 $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
260 $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
262 $entry = $reply->asAtomEntry();
264 $element = $this->_entryToElement($entry, true);
266 $this->assertEquals($this->targetUser1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
269 public function testMultipleReplyAttention()
271 $orig = $this->_fakeNotice($this->targetUser1);
273 $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
275 $reply = Notice::saveNew($this->targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
277 $text = "@" . $this->targetUser1->nickname . " @" . $this->targetUser2->nickname . " reply text " . common_random_hexstr(4);
279 $reply2 = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id));
281 $entry = $reply2->asAtomEntry();
283 $element = $this->_entryToElement($entry, true);
285 $links = ActivityUtils::getLinks($element, 'mentioned');
287 $this->assertEquals(2, count($links));
291 foreach ($links as $link) {
292 $hrefs[] = $link->getAttribute('href');
295 $this->assertTrue(in_array($this->targetUser1->getUri(), $hrefs));
296 $this->assertTrue(in_array($this->targetUser2->getUri(), $hrefs));
298 $links = ActivityUtils::getLinks($element, 'mentioned');
300 $this->assertEquals(2, count($links));
304 foreach ($links as $link) {
305 $hrefs[] = $link->getAttribute('href');
308 $this->assertTrue(in_array($this->targetUser1->getUri(), $hrefs));
309 $this->assertTrue(in_array($this->targetUser2->getUri(), $hrefs));
312 public function testGroupPostAttention()
314 $text = "!" . $this->targetGroup1->nickname . " reply text " . common_random_hexstr(4);
316 $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
318 $entry = $notice->asAtomEntry();
320 $element = $this->_entryToElement($entry, true);
322 $this->assertEquals($this->targetGroup1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
325 public function testMultipleGroupPostAttention()
327 $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_random_hexstr(4);
329 $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
331 $entry = $notice->asAtomEntry();
333 $element = $this->_entryToElement($entry, true);
335 $links = ActivityUtils::getLinks($element, 'mentioned');
337 $this->assertEquals(2, count($links));
341 foreach ($links as $link) {
342 $hrefs[] = $link->getAttribute('href');
345 $this->assertTrue(in_array($this->targetGroup1->getUri(), $hrefs));
346 $this->assertTrue(in_array($this->targetGroup2->getUri(), $hrefs));
348 $links = ActivityUtils::getLinks($element, 'mentioned');
350 $this->assertEquals(2, count($links));
354 foreach ($links as $link) {
355 $hrefs[] = $link->getAttribute('href');
358 $this->assertTrue(in_array($this->targetGroup1->getUri(), $hrefs));
359 $this->assertTrue(in_array($this->targetGroup2->getUri(), $hrefs));
362 public function testRepeatLink()
364 $notice = $this->_fakeNotice($this->author1);
365 $repeat = $notice->repeat($this->author2->getProfile(), 'test');
367 $entry = $repeat->asAtomEntry();
369 $element = $this->_entryToElement($entry, true);
371 $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0");
373 $this->assertNotNull($forward);
374 $this->assertEquals($notice->getUri(), $forward->getAttribute('ref'));
375 $this->assertEquals($notice->getUrl(), $forward->getAttribute('href'));
378 public function testTag()
380 $tag1 = common_random_hexstr(4);
382 $notice = $this->_fakeNotice($this->author1, '#' . $tag1);
384 $entry = $notice->asAtomEntry();
386 $element = $this->_entryToElement($entry, true);
388 $category = ActivityUtils::child($element, 'category');
390 $this->assertNotNull($category);
391 $this->assertEquals($tag1, $category->getAttribute('term'));
394 public function testMultiTag()
396 $tag1 = common_random_hexstr(4);
397 $tag2 = common_random_hexstr(4);
399 $notice = $this->_fakeNotice($this->author1, '#' . $tag1 . ' #' . $tag2);
401 $entry = $notice->asAtomEntry();
403 $element = $this->_entryToElement($entry, true);
405 $categories = $element->getElementsByTagName('category');
407 $this->assertNotNull($categories);
408 $this->assertEquals(2, $categories->length);
412 for ($i = 0; $i < $categories->length; $i++) {
413 $cat = $categories->item($i);
414 $terms[] = $cat->getAttribute('term');
417 $this->assertTrue(in_array($tag1, $terms));
418 $this->assertTrue(in_array($tag2, $terms));
421 public function testGeotaggedActivity()
423 $notice = Notice::saveNew($this->author1->id, common_random_hexstr(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
425 $entry = $notice->asAtomEntry();
427 $element = $this->_entryToElement($entry, true);
429 $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
432 public function testNoticeInfo()
434 $notice = $this->_fakeNotice();
436 $entry = $notice->asAtomEntry();
438 $element = $this->_entryToElement($entry, true);
440 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
442 $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
443 $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
444 $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
445 $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
446 // $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
447 $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
450 public function testNoticeInfoRepeatOf()
452 $notice = $this->_fakeNotice();
454 $repeat = $notice->repeat($this->author2->getProfile(), 'test');
456 $entry = $repeat->asAtomEntry();
458 $element = $this->_entryToElement($entry, true);
460 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
462 $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
465 public function testNoticeInfoRepeated()
467 $notice = $this->_fakeNotice();
469 $repeat = $notice->repeat($this->author2->getProfile(), 'test');
471 $entry = $notice->asAtomEntry(false, false, false, $this->author2);
473 $element = $this->_entryToElement($entry, true);
475 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
477 $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));
479 $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
481 $element = $this->_entryToElement($entry, true);
483 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
485 $this->assertEquals('false', $noticeInfo->getAttribute('repeated'));
488 /* public function testNoticeInfoFave()
490 $notice = $this->_fakeNotice();
492 $fave = Fave::addNew($this->author2->getProfile(), $notice);
494 // Should be set if user has faved
496 $entry = $notice->asAtomEntry(false, false, false, $this->author2);
498 $element = $this->_entryToElement($entry, true);
500 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
502 $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));
504 // Shouldn't be set if user has not faved
506 $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
508 $element = $this->_entryToElement($entry, true);
510 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
512 $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
515 public function testConversationLink()
517 $orig = $this->_fakeNotice($this->targetUser1);
519 $text = "@" . $this->targetUser1->nickname . " reply text " . common_random_hexstr(4);
521 $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
523 $conv = Conversation::getKV('id', $reply->conversation);
525 $entry = $reply->asAtomEntry();
527 $element = $this->_entryToElement($entry, true);
529 $this->assertEquals($conv->getUri(), ActivityUtils::getLink($element, 'ostatus:conversation'));
532 function __destruct()
534 if (!is_null($this->author1)) {
535 $this->author1->delete();
538 if (!is_null($this->author2)) {
539 $this->author2->delete();
542 if (!is_null($this->targetUser1)) {
543 $this->targetUser1->delete();
546 if (!is_null($this->targetUser2)) {
547 $this->targetUser2->delete();
550 if (!is_null($this->targetGroup1)) {
551 $this->targetGroup1->delete();
554 if (!is_null($this->targetGroup2)) {
555 $this->targetGroup2->delete();
559 private function _fakeNotice($user = null, $text = null)
562 $user = $this->author1;
566 $text = "fake-o text-o " . common_random_hexstr(32);
569 return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
572 private function _entryToElement($entry, $namespace = false)
574 $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
577 $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
578 $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
579 $xml .= ' xmlns:georss="http://www.georss.org/georss"';
580 $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
581 $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
582 $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
583 $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
584 $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
586 $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
587 $doc = DOMDocument::loadXML($xml);
588 $feed = $doc->documentElement;
589 $entries = $feed->getElementsByTagName('entry');
591 return $entries->item(0);