Improve OC\User\User coverage
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
parent
1afdce9ef8
commit
7b4265ab59
1 changed files with 204 additions and 49 deletions
|
@ -10,11 +10,13 @@
|
|||
namespace Test\User;
|
||||
|
||||
use OC\Hooks\PublicEmitter;
|
||||
use OC\User\Database;
|
||||
use OC\User\User;
|
||||
use OCP\Comments\ICommentsManager;
|
||||
use OCP\IConfig;
|
||||
use OCP\IUser;
|
||||
use OCP\Notification\IManager as INotificationManager;
|
||||
use OCP\Notification\INotification;
|
||||
use Test\TestCase;
|
||||
|
||||
/**
|
||||
* Class UserTest
|
||||
|
@ -23,7 +25,7 @@ use OCP\Notification\INotification;
|
|||
*
|
||||
* @package Test\User
|
||||
*/
|
||||
class UserTest extends \Test\TestCase {
|
||||
class UserTest extends TestCase {
|
||||
public function testDisplayName() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
|
@ -39,7 +41,7 @@ class UserTest extends \Test\TestCase {
|
|||
->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertEquals('Foo', $user->getDisplayName());
|
||||
}
|
||||
|
||||
|
@ -61,7 +63,7 @@ class UserTest extends \Test\TestCase {
|
|||
->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
|
||||
->will($this->returnValue(true));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertEquals('foo', $user->getDisplayName());
|
||||
}
|
||||
|
||||
|
@ -78,13 +80,13 @@ class UserTest extends \Test\TestCase {
|
|||
->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertEquals('foo', $user->getDisplayName());
|
||||
}
|
||||
|
||||
public function testSetPassword() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$backend->expects($this->once())
|
||||
|
@ -101,13 +103,13 @@ class UserTest extends \Test\TestCase {
|
|||
}
|
||||
}));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertTrue($user->setPassword('bar',''));
|
||||
}
|
||||
|
||||
public function testSetPasswordNotSupported() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$backend->expects($this->never())
|
||||
|
@ -117,13 +119,13 @@ class UserTest extends \Test\TestCase {
|
|||
->method('implementsActions')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertFalse($user->setPassword('bar',''));
|
||||
}
|
||||
|
||||
public function testChangeAvatarSupportedYes() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(AvatarUserDummy::class);
|
||||
$backend->expects($this->once())
|
||||
|
@ -141,13 +143,13 @@ class UserTest extends \Test\TestCase {
|
|||
}
|
||||
}));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertTrue($user->canChangeAvatar());
|
||||
}
|
||||
|
||||
public function testChangeAvatarSupportedNo() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(AvatarUserDummy::class);
|
||||
$backend->expects($this->once())
|
||||
|
@ -165,13 +167,13 @@ class UserTest extends \Test\TestCase {
|
|||
}
|
||||
}));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertFalse($user->canChangeAvatar());
|
||||
}
|
||||
|
||||
public function testChangeAvatarNotSupported() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(AvatarUserDummy::class);
|
||||
$backend->expects($this->never())
|
||||
|
@ -181,26 +183,26 @@ class UserTest extends \Test\TestCase {
|
|||
->method('implementsActions')
|
||||
->willReturn(false);
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertTrue($user->canChangeAvatar());
|
||||
}
|
||||
|
||||
public function testDelete() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$backend->expects($this->once())
|
||||
->method('deleteUser')
|
||||
->with($this->equalTo('foo'));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertTrue($user->delete());
|
||||
}
|
||||
|
||||
public function testGetHome() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$backend->expects($this->once())
|
||||
|
@ -218,20 +220,20 @@ class UserTest extends \Test\TestCase {
|
|||
}
|
||||
}));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertEquals('/home/foo', $user->getHome());
|
||||
}
|
||||
|
||||
public function testGetBackendClassName() {
|
||||
$user = new \OC\User\User('foo', new \Test\Util\User\Dummy());
|
||||
$user = new User('foo', new \Test\Util\User\Dummy());
|
||||
$this->assertEquals('Dummy', $user->getBackendClassName());
|
||||
$user = new \OC\User\User('foo', new \OC\User\Database());
|
||||
$user = new User('foo', new \OC\User\Database());
|
||||
$this->assertEquals('Database', $user->getBackendClassName());
|
||||
}
|
||||
|
||||
public function testGetHomeNotSupported() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$backend->expects($this->never())
|
||||
|
@ -252,13 +254,13 @@ class UserTest extends \Test\TestCase {
|
|||
->with($this->equalTo('datadirectory'))
|
||||
->will($this->returnValue('arbitrary/path'));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend, null, $allConfig);
|
||||
$user = new User('foo', $backend, null, $allConfig);
|
||||
$this->assertEquals('arbitrary/path/foo', $user->getHome());
|
||||
}
|
||||
|
||||
public function testCanChangePassword() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
|
@ -272,13 +274,13 @@ class UserTest extends \Test\TestCase {
|
|||
}
|
||||
}));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertTrue($user->canChangePassword());
|
||||
}
|
||||
|
||||
public function testCanChangePasswordNotSupported() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
|
@ -286,13 +288,13 @@ class UserTest extends \Test\TestCase {
|
|||
->method('implementsActions')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertFalse($user->canChangePassword());
|
||||
}
|
||||
|
||||
public function testCanChangeDisplayName() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
|
@ -306,13 +308,13 @@ class UserTest extends \Test\TestCase {
|
|||
}
|
||||
}));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertTrue($user->canChangeDisplayName());
|
||||
}
|
||||
|
||||
public function testCanChangeDisplayNameNotSupported() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
|
@ -320,15 +322,15 @@ class UserTest extends \Test\TestCase {
|
|||
->method('implementsActions')
|
||||
->will($this->returnValue(false));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertFalse($user->canChangeDisplayName());
|
||||
}
|
||||
|
||||
public function testSetDisplayNameSupported() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(Database::class);
|
||||
$backend = $this->createMock(\OC\User\Database::class);
|
||||
|
||||
$backend->expects($this->any())
|
||||
->method('implementsActions')
|
||||
|
@ -345,7 +347,7 @@ class UserTest extends \Test\TestCase {
|
|||
->with('foo','Foo')
|
||||
->willReturn(true);
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertTrue($user->setDisplayName('Foo'));
|
||||
$this->assertEquals('Foo',$user->getDisplayName());
|
||||
}
|
||||
|
@ -355,9 +357,9 @@ class UserTest extends \Test\TestCase {
|
|||
*/
|
||||
public function testSetDisplayNameEmpty() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(Database::class);
|
||||
$backend = $this->createMock(\OC\User\Database::class);
|
||||
|
||||
$backend->expects($this->any())
|
||||
->method('implementsActions')
|
||||
|
@ -369,16 +371,16 @@ class UserTest extends \Test\TestCase {
|
|||
}
|
||||
}));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertFalse($user->setDisplayName(' '));
|
||||
$this->assertEquals('foo',$user->getDisplayName());
|
||||
}
|
||||
|
||||
public function testSetDisplayNameNotSupported() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(Database::class);
|
||||
$backend = $this->createMock(\OC\User\Database::class);
|
||||
|
||||
$backend->expects($this->any())
|
||||
->method('implementsActions')
|
||||
|
@ -387,7 +389,7 @@ class UserTest extends \Test\TestCase {
|
|||
$backend->expects($this->never())
|
||||
->method('setDisplayName');
|
||||
|
||||
$user = new \OC\User\User('foo', $backend);
|
||||
$user = new User('foo', $backend);
|
||||
$this->assertFalse($user->setDisplayName('Foo'));
|
||||
$this->assertEquals('foo',$user->getDisplayName());
|
||||
}
|
||||
|
@ -397,14 +399,14 @@ class UserTest extends \Test\TestCase {
|
|||
$test = $this;
|
||||
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$backend->expects($this->once())
|
||||
->method('setPassword');
|
||||
|
||||
/**
|
||||
* @param \OC\User\User $user
|
||||
* @param User $user
|
||||
* @param string $password
|
||||
*/
|
||||
$hook = function ($user, $password) use ($test, &$hooksCalled) {
|
||||
|
@ -427,7 +429,7 @@ class UserTest extends \Test\TestCase {
|
|||
}
|
||||
}));
|
||||
|
||||
$user = new \OC\User\User('foo', $backend, $emitter);
|
||||
$user = new User('foo', $backend, $emitter);
|
||||
|
||||
$user->setPassword('bar','');
|
||||
$this->assertEquals(2, $hooksCalled);
|
||||
|
@ -450,17 +452,17 @@ class UserTest extends \Test\TestCase {
|
|||
$test = $this;
|
||||
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$backend->expects($this->once())
|
||||
->method('deleteUser')
|
||||
->willReturn($result);
|
||||
$emitter = new PublicEmitter();
|
||||
$user = new \OC\User\User('foo', $backend, $emitter);
|
||||
$user = new User('foo', $backend, $emitter);
|
||||
|
||||
/**
|
||||
* @param \OC\User\User $user
|
||||
* @param User $user
|
||||
*/
|
||||
$hook = function ($user) use ($test, &$hooksCalled) {
|
||||
$hooksCalled++;
|
||||
|
@ -527,7 +529,7 @@ class UserTest extends \Test\TestCase {
|
|||
|
||||
public function testGetCloudId() {
|
||||
/**
|
||||
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
$urlGenerator = $this->getMockBuilder('\OC\URLGenerator')
|
||||
|
@ -538,7 +540,160 @@ class UserTest extends \Test\TestCase {
|
|||
->method('getAbsoluteURL')
|
||||
->withAnyParameters()
|
||||
->willReturn('http://localhost:8888/owncloud');
|
||||
$user = new \OC\User\User('foo', $backend, null, null, $urlGenerator);
|
||||
$this->assertEquals("foo@localhost:8888/owncloud", $user->getCloudId());
|
||||
$user = new User('foo', $backend, null, null, $urlGenerator);
|
||||
$this->assertEquals('foo@localhost:8888/owncloud', $user->getCloudId());
|
||||
}
|
||||
|
||||
public function testSetEMailAddressEmpty() {
|
||||
/**
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
$test = $this;
|
||||
$hooksCalled = 0;
|
||||
|
||||
/**
|
||||
* @param IUser $user
|
||||
* @param string $feature
|
||||
* @param string $value
|
||||
*/
|
||||
$hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
|
||||
$hooksCalled++;
|
||||
$test->assertEquals('eMailAddress', $feature);
|
||||
$test->assertEquals('', $value);
|
||||
};
|
||||
|
||||
$emitter = new PublicEmitter();
|
||||
$emitter->listen('\OC\User', 'changeUser', $hook);
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->expects($this->once())
|
||||
->method('deleteUserValue')
|
||||
->with(
|
||||
'foo',
|
||||
'settings',
|
||||
'email'
|
||||
);
|
||||
|
||||
$user = new User('foo', $backend, $emitter, $config);
|
||||
$user->setEMailAddress('');
|
||||
}
|
||||
|
||||
public function testSetEMailAddress() {
|
||||
/**
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
$test = $this;
|
||||
$hooksCalled = 0;
|
||||
|
||||
/**
|
||||
* @param IUser $user
|
||||
* @param string $feature
|
||||
* @param string $value
|
||||
*/
|
||||
$hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
|
||||
$hooksCalled++;
|
||||
$test->assertEquals('eMailAddress', $feature);
|
||||
$test->assertEquals('foo@bar.com', $value);
|
||||
};
|
||||
|
||||
$emitter = new PublicEmitter();
|
||||
$emitter->listen('\OC\User', 'changeUser', $hook);
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->expects($this->once())
|
||||
->method('setUserValue')
|
||||
->with(
|
||||
'foo',
|
||||
'settings',
|
||||
'email',
|
||||
'foo@bar.com'
|
||||
);
|
||||
|
||||
$user = new User('foo', $backend, $emitter, $config);
|
||||
$user->setEMailAddress('foo@bar.com');
|
||||
}
|
||||
|
||||
public function testGetLastLogin() {
|
||||
/**
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->method('getUserValue')
|
||||
->will($this->returnCallback(function ($uid, $app, $key, $default) {
|
||||
if ($uid === 'foo' && $app === 'login' && $key === 'lastLogin') {
|
||||
return 42;
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}));
|
||||
|
||||
$user = new User('foo', $backend, null, $config);
|
||||
$this->assertSame(42, $user->getLastLogin());
|
||||
}
|
||||
|
||||
public function testSetEnabled() {
|
||||
/**
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->expects($this->once())
|
||||
->method('setUserValue')
|
||||
->with(
|
||||
$this->equalTo('foo'),
|
||||
$this->equalTo('core'),
|
||||
$this->equalTo('enabled'),
|
||||
'true'
|
||||
);
|
||||
|
||||
$user = new User('foo', $backend, null, $config);
|
||||
$user->setEnabled(true);
|
||||
}
|
||||
|
||||
public function testSetDisabled() {
|
||||
/**
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->expects($this->once())
|
||||
->method('setUserValue')
|
||||
->with(
|
||||
$this->equalTo('foo'),
|
||||
$this->equalTo('core'),
|
||||
$this->equalTo('enabled'),
|
||||
'false'
|
||||
);
|
||||
|
||||
$user = new User('foo', $backend, null, $config);
|
||||
$user->setEnabled(false);
|
||||
}
|
||||
|
||||
public function testGetEMailAddress() {
|
||||
/**
|
||||
* @var Backend | \PHPUnit_Framework_MockObject_MockObject $backend
|
||||
*/
|
||||
$backend = $this->createMock(\Test\Util\User\Dummy::class);
|
||||
|
||||
$config = $this->createMock(IConfig::class);
|
||||
$config->method('getUserValue')
|
||||
->will($this->returnCallback(function ($uid, $app, $key, $default) {
|
||||
if ($uid === 'foo' && $app === 'settings' && $key === 'email') {
|
||||
return 'foo@bar.com';
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
}));
|
||||
|
||||
$user = new User('foo', $backend, null, $config);
|
||||
$this->assertSame('foo@bar.com', $user->getEMailAddress());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue