aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/User
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/User')
-rw-r--r--tests/lib/User/AvailabilityCoordinatorTest.php11
-rw-r--r--tests/lib/User/AvatarUserDummy.php1
-rw-r--r--tests/lib/User/Backend.php1
-rw-r--r--tests/lib/User/DatabaseTest.php23
-rw-r--r--tests/lib/User/Dummy.php1
-rw-r--r--tests/lib/User/ManagerTest.php75
-rw-r--r--tests/lib/User/SessionTest.php127
-rw-r--r--tests/lib/User/UserTest.php74
8 files changed, 204 insertions, 109 deletions
diff --git a/tests/lib/User/AvailabilityCoordinatorTest.php b/tests/lib/User/AvailabilityCoordinatorTest.php
index 8b9446279a6..09c1528912b 100644
--- a/tests/lib/User/AvailabilityCoordinatorTest.php
+++ b/tests/lib/User/AvailabilityCoordinatorTest.php
@@ -88,10 +88,17 @@ class AvailabilityCoordinatorTest extends TestCase {
->method('getAbsence')
->with($user->getUID())
->willReturn($absence);
+
+ $calls = [
+ [$user->getUID() . '_timezone', 'Europe/Berlin', 3600],
+ [$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300],
+ ];
$this->cache->expects(self::exactly(2))
->method('set')
- ->withConsecutive([$user->getUID() . '_timezone', 'Europe/Berlin', 3600],
- [$user->getUID(), '{"id":"420","startDate":1696111200,"endDate":1696802340,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300]);
+ ->willReturnCallback(static function () use (&$calls): void {
+ $expected = array_shift($calls);
+ self::assertEquals($expected, func_get_args());
+ });
$expected = new OutOfOfficeData(
'420',
diff --git a/tests/lib/User/AvatarUserDummy.php b/tests/lib/User/AvatarUserDummy.php
index d443a31e3f0..001dabd24c6 100644
--- a/tests/lib/User/AvatarUserDummy.php
+++ b/tests/lib/User/AvatarUserDummy.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
diff --git a/tests/lib/User/Backend.php b/tests/lib/User/Backend.php
index ed17776476a..dc5b245fa06 100644
--- a/tests/lib/User/Backend.php
+++ b/tests/lib/User/Backend.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
diff --git a/tests/lib/User/DatabaseTest.php b/tests/lib/User/DatabaseTest.php
index bd74ab333fb..33101173c0a 100644
--- a/tests/lib/User/DatabaseTest.php
+++ b/tests/lib/User/DatabaseTest.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -7,6 +8,7 @@
namespace Test\User;
+use OC\User\Database;
use OC\User\User;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
@@ -25,6 +27,9 @@ class DatabaseTest extends Backend {
/** @var IEventDispatcher|MockObject */
private $eventDispatcher;
+ /** @var \OC\User\Database */
+ protected $backend;
+
public function getUser() {
$user = parent::getUser();
$this->users[] = $user;
@@ -36,7 +41,7 @@ class DatabaseTest extends Backend {
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
- $this->backend = new \OC\User\Database($this->eventDispatcher);
+ $this->backend = new Database($this->eventDispatcher);
}
protected function tearDown(): void {
@@ -55,7 +60,7 @@ class DatabaseTest extends Backend {
$this->eventDispatcher->expects($this->once())->method('dispatchTyped')
->willReturnCallback(
- function (Event $event) {
+ function (Event $event): void {
$this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
/** @var ValidatePasswordPolicyEvent $event */
$this->assertSame('newpass', $event->getPassword());
@@ -68,7 +73,7 @@ class DatabaseTest extends Backend {
public function testVerifyPasswordEventFail(): void {
- $this->expectException(\OCP\HintException::class);
+ $this->expectException(HintException::class);
$this->expectExceptionMessage('password change failed');
$user = $this->getUser();
@@ -76,7 +81,7 @@ class DatabaseTest extends Backend {
$this->eventDispatcher->expects($this->once())->method('dispatchTyped')
->willReturnCallback(
- function (Event $event) {
+ function (Event $event): void {
$this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
/** @var ValidatePasswordPolicyEvent $event */
$this->assertSame('newpass', $event->getPassword());
@@ -139,4 +144,14 @@ class DatabaseTest extends Backend {
$result = $this->backend->getDisplayNames('@nextcloud.COM');
$this->assertCount(2, $result);
}
+
+ public function testUserCount(): void {
+ $base = $this->backend->countUsers() ?: 0;
+ $users = $this->backend->getUsers();
+ self::assertEquals($base, count($users));
+
+ $user = $this->getUser();
+ $this->backend->createUser($user, $user);
+ self::assertEquals($base + 1, $this->backend->countUsers());
+ }
}
diff --git a/tests/lib/User/Dummy.php b/tests/lib/User/Dummy.php
index ffc5d343200..ec5be8ec60a 100644
--- a/tests/lib/User/Dummy.php
+++ b/tests/lib/User/Dummy.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
diff --git a/tests/lib/User/ManagerTest.php b/tests/lib/User/ManagerTest.php
index 53f57eee086..d5872787d0a 100644
--- a/tests/lib/User/ManagerTest.php
+++ b/tests/lib/User/ManagerTest.php
@@ -9,14 +9,17 @@
namespace Test\User;
use OC\AllConfig;
+use OC\USER\BACKEND;
use OC\User\Database;
use OC\User\Manager;
+use OC\User\User;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IUser;
use OCP\IUserManager;
+use OCP\Server;
use Psr\Log\LoggerInterface;
use Test\TestCase;
@@ -79,6 +82,20 @@ class ManagerTest extends TestCase {
$this->assertTrue($manager->userExists('foo'));
}
+ public function testUserExistsTooLong(): void {
+ /** @var \Test\Util\User\Dummy|MockObject $backend */
+ $backend = $this->createMock(\Test\Util\User\Dummy::class);
+ $backend->expects($this->never())
+ ->method('userExists')
+ ->with($this->equalTo('foo'))
+ ->willReturn(true);
+
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
+ $manager->registerBackend($backend);
+
+ $this->assertFalse($manager->userExists('foo' . str_repeat('a', 62)));
+ }
+
public function testUserExistsSingleBackendNotExists(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
@@ -164,7 +181,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->any())
->method('implementsActions')
->willReturnCallback(function ($actions) {
- if ($actions === \OC\USER\BACKEND::CHECK_PASSWORD) {
+ if ($actions === BACKEND::CHECK_PASSWORD) {
return true;
} else {
return false;
@@ -175,7 +192,7 @@ class ManagerTest extends TestCase {
$manager->registerBackend($backend);
$user = $manager->checkPassword('foo', 'bar');
- $this->assertTrue($user instanceof \OC\User\User);
+ $this->assertTrue($user instanceof User);
}
public function testCheckPasswordNotSupported(): void {
@@ -230,6 +247,20 @@ class ManagerTest extends TestCase {
$this->assertEquals(null, $manager->get('foo'));
}
+ public function testGetTooLong(): void {
+ /** @var \Test\Util\User\Dummy|MockObject $backend */
+ $backend = $this->createMock(\Test\Util\User\Dummy::class);
+ $backend->expects($this->never())
+ ->method('userExists')
+ ->with($this->equalTo('foo'))
+ ->willReturn(false);
+
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
+ $manager->registerBackend($backend);
+
+ $this->assertEquals(null, $manager->get('foo' . str_repeat('a', 62)));
+ }
+
public function testGetOneBackendDoNotTranslateLoginNames(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
@@ -305,7 +336,7 @@ class ManagerTest extends TestCase {
$this->assertEquals('foo3', array_shift($result)->getUID());
}
- public function dataCreateUserInvalid() {
+ public static function dataCreateUserInvalid(): array {
return [
['te?st', 'foo', 'Only the following characters are allowed in a username:'
. ' "a-z", "A-Z", "0-9", spaces and "_.@-\'"'],
@@ -333,12 +364,11 @@ class ManagerTest extends TestCase {
['..', 'foo', 'Username must not consist of dots only'],
['.test', '', 'A valid password must be provided'],
['test', '', 'A valid password must be provided'],
+ ['test' . str_repeat('a', 61), '', 'Login is too long'],
];
}
- /**
- * @dataProvider dataCreateUserInvalid
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataCreateUserInvalid')]
public function testCreateUserInvalid($uid, $password, $exception): void {
/** @var \Test\Util\User\Dummy|\PHPUnit\Framework\MockObject\MockObject $backend */
$backend = $this->createMock(\Test\Util\User\Dummy::class);
@@ -516,7 +546,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->once())
->method('implementsActions')
- ->with(\OC\USER\BACKEND::COUNT_USERS)
+ ->with(BACKEND::COUNT_USERS)
->willReturn(true);
$backend->expects($this->once())
@@ -545,7 +575,7 @@ class ManagerTest extends TestCase {
$backend1->expects($this->once())
->method('implementsActions')
- ->with(\OC\USER\BACKEND::COUNT_USERS)
+ ->with(BACKEND::COUNT_USERS)
->willReturn(true);
$backend1->expects($this->once())
->method('getBackendName')
@@ -558,7 +588,7 @@ class ManagerTest extends TestCase {
$backend2->expects($this->once())
->method('implementsActions')
- ->with(\OC\USER\BACKEND::COUNT_USERS)
+ ->with(BACKEND::COUNT_USERS)
->willReturn(true);
$backend2->expects($this->once())
->method('getBackendName')
@@ -580,7 +610,7 @@ class ManagerTest extends TestCase {
}
public function testCountUsersOnlyDisabled(): void {
- $manager = \OCP\Server::get(IUserManager::class);
+ $manager = Server::get(IUserManager::class);
// count other users in the db before adding our own
$countBefore = $manager->countDisabledUsers();
@@ -605,7 +635,7 @@ class ManagerTest extends TestCase {
}
public function testCountUsersOnlySeen(): void {
- $manager = \OCP\Server::get(IUserManager::class);
+ $manager = Server::get(IUserManager::class);
// count other users in the db before adding our own
$countBefore = $manager->countSeenUsers();
@@ -631,10 +661,10 @@ class ManagerTest extends TestCase {
}
public function testCallForSeenUsers(): void {
- $manager = \OCP\Server::get(IUserManager::class);
+ $manager = Server::get(IUserManager::class);
// count other users in the db before adding our own
$count = 0;
- $function = function (IUser $user) use (&$count) {
+ $function = function (IUser $user) use (&$count): void {
$count++;
};
$manager->callForAllUsers($function, '', true);
@@ -669,8 +699,8 @@ class ManagerTest extends TestCase {
* @preserveGlobalState disabled
*/
public function testRecentlyActive(): void {
- $config = \OCP\Server::get(IConfig::class);
- $manager = \OCP\Server::get(IUserManager::class);
+ $config = Server::get(IConfig::class);
+ $manager = Server::get(IUserManager::class);
// Create some users
$now = (string)time();
@@ -760,16 +790,11 @@ class ManagerTest extends TestCase {
$backend = $this->createMock(\Test\Util\User\Dummy::class);
$backend->expects($this->exactly(3))
->method('userExists')
- ->withConsecutive(
- [$this->equalTo('uid1')],
- [$this->equalTo('uid99')],
- [$this->equalTo('uid2')]
- )
- ->willReturnOnConsecutiveCalls(
- true,
- false,
- true
- );
+ ->willReturnMap([
+ ['uid1', true],
+ ['uid99', false],
+ ['uid2', true]
+ ]);
$manager = new \OC\User\Manager($config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php
index b3f040d71ec..50c449559a0 100644
--- a/tests/lib/User/SessionTest.php
+++ b/tests/lib/User/SessionTest.php
@@ -1,4 +1,5 @@
<?php
+
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
@@ -34,6 +35,7 @@ use OCP\Lockdown\ILockdownManager;
use OCP\Security\Bruteforce\IThrottler;
use OCP\Security\ISecureRandom;
use OCP\User\Events\PostLoginEvent;
+use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use function array_diff;
@@ -95,7 +97,7 @@ class SessionTest extends \Test\TestCase {
$this->logger,
$this->dispatcher
])
- ->setMethods([
+ ->onlyMethods([
'setMagicInCookie',
])
->getMock();
@@ -103,16 +105,14 @@ class SessionTest extends \Test\TestCase {
\OC_User::setIncognitoMode(false);
}
- public function isLoggedInData() {
+ public static function isLoggedInData(): array {
return [
[true],
[false],
];
}
- /**
- * @dataProvider isLoggedInData
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('isLoggedInData')]
public function testIsLoggedIn($isLoggedIn): void {
$session = $this->createMock(Memory::class);
@@ -120,7 +120,7 @@ class SessionTest extends \Test\TestCase {
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods([
+ ->onlyMethods([
'getUser'
])
->getMock();
@@ -157,7 +157,7 @@ class SessionTest extends \Test\TestCase {
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('bar')
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$session->expects($this->exactly(2))
->method('set')
->with($this->callback(function ($key) {
@@ -176,7 +176,7 @@ class SessionTest extends \Test\TestCase {
//keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -204,7 +204,7 @@ class SessionTest extends \Test\TestCase {
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods([
+ ->onlyMethods([
'prepareUserLogin'
])
->getMock();
@@ -215,9 +215,9 @@ class SessionTest extends \Test\TestCase {
->method('dispatchTyped')
->with(
$this->callback(function (PostLoginEvent $e) {
- return $e->getUser()->getUID() === 'foo' &&
- $e->getPassword() === 'bar' &&
- $e->isTokenLogin() === false;
+ return $e->getUser()->getUID() === 'foo'
+ && $e->getPassword() === 'bar'
+ && $e->isTokenLogin() === false;
})
);
@@ -237,13 +237,13 @@ class SessionTest extends \Test\TestCase {
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('bar')
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$managerMethods = get_class_methods(Manager::class);
//keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -277,7 +277,7 @@ class SessionTest extends \Test\TestCase {
//keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -297,7 +297,7 @@ class SessionTest extends \Test\TestCase {
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('bar')
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$user->expects($this->never())
->method('isEnabled');
@@ -321,7 +321,7 @@ class SessionTest extends \Test\TestCase {
// Keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -359,7 +359,7 @@ class SessionTest extends \Test\TestCase {
// Keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -403,7 +403,7 @@ class SessionTest extends \Test\TestCase {
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('bar')
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$manager->expects($this->once())
->method('checkPasswordNoLogging')
@@ -423,13 +423,13 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
+ ->onlyMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('doe')
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$this->config->expects($this->once())
->method('getSystemValueBool')
->with('token_auth_enforced', false)
@@ -459,13 +459,13 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
+ ->onlyMethods(['login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('doe')
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$this->config->expects($this->once())
->method('getSystemValueBool')
->with('token_auth_enforced', false)
@@ -485,7 +485,7 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
+ ->onlyMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$userSession->expects($this->once())
@@ -527,13 +527,13 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods(['login', 'isTwoFactorEnforced'])
+ ->onlyMethods(['login', 'isTwoFactorEnforced'])
->getMock();
$this->tokenProvider->expects($this->once())
->method('getToken')
->with('doe')
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$this->config->expects($this->once())
->method('getSystemValueBool')
->with('token_auth_enforced', false)
@@ -611,13 +611,52 @@ class SessionTest extends \Test\TestCase {
self::assertFalse($loginResult);
}
+ public function testTryTokenLoginNotAnAppPassword(): void {
+ $request = $this->createMock(IRequest::class);
+ $this->config->expects(self::once())
+ ->method('getSystemValueString')
+ ->with('instanceid')
+ ->willReturn('abc123');
+ $request->method('getHeader')->with('Authorization')->willReturn('');
+ $request->method('getCookie')->with('abc123')->willReturn('abcde12345');
+ $this->session->expects(self::once())
+ ->method('getId')
+ ->willReturn('abcde12345');
+ $dbToken = new PublicKeyToken();
+ $dbToken->setId(42);
+ $dbToken->setUid('johnny');
+ $dbToken->setLoginName('johnny');
+ $dbToken->setLastCheck(0);
+ $dbToken->setType(IToken::TEMPORARY_TOKEN);
+ $dbToken->setRemember(IToken::REMEMBER);
+ $this->tokenProvider->expects(self::any())
+ ->method('getToken')
+ ->with('abcde12345')
+ ->willReturn($dbToken);
+ $this->session->method('set')
+ ->willReturnCallback(function ($key, $value): void {
+ if ($key === 'app_password') {
+ throw new ExpectationFailedException('app_password should not be set in session');
+ }
+ });
+ $user = $this->createMock(IUser::class);
+ $user->method('isEnabled')->willReturn(true);
+ $this->manager->method('get')
+ ->with('johnny')
+ ->willReturn($user);
+
+ $loginResult = $this->userSession->tryTokenLogin($request);
+
+ self::assertTrue($loginResult);
+ }
+
public function testRememberLoginValidToken(): void {
$session = $this->createMock(Memory::class);
$managerMethods = get_class_methods(Manager::class);
//keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -627,7 +666,7 @@ class SessionTest extends \Test\TestCase {
->getMock();
$userSession = $this->getMockBuilder(Session::class)
//override, otherwise tests will fail because of setcookie()
- ->setMethods(['setMagicInCookie', 'setLoginName'])
+ ->onlyMethods(['setMagicInCookie', 'setLoginName'])
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->getMock();
@@ -685,7 +724,7 @@ class SessionTest extends \Test\TestCase {
$setUID = false;
$session
->method('set')
- ->willReturnCallback(function ($k, $v) use (&$setUID) {
+ ->willReturnCallback(function ($k, $v) use (&$setUID): void {
if ($k === 'user_id' && $v === 'foo') {
$setUID = true;
}
@@ -707,7 +746,7 @@ class SessionTest extends \Test\TestCase {
//keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -717,7 +756,7 @@ class SessionTest extends \Test\TestCase {
->getMock();
$userSession = $this->getMockBuilder(Session::class)
//override, otherwise tests will fail because of setcookie()
- ->setMethods(['setMagicInCookie'])
+ ->onlyMethods(['setMagicInCookie'])
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->getMock();
@@ -748,7 +787,7 @@ class SessionTest extends \Test\TestCase {
$this->tokenProvider->expects($this->once())
->method('renewSessionToken')
->with($oldSessionId, $sessionId)
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$user->expects($this->never())
->method('getUID')
@@ -772,7 +811,7 @@ class SessionTest extends \Test\TestCase {
//keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -782,7 +821,7 @@ class SessionTest extends \Test\TestCase {
->getMock();
$userSession = $this->getMockBuilder(Session::class)
//override, otherwise tests will fail because of setcookie()
- ->setMethods(['setMagicInCookie'])
+ ->onlyMethods(['setMagicInCookie'])
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->getMock();
@@ -825,7 +864,7 @@ class SessionTest extends \Test\TestCase {
//keep following methods intact in order to ensure hooks are working
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class)
- ->setMethods($mockedManagerMethods)
+ ->onlyMethods($mockedManagerMethods)
->setConstructorArgs([
$this->config,
$this->createMock(ICacheFactory::class),
@@ -835,7 +874,7 @@ class SessionTest extends \Test\TestCase {
->getMock();
$userSession = $this->getMockBuilder(Session::class)
//override, otherwise tests will fail because of setcookie()
- ->setMethods(['setMagicInCookie'])
+ ->onlyMethods(['setMagicInCookie'])
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
->getMock();
$token = 'goodToken';
@@ -885,7 +924,7 @@ class SessionTest extends \Test\TestCase {
$session->set('user_id', 'foo');
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods([
+ ->onlyMethods([
'validateSession'
])
->getMock();
@@ -932,7 +971,7 @@ class SessionTest extends \Test\TestCase {
$this->tokenProvider->expects($this->once())
->method('getToken')
->with($password)
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$this->tokenProvider->expects($this->once())
->method('generateToken')
@@ -973,7 +1012,7 @@ class SessionTest extends \Test\TestCase {
$this->tokenProvider->expects($this->once())
->method('getToken')
->with($password)
- ->will($this->throwException(new InvalidTokenException()));
+ ->willThrowException(new InvalidTokenException());
$this->tokenProvider->expects($this->once())
->method('generateToken')
@@ -1090,7 +1129,7 @@ class SessionTest extends \Test\TestCase {
$this->session
->method('set')
- ->willReturnCallback(function ($k, $v) use (&$davAuthenticatedSet, &$lastPasswordConfirmSet) {
+ ->willReturnCallback(function ($k, $v) use (&$davAuthenticatedSet, &$lastPasswordConfirmSet): void {
switch ($k) {
case Auth::DAV_AUTHENTICATED:
$davAuthenticatedSet = $v;
@@ -1115,7 +1154,7 @@ class SessionTest extends \Test\TestCase {
$this->logger,
$this->dispatcher
])
- ->setMethods([
+ ->onlyMethods([
'logClientIn',
'getUser',
])
@@ -1167,7 +1206,7 @@ class SessionTest extends \Test\TestCase {
$this->logger,
$this->dispatcher
])
- ->setMethods([
+ ->onlyMethods([
'logClientIn',
])
->getMock();
@@ -1195,7 +1234,7 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
+ ->onlyMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$userSession->expects($this->once())
@@ -1241,7 +1280,7 @@ class SessionTest extends \Test\TestCase {
/** @var Session $userSession */
$userSession = $this->getMockBuilder(Session::class)
->setConstructorArgs([$manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher])
- ->setMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
+ ->onlyMethods(['isTokenPassword', 'login', 'supportsCookies', 'createSessionToken', 'getUser'])
->getMock();
$userSession->expects($this->once())
diff --git a/tests/lib/User/UserTest.php b/tests/lib/User/UserTest.php
index e79aaadd593..05056c92193 100644
--- a/tests/lib/User/UserTest.php
+++ b/tests/lib/User/UserTest.php
@@ -11,9 +11,11 @@ namespace Test\User;
use OC\AllConfig;
use OC\Files\Mount\ObjectHomeMountProvider;
use OC\Hooks\PublicEmitter;
+use OC\User\Database;
use OC\User\User;
use OCP\Comments\ICommentsManager;
use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\FileInfo;
use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\IURLGenerator;
@@ -217,7 +219,7 @@ class UserTest extends TestCase {
public function testDeleteWithDifferentHome(): void {
/** @var ObjectHomeMountProvider $homeProvider */
- $homeProvider = \OC::$server->get(ObjectHomeMountProvider::class);
+ $homeProvider = Server::get(ObjectHomeMountProvider::class);
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('foo');
@@ -283,7 +285,7 @@ class UserTest extends TestCase {
public function testGetBackendClassName(): void {
$user = new User('foo', new \Test\Util\User\Dummy(), $this->dispatcher);
$this->assertEquals('Dummy', $user->getBackendClassName());
- $user = new User('foo', new \OC\User\Database(), $this->dispatcher);
+ $user = new User('foo', new Database(), $this->dispatcher);
$this->assertEquals('Database', $user->getBackendClassName());
}
@@ -391,7 +393,7 @@ class UserTest extends TestCase {
/**
* @var Backend | MockObject $backend
*/
- $backend = $this->createMock(\OC\User\Database::class);
+ $backend = $this->createMock(Database::class);
$backend->expects($this->any())
->method('implementsActions')
@@ -420,7 +422,7 @@ class UserTest extends TestCase {
/**
* @var Backend | MockObject $backend
*/
- $backend = $this->createMock(\OC\User\Database::class);
+ $backend = $this->createMock(Database::class);
$backend->expects($this->any())
->method('implementsActions')
@@ -441,7 +443,7 @@ class UserTest extends TestCase {
/**
* @var Backend | MockObject $backend
*/
- $backend = $this->createMock(\OC\User\Database::class);
+ $backend = $this->createMock(Database::class);
$backend->expects($this->any())
->method('implementsActions')
@@ -470,7 +472,7 @@ class UserTest extends TestCase {
* @param User $user
* @param string $password
*/
- $hook = function ($user, $password) use ($test, &$hooksCalled) {
+ $hook = function ($user, $password) use ($test, &$hooksCalled): void {
$hooksCalled++;
$test->assertEquals('foo', $user->getUID());
$test->assertEquals('bar', $password);
@@ -496,7 +498,7 @@ class UserTest extends TestCase {
$this->assertEquals(2, $hooksCalled);
}
- public function dataDeleteHooks() {
+ public static function dataDeleteHooks(): array {
return [
[true, 2],
[false, 1],
@@ -504,10 +506,10 @@ class UserTest extends TestCase {
}
/**
- * @dataProvider dataDeleteHooks
* @param bool $result
* @param int $expectedHooks
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteHooks')]
public function testDeleteHooks($result, $expectedHooks): void {
$hooksCalled = 0;
$test = $this;
@@ -536,7 +538,7 @@ class UserTest extends TestCase {
/**
* @param User $user
*/
- $hook = function ($user) use ($test, &$hooksCalled) {
+ $hook = function ($user) use ($test, &$hooksCalled): void {
$hooksCalled++;
$test->assertEquals('foo', $user->getUID());
};
@@ -585,14 +587,14 @@ class UserTest extends TestCase {
->method('markProcessed');
}
- $this->overwriteService(\OCP\Notification\IManager::class, $notificationManager);
- $this->overwriteService(\OCP\Comments\ICommentsManager::class, $commentsManager);
+ $this->overwriteService(INotificationManager::class, $notificationManager);
+ $this->overwriteService(ICommentsManager::class, $commentsManager);
$this->assertSame($result, $user->delete());
$this->restoreService(AllConfig::class);
- $this->restoreService(\OCP\Comments\ICommentsManager::class);
- $this->restoreService(\OCP\Notification\IManager::class);
+ $this->restoreService(ICommentsManager::class);
+ $this->restoreService(INotificationManager::class);
$this->assertEquals($expectedHooks, $hooksCalled);
}
@@ -616,7 +618,7 @@ class UserTest extends TestCase {
$userConfig = [];
$config->expects(self::atLeast(2))
->method('setUserValue')
- ->willReturnCallback(function () {
+ ->willReturnCallback(function (): void {
$userConfig[] = func_get_args();
});
@@ -625,14 +627,14 @@ class UserTest extends TestCase {
->method('deleteReferencesOfActor')
->willThrowException(new \Error('Test exception'));
- $this->overwriteService(\OCP\Comments\ICommentsManager::class, $commentsManager);
+ $this->overwriteService(ICommentsManager::class, $commentsManager);
$this->expectException(\Error::class);
$user = $this->getMockBuilder(User::class)
->onlyMethods(['getHome'])
->setConstructorArgs(['foo', $backend, $this->dispatcher, null, $config])
->getMock();
-
+
$user->expects(self::atLeastOnce())
->method('getHome')
->willReturn('/home/path');
@@ -647,19 +649,17 @@ class UserTest extends TestCase {
$userConfig,
);
- $this->restoreService(\OCP\Comments\ICommentsManager::class);
+ $this->restoreService(ICommentsManager::class);
}
- public function dataGetCloudId(): array {
+ public static function dataGetCloudId(): array {
return [
['https://localhost:8888/nextcloud', 'foo@localhost:8888/nextcloud'],
['http://localhost:8888/nextcloud', 'foo@http://localhost:8888/nextcloud'],
];
}
- /**
- * @dataProvider dataGetCloudId
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataGetCloudId')]
public function testGetCloudId(string $absoluteUrl, string $cloudId): void {
/** @var Backend|MockObject $backend */
$backend = $this->createMock(\Test\Util\User\Dummy::class);
@@ -685,7 +685,7 @@ class UserTest extends TestCase {
* @param string $feature
* @param string $value
*/
- $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
+ $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled): void {
$hooksCalled++;
$test->assertEquals('eMailAddress', $feature);
$test->assertEquals('', $value);
@@ -721,7 +721,7 @@ class UserTest extends TestCase {
* @param string $feature
* @param string $value
*/
- $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
+ $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled): void {
$hooksCalled++;
$test->assertEquals('eMailAddress', $feature);
$test->assertEquals('foo@bar.com', $value);
@@ -784,7 +784,7 @@ class UserTest extends TestCase {
* @param string $feature
* @param string $value
*/
- $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled) {
+ $hook = function (IUser $user, $feature, $value) use ($test, &$hooksCalled): void {
$hooksCalled++;
$test->assertEquals('quota', $feature);
$test->assertEquals('23 TB', $value);
@@ -830,12 +830,12 @@ class UserTest extends TestCase {
['files', 'allow_unlimited_quota', '1', '1'],
];
$config->method('getUserValue')
- ->will($this->returnValueMap($userValueMap));
+ ->willReturnMap($userValueMap);
$config->method('getAppValue')
- ->will($this->returnValueMap($appValueMap));
+ ->willReturnMap($appValueMap);
- $quota = $user->getQuota();
- $this->assertEquals('none', $quota);
+ $this->assertEquals('none', $user->getQuota());
+ $this->assertEquals(FileInfo::SPACE_UNLIMITED, $user->getQuotaBytes());
}
public function testGetDefaultUnlimitedQuotaForbidden(): void {
@@ -864,12 +864,12 @@ class UserTest extends TestCase {
['files', 'default_quota', '1 GB', '1 GB'],
];
$config->method('getUserValue')
- ->will($this->returnValueMap($userValueMap));
+ ->willReturnMap($userValueMap);
$config->method('getAppValue')
- ->will($this->returnValueMap($appValueMap));
+ ->willReturnMap($appValueMap);
- $quota = $user->getQuota();
- $this->assertEquals('1 GB', $quota);
+ $this->assertEquals('1 GB', $user->getQuota());
+ $this->assertEquals(1024 * 1024 * 1024, $user->getQuotaBytes());
}
public function testSetQuotaAddressNoChange(): void {
@@ -929,6 +929,12 @@ class UserTest extends TestCase {
$this->equalTo('enabled'),
'true'
);
+ /* dav event listener gets the manager list from config */
+ $config->expects(self::any())
+ ->method('getUserValue')
+ ->willReturnCallback(
+ fn ($user, $app, $key, $default) => ($key === 'enabled' ? 'false' : $default)
+ );
$user = new User('foo', $backend, $this->dispatcher, null, $config);
$user->setEnabled(true);
@@ -958,7 +964,7 @@ class UserTest extends TestCase {
null,
$config,
])
- ->setMethods(['isEnabled', 'triggerChange'])
+ ->onlyMethods(['isEnabled', 'triggerChange'])
->getMock();
$user->expects($this->once())
@@ -992,7 +998,7 @@ class UserTest extends TestCase {
null,
$config,
])
- ->setMethods(['isEnabled', 'triggerChange'])
+ ->onlyMethods(['isEnabled', 'triggerChange'])
->getMock();
$user->expects($this->once())