Merge pull request #13101 from owncloud/variable-is-undefined-please-use-a-proper-ide-in-the-future-whoever-did-that
Fix undefined variable and write unit tests for OC_Principal connector
This commit is contained in:
commit
c0ad6e818b
2 changed files with 306 additions and 26 deletions
|
@ -2,12 +2,34 @@
|
|||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
|
||||
namespace OC\Connector\Sabre;
|
||||
|
||||
use OCP\IUserManager;
|
||||
use OCP\IConfig;
|
||||
|
||||
class Principal implements \Sabre\DAVACL\PrincipalBackend\BackendInterface {
|
||||
/** @var IConfig */
|
||||
private $config;
|
||||
/** @var IUserManager */
|
||||
private $userManager;
|
||||
|
||||
/**
|
||||
* @param IConfig $config
|
||||
* @param IUserManager $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 +41,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 +76,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)) {
|
||||
if ($prefix === 'principals' && !is_null($user)) {
|
||||
$principal = [
|
||||
'uri' => 'principals/' . $user->getUID(),
|
||||
'{DAV:}displayname' => $user->getUID(),
|
||||
];
|
||||
|
||||
$principal = array(
|
||||
'uri' => 'principals/'.$name,
|
||||
'{DAV:}displayname' => $name,
|
||||
);
|
||||
|
||||
$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;
|
||||
}
|
||||
|
@ -79,6 +100,7 @@ class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\Bac
|
|||
*
|
||||
* @param string $principal
|
||||
* @return string[]
|
||||
* @throws \Sabre\DAV\Exception
|
||||
*/
|
||||
public function getGroupMemberSet($principal) {
|
||||
// TODO: for now the group principal has only one member, the user itself
|
||||
|
@ -87,9 +109,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']];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -97,12 +117,13 @@ class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\Bac
|
|||
*
|
||||
* @param string $principal
|
||||
* @return array
|
||||
* @throws \Sabre\DAV\Exception
|
||||
*/
|
||||
public function getGroupMembership($principal) {
|
||||
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');
|
||||
|
@ -128,17 +149,27 @@ class OC_Connector_Sabre_Principal implements \Sabre\DAVACL\PrincipalBackend\Bac
|
|||
*
|
||||
* @param string $principal
|
||||
* @param array $members
|
||||
* @return void
|
||||
* @throws \Sabre\DAV\Exception
|
||||
*/
|
||||
public function setGroupMemberSet($principal, array $members) {
|
||||
throw new \Sabre\DAV\Exception('Setting members of the group is not supported yet');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param array $mutations
|
||||
* @return int
|
||||
*/
|
||||
function updatePrincipal($path, $mutations) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prefixPath
|
||||
* @param array $searchProperties
|
||||
* @return array
|
||||
*/
|
||||
function searchPrincipals($prefixPath, array $searchProperties) {
|
||||
return array();
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
|
249
tests/lib/connector/sabre/principal.php
Normal file
249
tests/lib/connector/sabre/principal.php
Normal file
|
@ -0,0 +1,249 @@
|
|||
<?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.
|
||||
*/
|
||||
|
||||
namespace Test\Connector\Sabre;
|
||||
|
||||
use OCP\IUserManager;
|
||||
use OCP\IConfig;
|
||||
|
||||
class 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', []));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue