123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598 |
- <?php
- namespace Tests\Unit;
- if (!defined('INSTALLDIR')) {
- define('INSTALLDIR', dirname(dirname(__DIR__)));
- }
- if (!defined('PUBLICDIR')) {
- define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
- }
- if (!defined('GNUSOCIAL')) {
- define('GNUSOCIAL', true);
- }
- if (!defined('STATUSNET')) {
- define('STATUSNET', true);
- }
- use Activity;
- use ActivityObject;
- use ActivityUtils;
- use ActivityVerb;
- use Conversation;
- use DOMDocument;
- use Exception;
- use Notice;
- use PHPUnit\Framework\TestCase;
- use User;
- use User_group;
- require_once INSTALLDIR . '/lib/util/common.php';
- final class ActivityGenerationTests extends TestCase
- {
- public static $author1 = null;
- public static $author2 = null;
- public static $targetUser1 = null;
- public static $targetUser2 = null;
- public static $targetGroup1 = null;
- public static $targetGroup2 = null;
- public static function setUpBeforeClass(): void
- {
- $authorNick1 = 'activitygenerationtestsuser' . common_random_hexstr(4);
- $authorNick2 = 'activitygenerationtestsuser' . common_random_hexstr(4);
- $targetNick1 = 'activitygenerationteststarget' . common_random_hexstr(4);
- $targetNick2 = 'activitygenerationteststarget' . common_random_hexstr(4);
- $groupNick1 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
- $groupNick2 = 'activitygenerationtestsgroup' . common_random_hexstr(4);
- try {
- self::$author1 = User::register(['nickname' => $authorNick1,
- 'email' => $authorNick1 . '@example.net',
- 'email_confirmed' => true,]);
- self::$author2 = User::register(['nickname' => $authorNick2,
- 'email' => $authorNick2 . '@example.net',
- 'email_confirmed' => true,]);
- self::$targetUser1 = User::register(['nickname' => $targetNick1,
- 'email' => $targetNick1 . '@example.net',
- 'email_confirmed' => true,]);
- self::$targetUser2 = User::register(['nickname' => $targetNick2,
- 'email' => $targetNick2 . '@example.net',
- 'email_confirmed' => true,]);
- self::$targetGroup1 = User_group::register(['nickname' => $groupNick1,
- 'userid' => self::$author1->id,
- 'aliases' => [],
- 'local' => true,
- 'location' => null,
- 'description' => null,
- 'fullname' => null,
- 'homepage' => null,
- 'mainpage' => null,]);
- self::$targetGroup2 = User_group::register(['nickname' => $groupNick2,
- 'userid' => self::$author1->id,
- 'aliases' => [],
- 'local' => true,
- 'location' => null,
- 'description' => null,
- 'fullname' => null,
- 'homepage' => null,
- 'mainpage' => null,]);
- } catch (Exception $e) {
- static::tearDownAfterClass();
- throw $e;
- }
- }
- public function testBasicNoticeActivity()
- {
- $notice = $this->_fakeNotice();
- $entry = $notice->asAtomEntry(true);
- $element = $this->_entryToElement($entry, false);
- static::assertSame($notice->getUri(), ActivityUtils::childContent($element, 'id'));
- static::assertSame('New note by ' . self::$author1->nickname, ActivityUtils::childContent($element, 'title'));
- static::assertSame($notice->rendered, ActivityUtils::childContent($element, 'content'));
- static::assertSame(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'published')));
- static::assertSame(strtotime($notice->created), strtotime(ActivityUtils::childContent($element, 'updated')));
- static::assertSame(ActivityVerb::POST, ActivityUtils::childContent($element, 'verb', Activity::SPEC));
- static::assertSame(ActivityObject::NOTE, ActivityUtils::childContent($element, 'object-type', Activity::SPEC));
- }
- public function testNamespaceFlag()
- {
- $notice = $this->_fakeNotice();
- $entry = $notice->asAtomEntry(true);
- $element = $this->_entryToElement($entry, false);
- static::assertTrue($element->hasAttribute('xmlns'));
- static::assertTrue($element->hasAttribute('xmlns:thr'));
- static::assertTrue($element->hasAttribute('xmlns:georss'));
- static::assertTrue($element->hasAttribute('xmlns:activity'));
- static::assertTrue($element->hasAttribute('xmlns:media'));
- static::assertTrue($element->hasAttribute('xmlns:poco'));
- static::assertTrue($element->hasAttribute('xmlns:ostatus'));
- static::assertTrue($element->hasAttribute('xmlns:statusnet'));
- $entry = $notice->asAtomEntry(false);
- $element = $this->_entryToElement($entry, true);
- static::assertFalse($element->hasAttribute('xmlns'));
- static::assertFalse($element->hasAttribute('xmlns:thr'));
- static::assertFalse($element->hasAttribute('xmlns:georss'));
- static::assertFalse($element->hasAttribute('xmlns:activity'));
- static::assertFalse($element->hasAttribute('xmlns:media'));
- static::assertFalse($element->hasAttribute('xmlns:poco'));
- static::assertFalse($element->hasAttribute('xmlns:ostatus'));
- static::assertFalse($element->hasAttribute('xmlns:statusnet'));
- }
- public function testSourceFlag()
- {
- $notice = $this->_fakeNotice();
-
- $entry = $notice->asAtomEntry(false, false);
- $element = $this->_entryToElement($entry, true);
- $source = ActivityUtils::child($element, 'source');
- static::assertNull($source);
-
- $entry = $notice->asAtomEntry(false, true);
- $element = $this->_entryToElement($entry, true);
- $source = ActivityUtils::child($element, 'source');
- static::assertNotNull($source);
- }
- public function testSourceContent()
- {
- $notice = $this->_fakeNotice();
-
- sleep(2);
- $notice2 = $this->_fakeNotice();
- $entry = $notice->asAtomEntry(false, true);
- $element = $this->_entryToElement($entry, true);
- $source = ActivityUtils::child($element, 'source');
- $atomUrl = common_local_url('ApiTimelineUser', ['id' => self::$author1->id, 'format' => 'atom']);
- $profile = self::$author1->getProfile();
- static::assertSame($atomUrl, ActivityUtils::childContent($source, 'id'));
- static::assertSame($atomUrl, ActivityUtils::getLink($source, 'self', 'application/atom+xml'));
- static::assertSame($profile->profileurl, ActivityUtils::getPermalink($source));
- static::assertSame(strtotime($notice2->created), strtotime(ActivityUtils::childContent($source, 'updated')));
-
- static::assertFalse(is_null(ActivityUtils::childContent($source, 'title')));
- static::assertSame(common_config('license', 'url'), ActivityUtils::getLink($source, 'license'));
- }
- public function testAuthorFlag()
- {
- $notice = $this->_fakeNotice();
-
- $entry = $notice->asAtomEntry(false, false, false);
- $element = $this->_entryToElement($entry, true);
- static::assertNull(ActivityUtils::child($element, 'author'));
- static::assertNull(ActivityUtils::child($element, 'actor', Activity::SPEC));
-
- $entry = $notice->asAtomEntry(false, false, true);
- $element = $this->_entryToElement($entry, true);
- $author = ActivityUtils::child($element, 'author');
- $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
- static::assertFalse(is_null($author));
- static::assertTrue(is_null($actor));
- }
- public function testAuthorContent()
- {
- $notice = $this->_fakeNotice();
-
- $entry = $notice->asAtomEntry(false, false, true);
- $element = $this->_entryToElement($entry, true);
- $author = ActivityUtils::child($element, 'author');
- static::assertSame(self::$author1->getNickname(), ActivityUtils::childContent($author, 'name'));
- static::assertSame(self::$author1->getUri(), ActivityUtils::childContent($author, 'uri'));
- }
-
- public function testActorContent()
- {
- $notice = $this->_fakeNotice();
-
- $entry = $notice->asAtomEntry(false, false, true);
- $element = $this->_entryToElement($entry, true);
- $actor = ActivityUtils::child($element, 'actor', Activity::SPEC);
- static::assertSame($actor, null);
- }
- public function testReplyLink()
- {
- $orig = $this->_fakeNotice(self::$targetUser1);
- $text = '@' . self::$targetUser1->nickname . ' reply text ' . common_random_hexstr(4);
- $reply = Notice::saveNew(self::$author1->id, $text, 'test', ['uri' => null, 'reply_to' => $orig->id]);
- $entry = $reply->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- $irt = ActivityUtils::child($element, 'in-reply-to', 'http://purl.org/syndication/thread/1.0');
- static::assertNotNull($irt);
- static::assertSame($orig->getUri(), $irt->getAttribute('ref'));
- static::assertSame($orig->getUrl(), $irt->getAttribute('href'));
- }
- public function testReplyAttention()
- {
- $orig = $this->_fakeNotice(self::$targetUser1);
- $text = '@' . self::$targetUser1->nickname . ' reply text ' . common_random_hexstr(4);
- $reply = Notice::saveNew(self::$author1->id, $text, 'test', ['uri' => null, 'reply_to' => $orig->id]);
- $entry = $reply->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- static::assertSame(self::$targetUser1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
- }
- public function testMultipleReplyAttention()
- {
- $orig = $this->_fakeNotice(self::$targetUser1);
- $text = '@' . self::$targetUser1->nickname . ' reply text ' . common_random_hexstr(4);
- $reply = Notice::saveNew(self::$targetUser2->id, $text, 'test', ['uri' => null, 'reply_to' => $orig->id]);
- $text = '@' . self::$targetUser1->nickname . ' @' . self::$targetUser2->nickname . ' reply text ' . common_random_hexstr(4);
- $reply2 = Notice::saveNew(self::$author1->id, $text, 'test', ['uri' => null, 'reply_to' => $reply->id]);
- $entry = $reply2->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- $links = ActivityUtils::getLinks($element, 'mentioned');
- $hrefs = [];
- foreach ($links as $link) {
- $hrefs[] = $link->getAttribute('href');
- }
- static::assertTrue(in_array(self::$targetUser1->getUri(), $hrefs));
- static::assertTrue(in_array(self::$targetUser2->getUri(), $hrefs));
- }
- public function testGroupPostAttention()
- {
- $text = '!' . self::$targetGroup1->nickname . ' reply text ' . common_random_hexstr(4);
- $notice = Notice::saveNew(self::$author1->id, $text, 'test', ['uri' => null]);
- $entry = $notice->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- static::assertSame(self::$targetGroup1->getUri(), ActivityUtils::getLink($element, 'mentioned'));
- }
- public function testMultipleGroupPostAttention()
- {
- $text = '!' . self::$targetGroup1->nickname . ' !' . self::$targetGroup2->nickname . ' reply text ' . common_random_hexstr(4);
- $notice = Notice::saveNew(self::$author1->id, $text, 'test', ['uri' => null]);
- $entry = $notice->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- $links = ActivityUtils::getLinks($element, 'mentioned');
- $hrefs = [];
- foreach ($links as $link) {
- $hrefs[] = $link->getAttribute('href');
- }
- static::assertTrue(in_array(self::$targetGroup1->getUri(), $hrefs));
- static::assertTrue(in_array(self::$targetGroup2->getUri(), $hrefs));
- }
- public function testRepeatLink()
- {
- $notice = $this->_fakeNotice(self::$author1);
- $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
- $entry = $repeat->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- $noticeInfo = ActivityUtils::child($element, 'notice_info', 'http://status.net/schema/api/1/');
- static::assertNotNull($noticeInfo);
- static::assertSame($notice->id, $noticeInfo->getAttribute('repeat_of'));
- static::assertSame($repeat->id, $noticeInfo->getAttribute('local_id'));
- }
- public function testTag()
- {
- $tag1 = common_random_hexstr(4);
- $notice = $this->_fakeNotice(self::$author1, '#' . $tag1);
- $entry = $notice->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- $category = ActivityUtils::child($element, 'category');
- static::assertNotNull($category);
- static::assertSame($tag1, $category->getAttribute('term'));
- }
- public function testMultiTag()
- {
- $tag1 = common_random_hexstr(4);
- $tag2 = common_random_hexstr(4);
- $notice = $this->_fakeNotice(self::$author1, '#' . $tag1 . ' #' . $tag2);
- $entry = $notice->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- $categories = $element->getElementsByTagName('category');
- static::assertNotNull($categories);
- static::assertSame(2, $categories->length);
- $terms = [];
- for ($i = 0; $i < $categories->length; ++$i) {
- $cat = $categories->item($i);
- $terms[] = $cat->getAttribute('term');
- }
- static::assertTrue(in_array($tag1, $terms));
- static::assertTrue(in_array($tag2, $terms));
- }
- public function testGeotaggedActivity()
- {
- $notice = Notice::saveNew(self::$author1->id, common_random_hexstr(4), 'test', ['uri' => null, 'lat' => 45.5, 'lon' => -73.6]);
- $entry = $notice->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- static::assertSame('45.5000000 -73.6000000', ActivityUtils::childContent($element, 'point', 'http://www.georss.org/georss'));
- }
- public function testNoticeInfo()
- {
- $notice = $this->_fakeNotice();
- $entry = $notice->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- $noticeInfo = ActivityUtils::child($element, 'notice_info', 'http://status.net/schema/api/1/');
- static::assertSame($notice->id, $noticeInfo->getAttribute('local_id'));
- static::assertSame($notice->source, $noticeInfo->getAttribute('source'));
- static::assertSame('', $noticeInfo->getAttribute('repeat_of'));
- static::assertSame('', $noticeInfo->getAttribute('repeated'));
- static::assertSame('', $noticeInfo->getAttribute('source_link'));
- }
- public function testNoticeInfoRepeatOf()
- {
- $notice = $this->_fakeNotice();
- $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
- $entry = $repeat->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- $noticeInfo = ActivityUtils::child($element, 'notice_info', 'http://status.net/schema/api/1/');
- static::assertSame($notice->id, $noticeInfo->getAttribute('repeat_of'));
- }
- public function testNoticeInfoRepeated()
- {
- $notice = $this->_fakeNotice();
- $repeat = $notice->repeat(self::$author2->getProfile(), 'test');
- $entry = $notice->asAtomEntry(false, false, false, self::$author2->getProfile());
- $element = $this->_entryToElement($entry, true);
- $noticeInfo = ActivityUtils::child($element, 'notice_info', 'http://status.net/schema/api/1/');
- static::assertSame('true', $noticeInfo->getAttribute('repeated'));
- $entry = $notice->asAtomEntry(false, false, false, self::$targetUser1->getProfile());
- $element = $this->_entryToElement($entry, true);
- $noticeInfo = ActivityUtils::child($element, 'notice_info', 'http://status.net/schema/api/1/');
- static::assertSame('false', $noticeInfo->getAttribute('repeated'));
- }
-
- public function testConversationLink()
- {
- $orig = $this->_fakeNotice(self::$targetUser1);
- $text = '@' . self::$targetUser1->nickname . ' reply text ' . common_random_hexstr(4);
- $reply = Notice::saveNew(self::$author1->id, $text, 'test', ['uri' => null, 'reply_to' => $orig->id]);
- $conv = Conversation::getKV('id', $reply->conversation);
- $entry = $reply->asAtomEntry();
- $element = $this->_entryToElement($entry, true);
- static::assertSame($conv->getUrl(), ActivityUtils::getLink($element, 'ostatus:conversation'));
- }
- public static function tearDownAfterClass(): void
- {
- if (!is_null(self::$author1)) {
- self::$author1->getProfile()->delete();
- }
- if (!is_null(self::$author2)) {
- self::$author2->getProfile()->delete();
- }
- if (!is_null(self::$targetUser1)) {
- self::$targetUser1->getProfile()->delete();
- }
- if (!is_null(self::$targetUser2)) {
- self::$targetUser2->getProfile()->delete();
- }
- if (!is_null(self::$targetGroup1)) {
- self::$targetGroup1->delete();
- }
- if (!is_null(self::$targetGroup2)) {
- self::$targetGroup2->delete();
- }
- }
- private function _fakeNotice($user = null, $text = null)
- {
- if (empty($user)) {
- $user = self::$author1;
- }
- if (empty($text)) {
- $text = 'fake-o text-o ' . common_random_hexstr(32);
- }
- return Notice::saveNew($user->id, $text, 'test', ['uri' => null]);
- }
- private function _entryToElement($entry, $namespace = false)
- {
- $xml = '<?xml version="1.0" encoding="utf-8"?>' . "\n\n";
- $xml .= '<feed';
- if ($namespace) {
- $xml .= ' xmlns="http://www.w3.org/2005/Atom"';
- $xml .= ' xmlns:thr="http://purl.org/syndication/thread/1.0"';
- $xml .= ' xmlns:georss="http://www.georss.org/georss"';
- $xml .= ' xmlns:activity="http://activitystrea.ms/spec/1.0/"';
- $xml .= ' xmlns:media="http://purl.org/syndication/atommedia"';
- $xml .= ' xmlns:poco="http://portablecontacts.net/spec/1.0"';
- $xml .= ' xmlns:ostatus="http://ostatus.org/schema/1.0"';
- $xml .= ' xmlns:statusnet="http://status.net/schema/api/1/"';
- }
- $xml .= '>' . "\n" . $entry . "\n" . '</feed>' . "\n";
- $doc = new DOMDocument();
- $doc->loadXML($xml);
- $feed = $doc->documentElement;
- $entries = $feed->getElementsByTagName('entry');
- return $entries->item(0);
- }
- }
|