Bladeren bron

Write unit-tests and use DI

tags/v8.0.0alpha2
Lukas Reschke 9 jaren geleden
bovenliggende
commit
556c9b6f46
2 gewijzigde bestanden met toevoegingen van 284 en 24 verwijderingen
  1. 37
    24
      lib/private/connector/sabre/principal.php
  2. 247
    0
      tests/lib/connector/sabre/principal.php

+ 37
- 24
lib/private/connector/sabre/principal.php Bestand weergeven

@@ -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 [];
}
}

+ 247
- 0
tests/lib/connector/sabre/principal.php Bestand weergeven

@@ -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', []));
}
}

Laden…
Annuleren
Opslaan