From 73e2264c6aa7c1fa3a6e4b63f41b210af3c50597 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sun, 14 Feb 2010 13:19:32 -0500 Subject: test parsing a default atom feed for activities --- plugins/OStatus/lib/activity.php | 39 +++++++++++++--- plugins/OStatus/tests/ActivityParseTests.php | 67 ++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 6 deletions(-) diff --git a/plugins/OStatus/lib/activity.php b/plugins/OStatus/lib/activity.php index 11aab2848..048efda2c 100644 --- a/plugins/OStatus/lib/activity.php +++ b/plugins/OStatus/lib/activity.php @@ -68,9 +68,12 @@ class ActivityUtils $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK); foreach ($links as $link) { - if ($link->getAttributeNS(self::ATOM, self::REL) == 'alternate' && - $link->getAttributeNS(self::ATOM, self::TYPE) == 'text/html') { - return $link->getAttributeNS(self::ATOM, self::HREF); + + $rel = $link->getAttribute(self::REL); + $type = $link->getAttribute(self::TYPE); + + if ($rel == 'alternate' && $type == 'text/html') { + return $link->getAttribute(self::HREF); } } @@ -123,8 +126,9 @@ class ActivityObject const ID = 'id'; const SOURCE = 'source'; - const NAME = 'name'; - const URI = 'uri'; + const NAME = 'name'; + const URI = 'uri'; + const EMAIL = 'email'; public $type; public $id; @@ -154,11 +158,23 @@ class ActivityObject $this->title = $this->_childContent($element, self::NAME); $this->id = $this->_childContent($element, self::URI); + if (empty($this->id)) { + $email = $this->_childContent($element, self::EMAIL); + if (!empty($email)) { + // XXX: acct: ? + $this->id = 'mailto:'.$email; + } + } + } else { $this->type = $this->_childContent($element, Activity::OBJECTTYPE, Activity::SPEC); + if (empty($this->type)) { + $this->type = ActivityObject::NOTE; + } + $this->id = $this->_childContent($element, self::ID); $this->title = $this->_childContent($element, self::TITLE); $this->summary = $this->_childContent($element, self::SUMMARY); @@ -252,6 +268,7 @@ class Activity const AUTHOR = 'author'; const PUBLISHED = 'published'; + const UPDATED = 'updated'; public $actor; // an ActivityObject public $verb; // a string (the URL) @@ -281,7 +298,12 @@ class Activity $this->time = strtotime($pubEl->textContent); } else { // XXX technically an error; being liberal. Good idea...? - $this->time = null; + $updateEl = $this->_child($entry, self::UPDATED, self::ATOM); + if (!empty($updateEl)) { + $this->time = strtotime($updateEl->textContent); + } else { + $this->time = null; + } } $this->link = ActivityUtils::getLink($entry); @@ -317,6 +339,11 @@ class Activity } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) { $this->actor = new ActivityObject($authorEl); + + } else if (!empty($feed) && $authorEl = $this->_child($feed, self::AUTHOR, + self::ATOM)) { + + $this->actor = new ActivityObject($authorEl); } $contextEl = $this->_child($entry, self::CONTEXT); diff --git a/plugins/OStatus/tests/ActivityParseTests.php b/plugins/OStatus/tests/ActivityParseTests.php index 889fa892f..fa8bcdda2 100644 --- a/plugins/OStatus/tests/ActivityParseTests.php +++ b/plugins/OStatus/tests/ActivityParseTests.php @@ -25,6 +25,44 @@ class ActivityParseTests extends PHPUnit_Framework_TestCase $this->assertEquals($act->time, 1243860840); $this->assertEquals($act->verb, ActivityVerb::POST); } + + public function testExample3() + { + global $_example3; + $dom = DOMDocument::loadXML($_example3); + + $feed = $dom->documentElement; + + $entries = $feed->getElementsByTagName('entry'); + + $entry = $entries->item(0); + + $act = new Activity($entry, $feed); + + $this->assertFalse(empty($act)); + $this->assertEquals($act->time, 1071340202); + $this->assertEquals($act->link, 'http://example.org/2003/12/13/atom03.html'); + + $this->assertEquals($act->verb, ActivityVerb::POST); + + $this->assertFalse(empty($act->actor)); + $this->assertEquals($act->actor->type, ActivityObject::PERSON); + $this->assertEquals($act->actor->title, 'John Doe'); + $this->assertEquals($act->actor->id, 'mailto:johndoe@example.com'); + + $this->assertFalse(empty($act->object)); + $this->assertEquals($act->object->type, ActivityObject::NOTE); + $this->assertEquals($act->object->id, 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a'); + $this->assertEquals($act->object->title, 'Atom-Powered Robots Run Amok'); + $this->assertEquals($act->object->summary, 'Some text.'); + $this->assertEquals($act->object->link, 'http://example.org/2003/12/13/atom03.html'); + + $this->assertTrue(empty($act->context)); + $this->assertTrue(empty($act->target)); + + $this->assertEquals($act->entry, $entry); + $this->assertEquals($act->feed, $feed); + } } $_example1 = << EXAMPLE2; + +$_example3 = << + + + + Example Feed + A subtitle. + + + urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6 + 2003-12-13T18:30:02Z + + John Doe + johndoe@example.com + + + + Atom-Powered Robots Run Amok + + + + urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a + 2003-12-13T18:30:02Z + Some text. + + + +EXAMPLE3; -- cgit v1.2.3-54-g00ecf