diff options
author | Lukas Reschke <lukas@owncloud.com> | 2014-12-19 13:28:11 +0100 |
---|---|---|
committer | Morris Jobke <hey@morrisjobke.de> | 2015-01-05 09:54:12 +0100 |
commit | 556c9b6f460d462ff22f886ff678d4d4299b871c (patch) | |
tree | 11c95ab5d0cf3fbec7c4c5194eafe67a306177aa | |
parent | f2ddd565e843e7bccd24c10a45a30f9056bf24e3 (diff) | |
download | nextcloud-server-556c9b6f460d462ff22f886ff678d4d4299b871c.tar.gz nextcloud-server-556c9b6f460d462ff22f886ff678d4d4299b871c.zip |
Write unit-tests and use DI
-rw-r--r-- | lib/private/connector/sabre/principal.php | 61 | ||||
-rw-r--r-- | tests/lib/connector/sabre/principal.php | 247 |
2 files changed, 284 insertions, 24 deletions
diff --git a/lib/private/connector/sabre/principal.php b/lib/private/connector/sabre/principal.php index a5e026bbd36..5ddde98c358 100644 --- a/lib/private/connector/sabre/principal.php +++ b/lib/private/connector/sabre/principal.php @@ -2,12 +2,28 @@ /** * Copyright (c) 2011 Jakob Sack mail@jakobsack.de * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl> + * Copyright (c) 2014 Lukas Reschke lukas@owncloud.com + * * This file is licensed under the Affero General Public License version 3 or * later. * See the COPYING-README file. */ +use OCP\IUserManager; +use OCP\IConfig; + class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface { + /** @var IConfig */ + private $config; + /** @var IUserManager */ + private $userManager; + + public function __construct(IConfig $config, + IUserManager $userManager) { + $this->config = $config; + $this->userManager = $userManager; + } + /** * Returns a list of principals based on a prefix. * @@ -19,22 +35,21 @@ class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\Bac * {DAV:}displayname * * @param string $prefixPath - * @return array + * @return string[] */ - public function getPrincipalsByPrefix( $prefixPath ) { - $principals = array(); + public function getPrincipalsByPrefix($prefixPath) { + $principals = []; - if ($prefixPath == 'principals') { - foreach(OC_User::getUsers() as $user) { + if ($prefixPath === 'principals') { + foreach($this->userManager->search('') as $user) { - $user_uri = 'principals/'.$user; - $principal = array( - 'uri' => $user_uri, - '{DAV:}displayname' => $user, - ); + $principal = [ + 'uri' => 'principals/' . $user->getUID(), + '{DAV:}displayname' => $user->getUID() + ]; - $email = \OCP\Config::getUserValue($user, 'settings', 'email'); - if($email) { + $email = $this->config->getUserValue($user->getUID(), 'settings', 'email'); + if(!empty($email)) { $principal['{http://sabredav.org/ns}email-address'] = $email; } @@ -55,15 +70,15 @@ class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\Bac */ public function getPrincipalByPath($path) { list($prefix, $name) = explode('/', $path); + $user = $this->userManager->get($name); - if ($prefix == 'principals' && OC_User::userExists($name)) { - - $principal = array( - 'uri' => 'principals/'.$name, - '{DAV:}displayname' => $name, - ); + if ($prefix === 'principals' && !is_null($user)) { + $principal = [ + 'uri' => 'principals/' . $user->getUID(), + '{DAV:}displayname' => $user->getUID(), + ]; - $email= \OCP\Config::getUserValue($name, 'settings', 'email'); + $email = $this->config->getUserValue($user->getUID(), 'settings', 'email'); if($email) { $principal['{http://sabredav.org/ns}email-address'] = $email; } @@ -88,9 +103,7 @@ class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\Bac throw new \Sabre\DAV\Exception('Principal not found'); } - return array( - $principal['uri'] - ); + return [$principal['uri']]; } /** @@ -104,7 +117,7 @@ class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\Bac list($prefix, $name) = \Sabre\DAV\URLUtil::splitPath($principal); $group_membership = array(); - if ($prefix == 'principals') { + if ($prefix === 'principals') { $principal = $this->getPrincipalByPath($principal); if (!$principal) { throw new \Sabre\DAV\Exception('Principal not found'); @@ -141,6 +154,6 @@ class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\Bac } function searchPrincipals($prefixPath, array $searchProperties) { - return array(); + return []; } } diff --git a/tests/lib/connector/sabre/principal.php b/tests/lib/connector/sabre/principal.php new file mode 100644 index 00000000000..77edf0113ba --- /dev/null +++ b/tests/lib/connector/sabre/principal.php @@ -0,0 +1,247 @@ +<?php +/** + * @author Lukas Reschke + * @copyright 2014 Lukas Reschke lukas@owncloud.com + * + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +use OCP\IUserManager; +use OCP\IConfig; + +class Test_OC_Connector_Sabre_Principal extends \Test\TestCase { + /** @var IUserManager */ + private $userManager; + /** @var IConfig */ + private $config; + /** @var OC_Connector_Sabre_Principal */ + private $connector; + + public function setUp() { + $this->userManager = $this->getMockBuilder('\OCP\IUserManager') + ->disableOriginalConstructor()->getMock(); + $this->config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + + $this->connector = new OC_Connector_Sabre_Principal($this->config, $this->userManager); + parent::setUp(); + } + + public function testGetPrincipalsByPrefixWithoutPrefix() { + $response = $this->connector->getPrincipalsByPrefix(''); + $this->assertSame([], $response); + } + + public function testGetPrincipalsByPrefixWithUsers() { + $fooUser = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $fooUser + ->expects($this->exactly(3)) + ->method('getUID') + ->will($this->returnValue('foo')); + $barUser = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $barUser + ->expects($this->exactly(3)) + ->method('getUID') + ->will($this->returnValue('bar')); + $this->userManager + ->expects($this->once()) + ->method('search') + ->with('') + ->will($this->returnValue([$fooUser, $barUser])); + $this->config + ->expects($this->at(0)) + ->method('getUserValue') + ->with('foo', 'settings', 'email') + ->will($this->returnValue('')); + $this->config + ->expects($this->at(1)) + ->method('getUserValue') + ->with('bar', 'settings', 'email') + ->will($this->returnValue('bar@owncloud.org')); + + $expectedResponse = [ + 0 => [ + 'uri' => 'principals/foo', + '{DAV:}displayname' => 'foo' + ], + 1 => [ + 'uri' => 'principals/bar', + '{DAV:}displayname' => 'bar', + '{http://sabredav.org/ns}email-address' => 'bar@owncloud.org' + ] + ]; + $response = $this->connector->getPrincipalsByPrefix('principals'); + $this->assertSame($expectedResponse, $response); + } + + public function testGetPrincipalsByPrefixEmpty() { + $this->userManager + ->expects($this->once()) + ->method('search') + ->with('') + ->will($this->returnValue([])); + + $response = $this->connector->getPrincipalsByPrefix('principals'); + $this->assertSame([], $response); + } + + public function testGetPrincipalsByPathWithoutMail() { + $fooUser = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $fooUser + ->expects($this->exactly(3)) + ->method('getUID') + ->will($this->returnValue('foo')); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue($fooUser)); + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('foo', 'settings', 'email') + ->will($this->returnValue('')); + + $expectedResponse = [ + 'uri' => 'principals/foo', + '{DAV:}displayname' => 'foo' + ]; + $response = $this->connector->getPrincipalByPath('principals/foo'); + $this->assertSame($expectedResponse, $response); + } + + public function testGetPrincipalsByPathWithMail() { + $fooUser = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $fooUser + ->expects($this->exactly(3)) + ->method('getUID') + ->will($this->returnValue('foo')); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue($fooUser)); + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('foo', 'settings', 'email') + ->will($this->returnValue('foo@owncloud.org')); + + $expectedResponse = [ + 'uri' => 'principals/foo', + '{DAV:}displayname' => 'foo', + '{http://sabredav.org/ns}email-address' => 'foo@owncloud.org' + ]; + $response = $this->connector->getPrincipalByPath('principals/foo'); + $this->assertSame($expectedResponse, $response); + } + + public function testGetPrincipalsByPathEmpty() { + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue(null)); + + $response = $this->connector->getPrincipalByPath('principals/foo'); + $this->assertSame(null, $response); + } + + public function testGetGroupMemberSet() { + $fooUser = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $fooUser + ->expects($this->exactly(3)) + ->method('getUID') + ->will($this->returnValue('foo')); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue($fooUser)); + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('foo', 'settings', 'email') + ->will($this->returnValue('foo@owncloud.org')); + + $response = $this->connector->getGroupMemberSet('principals/foo'); + $this->assertSame(['principals/foo'], $response); + } + + /** + * @expectedException \Sabre\DAV\Exception + * @expectedExceptionMessage Principal not found + */ + public function testGetGroupMemberSetEmpty() { + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue(null)); + + $this->connector->getGroupMemberSet('principals/foo'); + } + + public function testGetGroupMembership() { + $fooUser = $this->getMockBuilder('\OC\User\User') + ->disableOriginalConstructor()->getMock(); + $fooUser + ->expects($this->exactly(3)) + ->method('getUID') + ->will($this->returnValue('foo')); + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue($fooUser)); + $this->config + ->expects($this->once()) + ->method('getUserValue') + ->with('foo', 'settings', 'email') + ->will($this->returnValue('foo@owncloud.org')); + + $expectedResponse = [ + 'principals/foo/calendar-proxy-read', + 'principals/foo/calendar-proxy-write' + ]; + $response = $this->connector->getGroupMembership('principals/foo'); + $this->assertSame($expectedResponse, $response); + } + + /** + * @expectedException \Sabre\DAV\Exception + * @expectedExceptionMessage Principal not found + */ + public function testGetGroupMembershipEmpty() { + $this->userManager + ->expects($this->once()) + ->method('get') + ->with('foo') + ->will($this->returnValue(null)); + + $this->connector->getGroupMembership('principals/foo'); + } + + /** + * @expectedException \Sabre\DAV\Exception + * @expectedExceptionMessage Setting members of the group is not supported yet + */ + public function testSetGroupMembership() { + $this->connector->setGroupMemberSet('principals/foo', ['foo']); + } + + public function testUpdatePrincipal() { + $this->assertSame(0, $this->connector->updatePrincipal('foo', [])); + } + + public function testSearchPrincipals() { + $this->assertSame([], $this->connector->searchPrincipals('principals', [])); + } +} |