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
18 static $author1 = null;
19 static $author2 = null;
21 static $targetUser1 = null;
22 static $targetUser2 = null;
24 static $targetGroup1 = null;
25 static $targetGroup2 = null;
27 public static function setUpBeforeClass()
29 $authorNick1 = 'activitygenerationtestsuser' . common_random_hexstr(4);
30 $authorNick2 = 'activitygenerationtestsuser' . common_random_hexstr(4);
32 $targetNick1 = 'activitygenerationteststarget' . common_random_hexstr(4);
33 $targetNick2 = 'activitygenerationteststarget' . common_random_hexstr(4);
35 $groupNick1 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
36 $groupNick2 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
39 self::$author1 = User::register(array('nickname' => $authorNick1,
40 'email' => $authorNick1 . '@example.net',
41 'email_confirmed' => true));
43 self::$author2 = User::register(array('nickname' => $authorNick2,
44 'email' => $authorNick2 . '@example.net',
45 'email_confirmed' => true));
47 self::$targetUser1 = User::register(array('nickname' => $targetNick1,
48 'email' => $targetNick1 . '@example.net',
49 'email_confirmed' => true));
51 self::$targetUser2 = User::register(array('nickname' => $targetNick2,
52 'email' => $targetNick2 . '@example.net',
53 'email_confirmed' => true));
55 self::$targetGroup1 = User_group::register(array('nickname' => $groupNick1,
56 'userid' => self::$author1->id,
60 'description' => null,
64 self::$targetGroup2 = User_group::register(array('nickname' => $groupNick2,
65 'userid' => self::$author1->id,
69 'description' => null,
73 } catch (Exception $e) {
74 self::tearDownAfterClass();
79 public function testBasicNoticeActivity()
81 $notice = $this->_fakeNotice();
83 $entry = $notice->asAtomEntry(true);
85 $element = $this->_entryToElement($entry, false);
87 $this->assertEquals($notice->getUri(), ActivityUtils::childContent($element, 'id'));
88 $this->assertEquals('New note by '. self::$author1->nickname, ActivityUtils::childContent($element, 'title'));
89 $this->assertEquals($notice->rendered, ActivityUtils::childContent($element, 'content'));
90 $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'published')));
91 $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'updated')));
92 $this->assertEquals(ActivityVerb::POST, ActivityUtils::childContent($element, 'verb', Activity::SPEC));
93 $this->assertEquals(ActivityObject::NOTE, ActivityUtils::childContent($element, 'object-type', Activity::SPEC));
96 public function testNamespaceFlag()
98 $notice = $this->_fakeNotice();
100 $entry = $notice->asAtomEntry(true);
102 $element = $this->_entryToElement($entry, false);
104 $this->assertTrue($element->hasAttribute('xmlns'));
105 $this->assertTrue($element->hasAttribute('xmlns:thr'));
106 $this->assertTrue($element->hasAttribute('xmlns:georss'));
107 $this->assertTrue($element->hasAttribute('xmlns:activity'));
108 $this->assertTrue($element->hasAttribute('xmlns:media'));
109 $this->assertTrue($element->hasAttribute('xmlns:poco'));
110 $this->assertTrue($element->hasAttribute('xmlns:ostatus'));
111 $this->assertTrue($element->hasAttribute('xmlns:statusnet'));
113 $entry = $notice->asAtomEntry(false);
115 $element = $this->_entryToElement($entry, true);
117 $this->assertFalse($element->hasAttribute('xmlns'));
118 $this->assertFalse($element->hasAttribute('xmlns:thr'));
119 $this->assertFalse($element->hasAttribute('xmlns:georss'));
120 $this->assertFalse($element->hasAttribute('xmlns:activity'));
121 $this->assertFalse($element->hasAttribute('xmlns:media'));
122 $this->assertFalse($element->hasAttribute('xmlns:poco'));
123 $this->assertFalse($element->hasAttribute('xmlns:ostatus'));
124 $this->assertFalse($element->hasAttribute('xmlns:statusnet'));
127 public function testSourceFlag()
129 $notice = $this->_fakeNotice();
131 // Test with no source
133 $entry = $notice->asAtomEntry(false, false);
135 $element = $this->_entryToElement($entry, true);
137 $source = ActivityUtils::child($element, 'source');
139 $this->assertNull($source);
143 $entry = $notice->asAtomEntry(false, true);
145 $element = $this->_entryToElement($entry, true);
147 $source = ActivityUtils::child($element, 'source');
149 $this->assertNotNull($source);
152 public function testSourceContent()
154 $notice = $this->_fakeNotice();
155 // make a time difference!
157 $notice2 = $this->_fakeNotice();
159 $entry = $notice->asAtomEntry(false, true);
161 $element = $this->_entryToElement($entry, true);
163 $source = ActivityUtils::child($element, 'source');
165 $atomUrl = common_local_url('ApiTimelineUser', array('id' => self::$author1->id, 'format' => 'atom'));
167 $profile = self::$author1->getProfile();
169 $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id'));
170 $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml'));
171 $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source));
172 $this->assertEquals(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated')));
173 // XXX: do we care here?
174 $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title')));
175 $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license'));
178 public function testAuthorFlag()
180 $notice = $this->_fakeNotice();
182 // Test with no author
184 $entry = $notice->asAtomEntry(false, false, false);
186 $element = $this->_entryToElement($entry, true);
188 $this->assertNull(ActivityUtils::child($element, 'author'));
189 $this->assertNull(ActivityUtils::child($element, 'actor', Activity::SPEC));
193 $entry = $notice->asAtomEntry(false, false, true);
195 $element = $this->_entryToElement($entry, true);
197 $author = ActivityUtils::child($element, 'author');
198 $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
200 $this->assertFalse(is_null($author));
201 $this->assertTrue(is_null($actor)); // <activity:actor> is obsolete, no longer added
204 public function testAuthorContent()
206 $notice = $this->_fakeNotice();
210 $entry = $notice->asAtomEntry(false, false, true);
212 $element = $this->_entryToElement($entry, true);
214 $author = ActivityUtils::child($element, 'author');
216 $this->assertEquals(self::$author1->getNickname(), ActivityUtils::childContent($author, 'name'));
217 $this->assertEquals(self::$author1->getUri(), ActivityUtils::childContent($author, 'uri'));
221 * We no longer create <activity:actor> entries, they have merged to <atom:author>
223 public function testActorContent()
225 $notice = $this->_fakeNotice();
229 $entry = $notice->asAtomEntry(false, false, true);
231 $element = $this->_entryToElement($entry, true);
233 $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
235 $this->assertEquals($actor, null);
238 public function testReplyLink()
240 $orig = $this->_fakeNotice(self::$targetUser1);
242 $text = "@" . self::$targetUser1->nickname . " reply text " . common_random_hexstr(4);
244 $reply = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
246 $entry = $reply->asAtomEntry();
248 $element = $this->_entryToElement($entry, true);
250 $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0');
252 $this->assertNotNull($irt);
253 $this->assertEquals($orig->getUri(), $irt->getAttribute('ref'));
254 $this->assertEquals($orig->getUrl(), $irt->getAttribute('href'));
257 public function testReplyAttention()
259 $orig = $this->_fakeNotice(self::$targetUser1);
261 $text = "@" . self::$targetUser1->nickname . " reply text " . common_random_hexstr(4);
263 $reply = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
265 $entry = $reply->asAtomEntry();
267 $element = $this->_entryToElement($entry, true);
269 $this->assertEquals(self::$targetUser1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
272 public function testMultipleReplyAttention()
274 $orig = $this->_fakeNotice(self::$targetUser1);
276 $text = "@" . self::$targetUser1->nickname . " reply text " . common_random_hexstr(4);
278 $reply = Notice::saveNew(self::$targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
280 $text = "@" . self::$targetUser1->nickname . " @" . self::$targetUser2->nickname . " reply text " . common_random_hexstr(4);
282 $reply2 = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id));
284 $entry = $reply2->asAtomEntry();
286 $element = $this->_entryToElement($entry, true);
288 $links = ActivityUtils::getLinks($element, 'mentioned');
292 foreach ($links as $link) {
293 $hrefs[] = $link->getAttribute('href');
296 $this->assertTrue(in_array(self::$targetUser1->getUri(), $hrefs));
297 $this->assertTrue(in_array(self::$targetUser2->getUri(), $hrefs));
300 public function testGroupPostAttention()
302 $text = "!" . self::$targetGroup1->nickname . " reply text " . common_random_hexstr(4);
304 $notice = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null));
306 $entry = $notice->asAtomEntry();
308 $element = $this->_entryToElement($entry, true);
310 $this->assertEquals(self::$targetGroup1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
313 public function testMultipleGroupPostAttention()
315 $text = "!" . self::$targetGroup1->nickname . " !" . self::$targetGroup2->nickname . " reply text " . common_random_hexstr(4);
317 $notice = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null));
319 $entry = $notice->asAtomEntry();
321 $element = $this->_entryToElement($entry, true);
323 $links = ActivityUtils::getLinks($element, 'mentioned');
327 foreach ($links as $link) {
328 $hrefs[] = $link->getAttribute('href');
331 $this->assertTrue(in_array(self::$targetGroup1->getUri(), $hrefs));
332 $this->assertTrue(in_array(self::$targetGroup2->getUri(), $hrefs));
336 public function testRepeatLink()
338 $notice = $this->_fakeNotice(self::$author1);
339 $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
341 $entry = $repeat->asAtomEntry();
343 $element = $this->_entryToElement($entry, true);
345 $noticeInfo = ActivityUtils::child($element, 'notice_info', 'http://status.net/schema/api/1/');
347 $this->assertNotNull($noticeInfo);
348 $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
349 $this->assertEquals($repeat->id, $noticeInfo->getAttribute('local_id'));
352 public function testTag()
354 $tag1 = common_random_hexstr(4);
356 $notice = $this->_fakeNotice(self::$author1, '#' . $tag1);
358 $entry = $notice->asAtomEntry();
360 $element = $this->_entryToElement($entry, true);
362 $category = ActivityUtils::child($element, 'category');
364 $this->assertNotNull($category);
365 $this->assertEquals($tag1, $category->getAttribute('term'));
368 public function testMultiTag()
370 $tag1 = common_random_hexstr(4);
371 $tag2 = common_random_hexstr(4);
373 $notice = $this->_fakeNotice(self::$author1, '#' . $tag1 . ' #' . $tag2);
375 $entry = $notice->asAtomEntry();
377 $element = $this->_entryToElement($entry, true);
379 $categories = $element->getElementsByTagName('category');
381 $this->assertNotNull($categories);
382 $this->assertEquals(2, $categories->length);
386 for ($i = 0; $i < $categories->length; $i++) {
387 $cat = $categories->item($i);
388 $terms[] = $cat->getAttribute('term');
391 $this->assertTrue(in_array($tag1, $terms));
392 $this->assertTrue(in_array($tag2, $terms));
395 public function testGeotaggedActivity()
397 $notice = Notice::saveNew(self::$author1->id, common_random_hexstr(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
399 $entry = $notice->asAtomEntry();
401 $element = $this->_entryToElement($entry, true);
403 $this->assertEquals('45.5000000 -73.6000000', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
406 public function testNoticeInfo()
408 $notice = $this->_fakeNotice();
410 $entry = $notice->asAtomEntry();
412 $element = $this->_entryToElement($entry, true);
414 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
416 $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
417 $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
418 $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
419 $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
420 // $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
421 $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
424 public function testNoticeInfoRepeatOf()
426 $notice = $this->_fakeNotice();
428 $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
430 $entry = $repeat->asAtomEntry();
432 $element = $this->_entryToElement($entry, true);
434 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
436 $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
439 public function testNoticeInfoRepeated()
441 $notice = $this->_fakeNotice();
443 $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
445 $entry = $notice->asAtomEntry(false, false, false, self::$author2->getProfile());
447 $element = $this->_entryToElement($entry, true);
449 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
451 $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));
453 $entry = $notice->asAtomEntry(false, false, false, self::$targetUser1->getProfile());
455 $element = $this->_entryToElement($entry, true);
457 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
459 $this->assertEquals('false', $noticeInfo->getAttribute('repeated'));
462 /* public function testNoticeInfoFave()
464 $notice = $this->_fakeNotice();
466 $fave = Fave::addNew(self::$author2->getProfile(), $notice);
468 // Should be set if user has faved
470 $entry = $notice->asAtomEntry(false, false, false, self::$author2);
472 $element = $this->_entryToElement($entry, true);
474 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
476 $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));
478 // Shouldn't be set if user has not faved
480 $entry = $notice->asAtomEntry(false, false, false, self::$targetUser1);
482 $element = $this->_entryToElement($entry, true);
484 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
486 $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
489 public function testConversationLink()
491 $orig = $this->_fakeNotice(self::$targetUser1);
493 $text = "@" . self::$targetUser1->nickname . " reply text " . common_random_hexstr(4);
495 $reply = Notice::saveNew(self::$author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
497 $conv = Conversation::getKV('id', $reply->conversation);
499 $entry = $reply->asAtomEntry();
501 $element = $this->_entryToElement($entry, true);
503 $this->assertEquals($conv->getUrl(), ActivityUtils::getLink($element, 'ostatus:conversation'));
506 public static function tearDownAfterClass()
508 if (!is_null(self::$author1)) {
509 self::$author1->getProfile()->delete();
512 if (!is_null(self::$author2)) {
513 self::$author2->getProfile()->delete();
516 if (!is_null(self::$targetUser1)) {
517 self::$targetUser1->getProfile()->delete();
520 if (!is_null(self::$targetUser2)) {
521 self::$targetUser2->getProfile()->delete();
524 if (!is_null(self::$targetGroup1)) {
525 self::$targetGroup1->delete();
528 if (!is_null(self::$targetGroup2)) {
529 self::$targetGroup2->delete();
533 private function _fakeNotice($user = null, $text = null)
536 $user = self::$author1;
540 $text = "fake-o text-o " . common_random_hexstr(32);
543 return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
546 private function _entryToElement($entry, $namespace = false)
548 $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
551 $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
552 $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
553 $xml .= ' xmlns:georss="http://www.georss.org/georss"';
554 $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
555 $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
556 $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
557 $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
558 $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
560 $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
561 $doc = DOMDocument::loadXML($xml);
562 $feed = $doc->documentElement;
563 $entries = $feed->getElementsByTagName('entry');
565 return $entries->item(0);