aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/User
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/User')
-rw-r--r--tests/lib/User/AvailabilityCoordinatorTest.php48
-rw-r--r--tests/lib/User/AvatarUserDummy.php22
-rw-r--r--tests/lib/User/Backend.php28
-rw-r--r--tests/lib/User/DatabaseTest.php54
-rw-r--r--tests/lib/User/Dummy.php22
-rw-r--r--tests/lib/User/ManagerTest.php251
-rw-r--r--tests/lib/User/SessionTest.php291
-rw-r--r--tests/lib/User/UserTest.php224
8 files changed, 586 insertions, 354 deletions
diff --git a/tests/lib/User/AvailabilityCoordinatorTest.php b/tests/lib/User/AvailabilityCoordinatorTest.php
index b41b1fbac2a..09c1528912b 100644
--- a/tests/lib/User/AvailabilityCoordinatorTest.php
+++ b/tests/lib/User/AvailabilityCoordinatorTest.php
@@ -3,25 +3,8 @@
declare(strict_types=1);
/**
- * @copyright Copyright (c) 2023 Richard Steinmetz <richard@steinmetz.cloud>
- *
- * @author Richard Steinmetz <richard@steinmetz.cloud>
- *
- * @license AGPL-3.0-or-later
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\User;
@@ -90,6 +73,8 @@ class AvailabilityCoordinatorTest extends TestCase {
$absence->setLastDay('2023-10-08');
$absence->setStatus('Vacation');
$absence->setMessage('On vacation');
+ $absence->setReplacementUserId('batman');
+ $absence->setReplacementUserDisplayName('Bruce Wayne');
$this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
$user = $this->createMock(IUser::class);
@@ -103,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"}', 300]);
+ ->willReturnCallback(static function () use (&$calls): void {
+ $expected = array_shift($calls);
+ self::assertEquals($expected, func_get_args());
+ });
$expected = new OutOfOfficeData(
'420',
@@ -115,6 +107,8 @@ class AvailabilityCoordinatorTest extends TestCase {
1696802340,
'Vacation',
'On vacation',
+ 'batman',
+ 'Bruce Wayne',
);
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
self::assertEquals($expected, $actual);
@@ -128,6 +122,8 @@ class AvailabilityCoordinatorTest extends TestCase {
$absence->setLastDay('2023-10-08');
$absence->setStatus('Vacation');
$absence->setMessage('On vacation');
+ $absence->setReplacementUserId('batman');
+ $absence->setReplacementUserDisplayName('Bruce Wayne');
$user = $this->createMock(IUser::class);
$user->method('getUID')
@@ -135,7 +131,7 @@ class AvailabilityCoordinatorTest extends TestCase {
$this->cache->expects(self::exactly(2))
->method('get')
- ->willReturnOnConsecutiveCalls('UTC', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation"}');
+ ->willReturnOnConsecutiveCalls('UTC', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}');
$this->absenceService->expects(self::never())
->method('getAbsence');
$this->cache->expects(self::exactly(1))
@@ -148,6 +144,8 @@ class AvailabilityCoordinatorTest extends TestCase {
1696809540,
'Vacation',
'On vacation',
+ 'batman',
+ 'Bruce Wayne'
);
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
self::assertEquals($expected, $actual);
@@ -187,6 +185,8 @@ class AvailabilityCoordinatorTest extends TestCase {
$absence->setLastDay('2023-10-08');
$absence->setStatus('Vacation');
$absence->setMessage('On vacation');
+ $absence->setReplacementUserId('batman');
+ $absence->setReplacementUserDisplayName('Bruce Wayne');
$this->timezoneService->method('getUserTimezone')->with('user')->willReturn('Europe/Berlin');
$user = $this->createMock(IUser::class);
@@ -202,7 +202,7 @@ class AvailabilityCoordinatorTest extends TestCase {
->willReturn($absence);
$this->cache->expects(self::once())
->method('set')
- ->with('user', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation"}', 300);
+ ->with('user', '{"id":"420","startDate":1696118400,"endDate":1696809540,"shortMessage":"Vacation","message":"On vacation","replacementUserId":"batman","replacementUserDisplayName":"Bruce Wayne"}', 300);
$expected = new OutOfOfficeData(
'420',
@@ -211,6 +211,8 @@ class AvailabilityCoordinatorTest extends TestCase {
1696809540,
'Vacation',
'On vacation',
+ 'batman',
+ 'Bruce Wayne'
);
$actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user);
self::assertEquals($expected, $actual);
diff --git a/tests/lib/User/AvatarUserDummy.php b/tests/lib/User/AvatarUserDummy.php
index 87e1cfc5f83..001dabd24c6 100644
--- a/tests/lib/User/AvatarUserDummy.php
+++ b/tests/lib/User/AvatarUserDummy.php
@@ -1,23 +1,9 @@
<?php
+
/**
- * ownCloud
- *
- * @author Arthur Schiwon
- * @copyright 2013 Arthur Schiwon blizzz@owncloud.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2020-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\User;
diff --git a/tests/lib/User/Backend.php b/tests/lib/User/Backend.php
index fd87da72364..dc5b245fa06 100644
--- a/tests/lib/User/Backend.php
+++ b/tests/lib/User/Backend.php
@@ -1,23 +1,9 @@
<?php
+
/**
- * ownCloud
- *
- * @author Robin Appelman
- * @copyright 2012 Robin Appelman icewind@owncloud.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\User;
@@ -47,7 +33,7 @@ abstract class Backend extends \Test\TestCase {
return $this->getUniqueID('test_');
}
- public function testAddRemove() {
+ public function testAddRemove(): void {
//get the number of groups we start with, in case there are exising groups
$startCount = count($this->backend->getUsers());
@@ -71,7 +57,7 @@ abstract class Backend extends \Test\TestCase {
$this->assertFalse((array_search($name2, $this->backend->getUsers()) !== false));
}
- public function testLogin() {
+ public function testLogin(): void {
$name1 = $this->getUser();
$name2 = $this->getUser();
@@ -99,7 +85,7 @@ abstract class Backend extends \Test\TestCase {
$this->assertFalse($this->backend->checkPassword($name2, 'newpass1'));
}
- public function testSearch() {
+ public function testSearch(): void {
$name1 = 'foobarbaz';
$name2 = 'bazbarfoo';
$name3 = 'notme';
diff --git a/tests/lib/User/DatabaseTest.php b/tests/lib/User/DatabaseTest.php
index 1a375cdebae..33101173c0a 100644
--- a/tests/lib/User/DatabaseTest.php
+++ b/tests/lib/User/DatabaseTest.php
@@ -1,27 +1,14 @@
<?php
+
/**
- * ownCloud
- *
- * @author Robin Appelman
- * @copyright 2012 Robin Appelman icewind@owncloud.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\User;
+use OC\User\Database;
use OC\User\User;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventDispatcher;
@@ -40,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;
@@ -51,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 {
@@ -64,13 +54,13 @@ class DatabaseTest extends Backend {
parent::tearDown();
}
- public function testVerifyPasswordEvent() {
+ public function testVerifyPasswordEvent(): void {
$user = $this->getUser();
$this->backend->createUser($user, 'pass1');
$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());
@@ -82,8 +72,8 @@ class DatabaseTest extends Backend {
}
- public function testVerifyPasswordEventFail() {
- $this->expectException(\OCP\HintException::class);
+ public function testVerifyPasswordEventFail(): void {
+ $this->expectException(HintException::class);
$this->expectExceptionMessage('password change failed');
$user = $this->getUser();
@@ -91,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());
@@ -103,14 +93,14 @@ class DatabaseTest extends Backend {
$this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
}
- public function testCreateUserInvalidatesCache() {
+ public function testCreateUserInvalidatesCache(): void {
$user1 = $this->getUniqueID('test_');
$this->assertFalse($this->backend->userExists($user1));
$this->backend->createUser($user1, 'pw');
$this->assertTrue($this->backend->userExists($user1));
}
- public function testDeleteUserInvalidatesCache() {
+ public function testDeleteUserInvalidatesCache(): void {
$user1 = $this->getUniqueID('test_');
$this->backend->createUser($user1, 'pw');
$this->assertTrue($this->backend->userExists($user1));
@@ -120,7 +110,7 @@ class DatabaseTest extends Backend {
$this->assertTrue($this->backend->userExists($user1));
}
- public function testSearch() {
+ public function testSearch(): void {
parent::testSearch();
$user1 = $this->getUser();
@@ -154,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 a9e4a2936a7..ec5be8ec60a 100644
--- a/tests/lib/User/Dummy.php
+++ b/tests/lib/User/Dummy.php
@@ -1,23 +1,9 @@
<?php
+
/**
- * ownCloud
- *
- * @author Robin Appelman
- * @copyright 2012 Robin Appelman icewind@owncloud.com
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
+ * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\User;
diff --git a/tests/lib/User/ManagerTest.php b/tests/lib/User/ManagerTest.php
index 5e8c2ed7131..d5872787d0a 100644
--- a/tests/lib/User/ManagerTest.php
+++ b/tests/lib/User/ManagerTest.php
@@ -1,22 +1,26 @@
<?php
/**
- * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
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;
/**
@@ -35,6 +39,8 @@ class ManagerTest extends TestCase {
private $cacheFactory;
/** @var ICache */
private $cache;
+ /** @var LoggerInterface */
+ private $logger;
protected function setUp(): void {
parent::setUp();
@@ -43,14 +49,15 @@ class ManagerTest extends TestCase {
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->cache = $this->createMock(ICache::class);
+ $this->logger = $this->createMock(LoggerInterface::class);
$this->cacheFactory->method('createDistributed')
->willReturn($this->cache);
}
- public function testGetBackends() {
+ public function testGetBackends(): void {
$userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($userDummyBackend);
$this->assertEquals([$userDummyBackend], $manager->getBackends());
$dummyDatabaseBackend = $this->createMock(Database::class);
@@ -59,7 +66,7 @@ class ManagerTest extends TestCase {
}
- public function testUserExistsSingleBackendExists() {
+ public function testUserExistsSingleBackendExists(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -69,13 +76,27 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo'))
->willReturn(true);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$this->assertTrue($manager->userExists('foo'));
}
- public function testUserExistsSingleBackendNotExists() {
+ 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
*/
@@ -85,19 +106,19 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo'))
->willReturn(false);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$this->assertFalse($manager->userExists('foo'));
}
- public function testUserExistsNoBackends() {
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ public function testUserExistsNoBackends(): void {
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$this->assertFalse($manager->userExists('foo'));
}
- public function testUserExistsTwoBackendsSecondExists() {
+ public function testUserExistsTwoBackendsSecondExists(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend1
*/
@@ -116,14 +137,14 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo'))
->willReturn(true);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
$this->assertTrue($manager->userExists('foo'));
}
- public function testUserExistsTwoBackendsFirstExists() {
+ public function testUserExistsTwoBackendsFirstExists(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend1
*/
@@ -140,14 +161,14 @@ class ManagerTest extends TestCase {
$backend2->expects($this->never())
->method('userExists');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
$this->assertTrue($manager->userExists('foo'));
}
- public function testCheckPassword() {
+ public function testCheckPassword(): void {
/**
* @var \OC\User\Backend | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -160,21 +181,21 @@ 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;
}
});
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$user = $manager->checkPassword('foo', 'bar');
- $this->assertTrue($user instanceof \OC\User\User);
+ $this->assertTrue($user instanceof User);
}
- public function testCheckPasswordNotSupported() {
+ public function testCheckPasswordNotSupported(): void {
/**
* @var \OC\User\Backend | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -186,13 +207,13 @@ class ManagerTest extends TestCase {
->method('implementsActions')
->willReturn(false);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$this->assertFalse($manager->checkPassword('foo', 'bar'));
}
- public function testGetOneBackendExists() {
+ public function testGetOneBackendExists(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -204,13 +225,13 @@ class ManagerTest extends TestCase {
$backend->expects($this->never())
->method('loginName2UserName');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$this->assertEquals('foo', $manager->get('foo')->getUID());
}
- public function testGetOneBackendNotExists() {
+ public function testGetOneBackendNotExists(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -220,13 +241,27 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo'))
->willReturn(false);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$this->assertEquals(null, $manager->get('foo'));
}
- public function testGetOneBackendDoNotTranslateLoginNames() {
+ 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
*/
@@ -238,13 +273,13 @@ class ManagerTest extends TestCase {
$backend->expects($this->never())
->method('loginName2UserName');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$this->assertEquals('bLeNdEr', $manager->get('bLeNdEr')->getUID());
}
- public function testSearchOneBackend() {
+ public function testSearchOneBackend(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -256,7 +291,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->never())
->method('loginName2UserName');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$result = $manager->search('fo');
@@ -267,7 +302,7 @@ class ManagerTest extends TestCase {
$this->assertEquals('foo', array_shift($result)->getUID());
}
- public function testSearchTwoBackendLimitOffset() {
+ public function testSearchTwoBackendLimitOffset(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend1
*/
@@ -290,7 +325,7 @@ class ManagerTest extends TestCase {
$backend2->expects($this->never())
->method('loginName2UserName');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
@@ -301,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 "_.@-\'"'],
@@ -329,13 +364,12 @@ 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
- */
- public function testCreateUserInvalid($uid, $password, $exception) {
+ #[\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);
$backend->expects($this->once())
@@ -344,14 +378,14 @@ class ManagerTest extends TestCase {
->willReturn(true);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$this->expectException(\InvalidArgumentException::class, $exception);
$manager->createUser($uid, $password);
}
- public function testCreateUserSingleBackendNotExists() {
+ public function testCreateUserSingleBackendNotExists(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -371,7 +405,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->never())
->method('loginName2UserName');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$user = $manager->createUser('foo', 'bar');
@@ -379,7 +413,7 @@ class ManagerTest extends TestCase {
}
- public function testCreateUserSingleBackendExists() {
+ public function testCreateUserSingleBackendExists(): void {
$this->expectException(\Exception::class);
/**
@@ -398,13 +432,13 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo'))
->willReturn(true);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$manager->createUser('foo', 'bar');
}
- public function testCreateUserSingleBackendNotSupported() {
+ public function testCreateUserSingleBackendNotSupported(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -419,20 +453,20 @@ class ManagerTest extends TestCase {
$backend->expects($this->never())
->method('userExists');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$this->assertFalse($manager->createUser('foo', 'bar'));
}
- public function testCreateUserNoBackends() {
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ public function testCreateUserNoBackends(): void {
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$this->assertFalse($manager->createUser('foo', 'bar'));
}
- public function testCreateUserFromBackendWithBackendError() {
+ public function testCreateUserFromBackendWithBackendError(): void {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Could not create account');
@@ -446,12 +480,12 @@ class ManagerTest extends TestCase {
->with('MyUid', 'MyPassword')
->willReturn(false);
- $manager = new Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->createUserFromBackend('MyUid', 'MyPassword', $backend);
}
- public function testCreateUserTwoBackendExists() {
+ public function testCreateUserTwoBackendExists(): void {
$this->expectException(\Exception::class);
/**
@@ -486,22 +520,22 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo'))
->willReturn(true);
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
$manager->createUser('foo', 'bar');
}
- public function testCountUsersNoBackend() {
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ public function testCountUsersNoBackend(): void {
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$result = $manager->countUsers();
$this->assertTrue(is_array($result));
$this->assertTrue(empty($result));
}
- public function testCountUsersOneBackend() {
+ public function testCountUsersOneBackend(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -512,14 +546,14 @@ 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())
->method('getBackendName')
->willReturn('Mock_Test_Util_User_Dummy');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$result = $manager->countUsers();
@@ -530,7 +564,7 @@ class ManagerTest extends TestCase {
$this->assertEquals(7, $users);
}
- public function testCountUsersTwoBackends() {
+ public function testCountUsersTwoBackends(): void {
/**
* @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend
*/
@@ -541,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')
@@ -554,13 +588,13 @@ 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')
->willReturn('Mock_Test_Util_User_Dummy');
- $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($this->config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend1);
$manager->registerBackend($backend2);
@@ -575,8 +609,8 @@ class ManagerTest extends TestCase {
$this->assertEquals(7 + 16, $users);
}
- public function testCountUsersOnlyDisabled() {
- $manager = \OC::$server->getUserManager();
+ public function testCountUsersOnlyDisabled(): void {
+ $manager = Server::get(IUserManager::class);
// count other users in the db before adding our own
$countBefore = $manager->countDisabledUsers();
@@ -600,8 +634,8 @@ class ManagerTest extends TestCase {
$user4->delete();
}
- public function testCountUsersOnlySeen() {
- $manager = \OC::$server->getUserManager();
+ public function testCountUsersOnlySeen(): void {
+ $manager = Server::get(IUserManager::class);
// count other users in the db before adding our own
$countBefore = $manager->countSeenUsers();
@@ -626,11 +660,11 @@ class ManagerTest extends TestCase {
$user4->delete();
}
- public function testCallForSeenUsers() {
- $manager = \OC::$server->getUserManager();
+ public function testCallForSeenUsers(): void {
+ $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);
@@ -660,7 +694,67 @@ class ManagerTest extends TestCase {
$user4->delete();
}
- public function testDeleteUser() {
+ /**
+ * @runInSeparateProcess
+ * @preserveGlobalState disabled
+ */
+ public function testRecentlyActive(): void {
+ $config = Server::get(IConfig::class);
+ $manager = Server::get(IUserManager::class);
+
+ // Create some users
+ $now = (string)time();
+ $user1 = $manager->createUser('test_active_1', 'test_active_1');
+ $config->setUserValue('test_active_1', 'login', 'lastLogin', $now);
+ $user1->setDisplayName('test active 1');
+ $user1->setSystemEMailAddress('roger@active.com');
+
+ $user2 = $manager->createUser('TEST_ACTIVE_2_FRED', 'TEST_ACTIVE_2');
+ $config->setUserValue('TEST_ACTIVE_2_FRED', 'login', 'lastLogin', $now);
+ $user2->setDisplayName('TEST ACTIVE 2 UPPER');
+ $user2->setSystemEMailAddress('Fred@Active.Com');
+
+ $user3 = $manager->createUser('test_active_3', 'test_active_3');
+ $config->setUserValue('test_active_3', 'login', 'lastLogin', $now + 1);
+ $user3->setDisplayName('test active 3');
+
+ $user4 = $manager->createUser('test_active_4', 'test_active_4');
+ $config->setUserValue('test_active_4', 'login', 'lastLogin', $now);
+ $user4->setDisplayName('Test Active 4');
+
+ $user5 = $manager->createUser('test_inactive_1', 'test_inactive_1');
+ $user5->setDisplayName('Test Inactive 1');
+ $user2->setSystemEMailAddress('jeanne@Active.Com');
+
+ // Search recently active
+ // - No search, case-insensitive order
+ $users = $manager->getLastLoggedInUsers(4);
+ $this->assertEquals(['test_active_3', 'test_active_1', 'TEST_ACTIVE_2_FRED', 'test_active_4'], $users);
+ // - Search, case-insensitive order
+ $users = $manager->getLastLoggedInUsers(search: 'act');
+ $this->assertEquals(['test_active_3', 'test_active_1', 'TEST_ACTIVE_2_FRED', 'test_active_4'], $users);
+ // - No search with offset
+ $users = $manager->getLastLoggedInUsers(2, 2);
+ $this->assertEquals(['TEST_ACTIVE_2_FRED', 'test_active_4'], $users);
+ // - Case insensitive search (email)
+ $users = $manager->getLastLoggedInUsers(search: 'active.com');
+ $this->assertEquals(['test_active_1', 'TEST_ACTIVE_2_FRED'], $users);
+ // - Case insensitive search (display name)
+ $users = $manager->getLastLoggedInUsers(search: 'upper');
+ $this->assertEquals(['TEST_ACTIVE_2_FRED'], $users);
+ // - Case insensitive search (uid)
+ $users = $manager->getLastLoggedInUsers(search: 'fred');
+ $this->assertEquals(['TEST_ACTIVE_2_FRED'], $users);
+
+ // Delete users and config keys
+ $user1->delete();
+ $user2->delete();
+ $user3->delete();
+ $user4->delete();
+ $user5->delete();
+ }
+
+ public function testDeleteUser(): void {
$config = $this->getMockBuilder(AllConfig::class)
->disableOriginalConstructor()
->getMock();
@@ -673,7 +767,7 @@ class ManagerTest extends TestCase {
->method('getAppValue')
->willReturnArgument(2);
- $manager = new \OC\User\Manager($config, $this->cacheFactory, $this->eventDispatcher);
+ $manager = new \OC\User\Manager($config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$backend = new \Test\Util\User\Dummy();
$manager->registerBackend($backend);
@@ -683,7 +777,7 @@ class ManagerTest extends TestCase {
$this->assertFalse($manager->userExists('foo'));
}
- public function testGetByEmail() {
+ public function testGetByEmail(): void {
$config = $this->getMockBuilder(AllConfig::class)
->disableOriginalConstructor()
->getMock();
@@ -696,18 +790,13 @@ 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
- );
-
- $manager = new \OC\User\Manager($config, $this->cacheFactory, $this->eventDispatcher);
+ ->willReturnMap([
+ ['uid1', true],
+ ['uid99', false],
+ ['uid2', true]
+ ]);
+
+ $manager = new \OC\User\Manager($config, $this->cacheFactory, $this->eventDispatcher, $this->logger);
$manager->registerBackend($backend);
$users = $manager->getByEmail('test@example.com');
diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php
index 50adda64afd..50c449559a0 100644
--- a/tests/lib/User/SessionTest.php
+++ b/tests/lib/User/SessionTest.php
@@ -1,9 +1,9 @@
<?php
+
/**
- * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\User;
@@ -11,9 +11,11 @@ namespace Test\User;
use OC\AppFramework\Http\Request;
use OC\Authentication\Events\LoginFailed;
use OC\Authentication\Exceptions\InvalidTokenException;
+use OC\Authentication\Exceptions\PasswordlessTokenException;
use OC\Authentication\Exceptions\PasswordLoginForbiddenException;
use OC\Authentication\Token\IProvider;
use OC\Authentication\Token\IToken;
+use OC\Authentication\Token\PublicKeyToken;
use OC\Security\CSRF\CsrfTokenManager;
use OC\Session\Memory;
use OC\User\LoginException;
@@ -33,8 +35,11 @@ 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;
+use function get_class_methods;
/**
* @group DB
@@ -92,7 +97,7 @@ class SessionTest extends \Test\TestCase {
$this->logger,
$this->dispatcher
])
- ->setMethods([
+ ->onlyMethods([
'setMagicInCookie',
])
->getMock();
@@ -100,24 +105,22 @@ class SessionTest extends \Test\TestCase {
\OC_User::setIncognitoMode(false);
}
- public function isLoggedInData() {
+ public static function isLoggedInData(): array {
return [
[true],
[false],
];
}
- /**
- * @dataProvider isLoggedInData
- */
- public function testIsLoggedIn($isLoggedIn) {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ #[\PHPUnit\Framework\Attributes\DataProvider('isLoggedInData')]
+ public function testIsLoggedIn($isLoggedIn): void {
+ $session = $this->createMock(Memory::class);
$manager = $this->createMock(Manager::class);
$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();
@@ -128,8 +131,8 @@ class SessionTest extends \Test\TestCase {
$this->assertEquals($isLoggedIn, $userSession->isLoggedIn());
}
- public function testSetUser() {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ public function testSetUser(): void {
+ $session = $this->createMock(Memory::class);
$session->expects($this->once())
->method('set')
->with('user_id', 'foo');
@@ -147,14 +150,14 @@ class SessionTest extends \Test\TestCase {
$userSession->setUser($user);
}
- public function testLoginValidPasswordEnabled() {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ public function testLoginValidPasswordEnabled(): void {
+ $session = $this->createMock(Memory::class);
$session->expects($this->once())
->method('regenerateId');
$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) {
@@ -173,11 +176,12 @@ 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),
$this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
])
->getMock();
@@ -200,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();
@@ -211,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;
})
);
@@ -222,10 +226,10 @@ class SessionTest extends \Test\TestCase {
}
- public function testLoginValidPasswordDisabled() {
+ public function testLoginValidPasswordDisabled(): void {
$this->expectException(LoginException::class);
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ $session = $this->createMock(Memory::class);
$session->expects($this->never())
->method('set');
$session->expects($this->once())
@@ -233,17 +237,18 @@ 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),
$this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
])
->getMock();
@@ -266,17 +271,18 @@ class SessionTest extends \Test\TestCase {
$userSession->login('foo', 'bar');
}
- public function testLoginInvalidPassword() {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ public function testLoginInvalidPassword(): 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),
$this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
])
->getMock();
$backend = $this->createMock(\Test\Util\User\Dummy::class);
@@ -291,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');
@@ -309,8 +315,84 @@ class SessionTest extends \Test\TestCase {
$userSession->login('foo', 'bar');
}
- public function testLoginNonExisting() {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ public function testPasswordlessLoginNoLastCheckUpdate(): 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)
+ ->onlyMethods($mockedManagerMethods)
+ ->setConstructorArgs([
+ $this->config,
+ $this->createMock(ICacheFactory::class),
+ $this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
+ ])
+ ->getMock();
+ $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
+
+ $session->expects($this->never())
+ ->method('set');
+ $session->expects($this->once())
+ ->method('regenerateId');
+ $token = new PublicKeyToken();
+ $token->setLoginName('foo');
+ $token->setLastCheck(0); // Never
+ $token->setUid('foo');
+ $this->tokenProvider
+ ->method('getPassword')
+ ->with($token)
+ ->willThrowException(new PasswordlessTokenException());
+ $this->tokenProvider
+ ->method('getToken')
+ ->with('app-password')
+ ->willReturn($token);
+ $this->tokenProvider->expects(self::never())
+ ->method('updateToken');
+
+ $userSession->login('foo', 'app-password');
+ }
+
+ public function testLoginLastCheckUpdate(): 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)
+ ->onlyMethods($mockedManagerMethods)
+ ->setConstructorArgs([
+ $this->config,
+ $this->createMock(ICacheFactory::class),
+ $this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
+ ])
+ ->getMock();
+ $userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
+
+ $session->expects($this->never())
+ ->method('set');
+ $session->expects($this->once())
+ ->method('regenerateId');
+ $token = new PublicKeyToken();
+ $token->setLoginName('foo');
+ $token->setLastCheck(0); // Never
+ $token->setUid('foo');
+ $this->tokenProvider
+ ->method('getPassword')
+ ->with($token)
+ ->willReturn('secret');
+ $this->tokenProvider
+ ->method('getToken')
+ ->with('app-password')
+ ->willReturn($token);
+ $this->tokenProvider->expects(self::once())
+ ->method('updateToken');
+
+ $userSession->login('foo', 'app-password');
+ }
+
+ public function testLoginNonExisting(): void {
+ $session = $this->createMock(Memory::class);
$manager = $this->createMock(Manager::class);
$userSession = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher);
@@ -321,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')
@@ -331,7 +413,7 @@ class SessionTest extends \Test\TestCase {
$userSession->login('foo', 'bar');
}
- public function testLogClientInNoTokenPasswordWith2fa() {
+ public function testLogClientInNoTokenPasswordWith2fa(): void {
$this->expectException(PasswordLoginForbiddenException::class);
$manager = $this->createMock(Manager::class);
@@ -341,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)
@@ -369,7 +451,7 @@ class SessionTest extends \Test\TestCase {
$userSession->logClientIn('john', 'doe', $request, $this->throttler);
}
- public function testLogClientInUnexist() {
+ public function testLogClientInUnexist(): void {
$manager = $this->createMock(Manager::class);
$session = $this->createMock(ISession::class);
$request = $this->createMock(IRequest::class);
@@ -377,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)
@@ -395,7 +477,7 @@ class SessionTest extends \Test\TestCase {
$this->assertFalse($userSession->logClientIn('unexist', 'doe', $request, $this->throttler));
}
- public function testLogClientInWithTokenPassword() {
+ public function testLogClientInWithTokenPassword(): void {
$manager = $this->createMock(Manager::class);
$session = $this->createMock(ISession::class);
$request = $this->createMock(IRequest::class);
@@ -403,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())
@@ -435,7 +517,7 @@ class SessionTest extends \Test\TestCase {
}
- public function testLogClientInNoTokenPasswordNo2fa() {
+ public function testLogClientInNoTokenPasswordNo2fa(): void {
$this->expectException(PasswordLoginForbiddenException::class);
$manager = $this->createMock(Manager::class);
@@ -445,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)
@@ -529,22 +611,62 @@ class SessionTest extends \Test\TestCase {
self::assertFalse($loginResult);
}
- public function testRememberLoginValidToken() {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ 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),
$this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
])
->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();
@@ -602,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;
}
@@ -618,22 +740,23 @@ class SessionTest extends \Test\TestCase {
$this->assertTrue($granted);
}
- public function testRememberLoginInvalidSessionToken() {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ public function testRememberLoginInvalidSessionToken(): 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),
$this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
])
->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();
@@ -664,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')
@@ -682,22 +805,23 @@ class SessionTest extends \Test\TestCase {
$this->assertFalse($granted);
}
- public function testRememberLoginInvalidToken() {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ public function testRememberLoginInvalidToken(): 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),
$this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
])
->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();
@@ -734,22 +858,23 @@ class SessionTest extends \Test\TestCase {
$this->assertFalse($granted);
}
- public function testRememberLoginInvalidUser() {
- $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock();
+ public function testRememberLoginInvalidUser(): 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),
$this->createMock(IEventDispatcher::class),
+ $this->createMock(LoggerInterface::class),
])
->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';
@@ -779,7 +904,7 @@ class SessionTest extends \Test\TestCase {
$this->assertFalse($granted);
}
- public function testActiveUserAfterSetSession() {
+ public function testActiveUserAfterSetSession(): void {
$users = [
'foo' => new User('foo', null, $this->createMock(IEventDispatcher::class)),
'bar' => new User('bar', null, $this->createMock(IEventDispatcher::class))
@@ -795,11 +920,11 @@ class SessionTest extends \Test\TestCase {
return $users[$uid];
});
- $session = new Memory('');
+ $session = new Memory();
$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();
@@ -808,13 +933,13 @@ class SessionTest extends \Test\TestCase {
$this->assertEquals($users['foo'], $userSession->getUser());
- $session2 = new Memory('');
+ $session2 = new Memory();
$session2->set('user_id', 'bar');
$userSession->setSession($session2);
$this->assertEquals($users['bar'], $userSession->getUser());
}
- public function testCreateSessionToken() {
+ public function testCreateSessionToken(): void {
$manager = $this->createMock(Manager::class);
$session = $this->createMock(ISession::class);
$user = $this->createMock(IUser::class);
@@ -846,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')
@@ -855,7 +980,7 @@ class SessionTest extends \Test\TestCase {
$this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password));
}
- public function testCreateRememberedSessionToken() {
+ public function testCreateRememberedSessionToken(): void {
$manager = $this->createMock(Manager::class);
$session = $this->createMock(ISession::class);
$user = $this->createMock(IUser::class);
@@ -887,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')
@@ -896,7 +1021,7 @@ class SessionTest extends \Test\TestCase {
$this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password, true));
}
- public function testCreateSessionTokenWithTokenPassword() {
+ public function testCreateSessionTokenWithTokenPassword(): void {
$manager = $this->getMockBuilder(Manager::class)
->disableOriginalConstructor()
->getMock();
@@ -945,7 +1070,7 @@ class SessionTest extends \Test\TestCase {
$this->assertTrue($userSession->createSessionToken($request, $uid, $loginName, $password));
}
- public function testCreateSessionTokenWithNonExistentUser() {
+ public function testCreateSessionTokenWithNonExistentUser(): void {
$manager = $this->getMockBuilder(Manager::class)
->disableOriginalConstructor()
->getMock();
@@ -965,7 +1090,7 @@ class SessionTest extends \Test\TestCase {
$this->assertFalse($userSession->createSessionToken($request, $uid, $loginName, $password));
}
- public function testCreateRememberMeToken() {
+ public function testCreateRememberMeToken(): void {
$user = $this->createMock(IUser::class);
$user
->expects($this->exactly(2))
@@ -988,7 +1113,7 @@ class SessionTest extends \Test\TestCase {
$this->userSession->createRememberMeToken($user);
}
- public function testTryBasicAuthLoginValid() {
+ public function testTryBasicAuthLoginValid(): void {
$request = $this->createMock(Request::class);
$request->method('__get')
->willReturn([
@@ -1004,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;
@@ -1029,7 +1154,7 @@ class SessionTest extends \Test\TestCase {
$this->logger,
$this->dispatcher
])
- ->setMethods([
+ ->onlyMethods([
'logClientIn',
'getUser',
])
@@ -1058,7 +1183,7 @@ class SessionTest extends \Test\TestCase {
$this->assertSame(1000, $lastPasswordConfirmSet);
}
- public function testTryBasicAuthLoginNoLogin() {
+ public function testTryBasicAuthLoginNoLogin(): void {
$request = $this->createMock(Request::class);
$request->method('__get')
->willReturn([]);
@@ -1081,7 +1206,7 @@ class SessionTest extends \Test\TestCase {
$this->logger,
$this->dispatcher
])
- ->setMethods([
+ ->onlyMethods([
'logClientIn',
])
->getMock();
@@ -1093,7 +1218,7 @@ class SessionTest extends \Test\TestCase {
$this->assertFalse($userSession->tryBasicAuthLogin($request, $this->throttler));
}
- public function testUpdateTokens() {
+ public function testUpdateTokens(): void {
$this->tokenProvider->expects($this->once())
->method('updatePasswords')
->with('uid', 'pass');
@@ -1101,7 +1226,7 @@ class SessionTest extends \Test\TestCase {
$this->userSession->updateTokens('uid', 'pass');
}
- public function testLogClientInThrottlerUsername() {
+ public function testLogClientInThrottlerUsername(): void {
$manager = $this->createMock(Manager::class);
$session = $this->createMock(ISession::class);
$request = $this->createMock(IRequest::class);
@@ -1109,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())
@@ -1147,7 +1272,7 @@ class SessionTest extends \Test\TestCase {
$this->assertFalse($userSession->logClientIn('john', 'I-AM-AN-PASSWORD', $request, $this->throttler));
}
- public function testLogClientInThrottlerEmail() {
+ public function testLogClientInThrottlerEmail(): void {
$manager = $this->createMock(Manager::class);
$session = $this->createMock(ISession::class);
$request = $this->createMock(IRequest::class);
@@ -1155,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 806cb094066..05056c92193 100644
--- a/tests/lib/User/UserTest.php
+++ b/tests/lib/User/UserTest.php
@@ -1,10 +1,9 @@
<?php
/**
- * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
- * This file is licensed under the Affero General Public License version 3 or
- * later.
- * See the COPYING-README file.
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\User;
@@ -12,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;
@@ -42,7 +43,7 @@ class UserTest extends TestCase {
$this->dispatcher = Server::get(IEventDispatcher::class);
}
- public function testDisplayName() {
+ public function testDisplayName(): void {
/**
* @var \OC\User\Backend | MockObject $backend
*/
@@ -64,7 +65,7 @@ class UserTest extends TestCase {
/**
* if the display name contain whitespaces only, we expect the uid as result
*/
- public function testDisplayNameEmpty() {
+ public function testDisplayNameEmpty(): void {
/**
* @var \OC\User\Backend | MockObject $backend
*/
@@ -83,7 +84,7 @@ class UserTest extends TestCase {
$this->assertEquals('foo', $user->getDisplayName());
}
- public function testDisplayNameNotSupported() {
+ public function testDisplayNameNotSupported(): void {
/**
* @var \OC\User\Backend | MockObject $backend
*/
@@ -100,7 +101,7 @@ class UserTest extends TestCase {
$this->assertEquals('foo', $user->getDisplayName());
}
- public function testSetPassword() {
+ public function testSetPassword(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -123,7 +124,7 @@ class UserTest extends TestCase {
$this->assertTrue($user->setPassword('bar', ''));
}
- public function testSetPasswordNotSupported() {
+ public function testSetPasswordNotSupported(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -139,7 +140,7 @@ class UserTest extends TestCase {
$this->assertFalse($user->setPassword('bar', ''));
}
- public function testChangeAvatarSupportedYes() {
+ public function testChangeAvatarSupportedYes(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -163,7 +164,7 @@ class UserTest extends TestCase {
$this->assertTrue($user->canChangeAvatar());
}
- public function testChangeAvatarSupportedNo() {
+ public function testChangeAvatarSupportedNo(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -187,7 +188,7 @@ class UserTest extends TestCase {
$this->assertFalse($user->canChangeAvatar());
}
- public function testChangeAvatarNotSupported() {
+ public function testChangeAvatarNotSupported(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -203,7 +204,7 @@ class UserTest extends TestCase {
$this->assertTrue($user->canChangeAvatar());
}
- public function testDelete() {
+ public function testDelete(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -216,14 +217,14 @@ class UserTest extends TestCase {
$this->assertTrue($user->delete());
}
- public function testDeleteWithDifferentHome() {
+ 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');
if ($homeProvider->getHomeMountForUser($user, $this->createMock(IStorageFactory::class)) !== null) {
- $this->markTestSkipped("Skipping test for non local home storage");
+ $this->markTestSkipped('Skipping test for non local home storage');
}
/**
@@ -257,7 +258,7 @@ class UserTest extends TestCase {
$this->assertTrue($user->delete());
}
- public function testGetHome() {
+ public function testGetHome(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -281,14 +282,14 @@ class UserTest extends TestCase {
$this->assertEquals('/home/foo', $user->getHome());
}
- public function testGetBackendClassName() {
+ 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());
}
- public function testGetHomeNotSupported() {
+ public function testGetHomeNotSupported(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -315,7 +316,7 @@ class UserTest extends TestCase {
$this->assertEquals('arbitrary/path/foo', $user->getHome());
}
- public function testCanChangePassword() {
+ public function testCanChangePassword(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -335,7 +336,7 @@ class UserTest extends TestCase {
$this->assertTrue($user->canChangePassword());
}
- public function testCanChangePasswordNotSupported() {
+ public function testCanChangePasswordNotSupported(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -349,7 +350,7 @@ class UserTest extends TestCase {
$this->assertFalse($user->canChangePassword());
}
- public function testCanChangeDisplayName() {
+ public function testCanChangeDisplayName(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -374,7 +375,7 @@ class UserTest extends TestCase {
$this->assertTrue($user->canChangeDisplayName());
}
- public function testCanChangeDisplayNameNotSupported() {
+ public function testCanChangeDisplayNameNotSupported(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -388,11 +389,11 @@ class UserTest extends TestCase {
$this->assertFalse($user->canChangeDisplayName());
}
- public function testSetDisplayNameSupported() {
+ public function testSetDisplayNameSupported(): void {
/**
* @var Backend | MockObject $backend
*/
- $backend = $this->createMock(\OC\User\Database::class);
+ $backend = $this->createMock(Database::class);
$backend->expects($this->any())
->method('implementsActions')
@@ -417,11 +418,11 @@ class UserTest extends TestCase {
/**
* don't allow display names containing whitespaces only
*/
- public function testSetDisplayNameEmpty() {
+ public function testSetDisplayNameEmpty(): void {
/**
* @var Backend | MockObject $backend
*/
- $backend = $this->createMock(\OC\User\Database::class);
+ $backend = $this->createMock(Database::class);
$backend->expects($this->any())
->method('implementsActions')
@@ -438,11 +439,11 @@ class UserTest extends TestCase {
$this->assertEquals('foo', $user->getDisplayName());
}
- public function testSetDisplayNameNotSupported() {
+ public function testSetDisplayNameNotSupported(): void {
/**
* @var Backend | MockObject $backend
*/
- $backend = $this->createMock(\OC\User\Database::class);
+ $backend = $this->createMock(Database::class);
$backend->expects($this->any())
->method('implementsActions')
@@ -456,7 +457,7 @@ class UserTest extends TestCase {
$this->assertEquals('foo', $user->getDisplayName());
}
- public function testSetPasswordHooks() {
+ public function testSetPasswordHooks(): void {
$hooksCalled = 0;
$test = $this;
@@ -471,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);
@@ -497,7 +498,7 @@ class UserTest extends TestCase {
$this->assertEquals(2, $hooksCalled);
}
- public function dataDeleteHooks() {
+ public static function dataDeleteHooks(): array {
return [
[true, 2],
[false, 1],
@@ -505,28 +506,39 @@ class UserTest extends TestCase {
}
/**
- * @dataProvider dataDeleteHooks
* @param bool $result
* @param int $expectedHooks
*/
- public function testDeleteHooks($result, $expectedHooks) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('dataDeleteHooks')]
+ public function testDeleteHooks($result, $expectedHooks): void {
$hooksCalled = 0;
$test = $this;
/**
- * @var Backend | MockObject $backend
+ * @var UserInterface&MockObject $backend
*/
$backend = $this->createMock(\Test\Util\User\Dummy::class);
$backend->expects($this->once())
->method('deleteUser')
->willReturn($result);
+
+ $config = $this->createMock(IConfig::class);
+ $config->method('getSystemValue')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueString')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueBool')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueInt')
+ ->willReturnArgument(1);
+
$emitter = new PublicEmitter();
- $user = new User('foo', $backend, $this->dispatcher, $emitter);
+ $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
/**
* @param User $user
*/
- $hook = function ($user) use ($test, &$hooksCalled) {
+ $hook = function ($user) use ($test, &$hooksCalled): void {
$hooksCalled++;
$test->assertEquals('foo', $user->getUID());
};
@@ -534,21 +546,11 @@ class UserTest extends TestCase {
$emitter->listen('\OC\User', 'preDelete', $hook);
$emitter->listen('\OC\User', 'postDelete', $hook);
- $config = $this->createMock(IConfig::class);
$commentsManager = $this->createMock(ICommentsManager::class);
$notificationManager = $this->createMock(INotificationManager::class);
- $config->method('getSystemValue')
- ->willReturnArgument(1);
- $config->method('getSystemValueString')
- ->willReturnArgument(1);
- $config->method('getSystemValueBool')
- ->willReturnArgument(1);
- $config->method('getSystemValueInt')
- ->willReturnArgument(1);
-
if ($result) {
- $config->expects($this->once())
+ $config->expects($this->atLeastOnce())
->method('deleteAllUserValues')
->with('foo');
@@ -585,29 +587,79 @@ class UserTest extends TestCase {
->method('markProcessed');
}
- $this->overwriteService(\OCP\Notification\IManager::class, $notificationManager);
- $this->overwriteService(\OCP\Comments\ICommentsManager::class, $commentsManager);
- $this->overwriteService(AllConfig::class, $config);
+ $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);
}
- public function dataGetCloudId(): array {
+ public function testDeleteRecoverState() {
+ $backend = $this->createMock(\Test\Util\User\Dummy::class);
+ $backend->expects($this->once())
+ ->method('deleteUser')
+ ->willReturn(true);
+
+ $config = $this->createMock(IConfig::class);
+ $config->method('getSystemValue')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueString')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueBool')
+ ->willReturnArgument(1);
+ $config->method('getSystemValueInt')
+ ->willReturnArgument(1);
+
+ $userConfig = [];
+ $config->expects(self::atLeast(2))
+ ->method('setUserValue')
+ ->willReturnCallback(function (): void {
+ $userConfig[] = func_get_args();
+ });
+
+ $commentsManager = $this->createMock(ICommentsManager::class);
+ $commentsManager->expects($this->once())
+ ->method('deleteReferencesOfActor')
+ ->willThrowException(new \Error('Test exception'));
+
+ $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');
+
+ $user->delete();
+
+ $this->assertEqualsCanonicalizing(
+ [
+ ['foo', 'core', 'deleted', 'true', null],
+ ['foo', 'core', 'deleted.backup-home', '/home/path', null],
+ ],
+ $userConfig,
+ );
+
+ $this->restoreService(ICommentsManager::class);
+ }
+
+ 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);
@@ -619,7 +671,7 @@ class UserTest extends TestCase {
$this->assertEquals($cloudId, $user->getCloudId());
}
- public function testSetEMailAddressEmpty() {
+ public function testSetEMailAddressEmpty(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -633,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);
@@ -655,7 +707,7 @@ class UserTest extends TestCase {
$user->setEMailAddress('');
}
- public function testSetEMailAddress() {
+ public function testSetEMailAddress(): void {
/**
* @var UserInterface | MockObject $backend
*/
@@ -669,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);
@@ -692,7 +744,7 @@ class UserTest extends TestCase {
$user->setEMailAddress('foo@bar.com');
}
- public function testSetEMailAddressNoChange() {
+ public function testSetEMailAddressNoChange(): void {
/**
* @var UserInterface | MockObject $backend
*/
@@ -718,7 +770,7 @@ class UserTest extends TestCase {
$user->setEMailAddress('foo@bar.com');
}
- public function testSetQuota() {
+ public function testSetQuota(): void {
/**
* @var UserInterface | MockObject $backend
*/
@@ -732,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);
@@ -755,7 +807,7 @@ class UserTest extends TestCase {
$user->setQuota('23 TB');
}
- public function testGetDefaultUnlimitedQuota() {
+ public function testGetDefaultUnlimitedQuota(): void {
/**
* @var UserInterface | MockObject $backend
*/
@@ -778,15 +830,15 @@ 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() {
+ public function testGetDefaultUnlimitedQuotaForbidden(): void {
/**
* @var UserInterface | MockObject $backend
*/
@@ -812,15 +864,15 @@ 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() {
+ public function testSetQuotaAddressNoChange(): void {
/**
* @var UserInterface | MockObject $backend
*/
@@ -842,7 +894,7 @@ class UserTest extends TestCase {
$user->setQuota('23 TB');
}
- public function testGetLastLogin() {
+ public function testGetLastLogin(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -862,7 +914,7 @@ class UserTest extends TestCase {
$this->assertSame(42, $user->getLastLogin());
}
- public function testSetEnabled() {
+ public function testSetEnabled(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -877,12 +929,18 @@ 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);
}
- public function testSetDisabled() {
+ public function testSetDisabled(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -906,7 +964,7 @@ class UserTest extends TestCase {
null,
$config,
])
- ->setMethods(['isEnabled', 'triggerChange'])
+ ->onlyMethods(['isEnabled', 'triggerChange'])
->getMock();
$user->expects($this->once())
@@ -922,7 +980,7 @@ class UserTest extends TestCase {
$user->setEnabled(false);
}
- public function testSetDisabledAlreadyDisabled() {
+ public function testSetDisabledAlreadyDisabled(): void {
/**
* @var Backend | MockObject $backend
*/
@@ -940,7 +998,7 @@ class UserTest extends TestCase {
null,
$config,
])
- ->setMethods(['isEnabled', 'triggerChange'])
+ ->onlyMethods(['isEnabled', 'triggerChange'])
->getMock();
$user->expects($this->once())
@@ -952,7 +1010,7 @@ class UserTest extends TestCase {
$user->setEnabled(false);
}
- public function testGetEMailAddress() {
+ public function testGetEMailAddress(): void {
/**
* @var Backend | MockObject $backend
*/