2015-02-12 11:29:01 +00:00
|
|
|
<?php
|
|
|
|
|
2015-10-30 15:05:25 +00:00
|
|
|
namespace OCA\DAV\Tests\Unit\Connector\Sabre;
|
2015-02-12 11:29:01 +00:00
|
|
|
|
2015-12-09 16:31:14 +00:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
class FilesPlugin extends \Test\TestCase {
|
2015-08-30 17:13:01 +00:00
|
|
|
const GETETAG_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::GETETAG_PROPERTYNAME;
|
|
|
|
const FILEID_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::FILEID_PROPERTYNAME;
|
2015-11-18 10:41:04 +00:00
|
|
|
const INTERNAL_FILEID_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::INTERNAL_FILEID_PROPERTYNAME;
|
2015-08-30 17:13:01 +00:00
|
|
|
const SIZE_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::SIZE_PROPERTYNAME;
|
|
|
|
const PERMISSIONS_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::PERMISSIONS_PROPERTYNAME;
|
|
|
|
const LASTMODIFIED_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::LASTMODIFIED_PROPERTYNAME;
|
|
|
|
const DOWNLOADURL_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::DOWNLOADURL_PROPERTYNAME;
|
2015-11-02 14:05:16 +00:00
|
|
|
const OWNER_ID_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::OWNER_ID_PROPERTYNAME;
|
|
|
|
const OWNER_DISPLAY_NAME_PROPERTYNAME = \OCA\DAV\Connector\Sabre\FilesPlugin::OWNER_DISPLAY_NAME_PROPERTYNAME;
|
2015-02-12 11:29:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
2015-08-30 17:13:01 +00:00
|
|
|
* @var \Sabre\DAV\Tree
|
2015-02-12 11:29:01 +00:00
|
|
|
*/
|
|
|
|
private $tree;
|
|
|
|
|
|
|
|
/**
|
2015-08-30 17:13:01 +00:00
|
|
|
* @var \OCA\DAV\Connector\Sabre\FilesPlugin
|
2015-02-12 11:29:01 +00:00
|
|
|
*/
|
|
|
|
private $plugin;
|
|
|
|
|
2015-10-06 13:36:54 +00:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\View
|
|
|
|
*/
|
|
|
|
private $view;
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2015-10-06 13:36:54 +00:00
|
|
|
$this->server = $this->getMockBuilder('\Sabre\DAV\Server')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->tree = $this->getMockBuilder('\Sabre\DAV\Tree')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-10-06 13:36:54 +00:00
|
|
|
$this->view = $this->getMockBuilder('\OC\Files\View')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
2015-08-30 17:13:01 +00:00
|
|
|
$this->plugin = new \OCA\DAV\Connector\Sabre\FilesPlugin($this->tree, $this->view);
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:42:34 +00:00
|
|
|
/**
|
|
|
|
* @param string $class
|
|
|
|
*/
|
2015-02-12 11:29:01 +00:00
|
|
|
private function createTestNode($class) {
|
|
|
|
$node = $this->getMockBuilder($class)
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue(123));
|
|
|
|
|
|
|
|
$this->tree->expects($this->any())
|
|
|
|
->method('getNodeForPath')
|
|
|
|
->with('/dummypath')
|
|
|
|
->will($this->returnValue($node));
|
|
|
|
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getFileId')
|
2015-11-18 10:41:04 +00:00
|
|
|
->will($this->returnValue('00000123instanceid'));
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getInternalFileId')
|
|
|
|
->will($this->returnValue('123'));
|
2015-02-12 11:29:01 +00:00
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getEtag')
|
|
|
|
->will($this->returnValue('"abc"'));
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getDavPermissions')
|
2015-07-13 12:20:35 +00:00
|
|
|
->will($this->returnValue('DWCKMSR'));
|
2015-02-12 11:29:01 +00:00
|
|
|
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
*/
|
|
|
|
public function testGetPropertiesForFile() {
|
2015-08-30 17:13:01 +00:00
|
|
|
$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\File');
|
2015-02-12 11:29:01 +00:00
|
|
|
|
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
'/dummyPath',
|
|
|
|
array(
|
|
|
|
self::GETETAG_PROPERTYNAME,
|
|
|
|
self::FILEID_PROPERTYNAME,
|
2015-11-18 10:41:04 +00:00
|
|
|
self::INTERNAL_FILEID_PROPERTYNAME,
|
2015-02-12 11:29:01 +00:00
|
|
|
self::SIZE_PROPERTYNAME,
|
|
|
|
self::PERMISSIONS_PROPERTYNAME,
|
|
|
|
self::DOWNLOADURL_PROPERTYNAME,
|
2015-11-02 14:05:16 +00:00
|
|
|
self::OWNER_ID_PROPERTYNAME,
|
|
|
|
self::OWNER_DISPLAY_NAME_PROPERTYNAME
|
2015-02-12 11:29:01 +00:00
|
|
|
),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2015-11-02 14:05:16 +00:00
|
|
|
$user = $this->getMockBuilder('\OC\User\User')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->will($this->returnValue('foo'));
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getDisplayName')
|
|
|
|
->will($this->returnValue('M. Foo'));
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getDirectDownload')
|
|
|
|
->will($this->returnValue(array('url' => 'http://example.com/')));
|
2015-11-02 14:05:16 +00:00
|
|
|
$node->expects($this->exactly(2))
|
|
|
|
->method('getOwner')
|
|
|
|
->will($this->returnValue($user));
|
2015-02-12 11:29:01 +00:00
|
|
|
$node->expects($this->never())
|
|
|
|
->method('getSize');
|
|
|
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind,
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('"abc"', $propFind->get(self::GETETAG_PROPERTYNAME));
|
2015-11-18 10:41:04 +00:00
|
|
|
$this->assertEquals('00000123instanceid', $propFind->get(self::FILEID_PROPERTYNAME));
|
|
|
|
$this->assertEquals('123', $propFind->get(self::INTERNAL_FILEID_PROPERTYNAME));
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->assertEquals(null, $propFind->get(self::SIZE_PROPERTYNAME));
|
2015-07-13 12:20:35 +00:00
|
|
|
$this->assertEquals('DWCKMSR', $propFind->get(self::PERMISSIONS_PROPERTYNAME));
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->assertEquals('http://example.com/', $propFind->get(self::DOWNLOADURL_PROPERTYNAME));
|
2015-11-02 14:05:16 +00:00
|
|
|
$this->assertEquals('foo', $propFind->get(self::OWNER_ID_PROPERTYNAME));
|
|
|
|
$this->assertEquals('M. Foo', $propFind->get(self::OWNER_DISPLAY_NAME_PROPERTYNAME));
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->assertEquals(array(self::SIZE_PROPERTYNAME), $propFind->get404Properties());
|
|
|
|
}
|
|
|
|
|
2015-12-09 16:31:14 +00:00
|
|
|
public function testGetPropertiesStorageNotAvailable() {
|
|
|
|
$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\File');
|
|
|
|
|
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
'/dummyPath',
|
|
|
|
array(
|
|
|
|
self::DOWNLOADURL_PROPERTYNAME,
|
|
|
|
),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getDirectDownload')
|
|
|
|
->will($this->throwException(new StorageNotAvailableException()));
|
|
|
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind,
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals(null, $propFind->get(self::DOWNLOADURL_PROPERTYNAME));
|
|
|
|
}
|
|
|
|
|
2015-07-13 12:20:35 +00:00
|
|
|
public function testGetPublicPermissions() {
|
2015-08-30 17:13:01 +00:00
|
|
|
$this->plugin = new \OCA\DAV\Connector\Sabre\FilesPlugin($this->tree, $this->view, true);
|
2015-07-13 12:20:35 +00:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
|
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
'/dummyPath',
|
|
|
|
[
|
|
|
|
self::PERMISSIONS_PROPERTYNAME,
|
|
|
|
],
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
2015-08-30 17:13:01 +00:00
|
|
|
$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\File');
|
2015-07-13 12:20:35 +00:00
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getDavPermissions')
|
|
|
|
->will($this->returnValue('DWCKMSR'));
|
|
|
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind,
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('DWCKR', $propFind->get(self::PERMISSIONS_PROPERTYNAME));
|
|
|
|
}
|
|
|
|
|
2015-02-12 11:29:01 +00:00
|
|
|
public function testGetPropertiesForDirectory() {
|
2015-08-30 17:13:01 +00:00
|
|
|
$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\Directory');
|
2015-02-12 11:29:01 +00:00
|
|
|
|
|
|
|
$propFind = new \Sabre\DAV\PropFind(
|
|
|
|
'/dummyPath',
|
|
|
|
array(
|
|
|
|
self::GETETAG_PROPERTYNAME,
|
|
|
|
self::FILEID_PROPERTYNAME,
|
|
|
|
self::SIZE_PROPERTYNAME,
|
|
|
|
self::PERMISSIONS_PROPERTYNAME,
|
|
|
|
self::DOWNLOADURL_PROPERTYNAME,
|
|
|
|
),
|
|
|
|
0
|
|
|
|
);
|
|
|
|
|
|
|
|
$node->expects($this->never())
|
|
|
|
->method('getDirectDownload');
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getSize')
|
|
|
|
->will($this->returnValue(1025));
|
|
|
|
|
|
|
|
$this->plugin->handleGetProperties(
|
|
|
|
$propFind,
|
|
|
|
$node
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertEquals('"abc"', $propFind->get(self::GETETAG_PROPERTYNAME));
|
2015-11-18 10:41:04 +00:00
|
|
|
$this->assertEquals('00000123instanceid', $propFind->get(self::FILEID_PROPERTYNAME));
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->assertEquals(1025, $propFind->get(self::SIZE_PROPERTYNAME));
|
2015-07-13 12:20:35 +00:00
|
|
|
$this->assertEquals('DWCKMSR', $propFind->get(self::PERMISSIONS_PROPERTYNAME));
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->assertEquals(null, $propFind->get(self::DOWNLOADURL_PROPERTYNAME));
|
|
|
|
$this->assertEquals(array(self::DOWNLOADURL_PROPERTYNAME), $propFind->get404Properties());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateProps() {
|
2015-08-30 17:13:01 +00:00
|
|
|
$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\File');
|
2015-02-12 11:29:01 +00:00
|
|
|
|
|
|
|
$testDate = 'Fri, 13 Feb 2015 00:01:02 GMT';
|
|
|
|
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('touch')
|
|
|
|
->with($testDate);
|
|
|
|
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('setEtag')
|
|
|
|
->with('newetag')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
// properties to set
|
|
|
|
$propPatch = new \Sabre\DAV\PropPatch(array(
|
|
|
|
self::GETETAG_PROPERTYNAME => 'newetag',
|
2015-10-13 10:51:21 +00:00
|
|
|
self::LASTMODIFIED_PROPERTYNAME => $testDate
|
2015-02-12 11:29:01 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
$this->plugin->handleUpdateProperties(
|
|
|
|
'/dummypath',
|
|
|
|
$propPatch
|
|
|
|
);
|
|
|
|
|
|
|
|
$propPatch->commit();
|
|
|
|
|
|
|
|
$this->assertEmpty($propPatch->getRemainingMutations());
|
|
|
|
|
|
|
|
$result = $propPatch->getResult();
|
2015-10-13 10:51:21 +00:00
|
|
|
$this->assertEquals(200, $result[self::LASTMODIFIED_PROPERTYNAME]);
|
2015-02-12 11:29:01 +00:00
|
|
|
$this->assertEquals(200, $result[self::GETETAG_PROPERTYNAME]);
|
|
|
|
}
|
|
|
|
|
2015-11-02 14:05:16 +00:00
|
|
|
public function testUpdatePropsForbidden() {
|
|
|
|
$node = $this->createTestNode('\OCA\DAV\Connector\Sabre\File');
|
|
|
|
|
|
|
|
$propPatch = new \Sabre\DAV\PropPatch(array(
|
|
|
|
self::OWNER_ID_PROPERTYNAME => 'user2',
|
|
|
|
self::OWNER_DISPLAY_NAME_PROPERTYNAME => 'User Two',
|
|
|
|
self::FILEID_PROPERTYNAME => 12345,
|
|
|
|
self::PERMISSIONS_PROPERTYNAME => 'C',
|
|
|
|
self::SIZE_PROPERTYNAME => 123,
|
|
|
|
self::DOWNLOADURL_PROPERTYNAME => 'http://example.com/',
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->plugin->handleUpdateProperties(
|
|
|
|
'/dummypath',
|
|
|
|
$propPatch
|
|
|
|
);
|
|
|
|
|
|
|
|
$propPatch->commit();
|
|
|
|
|
|
|
|
$this->assertEmpty($propPatch->getRemainingMutations());
|
|
|
|
|
|
|
|
$result = $propPatch->getResult();
|
|
|
|
$this->assertEquals(403, $result[self::OWNER_ID_PROPERTYNAME]);
|
|
|
|
$this->assertEquals(403, $result[self::OWNER_DISPLAY_NAME_PROPERTYNAME]);
|
|
|
|
$this->assertEquals(403, $result[self::FILEID_PROPERTYNAME]);
|
|
|
|
$this->assertEquals(403, $result[self::PERMISSIONS_PROPERTYNAME]);
|
|
|
|
$this->assertEquals(403, $result[self::SIZE_PROPERTYNAME]);
|
|
|
|
$this->assertEquals(403, $result[self::DOWNLOADURL_PROPERTYNAME]);
|
|
|
|
}
|
|
|
|
|
2015-10-06 13:36:54 +00:00
|
|
|
/**
|
|
|
|
* Testcase from https://github.com/owncloud/core/issues/5251
|
|
|
|
*
|
|
|
|
* |-FolderA
|
|
|
|
* |-text.txt
|
|
|
|
* |-test.txt
|
|
|
|
*
|
|
|
|
* FolderA is an incomming shared folder and there are no delete permissions.
|
|
|
|
* Thus moving /FolderA/test.txt to /test.txt should fail already on that check
|
|
|
|
*
|
|
|
|
* @expectedException \Sabre\DAV\Exception\Forbidden
|
|
|
|
* @expectedExceptionMessage FolderA/test.txt cannot be deleted
|
|
|
|
*/
|
|
|
|
public function testMoveSrcNotDeletable() {
|
|
|
|
$fileInfoFolderATestTXT = $this->getMockBuilder('\OCP\Files\FileInfo')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$fileInfoFolderATestTXT->expects($this->once())
|
|
|
|
->method('isDeletable')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('FolderA/test.txt')
|
|
|
|
->willReturn($fileInfoFolderATestTXT);
|
|
|
|
|
|
|
|
$this->plugin->checkMove('FolderA/test.txt', 'test.txt');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMoveSrcDeletable() {
|
|
|
|
$fileInfoFolderATestTXT = $this->getMockBuilder('\OCP\Files\FileInfo')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$fileInfoFolderATestTXT->expects($this->once())
|
|
|
|
->method('isDeletable')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('FolderA/test.txt')
|
|
|
|
->willReturn($fileInfoFolderATestTXT);
|
|
|
|
|
|
|
|
$this->plugin->checkMove('FolderA/test.txt', 'test.txt');
|
|
|
|
}
|
2015-10-27 08:14:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \Sabre\DAV\Exception\NotFound
|
|
|
|
* @expectedExceptionMessage FolderA/test.txt does not exist
|
|
|
|
*/
|
|
|
|
public function testMoveSrcNotExist() {
|
|
|
|
$this->view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('FolderA/test.txt')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->plugin->checkMove('FolderA/test.txt', 'test.txt');
|
|
|
|
}
|
2015-02-12 11:29:01 +00:00
|
|
|
}
|