[Pkg-owncloud-commits] [php-sabredav] 02/16: Test for #705

David Prévot taffit at moszumanska.debian.org
Sat Sep 5 15:23:55 UTC 2015


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

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

commit 05fcf405c7520fa78e30562f302e2b0fe1770645
Author: Evert Pot <me at evertpot.com>
Date:   Wed Sep 2 15:39:43 2015 +0200

    Test for #705
---
 lib/DAV/Collection.php                 |   2 +-
 tests/Sabre/DAV/HttpMoveTest.php       | 118 +++++++++++++++++++++++++++++++++
 tests/Sabre/DAV/Mock/Collection.php    |  36 ++++------
 tests/Sabre/DAV/Mock/File.php          |   6 ++
 tests/Sabre/DAV/ServerCopyMoveTest.php |  66 ------------------
 5 files changed, 139 insertions(+), 89 deletions(-)

diff --git a/lib/DAV/Collection.php b/lib/DAV/Collection.php
index caec029..2b0025e 100644
--- a/lib/DAV/Collection.php
+++ b/lib/DAV/Collection.php
@@ -32,7 +32,7 @@ abstract class Collection extends Node implements ICollection {
 
         foreach($this->getChildren() as $child) {
 
-            if ($child->getName()==$name) return $child;
+            if ($child->getName()===$name) return $child;
 
         }
         throw new Exception\NotFound('File not found: ' . $name);
diff --git a/tests/Sabre/DAV/HttpMoveTest.php b/tests/Sabre/DAV/HttpMoveTest.php
new file mode 100644
index 0000000..03ad150
--- /dev/null
+++ b/tests/Sabre/DAV/HttpMoveTest.php
@@ -0,0 +1,118 @@
+<?php
+
+namespace Sabre\DAV;
+
+use Sabre\DAVServerTest;
+use Sabre\HTTP;
+
+/**
+ * Tests related to the MOVE request.
+ *
+ * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
+ * @author Evert Pot (http://evertpot.com/)
+ * @license http://sabre.io/license/ Modified BSD License
+ */
+class HttpMoveTest extends DAVServerTest {
+
+    /**
+     * Sets up the DAV tree.
+     *
+     * @return void
+     */
+    function setUpTree() {
+
+        $this->tree = new Mock\Collection('root', [
+            'file1' => 'content1',
+            'file2' => 'content2',
+        ]);
+
+    }
+
+    function testMoveToSelf() {
+
+        $request = new HTTP\Request('MOVE', '/file1', [
+            'Destination' => '/file1'
+        ]);
+        $response = $this->request($request);
+        $this->assertEquals(403, $response->getStatus());
+        $this->assertEquals('content1', $this->tree->getChild('file1')->get());
+
+    }    
+
+    function testMove() {
+
+        $request = new HTTP\Request('MOVE', '/file1', [
+            'Destination' => '/file3'
+        ]);
+        $response = $this->request($request);
+        $this->assertEquals(201, $response->getStatus(), print_r($response,true));
+        $this->assertEquals('content1', $this->tree->getChild('file3')->get());
+        $this->assertFalse($this->tree->childExists('file1'));
+
+    }    
+
+    function testMoveToExisting() {
+
+        $request = new HTTP\Request('MOVE', '/file1', [
+            'Destination' => '/file2'
+        ]);
+        $response = $this->request($request);
+        $this->assertEquals(204, $response->getStatus(), print_r($response,true));
+        $this->assertEquals('content1', $this->tree->getChild('file2')->get());
+        $this->assertFalse($this->tree->childExists('file1'));
+
+    }    
+
+    function testMoveToExistingOverwriteT() {
+
+        $request = new HTTP\Request('MOVE', '/file1', [
+            'Destination' => '/file2',
+            'Overwrite' => 'T',
+        ]);
+        $response = $this->request($request);
+        $this->assertEquals(204, $response->getStatus(), print_r($response,true));
+        $this->assertEquals('content1', $this->tree->getChild('file2')->get());
+        $this->assertFalse($this->tree->childExists('file1'));
+
+    }    
+
+    function testMoveToExistingOverwriteF() {
+
+        $request = new HTTP\Request('MOVE', '/file1', [
+            'Destination' => '/file2',
+            'Overwrite' => 'F',
+        ]);
+        $response = $this->request($request);
+        $this->assertEquals(412, $response->getStatus(), print_r($response,true));
+        $this->assertEquals('content1', $this->tree->getChild('file1')->get());
+        $this->assertEquals('content2', $this->tree->getChild('file2')->get());
+        $this->assertTrue($this->tree->childExists('file1'));
+        $this->assertTrue($this->tree->childExists('file2'));
+
+    }
+
+    /**
+     * If we MOVE to an existing file, but a plugin prevents the original from
+     * being deleted, we need to make sure that the server does not delete
+     * the destination.
+     */
+    function testMoveToExistingBlockedDeleteSource() {
+
+        $this->server->on('beforeUnbind', function($path) {
+
+            if ($path==='file1') {
+                // Block file1 from being deleted.
+                return false;
+            }
+
+        });
+        $request = new HTTP\Request('MOVE', '/file1', [
+            'Destination' => '/file2'
+        ]);
+        $response = $this->request($request);
+        $this->assertEquals(204, $response->getStatus(), print_r($response,true));
+        $this->assertEquals('content1', $this->tree->getChild('file2')->get());
+        $this->assertFalse($this->tree->childExists('file1'));
+
+    }    
+}
diff --git a/tests/Sabre/DAV/Mock/Collection.php b/tests/Sabre/DAV/Mock/Collection.php
index bc18708..5644bdd 100644
--- a/tests/Sabre/DAV/Mock/Collection.php
+++ b/tests/Sabre/DAV/Mock/Collection.php
@@ -35,7 +35,17 @@ class Collection extends DAV\Collection {
     public function __construct($name, array $children = array(), Collection $parent = null) {
 
         $this->name = $name;
-        $this->children = $children;
+        foreach($children as $key=>$value) {
+            if (is_string($value)) {
+                $this->children[] = new File($key, $value, $this);
+            } elseif (is_array($value)) {
+                $this->children[] = new Collection($key, $value, $this);
+            } elseif ($value instanceof \Sabre\DAV\INode) {
+                $this->children[] = $value;
+            } else {
+                throw new \InvalidArgumentException('Unknown value passed in $children');
+            }
+        }
         $this->parent = $parent;
 
     }
@@ -95,7 +105,7 @@ class Collection extends DAV\Collection {
      */
     public function createDirectory($name) {
 
-        $this->children[$name] = array();
+        $this->children = new Collection($name);
 
     }
 
@@ -106,20 +116,7 @@ class Collection extends DAV\Collection {
      */
     public function getChildren() {
 
-        $result = array();
-        foreach($this->children as $key=>$value) {
-
-            if ($value instanceof DAV\INode) {
-                $result[] = $value;
-            } elseif (is_array($value)) {
-                $result[] = new Collection($key, $value, $this);
-            } else {
-                $result[] = new File($key, $value, $this);
-            }
-
-        }
-
-        return $result;
+        return $this->children;
 
     }
 
@@ -133,12 +130,7 @@ class Collection extends DAV\Collection {
 
         foreach($this->children as $key=>$value) {
 
-            if ($value instanceof DAV\INode) {
-                if ($value->getName() == $name) {
-                    unset($this->children[$key]);
-                    return;
-                }
-            } elseif ($key === $name) {
+            if ($value->getName() == $name) {
                 unset($this->children[$key]);
                 return;
             }
diff --git a/tests/Sabre/DAV/Mock/File.php b/tests/Sabre/DAV/Mock/File.php
index 354087d..b0a0a64 100644
--- a/tests/Sabre/DAV/Mock/File.php
+++ b/tests/Sabre/DAV/Mock/File.php
@@ -47,6 +47,12 @@ class File extends DAV\File {
 
     }
 
+    function setName($name) {
+
+        $this->name = $name;
+
+    }
+
     /**
      * Updates the data
      *
diff --git a/tests/Sabre/DAV/ServerCopyMoveTest.php b/tests/Sabre/DAV/ServerCopyMoveTest.php
index 3e5c0db..fe6d770 100644
--- a/tests/Sabre/DAV/ServerCopyMoveTest.php
+++ b/tests/Sabre/DAV/ServerCopyMoveTest.php
@@ -89,72 +89,6 @@ class ServerCopyMoveTest extends \PHPUnit_Framework_TestCase {
 
     }
 
-    function testMoveToSelf() {
-
-        $serverVars = array(
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'MOVE',
-            'HTTP_DESTINATION' => '/test.txt',
-        );
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
-
-        $this->assertEquals(403, $this->response->status,'Received an incorrect HTTP status. Full body inspection: ' . $this->response->body);
-        $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR. '/test.txt'));
-
-    }
-
-    function testMoveOverWrite() {
-
-        $serverVars = array(
-            'REQUEST_URI'    => '/test.txt',
-            'REQUEST_METHOD' => 'MOVE',
-            'HTTP_DESTINATION' => '/test2.txt',
-        );
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
-
-        $this->assertEquals(array(
-                'Content-Length' => 0,
-            ),
-            $this->response->headers
-         );
-
-        $this->assertEquals(204, $this->response->status);
-        $this->assertEquals('Test contents',file_get_contents(SABRE_TEMPDIR . '/test2.txt'));
-        $this->assertFalse(file_exists(SABRE_TEMPDIR . '/test.txt'),'The sourcefile test.txt should no longer exist at this point');
-
-    }
-
-    function testBlockedOverWrite() {
-
-        $serverVars = array(
-            'REQUEST_URI'      => '/test.txt',
-            'REQUEST_METHOD'   => 'COPY',
-            'HTTP_DESTINATION' => '/test2.txt',
-            'HTTP_OVERWRITE'   => 'F',
-        );
-
-        $request = HTTP\Sapi::createFromServerArray($serverVars);
-        $this->server->httpRequest = ($request);
-        $this->server->exec();
-
-        $this->assertEquals(array(
-                'Content-Type' => 'application/xml; charset=utf-8',
-            ),
-            $this->response->headers
-         );
-
-        $this->assertEquals(412, $this->response->status);
-        $this->assertEquals('Test contents2',file_get_contents(SABRE_TEMPDIR . '/test2.txt'));
-
-
-    }
-
     function testNonExistantParent() {
 
         $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