[Pkg-owncloud-commits] [php-sabredav] 05/24: Fix for #427: Preconditions checking for http delete.

David Prévot taffit at moszumanska.debian.org
Tue May 20 17:19:41 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch 1.7
in repository php-sabredav.

commit 1064224e0f67c8e71e045997b287119113b2c46a
Author: Evert Pot <evert at rooftopsolutions.nl>
Date:   Thu Apr 3 14:21:59 2014 -0400

    Fix for #427: Preconditions checking for http delete.
    
    And also refactored the DELETE tests a bit.
---
 ChangeLog                            |   3 +-
 lib/Sabre/DAV/Server.php             |   3 +
 tests/Sabre/DAV/HttpDeleteTest.php   | 144 +++++++++++++++++++++++++++++++++++
 tests/Sabre/DAV/Mock/Collection.php  |  46 ++++++++++-
 tests/Sabre/DAV/Mock/File.php        |  15 +++-
 tests/Sabre/DAV/ServerSimpleTest.php |  44 -----------
 6 files changed, 206 insertions(+), 49 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index bf37ba6..251b05f 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,9 +1,10 @@
 1.7.12-stable (????-??-??)
 	* Fixed: Restoring old setting after changing
 	  libxml_disable_entity_loader.
-	* Fixed: Issue #422 Preconditions were not being set on PUT on non-
+	* Fixed: Issue #422: Preconditions were not being set on PUT on non-
 	  existant files. Not really a chance for data-loss, but incorrect
 	  nevertheless.
+	* Fixed: Issue #427: Now checking preconditions on DELETE requests.
 
 1.7.11-stable (2014-02-26)
 	* The zip release ships with sabre/vobject 2.1.3.
