test parsing a default atom feed for activities

This commit is contained in:
Evan Prodromou 2010-02-14 13:19:32 -05:00
parent f78cf31303
commit 73e2264c6a
2 changed files with 100 additions and 6 deletions

View File

@ -68,9 +68,12 @@ class ActivityUtils
$links = $element->getElementsByTagnameNS(self::ATOM, self::LINK); $links = $element->getElementsByTagnameNS(self::ATOM, self::LINK);
foreach ($links as $link) { foreach ($links as $link) {
if ($link->getAttributeNS(self::ATOM, self::REL) == 'alternate' &&
$link->getAttributeNS(self::ATOM, self::TYPE) == 'text/html') { $rel = $link->getAttribute(self::REL);
return $link->getAttributeNS(self::ATOM, self::HREF); $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 ID = 'id';
const SOURCE = 'source'; const SOURCE = 'source';
const NAME = 'name'; const NAME = 'name';
const URI = 'uri'; const URI = 'uri';
const EMAIL = 'email';
public $type; public $type;
public $id; public $id;
@ -154,11 +158,23 @@ class ActivityObject
$this->title = $this->_childContent($element, self::NAME); $this->title = $this->_childContent($element, self::NAME);
$this->id = $this->_childContent($element, self::URI); $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 { } else {
$this->type = $this->_childContent($element, Activity::OBJECTTYPE, $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
Activity::SPEC); Activity::SPEC);
if (empty($this->type)) {
$this->type = ActivityObject::NOTE;
}
$this->id = $this->_childContent($element, self::ID); $this->id = $this->_childContent($element, self::ID);
$this->title = $this->_childContent($element, self::TITLE); $this->title = $this->_childContent($element, self::TITLE);
$this->summary = $this->_childContent($element, self::SUMMARY); $this->summary = $this->_childContent($element, self::SUMMARY);
@ -252,6 +268,7 @@ class Activity
const AUTHOR = 'author'; const AUTHOR = 'author';
const PUBLISHED = 'published'; const PUBLISHED = 'published';
const UPDATED = 'updated';
public $actor; // an ActivityObject public $actor; // an ActivityObject
public $verb; // a string (the URL) public $verb; // a string (the URL)
@ -281,7 +298,12 @@ class Activity
$this->time = strtotime($pubEl->textContent); $this->time = strtotime($pubEl->textContent);
} else { } else {
// XXX technically an error; being liberal. Good idea...? // 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); $this->link = ActivityUtils::getLink($entry);
@ -317,6 +339,11 @@ class Activity
} else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) { } else if ($authorEl = $this->_child($entry, self::AUTHOR, self::ATOM)) {
$this->actor = new ActivityObject($authorEl); $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); $contextEl = $this->_child($entry, self::CONTEXT);

View File

@ -25,6 +25,44 @@ class ActivityParseTests extends PHPUnit_Framework_TestCase
$this->assertEquals($act->time, 1243860840); $this->assertEquals($act->time, 1243860840);
$this->assertEquals($act->verb, ActivityVerb::POST); $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 = <<<EXAMPLE1 $_example1 = <<<EXAMPLE1
@ -78,3 +116,32 @@ $_example2 = <<<EXAMPLE2
</content> </content>
</entry> </entry>
EXAMPLE2; EXAMPLE2;
$_example3 = <<<EXAMPLE3
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Example Feed</title>
<subtitle>A subtitle.</subtitle>
<link href="http://example.org/feed/" rel="self" />
<link href="http://example.org/" />
<id>urn:uuid:60a76c80-d399-11d9-b91C-0003939e0af6</id>
<updated>2003-12-13T18:30:02Z</updated>
<author>
<name>John Doe</name>
<email>johndoe@example.com</email>
</author>
<entry>
<title>Atom-Powered Robots Run Amok</title>
<link href="http://example.org/2003/12/13/atom03" />
<link rel="alternate" type="text/html" href="http://example.org/2003/12/13/atom03.html"/>
<link rel="edit" href="http://example.org/2003/12/13/atom03/edit"/>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<updated>2003-12-13T18:30:02Z</updated>
<summary>Some text.</summary>
</entry>
</feed>
EXAMPLE3;