diff options
Diffstat (limited to 'tests/lib/User')
-rw-r--r-- | tests/lib/User/AvailabilityCoordinatorTest.php | 27 | ||||
-rw-r--r-- | tests/lib/User/AvatarUserDummy.php | 1 | ||||
-rw-r--r-- | tests/lib/User/Backend.php | 7 | ||||
-rw-r--r-- | tests/lib/User/DatabaseTest.php | 33 | ||||
-rw-r--r-- | tests/lib/User/Dummy.php | 1 | ||||
-rw-r--r-- | tests/lib/User/ManagerTest.php | 244 | ||||
-rw-r--r-- | tests/lib/User/SessionTest.php | 214 | ||||
-rw-r--r-- | tests/lib/User/UserTest.php | 217 |
8 files changed, 489 insertions, 255 deletions
diff --git a/tests/lib/User/AvailabilityCoordinatorTest.php b/tests/lib/User/AvailabilityCoordinatorTest.php index 1b880575448..09c1528912b 100644 --- a/tests/lib/User/AvailabilityCoordinatorTest.php +++ b/tests/lib/User/AvailabilityCoordinatorTest.php @@ -73,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); @@ -86,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', @@ -98,6 +107,8 @@ class AvailabilityCoordinatorTest extends TestCase { 1696802340, 'Vacation', 'On vacation', + 'batman', + 'Bruce Wayne', ); $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user); self::assertEquals($expected, $actual); @@ -111,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') @@ -118,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)) @@ -131,6 +144,8 @@ class AvailabilityCoordinatorTest extends TestCase { 1696809540, 'Vacation', 'On vacation', + 'batman', + 'Bruce Wayne' ); $actual = $this->availabilityCoordinator->getCurrentOutOfOfficeData($user); self::assertEquals($expected, $actual); @@ -170,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); @@ -185,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', @@ -194,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 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 7efba2eaeb6..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. @@ -32,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()); @@ -56,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(); @@ -84,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 16b223aaaba..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 { @@ -49,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()); @@ -67,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(); @@ -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()); @@ -88,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)); @@ -105,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(); @@ -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 c516b1de945..d5872787d0a 100644 --- a/tests/lib/User/ManagerTest.php +++ b/tests/lib/User/ManagerTest.php @@ -9,13 +9,18 @@ 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; /** @@ -34,6 +39,8 @@ class ManagerTest extends TestCase { private $cacheFactory; /** @var ICache */ private $cache; + /** @var LoggerInterface */ + private $logger; protected function setUp(): void { parent::setUp(); @@ -42,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); @@ -58,7 +66,7 @@ class ManagerTest extends TestCase { } - public function testUserExistsSingleBackendExists() { + public function testUserExistsSingleBackendExists(): void { /** * @var \Test\Util\User\Dummy | \PHPUnit\Framework\MockObject\MockObject $backend */ @@ -68,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 */ @@ -84,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 */ @@ -115,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 */ @@ -139,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 */ @@ -159,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 */ @@ -185,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 */ @@ -203,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 */ @@ -219,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 */ @@ -237,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 */ @@ -255,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'); @@ -266,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 */ @@ -289,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); @@ -300,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 "_.@-\'"'], @@ -328,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()) @@ -343,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 */ @@ -370,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'); @@ -378,7 +413,7 @@ class ManagerTest extends TestCase { } - public function testCreateUserSingleBackendExists() { + public function testCreateUserSingleBackendExists(): void { $this->expectException(\Exception::class); /** @@ -397,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 */ @@ -418,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'); @@ -445,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); /** @@ -485,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 */ @@ -511,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(); @@ -529,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 */ @@ -540,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') @@ -553,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); @@ -574,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(); @@ -599,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(); @@ -625,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); @@ -659,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(); @@ -672,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); @@ -682,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(); @@ -695,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 236da2a1dcc..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,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(); @@ -131,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'); @@ -150,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) { @@ -176,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(); @@ -203,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(); @@ -214,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; }) ); @@ -225,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()) @@ -236,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(); @@ -269,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); @@ -294,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'); @@ -313,16 +316,17 @@ class SessionTest extends \Test\TestCase { } public function testPasswordlessLoginNoLastCheckUpdate(): void { - $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); + $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 = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); @@ -350,16 +354,17 @@ class SessionTest extends \Test\TestCase { } public function testLoginLastCheckUpdate(): void { - $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); + $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 = new Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger, $this->dispatcher); @@ -386,8 +391,8 @@ class SessionTest extends \Test\TestCase { $userSession->login('foo', 'app-password'); } - public function testLoginNonExisting() { - $session = $this->getMockBuilder(Memory::class)->setConstructorArgs([''])->getMock(); + 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); @@ -398,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') @@ -408,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); @@ -418,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) @@ -446,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); @@ -454,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) @@ -472,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); @@ -480,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()) @@ -512,7 +517,7 @@ class SessionTest extends \Test\TestCase { } - public function testLogClientInNoTokenPasswordNo2fa() { + public function testLogClientInNoTokenPasswordNo2fa(): void { $this->expectException(PasswordLoginForbiddenException::class); $manager = $this->createMock(Manager::class); @@ -522,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) @@ -606,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(); @@ -679,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; } @@ -695,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(); @@ -741,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') @@ -759,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(); @@ -811,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'; @@ -856,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)) @@ -872,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(); @@ -885,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); @@ -923,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') @@ -932,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); @@ -964,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') @@ -973,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(); @@ -1022,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(); @@ -1042,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)) @@ -1065,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([ @@ -1081,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; @@ -1106,7 +1154,7 @@ class SessionTest extends \Test\TestCase { $this->logger, $this->dispatcher ]) - ->setMethods([ + ->onlyMethods([ 'logClientIn', 'getUser', ]) @@ -1135,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([]); @@ -1158,7 +1206,7 @@ class SessionTest extends \Test\TestCase { $this->logger, $this->dispatcher ]) - ->setMethods([ + ->onlyMethods([ 'logClientIn', ]) ->getMock(); @@ -1170,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'); @@ -1178,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); @@ -1186,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()) @@ -1224,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); @@ -1232,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 8e62c06e1ba..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; @@ -41,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 */ @@ -63,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 */ @@ -82,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 */ @@ -99,7 +101,7 @@ class UserTest extends TestCase { $this->assertEquals('foo', $user->getDisplayName()); } - public function testSetPassword() { + public function testSetPassword(): void { /** * @var Backend | MockObject $backend */ @@ -122,7 +124,7 @@ class UserTest extends TestCase { $this->assertTrue($user->setPassword('bar', '')); } - public function testSetPasswordNotSupported() { + public function testSetPasswordNotSupported(): void { /** * @var Backend | MockObject $backend */ @@ -138,7 +140,7 @@ class UserTest extends TestCase { $this->assertFalse($user->setPassword('bar', '')); } - public function testChangeAvatarSupportedYes() { + public function testChangeAvatarSupportedYes(): void { /** * @var Backend | MockObject $backend */ @@ -162,7 +164,7 @@ class UserTest extends TestCase { $this->assertTrue($user->canChangeAvatar()); } - public function testChangeAvatarSupportedNo() { + public function testChangeAvatarSupportedNo(): void { /** * @var Backend | MockObject $backend */ @@ -186,7 +188,7 @@ class UserTest extends TestCase { $this->assertFalse($user->canChangeAvatar()); } - public function testChangeAvatarNotSupported() { + public function testChangeAvatarNotSupported(): void { /** * @var Backend | MockObject $backend */ @@ -202,7 +204,7 @@ class UserTest extends TestCase { $this->assertTrue($user->canChangeAvatar()); } - public function testDelete() { + public function testDelete(): void { /** * @var Backend | MockObject $backend */ @@ -215,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'); } /** @@ -256,7 +258,7 @@ class UserTest extends TestCase { $this->assertTrue($user->delete()); } - public function testGetHome() { + public function testGetHome(): void { /** * @var Backend | MockObject $backend */ @@ -280,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 */ @@ -314,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 */ @@ -334,7 +336,7 @@ class UserTest extends TestCase { $this->assertTrue($user->canChangePassword()); } - public function testCanChangePasswordNotSupported() { + public function testCanChangePasswordNotSupported(): void { /** * @var Backend | MockObject $backend */ @@ -348,7 +350,7 @@ class UserTest extends TestCase { $this->assertFalse($user->canChangePassword()); } - public function testCanChangeDisplayName() { + public function testCanChangeDisplayName(): void { /** * @var Backend | MockObject $backend */ @@ -373,7 +375,7 @@ class UserTest extends TestCase { $this->assertTrue($user->canChangeDisplayName()); } - public function testCanChangeDisplayNameNotSupported() { + public function testCanChangeDisplayNameNotSupported(): void { /** * @var Backend | MockObject $backend */ @@ -387,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') @@ -416,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') @@ -437,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') @@ -455,7 +457,7 @@ class UserTest extends TestCase { $this->assertEquals('foo', $user->getDisplayName()); } - public function testSetPasswordHooks() { + public function testSetPasswordHooks(): void { $hooksCalled = 0; $test = $this; @@ -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,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()); }; @@ -533,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'); @@ -584,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); @@ -618,7 +671,7 @@ class UserTest extends TestCase { $this->assertEquals($cloudId, $user->getCloudId()); } - public function testSetEMailAddressEmpty() { + public function testSetEMailAddressEmpty(): void { /** * @var Backend | MockObject $backend */ @@ -632,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); @@ -654,7 +707,7 @@ class UserTest extends TestCase { $user->setEMailAddress(''); } - public function testSetEMailAddress() { + public function testSetEMailAddress(): void { /** * @var UserInterface | MockObject $backend */ @@ -668,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); @@ -691,7 +744,7 @@ class UserTest extends TestCase { $user->setEMailAddress('foo@bar.com'); } - public function testSetEMailAddressNoChange() { + public function testSetEMailAddressNoChange(): void { /** * @var UserInterface | MockObject $backend */ @@ -717,7 +770,7 @@ class UserTest extends TestCase { $user->setEMailAddress('foo@bar.com'); } - public function testSetQuota() { + public function testSetQuota(): void { /** * @var UserInterface | MockObject $backend */ @@ -731,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); @@ -754,7 +807,7 @@ class UserTest extends TestCase { $user->setQuota('23 TB'); } - public function testGetDefaultUnlimitedQuota() { + public function testGetDefaultUnlimitedQuota(): void { /** * @var UserInterface | MockObject $backend */ @@ -777,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 */ @@ -811,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 */ @@ -841,7 +894,7 @@ class UserTest extends TestCase { $user->setQuota('23 TB'); } - public function testGetLastLogin() { + public function testGetLastLogin(): void { /** * @var Backend | MockObject $backend */ @@ -861,7 +914,7 @@ class UserTest extends TestCase { $this->assertSame(42, $user->getLastLogin()); } - public function testSetEnabled() { + public function testSetEnabled(): void { /** * @var Backend | MockObject $backend */ @@ -876,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 */ @@ -905,7 +964,7 @@ class UserTest extends TestCase { null, $config, ]) - ->setMethods(['isEnabled', 'triggerChange']) + ->onlyMethods(['isEnabled', 'triggerChange']) ->getMock(); $user->expects($this->once()) @@ -921,7 +980,7 @@ class UserTest extends TestCase { $user->setEnabled(false); } - public function testSetDisabledAlreadyDisabled() { + public function testSetDisabledAlreadyDisabled(): void { /** * @var Backend | MockObject $backend */ @@ -939,7 +998,7 @@ class UserTest extends TestCase { null, $config, ]) - ->setMethods(['isEnabled', 'triggerChange']) + ->onlyMethods(['isEnabled', 'triggerChange']) ->getMock(); $user->expects($this->once()) @@ -951,7 +1010,7 @@ class UserTest extends TestCase { $user->setEnabled(false); } - public function testGetEMailAddress() { + public function testGetEMailAddress(): void { /** * @var Backend | MockObject $backend */ |