]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
test parsing a default atom feed for activities
authorEvan Prodromou <evan@status.net>
Sun, 14 Feb 2010 18:19:32 +0000 (13:19 -0500)
committerEvan Prodromou <evan@status.net>
Sun, 14 Feb 2010 18:19:32 +0000 (13:19 -0500)
plugins/OStatus/lib/activity.php
plugins/OStatus/tests/ActivityParseTests.php

index 11aab2848650944cf0afcf4fad21581a26758845..048efda2c9ee1dac0264a16cb1065f7cb274f151 100644 (file)
@@ -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);
index 889fa892f0c451f665c72e57a4c7d70eaae89d57..fa8bcdda23c4c528021f2681b83ec04331bb0e7d 100644 (file)
@@ -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 = <<<EXAMPLE1
@@ -78,3 +116,32 @@ $_example2 = <<<EXAMPLE2
   </content>
 </entry>
 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;