[Pkg-owncloud-commits] [php-sabredav] 106/148: Cleaned up some property handling.
David Prévot
taffit at moszumanska.debian.org
Wed Apr 15 01:37:25 UTC 2015
This is an automated email from the git hooks/post-receive script.
taffit pushed a commit to branch master
in repository php-sabredav.
commit b312d68cbba7347c825afdfc6354ad188fe1cf35
Author: Evert Pot <me at evertpot.com>
Date: Tue Mar 31 19:31:57 2015 -0400
Cleaned up some property handling.
---
lib/DAV/FSExt/Directory.php | 3 +-
lib/DAV/FSExt/File.php | 5 +-
lib/DAV/FSExt/Node.php | 226 ---------------------
lib/DAV/PropFind.php | 11 +
.../PropertyStorage/Backend/BackendInterface.php | 3 +
lib/DAV/PropertyStorage/Backend/PDO.php | 6 +-
tests/Sabre/DAV/FSExt/NodeTest.php | 191 -----------------
tests/Sabre/DAV/ServerPropsTest.php | 170 ----------------
8 files changed, 24 insertions(+), 591 deletions(-)
diff --git a/lib/DAV/FSExt/Directory.php b/lib/DAV/FSExt/Directory.php
index 3a7283d..e1f7c6a 100644
--- a/lib/DAV/FSExt/Directory.php
+++ b/lib/DAV/FSExt/Directory.php
@@ -3,6 +3,7 @@
namespace Sabre\DAV\FSExt;
use Sabre\DAV;
+use Sabre\DAV\FS\Node;
/**
* Directory class
@@ -158,7 +159,7 @@ class Directory extends Node implements DAV\ICollection, DAV\IQuota, DAV\IMoveTa
// Removing the directory itself
rmdir($this->path);
- return parent::delete();
+ return true;
}
diff --git a/lib/DAV/FSExt/File.php b/lib/DAV/FSExt/File.php
index 7a7bfe5..cc22509 100644
--- a/lib/DAV/FSExt/File.php
+++ b/lib/DAV/FSExt/File.php
@@ -1,7 +1,9 @@
<?php
namespace Sabre\DAV\FSExt;
+
use Sabre\DAV;
+use Sabre\DAV\FS\Node;
/**
* File class
@@ -98,7 +100,8 @@ class File extends Node implements DAV\PartialUpdate\IPatchSupport {
* @return bool
*/
function delete() {
- return unlink($this->path) && parent::delete();
+
+ return unlink($this->path);
}
diff --git a/lib/DAV/FSExt/Node.php b/lib/DAV/FSExt/Node.php
deleted file mode 100644
index 8ed7c84..0000000
--- a/lib/DAV/FSExt/Node.php
+++ /dev/null
@@ -1,226 +0,0 @@
-<?php
-
-namespace Sabre\DAV\FSExt;
-
-use
- Sabre\DAV,
- Sabre\HTTP\URLUtil,
- Sabre\DAV\PropPatch;
-
-/**
- * Base node-class
- *
- * The node class implements the method used by both the File and the Directory classes
- *
- * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/).
- * @author Evert Pot (http://evertpot.com/)
- * @license http://sabre.io/license/ Modified BSD License
- */
-abstract class Node extends DAV\FS\Node implements DAV\IProperties {
-
- /**
- * Updates properties on this node.
- *
- * This method received a PropPatch object, which contains all the
- * information about the update.
- *
- * To update specific properties, call the 'handle' method on this object.
- * Read the PropPatch documentation for more information.
- *
- * @param PropPatch $propPatch
- * @return void
- */
- function propPatch(PropPatch $propPatch) {
-
- $propPatch->handleRemaining(function(array $properties) {
-
- $resourceData = $this->getResourceData();
- foreach($properties as $propertyName=>$propertyValue) {
-
- // If it was null, we need to delete the property
- if (is_null($propertyValue)) {
- unset($resourceData['properties'][$propertyName]);
- } else {
- $resourceData['properties'][$propertyName] = $propertyValue;
- }
-
- }
- $this->putResourceData($resourceData);
-
- return true;
- });
-
- }
-
- /**
- * Returns a list of properties for this nodes.
- *
- * The properties list is a list of propertynames the client requested, encoded as xmlnamespace#tagName, for example: http://www.example.org/namespace#author
- * If the array is empty, all properties should be returned.
- *
- * @param array $properties
- * @return array
- */
- function getProperties($properties) {
-
- $resourceData = $this->getResourceData();
-
- // if the array was empty, we need to return everything
- if (!$properties) return $resourceData['properties'];
-
- $props = [];
- foreach($properties as $property) {
- if (isset($resourceData['properties'][$property])) $props[$property] = $resourceData['properties'][$property];
- }
-
- return $props;
-
- }
-
- /**
- * Returns the path to the resource file.
- *
- * @return string
- */
- protected function getResourceInfoPath() {
-
- list($parentDir) = URLUtil::splitPath($this->path);
- return $parentDir . '/.sabredav';
-
- }
-
- /**
- * Returns all the stored resource information.
- *
- * @return array
- */
- protected function getResourceData() {
-
- $path = $this->getResourceInfoPath();
- if (!file_exists($path)) return ['properties' => []];
-
- // opening up the file, and creating a shared lock
- $handle = fopen($path,'r');
- flock($handle,LOCK_SH);
- $data = '';
-
- // Reading data until the eof
- while(!feof($handle)) {
- $data.=fread($handle,8192);
- }
-
- // We're all good
- flock($handle,LOCK_UN);
- fclose($handle);
-
- // Unserializing and checking if the resource file contains data for this file
- $data = unserialize($data);
- if (!isset($data[$this->getName()])) {
- return ['properties' => []];
- }
-
- $data = $data[$this->getName()];
- if (!isset($data['properties'])) $data['properties'] = [];
- return $data;
-
- }
-
- /**
- * Updates the resource information.
- *
- * @param array $newData
- * @return void
- */
- protected function putResourceData(array $newData) {
-
- $path = $this->getResourceInfoPath();
-
- // opening up the file, and creating a shared lock
- $handle = fopen($path,'a+');
- flock($handle,LOCK_EX);
- $data = '';
-
- rewind($handle);
-
- // Reading data until the eof
- while(!feof($handle)) {
- $data.=fread($handle,8192);
- }
-
- // Unserializing and checking if the resource file contains data for this file
- $data = unserialize($data);
- $data[$this->getName()] = $newData;
- ftruncate($handle,0);
- rewind($handle);
-
- fwrite($handle,serialize($data));
- flock($handle,LOCK_UN);
- fclose($handle);
-
- }
-
- /**
- * Renames the node.
- *
- * @param string $name The new name
- * @return void
- */
- function setName($name) {
-
- list($parentPath, ) = URLUtil::splitPath($this->path);
- list(, $newName) = URLUtil::splitPath($name);
- $newPath = $parentPath . '/' . $newName;
-
- // We're deleting the existing resourcedata, and recreating it
- // for the new path.
- $resourceData = $this->getResourceData();
- $this->deleteResourceData();
-
- rename($this->path,$newPath);
- $this->path = $newPath;
- $this->putResourceData($resourceData);
-
-
- }
-
- /**
- * Deletes the resource information.
- * @return bool
- */
- protected function deleteResourceData() {
-
- // When we're deleting this node, we also need to delete any resource information
- $path = $this->getResourceInfoPath();
- if (!file_exists($path)) return true;
-
- // opening up the file, and creating a shared lock
- $handle = fopen($path,'a+');
- flock($handle,LOCK_EX);
- $data = '';
-
- rewind($handle);
-
- // Reading data until the eof
- while(!feof($handle)) {
- $data.=fread($handle,8192);
- }
-
- // Unserializing and checking if the resource file contains data for this file
- $data = unserialize($data);
- unset($data[$this->getName()]);
- ftruncate($handle,0);
- rewind($handle);
- fwrite($handle,serialize($data));
- flock($handle,LOCK_UN);
- fclose($handle);
-
- return true;
- }
-
- function delete() {
-
- return $this->deleteResourceData();
-
- }
-
-}
diff --git a/lib/DAV/PropFind.php b/lib/DAV/PropFind.php
index 9f9dae2..e52d1c1 100644
--- a/lib/DAV/PropFind.php
+++ b/lib/DAV/PropFind.php
@@ -248,6 +248,17 @@ class PropFind {
}
/**
+ * Returns true if this was an '{DAV:}allprops' request.
+ *
+ * @return bool
+ */
+ function isAllProps() {
+
+ return $this->requestType === self::ALLPROPS;
+
+ }
+
+ /**
* Returns a result array that's often used in multistatus responses.
*
* The array uses status codes as keys, and property names and value pairs
diff --git a/lib/DAV/PropertyStorage/Backend/BackendInterface.php b/lib/DAV/PropertyStorage/Backend/BackendInterface.php
index 5eee5c9..88c6593 100644
--- a/lib/DAV/PropertyStorage/Backend/BackendInterface.php
+++ b/lib/DAV/PropertyStorage/Backend/BackendInterface.php
@@ -27,6 +27,9 @@ interface BackendInterface {
* as this will give you the _exact_ list of properties that need to be
* fetched, and haven't yet.
*
+ * However, you can also support the 'allprops' property here. In that
+ * case, you should check for $propFind->isAllProps().
+ *
* @param string $path
* @param PropFind $propFind
* @return void
diff --git a/lib/DAV/PropertyStorage/Backend/PDO.php b/lib/DAV/PropertyStorage/Backend/PDO.php
index 1e3e174..4b16ece 100644
--- a/lib/DAV/PropertyStorage/Backend/PDO.php
+++ b/lib/DAV/PropertyStorage/Backend/PDO.php
@@ -64,14 +64,16 @@ class PDO implements BackendInterface {
* as this will give you the _exact_ list of properties that need to be
* fetched, and haven't yet.
*
+ * However, you can also support the 'allprops' property here. In that
+ * case, you should check for $propFind->isAllProps().
+ *
* @param string $path
* @param PropFind $propFind
* @return void
*/
function propFind($path, PropFind $propFind) {
- $propertyNames = $propFind->get404Properties();
- if (!$propertyNames) {
+ if (!$propFind->isAllProps() && count($propFind->get404Properties()) === 0) {
return;
}
diff --git a/tests/Sabre/DAV/FSExt/NodeTest.php b/tests/Sabre/DAV/FSExt/NodeTest.php
deleted file mode 100644
index 3b20a58..0000000
--- a/tests/Sabre/DAV/FSExt/NodeTest.php
+++ /dev/null
@@ -1,191 +0,0 @@
-<?php
-
-namespace Sabre\DAV\FSExt;
-
-use Sabre\DAV;
-use Sabre\DAV\PropPatch;
-
-require_once 'Sabre/TestUtil.php';
-
-class NodeTest extends \PHPUnit_Framework_TestCase {
-
- function setUp() {
-
- mkdir(SABRE_TEMPDIR . '/dir');
- file_put_contents(SABRE_TEMPDIR . '/dir/file.txt', 'Contents');
- file_put_contents(SABRE_TEMPDIR . '/dir/file2.txt', 'Contents2');
-
- }
-
- function tearDown() {
-
- \Sabre\TestUtil::clearTempDir();
-
- }
-
- function testUpdateProperties() {
-
- $file = new File(SABRE_TEMPDIR . '/dir/file.txt');
- $properties = array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- );
-
- $propPatch = new PropPatch($properties);
- $file->propPatch($propPatch);
- $propPatch->commit();
-
- $getProperties = $file->getProperties(array_keys($properties));
- $this->assertEquals($properties, $getProperties);
-
- }
-
- /**
- * @depends testUpdateProperties
- */
- function testUpdatePropertiesAgain() {
-
- $file = new File(SABRE_TEMPDIR . '/dir/file.txt');
- $mutations = array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- );
-
- $propPatch = new PropPatch($mutations);
- $file->propPatch($propPatch);
- $result = $propPatch->commit();
-
- $this->assertEquals(true, $result);
-
- $mutations = array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test3' => 'baz',
- );
-
- $propPatch = new PropPatch($mutations);
- $file->propPatch($propPatch);
- $result = $propPatch->commit();
-
- $this->assertEquals(true, $result);
- }
-
- /**
- * @depends testUpdateProperties
- */
- function testUpdatePropertiesDelete() {
-
- $file = new File(SABRE_TEMPDIR . '/dir/file.txt');
-
- $mutations = array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- );
-
- $propPatch = new PropPatch($mutations);
- $file->propPatch($propPatch);
- $result = $propPatch->commit();
-
- $this->assertEquals(true, $result);
-
- $mutations = array(
- '{http://sabredav.org/NS/2010}test1' => null,
- '{http://sabredav.org/NS/2010}test3' => null
- );
-
-
- $propPatch = new PropPatch($mutations);
- $file->propPatch($propPatch);
- $result = $propPatch->commit();
-
- $this->assertEquals(true, $result);
-
- $properties = $file->getProperties(array('http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
-
- $this->assertEquals(array(
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- ), $properties);
- }
-
- /**
- * @depends testUpdateProperties
- */
- function testUpdatePropertiesMove() {
-
- $file = new File(SABRE_TEMPDIR . '/dir/file.txt');
-
- $mutations = array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- );
-
- $propPatch = new PropPatch($mutations);
- $file->propPatch($propPatch);
- $result = $propPatch->commit();
-
- $this->assertEquals(true, $result);
-
- $properties = $file->getProperties(array('{http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
-
- $this->assertEquals(array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- ), $properties);
-
- // Renaming
- $file->setName('file3.txt');
-
- $this->assertFalse(file_exists(SABRE_TEMPDIR . '/dir/file.txt'));
- $this->assertTrue(file_exists(SABRE_TEMPDIR . '/dir/file3.txt'));
- $this->assertEquals('file3.txt',$file->getName());
-
- $newFile = new File(SABRE_TEMPDIR . '/dir/file3.txt');
- $this->assertEquals('file3.txt',$newFile->getName());
-
- $properties = $newFile->getProperties(array('{http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
-
- $this->assertEquals(array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- ), $properties);
- }
-
- /**
- * @depends testUpdatePropertiesMove
- */
- function testUpdatePropertiesDeleteBleed() {
-
- $file = new File(SABRE_TEMPDIR . '/dir/file.txt');
- $mutations = array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- );
-
- $propPatch = new PropPatch($mutations);
- $file->propPatch($propPatch);
- $result = $propPatch->commit();
-
- $this->assertEquals(true, $result);
-
- $properties = $file->getProperties(array('{http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
-
- $this->assertEquals(array(
- '{http://sabredav.org/NS/2010}test1' => 'foo',
- '{http://sabredav.org/NS/2010}test2' => 'bar',
- ), $properties);
-
- // Deleting
- $file->delete();
-
- $this->assertFalse(file_exists(SABRE_TEMPDIR . '/dir/file.txt'));
-
- // Creating it again
- file_put_contents(SABRE_TEMPDIR . '/dir/file.txt','New Contents');
- $file = new File(SABRE_TEMPDIR . '/dir/file.txt');
-
- $properties = $file->getProperties(array('http://sabredav.org/NS/2010}test1','{http://sabredav.org/NS/2010}test2','{http://sabredav.org/NS/2010}test3'));
-
- $this->assertEquals(array(), $properties);
-
- }
-
-}
diff --git a/tests/Sabre/DAV/ServerPropsTest.php b/tests/Sabre/DAV/ServerPropsTest.php
index 4f41b37..ac9137a 100644
--- a/tests/Sabre/DAV/ServerPropsTest.php
+++ b/tests/Sabre/DAV/ServerPropsTest.php
@@ -197,174 +197,4 @@ class ServerPropsTest extends AbstractServer {
}
- function testUpdateProperties() {
-
- $props = array(
- '{http://sabredav.org/NS/test}someprop' => 'somevalue',
- );
-
- $result = $this->server->updateProperties('/test2.txt',$props);
-
- $this->assertEquals(array(
- '{http://sabredav.org/NS/test}someprop' => 200
- ), $result);
-
- }
-
- function testUpdatePropertiesProtected() {
-
- $props = array(
- '{http://sabredav.org/NS/test}someprop' => 'somevalue',
- '{DAV:}getcontentlength' => 50,
- );
-
- $result = $this->server->updateProperties('/test2.txt',$props);
-
- $this->assertEquals(array(
- '{http://sabredav.org/NS/test}someprop' => 424,
- '{DAV:}getcontentlength' => 403,
- ), $result);
-
- }
-
- function testUpdatePropertiesFail1() {
-
- $dir = new Mock\PropertiesCollection('root', []);
- $dir->failMode = 'updatepropsfalse';
-
- $objectTree = new Tree($dir);
- $this->server->tree = $objectTree;
-
- $props = array(
- '{http://sabredav.org/NS/test}someprop' => 'somevalue',
- );
-
- $result = $this->server->updateProperties('/',$props);
-
- $this->assertEquals(array(
- '{http://sabredav.org/NS/test}someprop' => 403,
- ), $result);
-
- }
-
- /**
- * @depends testUpdateProperties
- */
- function testUpdatePropertiesFail2() {
-
- $dir = new Mock\PropertiesCollection('root', []);
- $dir->failMode = 'updatepropsarray';
-
- $objectTree = new Tree($dir);
- $this->server->tree = $objectTree;
-
- $props = array(
- '{http://sabredav.org/NS/test}someprop' => 'somevalue',
- );
-
- $result = $this->server->updateProperties('/',$props);
-
- $this->assertEquals(array(
- '{http://sabredav.org/NS/test}someprop' => 402
- ), $result);
-
- }
-
- /**
- * @depends testUpdateProperties
- * @expectedException \UnexpectedValueException
- */
- function testUpdatePropertiesFail3() {
-
- $dir = new Mock\PropertiesCollection('root', []);
- $dir->failMode = 'updatepropsobj';
-
- $objectTree = new Tree($dir);
- $this->server->tree = $objectTree;
-
- $props = array(
- '{http://sabredav.org/NS/test}someprop' => 'somevalue',
- );
-
- $result = $this->server->updateProperties('/',$props);
-
- }
-
- /**
- * @depends testParsePropPatchRequest
- * @depends testUpdateProperties
- */
- function testPropPatch() {
-
- $serverVars = array(
- 'REQUEST_URI' => '/',
- 'REQUEST_METHOD' => 'PROPPATCH',
- );
-
- $body = '<?xml version="1.0"?>
-<d:propertyupdate xmlns:d="DAV:" xmlns:s="http://www.rooftopsolutions.nl/testnamespace">
- <d:set><d:prop><s:someprop>somevalue</s:someprop></d:prop></d:set>
-</d:propertyupdate>';
-
- $request = HTTP\Sapi::createFromServerArray($serverVars);
- $request->setBody($body);
-
- $this->server->httpRequest = ($request);
- $this->server->exec();
-
- $this->assertEquals(array(
- 'X-Sabre-Version' => [Version::VERSION],
- 'Content-Type' => ['application/xml; charset=utf-8'],
- 'Vary' => ['Brief,Prefer'],
- ),
- $this->response->getHeaders()
- );
-
- $this->assertEquals(207, $this->response->status,'We got the wrong status. Full XML response: ' . $this->response->body);
-
- $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
- $xml = simplexml_load_string($body);
- $xml->registerXPathNamespace('d','urn:DAV');
- $xml->registerXPathNamespace('bla','http://www.rooftopsolutions.nl/testnamespace');
-
- $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:prop');
- $this->assertEquals(1,count($data),'We expected one \'d:prop\' element. Response body: ' . $body);
-
- $data = $xml->xpath('//bla:someprop');
- $this->assertEquals(1,count($data),'We expected one \'s:someprop\' element. Response body: ' . $body);
-
- $data = $xml->xpath('/d:multistatus/d:response/d:propstat/d:status');
- $this->assertEquals(1,count($data),'We expected one \'s:status\' element. Response body: ' . $body);
-
- $this->assertEquals('HTTP/1.1 200 OK',(string)$data[0]);
-
- }
-
- /**
- * @depends testPropPatch
- */
- function testPropPatchAndFetch() {
-
- $this->testPropPatch();
- $xml = '<?xml version="1.0"?>
-<d:propfind xmlns:d="DAV:" xmlns:s="http://www.rooftopsolutions.nl/testnamespace">
- <d:prop>
- <s:someprop />
- </d:prop>
-</d:propfind>';
-
- $this->sendRequest($xml);
-
- $body = preg_replace("/xmlns(:[A-Za-z0-9_])?=(\"|\')DAV:(\"|\')/","xmlns\\1=\"urn:DAV\"",$this->response->body);
- $xml = simplexml_load_string($body);
- $xml->registerXPathNamespace('d','urn:DAV');
- $xml->registerXPathNamespace('bla','http://www.rooftopsolutions.nl/testnamespace');
-
- $xpath='//bla:someprop';
- $result = $xml->xpath($xpath);
- $this->assertEquals(1,count($result),'We couldn\'t find our new property in the response. Full response body:' . "\n" . $body);
- $this->assertEquals('somevalue',(string)$result[0],'We couldn\'t find our new property in the response. Full response body:' . "\n" . $body);
-
- }
-
}
--
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