test custom properties backend against real database

test behaviour not implementation

Signed-off-by: Robin Appelman <robin@icewind.nl>
This commit is contained in:
Robin Appelman 2020-01-31 15:47:13 +01:00
parent a5164eef35
commit 594873da07
No known key found for this signature in database
GPG Key ID: 42B69D8A64526EFB
1 changed files with 87 additions and 52 deletions

View File

@ -32,12 +32,15 @@ use Sabre\DAV\PropPatch;
use Sabre\DAV\Tree; use Sabre\DAV\Tree;
use Test\TestCase; use Test\TestCase;
/**
* @group DB
*/
class CustomPropertiesBackendTest extends TestCase { class CustomPropertiesBackendTest extends TestCase {
/** @var Tree | \PHPUnit_Framework_MockObject_MockObject */ /** @var Tree | \PHPUnit_Framework_MockObject_MockObject */
private $tree; private $tree;
/** @var IDBConnection | \PHPUnit_Framework_MockObject_MockObject */ /** @var IDBConnection */
private $dbConnection; private $dbConnection;
/** @var IUser | \PHPUnit_Framework_MockObject_MockObject */ /** @var IUser | \PHPUnit_Framework_MockObject_MockObject */
@ -53,14 +56,17 @@ class CustomPropertiesBackendTest extends TestCase {
parent::setUp(); parent::setUp();
$this->tree = $this->createMock(Tree::class); $this->tree = $this->createMock(Tree::class);
$this->dbConnection = $this->createMock(IDBConnection::class);
$this->user = $this->createMock(IUser::class); $this->user = $this->createMock(IUser::class);
$this->user->method('getUID') $this->user->method('getUID')
->with() ->with()
->will($this->returnValue('dummy_user_42')); ->will($this->returnValue('dummy_user_42'));
$this->dbConnection = \OC::$server->getDatabaseConnection();
$this->backend = new CustomPropertiesBackend($this->tree, $this->backend = new CustomPropertiesBackend(
$this->dbConnection, $this->user); $this->tree,
$this->dbConnection,
$this->user
);
$this->tree->method('getNodeForPath') $this->tree->method('getNodeForPath')
->willReturnCallback(function ($path) { ->willReturnCallback(function ($path) {
@ -84,7 +90,49 @@ class CustomPropertiesBackendTest extends TestCase {
return $node; return $node;
} }
protected function tearDown(): void {
$query = $this->dbConnection->getQueryBuilder();
$query->delete('properties');
$query->execute();
parent::tearDown();
}
protected function insertProps(string $user, string $path, array $props) {
foreach ($props as $name => $value) {
$this->insertProp($user, $path, $name, $value);
}
}
protected function insertProp(string $user, string $path, string $name, string $value) {
$query = $this->dbConnection->getQueryBuilder();
$query->insert('properties')
->values([
'userid' => $query->createNamedParameter($user),
'propertypath' => $query->createNamedParameter($path),
'propertyname' => $query->createNamedParameter($name),
'propertyvalue' => $query->createNamedParameter($value),
]);
$query->execute();
}
protected function getProps(string $user, string $path) {
$query = $this->dbConnection->getQueryBuilder();
$query->select('propertyname', 'propertyvalue')
->from('properties')
->where($query->expr()->eq('userid', $query->createNamedParameter($user)))
->where($query->expr()->eq('propertypath', $query->createNamedParameter($path)));
return $query->execute()->fetchAll(\PDO::FETCH_KEY_PAIR);
}
public function testPropFindNoDbCalls() { public function testPropFindNoDbCalls() {
$db = $this->createMock(IDBConnection::class);
$backend = new CustomPropertiesBackend(
$this->tree,
$db,
$this->user
);
$propFind = $this->createMock(PropFind::class); $propFind = $this->createMock(PropFind::class);
$propFind->expects($this->at(0)) $propFind->expects($this->at(0))
->method('get404Properties') ->method('get404Properties')
@ -96,17 +144,16 @@ class CustomPropertiesBackendTest extends TestCase {
'{http://owncloud.org/ns}size', '{http://owncloud.org/ns}size',
])); ]));
$this->dbConnection->expects($this->never()) $db->expects($this->never())
->method($this->anything()); ->method($this->anything());
$this->addNode('foo_bar_path_1337_0'); $this->addNode('foo_bar_path_1337_0');
$this->backend->propFind('foo_bar_path_1337_0', $propFind); $backend->propFind('foo_bar_path_1337_0', $propFind);
} }
public function testPropFindCalendarCall() { public function testPropFindCalendarCall() {
$propFind = $this->createMock(PropFind::class); $propFind = $this->createMock(PropFind::class);
$propFind->expects($this->at(0)) $propFind->method('get404Properties')
->method('get404Properties')
->with() ->with()
->will($this->returnValue([ ->will($this->returnValue([
'{DAV:}getcontentlength', '{DAV:}getcontentlength',
@ -115,8 +162,7 @@ class CustomPropertiesBackendTest extends TestCase {
'{abc}def', '{abc}def',
])); ]));
$propFind->expects($this->at(1)) $propFind->method('getRequestedProperties')
->method('getRequestedProperties')
->with() ->with()
->will($this->returnValue([ ->will($this->returnValue([
'{DAV:}getcontentlength', '{DAV:}getcontentlength',
@ -128,71 +174,60 @@ class CustomPropertiesBackendTest extends TestCase {
'{abc}def', '{abc}def',
])); ]));
$statement = $this->createMock('\Doctrine\DBAL\Driver\Statement'); $props = [
$this->dbConnection->expects($this->once()) '{abc}def' => 'a',
->method('executeQuery') '{DAV:}displayname' => 'b',
->with('SELECT * FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ? AND `propertyname` in (?)', '{urn:ietf:params:xml:ns:caldav}calendar-description' => 'c',
['dummy_user_42', 'calendars/foo/bar_path_1337_0', [ '{urn:ietf:params:xml:ns:caldav}calendar-timezone' => 'd',
3 => '{abc}def', ];
4 => '{DAV:}displayname',
5 => '{urn:ietf:params:xml:ns:caldav}calendar-description', $this->insertProps('dummy_user_42', 'calendars/foo/bar_path_1337_0', $props);
6 => '{urn:ietf:params:xml:ns:caldav}calendar-timezone']],
[null, null, 102]) $setProps = [];
->will($this->returnValue($statement)); $propFind->method('set')
->willReturnCallback(function ($name, $value, $status) use (&$setProps) {
$setProps[$name] = $value;
});
$this->addNode('calendars/foo/bar_path_1337_0'); $this->addNode('calendars/foo/bar_path_1337_0');
$this->backend->propFind('calendars/foo/bar_path_1337_0', $propFind); $this->backend->propFind('calendars/foo/bar_path_1337_0', $propFind);
$this->assertEquals($props, $setProps);
} }
/** /**
* @dataProvider propPatchProvider * @dataProvider propPatchProvider
*/ */
public function testPropPatch($path, $propPatch) { public function testPropPatch(string $path, array $existing, array $props, array $result) {
$this->insertProps($this->user->getUID(), $path, $existing);
$this->addNode($path); $this->addNode($path);
$propPatch->expects($this->once()) $propPatch = new PropPatch($props);
->method('handleRemaining');
$this->backend->propPatch($path, $propPatch); $this->backend->propPatch($path, $propPatch);
$propPatch->commit();
$storedProps = $this->getProps($this->user->getUID(), $path);
$this->assertEquals($result, $storedProps);
} }
public function propPatchProvider() { public function propPatchProvider() {
$propPatchMock = $this->createMock(PropPatch::class);
return [ return [
['foo_bar_path_1337', $propPatchMock], ['foo_bar_path_1337', [], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']],
['foo_bar_path_1337', ['{DAV:}displayname' => 'foo'], ['{DAV:}displayname' => 'anything'], ['{DAV:}displayname' => 'anything']],
['foo_bar_path_1337', ['{DAV:}displayname' => 'foo'], ['{DAV:}displayname' => null], []],
]; ];
} }
public function testDelete() { public function testDelete() {
$statement = $this->createMock('\Doctrine\DBAL\Driver\Statement'); $this->insertProps('dummy_user_42', 'foo_bar_path_1337', ['foo' => 'bar']);
$statement->expects($this->at(0))
->method('execute')
->with(['dummy_user_42', 'foo_bar_path_1337']);
$statement->expects($this->at(1))
->method('closeCursor')
->with();
$this->dbConnection->expects($this->at(0))
->method('prepare')
->with('DELETE FROM `*PREFIX*properties` WHERE `userid` = ? AND `propertypath` = ?')
->will($this->returnValue($statement));
$this->backend->delete('foo_bar_path_1337'); $this->backend->delete('foo_bar_path_1337');
$this->assertEquals([], $this->getProps('dummy_user_42', 'foo_bar_path_1337'));
} }
public function testMove() { public function testMove() {
$statement = $this->createMock('\Doctrine\DBAL\Driver\Statement'); $this->insertProps('dummy_user_42', 'foo_bar_path_1337', ['foo' => 'bar']);
$statement->expects($this->at(0))
->method('execute')
->with(['bar_foo_path_7331', 'dummy_user_42', 'foo_bar_path_1337']);
$statement->expects($this->at(1))
->method('closeCursor')
->with();
$this->dbConnection->expects($this->at(0))
->method('prepare')
->with('UPDATE `*PREFIX*properties` SET `propertypath` = ? WHERE `userid` = ? AND `propertypath` = ?')
->will($this->returnValue($statement));
$this->backend->move('foo_bar_path_1337', 'bar_foo_path_7331'); $this->backend->move('foo_bar_path_1337', 'bar_foo_path_7331');
$this->assertEquals([], $this->getProps('dummy_user_42', 'foo_bar_path_1337'));
$this->assertEquals(['foo' => 'bar'], $this->getProps('dummy_user_42', 'bar_foo_path_7331'));
} }
} }