diff --git a/lib/Sabre/DAV/Server.php b/lib/Sabre/DAV/Server.php
index d11c75e..b791b31 100644
--- a/lib/Sabre/DAV/Server.php
+++ b/lib/Sabre/DAV/Server.php
@@ -683,6 +683,9 @@ class Sabre_DAV_Server {
      */
     protected function httpDelete($uri) {
 
+        // Checking If-None-Match and related headers.
+        if (!$this->checkPreconditions()) return;
+
         if (!$this->broadcastEvent('beforeUnbind',array($uri))) return;
         $this->tree->delete($uri);
         $this->broadcastEvent('afterUnbind',array($uri));
diff --git a/tests/Sabre/DAV/HttpDeleteTest.php b/tests/Sabre/DAV/HttpDeleteTest.php
new file mode 100644
index 0000000..2ddc611
--- /dev/null
+++ b/tests/Sabre/DAV/HttpDeleteTest.php
@@ -0,0 +1,144 @@
+<?php
+
+/**
+ * Tests related to the PUT request.
+ *
+ * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class Sabre_DAV_HttpDeleteTest extends Sabre_DAVServerTest {
+
+    /**
+     * Sets up the DAV tree.
+     *
+     * @return void
+     */
+    public function setUpTree() {
+
+        $this->tree = new Sabre_DAV_Mock_Collection('root', array(
+            'file1' => 'foo',
+            'dir' => array(
+                'subfile' => 'bar',
+                'subfile2' => 'baz',
+            ),
+        ));
+
+    }
+
+    /**
+     * A successful DELETE
+     */
+    public function testDelete() {
+
+        $request = new Sabre_HTTP_Request(array(
+            'REQUEST_URI' => '/file1',
+            'REQUEST_METHOD' => 'DELETE',
+        ));
+
+        $response = $this->request($request);
+
+        $this->assertEquals(
+            'HTTP/1.1 204 No Content',
+            $response->status,
+            "Incorrect status code. Response body:  " . $response->body
+        );
+
+        $this->assertEquals(
+            array(
+                'Content-Length' => '0',
+            ),
+            $response->headers
+        );
+
+    }
+
+    /**
+     * Deleting a Directory
+     */
+    public function testDeleteDirectory() {
+
+        $request = new Sabre_HTTP_Request(array(
+            'REQUEST_URI' => '/dir',
+            'REQUEST_METHOD' => 'DELETE',
+        ));
+
+        $response = $this->request($request);
+
+        $this->assertEquals(
+            'HTTP/1.1 204 No Content',
+            $response->status,
+            "Incorrect status code. Response body:  " . $response->body
+        );
+
+        $this->assertEquals(
+            array(
+                'Content-Length' => '0',
+            ),
+            $response->headers
+        );
+
+    }
+
+    /**
+     * DELETE on a node that does not exist
+     */
+    public function testDeleteNotFound() {
+
+        $request = new Sabre_HTTP_Request(array(
+            'REQUEST_URI' => '/file2',
+            'REQUEST_METHOD' => 'DELETE',
+        ));
+
+        $response = $this->request($request);
+
+        $this->assertEquals(
+            'HTTP/1.1 404 Not Found',
+            $response->status,
+            "Incorrect status code. Response body:  " . $response->body
+        );
+
+    }
+
+    /**
+     * DELETE with preconditions
+     */
+    public function testDeletePreconditions() {
+
+        $request = new Sabre_HTTP_Request(array(
+            'REQUEST_URI' => '/file1',
+            'REQUEST_METHOD' => 'DELETE',
+            'HTTP_IF_MATCH' => '"' . md5('foo') . '"',
+        ));
+
+        $response = $this->request($request);
+
+        $this->assertEquals(
+            'HTTP/1.1 204 No Content',
+            $response->status,
+            "Incorrect status code. Response body:  " . $response->body
+        );
+
+    }
+
+    /**
+     * DELETE with incorrect preconditions
+     */
+    public function testDeletePreconditionsFailed() {
+
+        $request = new Sabre_HTTP_Request(array(
+            'REQUEST_URI' => '/file1',
+            'REQUEST_METHOD' => 'DELETE',
+            'HTTP_IF_MATCH' => '"' . md5('bar') . '"',
+        ));
+
+        $response = $this->request($request);
+
+        $this->assertEquals(
+            'HTTP/1.1 412 Precondition failed',
+            $response->status,
+            "Incorrect status code. Response body:  " . $response->body
+        );
+
+    }
+}
diff --git a/tests/Sabre/DAV/Mock/Collection.php b/tests/Sabre/DAV/Mock/Collection.php
index d27dd3f..f0c674f 100644
--- a/tests/Sabre/DAV/Mock/Collection.php
+++ b/tests/Sabre/DAV/Mock/Collection.php
@@ -19,6 +19,7 @@ class Sabre_DAV_Mock_Collection extends Sabre_DAV_Collection {
 
     protected $name;
     protected $children;
+    protected $parent;
 
     /**
      * Creates the object
@@ -27,10 +28,11 @@ class Sabre_DAV_Mock_Collection extends Sabre_DAV_Collection {
      * @param array $children
      * @return void
      */
-    public function __construct($name, array $children = array()) {
+    public function __construct($name, array $children = array(), Sabre_DAV_Mock_Collection $parent = null) {
 
         $this->name = $name;
         $this->children = $children;
+        $this->parent = $parent;
 
     }
 
@@ -106,9 +108,9 @@ class Sabre_DAV_Mock_Collection extends Sabre_DAV_Collection {
             if ($value instanceof Sabre_DAV_INode) {
                 $result[] = $value;
             } elseif (is_array($value)) {
-                $result[] = new Sabre_DAV_Mock_Collection($key, $value);
+                $result[] = new Sabre_DAV_Mock_Collection($key, $value, $this);
             } else {
-                $result[] = new Sabre_DAV_Mock_File($key, $value);
+                $result[] = new Sabre_DAV_Mock_File($key, $value, $this);
             }
 
         }
@@ -117,4 +119,42 @@ class Sabre_DAV_Mock_Collection extends Sabre_DAV_Collection {
 
     }
 
+    /**
+     * Removes a childnode from this node.
+     *
+     * @param string $name
+     * @return void
+     */
+    public function deleteChild($name) {
+
+        foreach($this->children as $key=>$value) {
+
+            if ($value instanceof Sabre_DAV_INode) {
+                if ($value->getName() == $name) {
+                    unset($this->children[$key]);
+                    return;
+                }
+            } elseif ($key === $name) {
+                unset($this->children[$key]);
+                return;
+            }
+
+        }
+
+    }
+
+    /**
+     * Deletes this collection and all its children,.
+     *
+     * @return void
+     */
+    public function delete() {
+
+        foreach($this->getChildren() as $child) {
+            $this->deleteChild($child->getName());
+        }
+        $this->parent->deleteChild($this->getName());
+
+    }
+
 }
diff --git a/tests/Sabre/DAV/Mock/File.php b/tests/Sabre/DAV/Mock/File.php
index c0927a0..570052d 100644
--- a/tests/Sabre/DAV/Mock/File.php
+++ b/tests/Sabre/DAV/Mock/File.php
@@ -13,6 +13,7 @@ class Sabre_DAV_Mock_File extends Sabre_DAV_File {
 
     protected $name;
     protected $contents;
+    protected $parent;
 
     /**
      * Creates the object
@@ -21,10 +22,11 @@ class Sabre_DAV_Mock_File extends Sabre_DAV_File {
      * @param array $children
      * @return void
      */
-    public function __construct($name, $contents) {
+    public function __construct($name, $contents, Sabre_DAV_Mock_Collection $parent) {
 
         $this->name = $name;
         $this->put($contents);
+        $this->parent = $parent;
 
     }
 
@@ -110,4 +112,15 @@ class Sabre_DAV_Mock_File extends Sabre_DAV_File {
 
     }
 
+    /**
+     * Delete the node
+     *
+     * @return void
+     */
+    public function delete() {
+
+        $this->parent->deleteChild($this->name);
+
+    }
+
 }
diff --git a/tests/Sabre/DAV/ServerSimpleTest.php b/tests/Sabre/DAV/ServerSimpleTest.php
index 6dada01..49cda4f 100644
--- a/tests/Sabre/DAV/ServerSimpleTest.php
+++ b/tests/Sabre/DAV/ServerSimpleTest.php
@@ -171,50 +171,6 @@ class Sabre_DAV_ServerSimpleTest extends Sabre_DAV_AbstractServer{
 
     }
 
-    function testDelete() {
-
-        $serverVars = array(
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'DELETE',
-        );
-
-        $request = new Sabre_HTTP_Request($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
-
-        $this->assertEquals(array(
-            'Content-Length' => '0',
-        ),$this->response->headers);
-
-        $this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
-        $this->assertEquals('', $this->response->body);
-        $this->assertFalse(file_exists($this->tempDir . '/test.txt'));
-
-    }
-
-    function testDeleteDirectory() {
-
-        $serverVars = array(
-            'REQUEST_URI'    => '/testcol',
-            'REQUEST_METHOD' => 'DELETE',
-        );
-
-        mkdir($this->tempDir.'/testcol');
-        file_put_contents($this->tempDir.'/testcol/test.txt','Hi! I\'m a file with a short lifespan');
-
-        $request = new Sabre_HTTP_Request($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
-
-        $this->assertEquals(array(
-            'Content-Length' => '0',
-        ),$this->response->headers);
-        $this->assertEquals('HTTP/1.1 204 No Content',$this->response->status);
-        $this->assertEquals('', $this->response->body);
-        $this->assertFalse(file_exists($this->tempDir . '/col'));
-
-    }
-
     function testOptions() {
 
         $serverVars = array(

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/php-sabredav.git



More information about the Pkg-owncloud-commits mailing list