]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - tests/atompub/atompub_test.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / tests / atompub / atompub_test.php
index ebe948572246e7e79754fbb8385b2ef7a0fd833d..e23e4a711bb65b89666442edcd111548e9386401 100644 (file)
@@ -65,9 +65,9 @@ class AtomPubClient
      */
     private function httpClient($method='GET')
     {
-        $client = new HTTPClient($this->url, 'GET');
-        // basic auth, whee
-        $client->setAuth($this->user, $this->password);
+        $client = new HTTPClient($this->url);
+        $client->setMethod($method);
+        $client->setAuth($this->user, $this->pass);
         return $client;
     }
 
@@ -78,7 +78,7 @@ class AtomPubClient
         if ($response->isOk()) {
             return $response->getBody();
         } else {
-            throw new Exception("Bogus return code: " . $response->getStatus());
+            throw new Exception("Bogus return code: " . $response->getStatus() . ': ' . $response->getBody());
         }
     }
 
@@ -102,7 +102,7 @@ class AtomPubClient
         $response = $client->send();
 
         if ($response->getStatus() != '201') {
-            throw new Exception("Expected HTTP 201 on POST, got " . $response->getStatus());
+            throw new Exception("Expected HTTP 201 on POST, got " . $response->getStatus() . ': ' . $response->getBody());
         }
         $loc = $response->getHeader('Location');
         $contentLoc = $response->getHeader('Content-Location');
@@ -142,7 +142,7 @@ class AtomPubClient
         $response = $client->send();
 
         if ($response->getStatus() != '200' && $response->getStatus() != '204') {
-            throw new Exception("Expected HTTP 200 or 204 on PUT, got " . $response->getStatus());
+            throw new Exception("Expected HTTP 200 or 204 on PUT, got " . $response->getStatus() . ': ' . $response->getBody());
         }
 
         return true;
@@ -157,12 +157,12 @@ class AtomPubClient
      */
     function delete()
     {
-        $client = $this->httpClient('GET');
+        $client = $this->httpClient('DELETE');
         $client->setBody($data);
         $response = $client->send();
 
         if ($response->getStatus() != '200' && $response->getStatus() != '204') {
-            throw new Exception("Expected HTTP 200 or 204 on DELETE, got " . $response->getStatus());
+            throw new Exception("Expected HTTP 200 or 204 on DELETE, got " . $response->getStatus() . ': ' . $response->getBody());
         }
 
         return true;
@@ -185,9 +185,63 @@ class AtomPubClient
             throw new Exception('Bad Atom entry: XML is not well formed.');
         }
 
-        $activity = new Activity($dom);
+        $activity = new Activity($dom->documentRoot);
         return true;
     }
+
+    static function entryEditURL($str) {
+        $dom = new DOMDocument;
+        $dom->loadXML($str);
+        $path = new DOMXPath($dom);
+        $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
+
+        $links = $path->query('/atom:entry/atom:link[@rel="edit"]', $dom->documentRoot);
+        if ($links && $links->length) {
+            if ($links->length > 1) {
+                throw new Exception('Bad Atom entry; has multiple rel=edit links.');
+            }
+            $link = $links->item(0);
+            $url = $link->getAttribute('href');
+            return $url;
+        } else {
+            throw new Exception('Atom entry lists no rel=edit link.');
+        }
+    }
+
+    static function entryId($str) {
+        $dom = new DOMDocument;
+        $dom->loadXML($str);
+        $path = new DOMXPath($dom);
+        $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
+
+        $links = $path->query('/atom:entry/atom:id', $dom->documentRoot);
+        if ($links && $links->length) {
+            if ($links->length > 1) {
+                throw new Exception('Bad Atom entry; has multiple id entries.');
+            }
+            $link = $links->item(0);
+            $url = $link->textContent;
+            return $url;
+        } else {
+            throw new Exception('Atom entry lists no id.');
+        }
+    }
+
+    static function getEntryInFeed($str, $id)
+    {
+        $dom = new DOMDocument;
+        $dom->loadXML($str);
+        $path = new DOMXPath($dom);
+        $path->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
+
+        $query = '/atom:feed/atom:entry[atom:id="'.$id.'"]';
+        $items = $path->query($query, $dom->documentRoot);
+        if ($items && $items->length) {
+            return $items->item(0);
+        } else {
+            return null;
+        }
+    }
 }
 
 
@@ -211,9 +265,39 @@ $collection = new AtomPubClient($url, $user, $pass);
 
 // confirm the feed has edit links ..... ?
 
-// $atom = '';
+echo "Posting an empty message (should fail)... ";
+try {
+    $noticeUrl = $collection->post('');
+    die("FAILED, succeeded!\n");
+} catch (Exception $e) {
+    echo "ok\n";
+}
+
+echo "Posting an invalid XML message (should fail)... ";
+try {
+    $noticeUrl = $collection->post('<feed<entry>barf</yomomma>');
+    die("FAILED, succeeded!\n");
+} catch (Exception $e) {
+    echo "ok\n";
+}
+
+echo "Posting a valid XML but non-Atom message (should fail)... ";
+try {
+    $noticeUrl = $collection->post('<feed xmlns="http://notatom.com"><id>arf</id><entry><id>barf</id></entry></feed>');
+    die("FAILED, succeeded!\n");
+} catch (Exception $e) {
+    echo "ok\n";
+}
 
 // post!
+$rand = mt_rand(0, 99999);
+$atom = <<<END_ATOM
+<entry xmlns="http://www.w3.org/2005/Atom">
+    <title>This is an AtomPub test post title ($rand)</title>
+    <content>This is an AtomPub test post content ($rand)</content>
+</entry>
+END_ATOM;
+
 echo "Posting a new message... ";
 $noticeUrl = $collection->post($atom);
 echo "ok, got $noticeUrl\n";
@@ -224,19 +308,28 @@ $body = $notice->get();
 AtomPubClient::validateAtomEntry($body);
 echo "ok\n";
 
-echo "Confirming new entry looks right... ";
-// confirm that it actually is what we expected
-// confirm it has an edit URL that matches $target
-echo "NYI\n";
+echo "Getting the notice ID URI... ";
+$noticeUri = AtomPubClient::entryId($body);
+echo "ok: $noticeUri\n";
+
+echo "Confirming new entry points to itself right... ";
+$editUrl = AtomPubClient::entryEditURL($body);
+if ($editUrl != $noticeUrl) {
+    die("Entry lists edit URL as $editUrl, no match!\n");
+}
+echo "OK\n";
 
 echo "Refetching the collection... ";
 $feed = $collection->get();
 echo "ok\n";
 
 echo "Confirming new entry is in the feed... ";
-// make sure the new entry is in there
+$entry = AtomPubClient::getEntryInFeed($feed, $noticeUri);
+if (!$entry) {
+    die("missing!\n");
+}
 //  edit URL should match
-echo "NYI\n";
+echo "ok\n";
 
 echo "Editing notice (should fail)... ";
 try {
@@ -253,6 +346,7 @@ echo "ok\n";
 echo "Refetching deleted notice to confirm it's gone... ";
 try {
     $body = $notice->get();
+    var_dump($body);
     die("ERROR: notice should be gone now.\n");
 } catch (Exception $e) {
     echo "ok\n";
@@ -263,7 +357,11 @@ $feed = $collection->get();
 echo "ok\n";
 
 echo "Confirming deleted notice is no longer in the feed... ";
-echo "NYI\n";
+$entry = AtomPubClient::getEntryInFeed($feed, $noticeUri);
+if ($entry) {
+    die("still there!\n");
+}
+echo "ok\n";
 
 // make subscriptions
 // make some posts