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('STATUSNET', true);
13 require_once INSTALLDIR . '/lib/common.php';
15 class ActivityGenerationTests extends PHPUnit_Framework_TestCase
20 var $targetUser1 = null;
21 var $targetUser2 = null;
23 var $targetGroup1 = null;
24 var $targetGroup2 = null;
26 function __construct()
28 parent::__construct();
30 $authorNick1 = 'activitygenerationtestsuser' . common_good_rand(4);
31 $authorNick2 = 'activitygenerationtestsuser' . common_good_rand(4);
33 $targetNick1 = 'activitygenerationteststarget' . common_good_rand(4);
34 $targetNick2 = 'activitygenerationteststarget' . common_good_rand(4);
36 $groupNick1 = 'activitygenerationtestsgroup' . common_good_rand(4);
37 $groupNick2 = 'activitygenerationtestsgroup' . common_good_rand(4);
39 $this->author1 = User::register(array('nickname' => $authorNick1,
40 'email' => $authorNick1 . '@example.net',
41 'email_confirmed' => true));
43 $this->author2 = User::register(array('nickname' => $authorNick2,
44 'email' => $authorNick2 . '@example.net',
45 'email_confirmed' => true));
47 $this->targetUser1 = User::register(array('nickname' => $targetNick1,
48 'email' => $targetNick1 . '@example.net',
49 'email_confirmed' => true));
51 $this->targetUser2 = User::register(array('nickname' => $targetNick2,
52 'email' => $targetNick2 . '@example.net',
53 'email_confirmed' => true));
55 $this->targetGroup1 = User_group::register(array('nickname' => $groupNick1,
56 'userid' => $this->author1->id,
60 'description' => null,
64 $this->targetGroup2 = User_group::register(array('nickname' => $groupNick2,
65 'userid' => $this->author1->id,
69 'description' => null,
75 public function testBasicNoticeActivity()
77 $notice = $this->_fakeNotice();
79 $entry = $notice->asAtomEntry(true);
81 $element = $this->_entryToElement($entry, false);
83 $this->assertEquals($notice->uri, ActivityUtils::childContent($element, 'id'));
84 $this->assertEquals($notice->content, ActivityUtils::childContent($element, 'title'));
85 $this->assertEquals($notice->rendered, ActivityUtils::childContent($element, 'content'));
86 $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'published')));
87 $this->assertEquals(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'updated')));
88 $this->assertEquals(ActivityVerb::POST, ActivityUtils::childContent($element, 'verb', Activity::SPEC));
89 $this->assertEquals(ActivityObject::NOTE, ActivityUtils::childContent($element, 'object-type', Activity::SPEC));
92 public function testNamespaceFlag()
94 $notice = $this->_fakeNotice();
96 $entry = $notice->asAtomEntry(true);
98 $element = $this->_entryToElement($entry, false);
100 $this->assertTrue($element->hasAttribute('xmlns'));
101 $this->assertTrue($element->hasAttribute('xmlns:thr'));
102 $this->assertTrue($element->hasAttribute('xmlns:georss'));
103 $this->assertTrue($element->hasAttribute('xmlns:activity'));
104 $this->assertTrue($element->hasAttribute('xmlns:media'));
105 $this->assertTrue($element->hasAttribute('xmlns:poco'));
106 $this->assertTrue($element->hasAttribute('xmlns:ostatus'));
107 $this->assertTrue($element->hasAttribute('xmlns:statusnet'));
109 $entry = $notice->asAtomEntry(false);
111 $element = $this->_entryToElement($entry, true);
113 $this->assertFalse($element->hasAttribute('xmlns'));
114 $this->assertFalse($element->hasAttribute('xmlns:thr'));
115 $this->assertFalse($element->hasAttribute('xmlns:georss'));
116 $this->assertFalse($element->hasAttribute('xmlns:activity'));
117 $this->assertFalse($element->hasAttribute('xmlns:media'));
118 $this->assertFalse($element->hasAttribute('xmlns:poco'));
119 $this->assertFalse($element->hasAttribute('xmlns:ostatus'));
120 $this->assertFalse($element->hasAttribute('xmlns:statusnet'));
123 public function testSourceFlag()
125 $notice = $this->_fakeNotice();
127 // Test with no source
129 $entry = $notice->asAtomEntry(false, false);
131 $element = $this->_entryToElement($entry, true);
133 $source = ActivityUtils::child($element, 'source');
135 $this->assertNull($source);
139 $entry = $notice->asAtomEntry(false, true);
141 $element = $this->_entryToElement($entry, true);
143 $source = ActivityUtils::child($element, 'source');
145 $this->assertNotNull($source);
148 public function testSourceContent()
150 $notice = $this->_fakeNotice();
151 // make a time difference!
153 $notice2 = $this->_fakeNotice();
155 $entry = $notice->asAtomEntry(false, true);
157 $element = $this->_entryToElement($entry, true);
159 $source = ActivityUtils::child($element, 'source');
161 $atomUrl = common_local_url('ApiTimelineUser', array('id' => $this->author1->id, 'format' => 'atom'));
163 $profile = $this->author1->getProfile();
165 $this->assertEquals($atomUrl, ActivityUtils::childContent($source, 'id'));
166 $this->assertEquals($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml'));
167 $this->assertEquals($profile->profileurl, ActivityUtils::getPermalink($source));
168 $this->assertEquals(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated')));
169 // XXX: do we care here?
170 $this->assertFalse(is_null(ActivityUtils::childContent($source, 'title')));
171 $this->assertEquals(common_config('license', 'url'), ActivityUtils::getLink($source, 'license'));
174 public function testAuthorFlag()
176 $notice = $this->_fakeNotice();
178 // Test with no author
180 $entry = $notice->asAtomEntry(false, false, false);
182 $element = $this->_entryToElement($entry, true);
184 $this->assertNull(ActivityUtils::child($element, 'author'));
185 $this->assertNull(ActivityUtils::child($element, 'actor', Activity::SPEC));
189 $entry = $notice->asAtomEntry(false, false, true);
191 $element = $this->_entryToElement($entry, true);
193 $author = ActivityUtils::child($element, 'author');
194 $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
196 $this->assertFalse(is_null($author));
197 $this->assertTrue(is_null($actor)); // <activity:actor> is obsolete, no longer added
200 public function testAuthorContent()
202 $notice = $this->_fakeNotice();
206 $entry = $notice->asAtomEntry(false, false, true);
208 $element = $this->_entryToElement($entry, true);
210 $author = ActivityUtils::child($element, 'author');
212 $this->assertEquals($this->author1->nickname, ActivityUtils::childContent($author, 'name'));
213 $this->assertEquals($this->author1->uri, ActivityUtils::childContent($author, 'uri'));
217 * We no longer create <activity:actor> entries, they have merged to <atom:author>
219 public function testActorContent()
221 $notice = $this->_fakeNotice();
225 $entry = $notice->asAtomEntry(false, false, true);
227 $element = $this->_entryToElement($entry, true);
229 $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
231 $this->assertEquals($actor, null);
234 public function testReplyLink()
236 $orig = $this->_fakeNotice($this->targetUser1);
238 $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4);
240 $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
242 $entry = $reply->asAtomEntry();
244 $element = $this->_entryToElement($entry, true);
246 $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0');
248 $this->assertNotNull($irt);
249 $this->assertEquals($orig->uri, $irt->getAttribute('ref'));
250 $this->assertEquals($orig->bestUrl(), $irt->getAttribute('href'));
253 public function testReplyAttention()
255 $orig = $this->_fakeNotice($this->targetUser1);
257 $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4);
259 $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
261 $entry = $reply->asAtomEntry();
263 $element = $this->_entryToElement($entry, true);
265 $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'ostatus:attention'));
266 $this->assertEquals($this->targetUser1->uri, ActivityUtils::getLink($element, 'mentioned'));
269 public function testMultipleReplyAttention()
271 $orig = $this->_fakeNotice($this->targetUser1);
273 $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(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_good_rand(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, 'ostatus:attention');
287 $this->assertEquals(2, count($links));
291 foreach ($links as $link) {
292 $hrefs[] = $link->getAttribute('href');
295 $this->assertTrue(in_array($this->targetUser1->uri, $hrefs));
296 $this->assertTrue(in_array($this->targetUser2->uri, $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->uri, $hrefs));
309 $this->assertTrue(in_array($this->targetUser2->uri, $hrefs));
312 public function testGroupPostAttention()
314 $text = "!" . $this->targetGroup1->nickname . " reply text " . common_good_rand(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->uri, ActivityUtils::getLink($element, 'ostatus:attention'));
323 $this->assertEquals($this->targetGroup1->uri, ActivityUtils::getLink($element, 'mentioned'));
326 public function testMultipleGroupPostAttention()
328 $text = "!" . $this->targetGroup1->nickname . " !" . $this->targetGroup2->nickname . " reply text " . common_good_rand(4);
330 $notice = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null));
332 $entry = $notice->asAtomEntry();
334 $element = $this->_entryToElement($entry, true);
336 $links = ActivityUtils::getLinks($element, 'ostatus:attention');
338 $this->assertEquals(2, count($links));
342 foreach ($links as $link) {
343 $hrefs[] = $link->getAttribute('href');
346 $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs));
347 $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs));
349 $links = ActivityUtils::getLinks($element, 'mentioned');
351 $this->assertEquals(2, count($links));
355 foreach ($links as $link) {
356 $hrefs[] = $link->getAttribute('href');
359 $this->assertTrue(in_array($this->targetGroup1->uri, $hrefs));
360 $this->assertTrue(in_array($this->targetGroup2->uri, $hrefs));
363 public function testRepeatLink()
365 $notice = $this->_fakeNotice($this->author1);
366 $repeat = $notice->repeat($this->author2->id, 'test');
368 $entry = $repeat->asAtomEntry();
370 $element = $this->_entryToElement($entry, true);
372 $forward = ActivityUtils::child($element, 'forward', "http://ostatus.org/schema/1.0");
374 $this->assertNotNull($forward);
375 $this->assertEquals($notice->uri, $forward->getAttribute('ref'));
376 $this->assertEquals($notice->bestUrl(), $forward->getAttribute('href'));
379 public function testTag()
381 $tag1 = common_good_rand(4);
383 $notice = $this->_fakeNotice($this->author1, '#' . $tag1);
385 $entry = $notice->asAtomEntry();
387 $element = $this->_entryToElement($entry, true);
389 $category = ActivityUtils::child($element, 'category');
391 $this->assertNotNull($category);
392 $this->assertEquals($tag1, $category->getAttribute('term'));
395 public function testMultiTag()
397 $tag1 = common_good_rand(4);
398 $tag2 = common_good_rand(4);
400 $notice = $this->_fakeNotice($this->author1, '#' . $tag1 . ' #' . $tag2);
402 $entry = $notice->asAtomEntry();
404 $element = $this->_entryToElement($entry, true);
406 $categories = $element->getElementsByTagName('category');
408 $this->assertNotNull($categories);
409 $this->assertEquals(2, $categories->length);
413 for ($i = 0; $i < $categories->length; $i++) {
414 $cat = $categories->item($i);
415 $terms[] = $cat->getAttribute('term');
418 $this->assertTrue(in_array($tag1, $terms));
419 $this->assertTrue(in_array($tag2, $terms));
422 public function testGeotaggedActivity()
424 $notice = Notice::saveNew($this->author1->id, common_good_rand(4), 'test', array('uri' => null, 'lat' => 45.5, 'lon' => -73.6));
426 $entry = $notice->asAtomEntry();
428 $element = $this->_entryToElement($entry, true);
430 $this->assertEquals('45.5 -73.6', ActivityUtils::childContent($element, 'point', "http://www.georss.org/georss"));
433 public function testNoticeInfo()
435 $notice = $this->_fakeNotice();
437 $entry = $notice->asAtomEntry();
439 $element = $this->_entryToElement($entry, true);
441 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
443 $this->assertEquals($notice->id, $noticeInfo->getAttribute('local_id'));
444 $this->assertEquals($notice->source, $noticeInfo->getAttribute('source'));
445 $this->assertEquals('', $noticeInfo->getAttribute('repeat_of'));
446 $this->assertEquals('', $noticeInfo->getAttribute('repeated'));
447 $this->assertEquals('', $noticeInfo->getAttribute('favorite'));
448 $this->assertEquals('', $noticeInfo->getAttribute('source_link'));
451 public function testNoticeInfoRepeatOf()
453 $notice = $this->_fakeNotice();
455 $repeat = $notice->repeat($this->author2->id, 'test');
457 $entry = $repeat->asAtomEntry();
459 $element = $this->_entryToElement($entry, true);
461 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
463 $this->assertEquals($notice->id, $noticeInfo->getAttribute('repeat_of'));
466 public function testNoticeInfoRepeated()
468 $notice = $this->_fakeNotice();
470 $repeat = $notice->repeat($this->author2->id, 'test');
472 $entry = $notice->asAtomEntry(false, false, false, $this->author2);
474 $element = $this->_entryToElement($entry, true);
476 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
478 $this->assertEquals('true', $noticeInfo->getAttribute('repeated'));
480 $entry = $notice->asAtomEntry(false, false, false, $this->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('repeated'));
489 public function testNoticeInfoFave()
491 $notice = $this->_fakeNotice();
493 $fave = Fave::addNew($this->author2->getProfile(), $notice);
495 // Should be set if user has faved
497 $entry = $notice->asAtomEntry(false, false, false, $this->author2);
499 $element = $this->_entryToElement($entry, true);
501 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
503 $this->assertEquals('true', $noticeInfo->getAttribute('favorite'));
505 // Shouldn't be set if user has not faved
507 $entry = $notice->asAtomEntry(false, false, false, $this->targetUser1);
509 $element = $this->_entryToElement($entry, true);
511 $noticeInfo = ActivityUtils::child($element, 'notice_info', "http://status.net/schema/api/1/");
513 $this->assertEquals('false', $noticeInfo->getAttribute('favorite'));
516 public function testConversationLink()
518 $orig = $this->_fakeNotice($this->targetUser1);
520 $text = "@" . $this->targetUser1->nickname . " reply text " . common_good_rand(4);
522 $reply = Notice::saveNew($this->author1->id, $text, 'test', array('uri' => null, 'reply_to' => $orig->id));
524 $conv = Conversation::staticGet('id', $reply->conversation);
526 $entry = $reply->asAtomEntry();
528 $element = $this->_entryToElement($entry, true);
530 $this->assertEquals($conv->uri, ActivityUtils::getLink($element, 'ostatus:conversation'));
533 function __destruct()
535 if (!is_null($this->author1)) {
536 $this->author1->delete();
539 if (!is_null($this->author2)) {
540 $this->author2->delete();
543 if (!is_null($this->targetUser1)) {
544 $this->targetUser1->delete();
547 if (!is_null($this->targetUser2)) {
548 $this->targetUser2->delete();
551 if (!is_null($this->targetGroup1)) {
552 $this->targetGroup1->delete();
555 if (!is_null($this->targetGroup2)) {
556 $this->targetGroup2->delete();
560 private function _fakeNotice($user = null, $text = null)
563 $user = $this->author1;
567 $text = "fake-o text-o " . common_good_rand(32);
570 return Notice::saveNew($user->id, $text, 'test', array('uri' => null));
573 private function _entryToElement($entry, $namespace = false)
575 $xml = '<?xml version="1.0" encoding="utf-8"?>'."\n\n";
578 $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
579 $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
580 $xml .= ' xmlns:georss="http://www.georss.org/georss"';
581 $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
582 $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
583 $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
584 $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
585 $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
587 $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
588 $doc = DOMDocument::loadXML($xml);
589 $feed = $doc->documentElement;
590 $entries = $feed->getElementsByTagName('entry');
592 return $entries->item(0);