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_good_rand(4);
32 $authorNick2 = 'activitygenerationtestsuser' . common_good_rand(4);
34 $targetNick1 = 'activitygenerationteststarget' . common_good_rand(4);
35 $targetNick2 = 'activitygenerationteststarget' . common_good_rand(4);
37 $groupNick1 = 'activitygenerationtestsgroup' . common_good_rand(4);
38 $groupNick2 = 'activitygenerationtestsgroup' . common_good_rand(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->uri, 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->nickname, ActivityUtils::childContent($author, 'name'));
214 $this->assertEquals($this->author1->uri, 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_good_rand(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->uri, $irt->getAttribute('ref'));
251 $this->assertEquals($orig->bestUrl(), $irt->getAttribute('href'));
254 public function testReplyAttention()
256 $orig = $this->_fakeNotice($this->targetUser1);
258 $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(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->uri, ActivityUtils::getLink($element, 'ostatus:attention'));
267 $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'mentioned'));
270 public function testMultipleReplyAttention()
272 $orig = $this->_fakeNotice($this->targetUser1);
274 $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4);
276 $reply = Notice::saveNew($this->targetUser2->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
278 $text = "@" . $this->targetUser1->nickname . " @" . $this->targetUser2->nickname . " reply text " . common_good_rand(4);
280 $reply2 = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $reply->id));
282 $entry = $reply2->asAtomEntry();
284 $element = $this->_entryToElement($entry, true);
286 $links = ActivityUtils::getLinks($element, 'ostatus:attention');
288 $this->assertEquals(2, count($links));
292 foreach ($links as $link) {
293 $hrefs[] = $link->getAttribute('href');
296 $this->assertTrue(in_array($this->targetUser1->uri, $hrefs));
297 $this->assertTrue(in_array($this->targetUser2->uri, $hrefs));
299 $links = ActivityUtils::getLinks($element, 'mentioned');
301 $this->assertEquals(2, count($links));
305 foreach ($links as $link) {
306 $hrefs[] = $link->getAttribute('href');
309 $this->assertTrue(in_array($this->targetUser1->uri, $hrefs));
310 $this->assertTrue(in_array($this->targetUser2->uri, $hrefs));
313 public function testGroupPostAttention()
315 $text = "!" . $this->targetGroup1->nickname . " reply text " . common_good_rand(4);
317 $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
319 $entry = $notice->asAtomEntry();
321 $element = $this->_entryToElement($entry, true);
323 $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'ostatus:attention'));
324 $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'mentioned'));
327 public function testMultipleGroupPostAttention()
329 $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_good_rand(4);
331 $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
333 $entry = $notice->asAtomEntry();
335 $element = $this->_entryToElement($entry, true);
337 $links = ActivityUtils::getLinks($element, 'ostatus:attention');
339 $this->assertEquals(2, count($links));
343 foreach ($links as $link) {
344 $hrefs[] = $link->getAttribute('href');
347 $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs));
348 $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs));
350 $links = ActivityUtils::getLinks($element, 'mentioned');
352 $this->assertEquals(2, count($links));
356 foreach ($links as $link) {
357 $hrefs[] = $link->getAttribute('href');
360 $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs));
361 $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs));
364 public function testRepeatLink()
366 $notice = $this->_fakeNotice($this->author1);
367 $repeat = $notice->repeat($this->author2->id, 'test');
369 $entry = $repeat->asAtomEntry();
371 $element = $this->_entryToElement($entry, true);
373 $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0");
375 $this->assertNotNull($forward);
376 $this->assertEquals($notice->uri, $forward->getAttribute('ref'));
377 $this->assertEquals($notice->bestUrl(), $forward->getAttribute('href'));
380 public function testTag()
382 $tag1 = common_good_rand(4);
384 $notice = $this->_fakeNotice($this->author1, '#' . $tag1);
386 $entry = $notice->asAtomEntry();
388 $element = $this->_entryToElement($entry, true);
390 $category = ActivityUtils::child($element, 'category');
392 $this->assertNotNull($category);
393 $this->assertEquals($tag1, $category->getAttribute('term'));
396 public function testMultiTag()
398 $tag1 = common_good_rand(4);
399 $tag2 = common_good_rand(4);
401 $notice = $this->_fakeNotice($this->author1, '#' . $tag1 . ' #' . $tag2);
403 $entry = $notice->asAtomEntry();
405 $element = $this->_entryToElement($entry, true);
407 $categories = $element->getElementsByTagName('category');
409 $this->assertNotNull($categories);
410 $this->assertEquals(2, $categories->length);
414 for ($i = 0; $i < $categories->length; $i++) {
415 $cat = $categories->item($i);
416 $terms[] = $cat->getAttribute('term');
419 $this->assertTrue(in_array($tag1, $terms));
420 $this->assertTrue(in_array($tag2, $terms));
423 public function testGeotaggedActivity()
425 $notice = Notice::saveNew($this->author1->id, common_good_rand(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
427 $entry = $notice->asAtomEntry();
429 $element = $this->_entryToElement($entry, true);
431 $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
434 public function testNoticeInfo()
436 $notice = $this->_fakeNotice();
438 $entry = $notice->asAtomEntry();
440 $element = $this->_entryToElement($entry, true);
442 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
444 $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
445 $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
446 $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
447 $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
448 $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
449 $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
452 public function testNoticeInfoRepeatOf()
454 $notice = $this->_fakeNotice();
456 $repeat = $notice->repeat($this->author2->id, 'test');
458 $entry = $repeat->asAtomEntry();
460 $element = $this->_entryToElement($entry, true);
462 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
464 $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
467 public function testNoticeInfoRepeated()
469 $notice = $this->_fakeNotice();
471 $repeat = $notice->repeat($this->author2->id, 'test');
473 $entry = $notice->asAtomEntry(false, false, false, $this->author2);
475 $element = $this->_entryToElement($entry, true);
477 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
479 $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));
481 $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
483 $element = $this->_entryToElement($entry, true);
485 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
487 $this->assertEquals('false', $noticeInfo->getAttribute('repeated'));
490 public function testNoticeInfoFave()
492 $notice = $this->_fakeNotice();
494 $fave = Fave::addNew($this->author2->getProfile(), $notice);
496 // Should be set if user has faved
498 $entry = $notice->asAtomEntry(false, false, false, $this->author2);
500 $element = $this->_entryToElement($entry, true);
502 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
504 $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));
506 // Shouldn't be set if user has not faved
508 $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
510 $element = $this->_entryToElement($entry, true);
512 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
514 $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
517 public function testConversationLink()
519 $orig = $this->_fakeNotice($this->targetUser1);
521 $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4);
523 $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
525 $conv = Conversation::getKV('id', $reply->conversation);
527 $entry = $reply->asAtomEntry();
529 $element = $this->_entryToElement($entry, true);
531 $this->assertEquals($conv->uri, ActivityUtils::getLink($element, 'ostatus:conversation'));
534 function __destruct()
536 if (!is_null($this->author1)) {
537 $this->author1->delete();
540 if (!is_null($this->author2)) {
541 $this->author2->delete();
544 if (!is_null($this->targetUser1)) {
545 $this->targetUser1->delete();
548 if (!is_null($this->targetUser2)) {
549 $this->targetUser2->delete();
552 if (!is_null($this->targetGroup1)) {
553 $this->targetGroup1->delete();
556 if (!is_null($this->targetGroup2)) {
557 $this->targetGroup2->delete();
561 private function _fakeNotice($user = null, $text = null)
564 $user = $this->author1;
568 $text = "fake-o text-o " . common_good_rand(32);
571 return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
574 private function _entryToElement($entry, $namespace = false)
576 $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
579 $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
580 $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
581 $xml .= ' xmlns:georss="http://www.georss.org/georss"';
582 $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
583 $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
584 $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
585 $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
586 $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
588 $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
589 $doc = DOMDocument::loadXML($xml);
590 $feed = $doc->documentElement;
591 $entries = $feed->getElementsByTagName('entry');
593 return $entries->item(0);