From 08d33a9f571712c6ee0cfa40bcc23097e67fbea2 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sun, 23 Feb 2025 19:08:10 +0100 Subject: fix: validate account properties as a repair step Replace `ValidatePhoneNumber` from Nextcloud 21 with a new repair step, `ValidateAccountProperties` which validates and sanitizes all account properties. Signed-off-by: Ferdinand Thiessen --- .../Repair/NC29/ValidateAccountPropertiesTest.php | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/lib/Repair/NC29/ValidateAccountPropertiesTest.php (limited to 'tests') diff --git a/tests/lib/Repair/NC29/ValidateAccountPropertiesTest.php b/tests/lib/Repair/NC29/ValidateAccountPropertiesTest.php new file mode 100644 index 00000000000..cc43a63ca19 --- /dev/null +++ b/tests/lib/Repair/NC29/ValidateAccountPropertiesTest.php @@ -0,0 +1,89 @@ +userManager = $this->createMock(IUserManager::class); + $this->accountManager = $this->createMock(IAccountManager::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->repairStep = new ValidateAccountProperties($this->userManager, $this->accountManager, $this->logger); + } + + public function testGetName(): void { + self::assertStringContainsString('Validate account properties', $this->repairStep->getName()); + } + + public function testRun(): void { + $users = [ + $this->createMock(IUser::class), + $this->createMock(IUser::class), + ]; + $this->userManager + ->expects(self::once()) + ->method('callForSeenUsers') + ->willReturnCallback(fn ($fn) => array_map($fn, $users)); + + $property = $this->createMock(IAccountProperty::class); + $property->expects(self::once())->method('getName')->willReturn(IAccountManager::PROPERTY_PHONE); + $property->expects(self::once())->method('getScope')->willReturn(IAccountManager::SCOPE_LOCAL); + + $account1 = $this->createMock(IAccount::class); + $account1->expects(self::once()) + ->method('getProperty') + ->with(IAccountManager::PROPERTY_PHONE) + ->willReturn($property); + $account1->expects(self::once()) + ->method('setProperty') + ->with(IAccountManager::PROPERTY_PHONE, '', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED); + $account2 = $this->createMock(IAccount::class); + $account2->expects(self::never()) + ->method('getProperty'); + $this->accountManager + ->expects(self::exactly(2)) + ->method('getAccount') + ->willReturnMap([ + [$users[0], $account1], + [$users[1], $account2], + ]); + $valid = false; + $this->accountManager->expects(self::exactly(3)) + ->method('updateAccount') + ->willReturnCallback(function (IAccount $account) use (&$account1, &$valid) { + if (!$valid && $account === $account1) { + $valid = true; + throw new InvalidArgumentException(IAccountManager::PROPERTY_PHONE); + } + }); + + $output = $this->createMock(IOutput::class); + $output->expects(self::once())->method('info')->with('Cleaned 1 invalid account property entries'); + + $this->repairStep->run($output); + } +} -- cgit v1.2.3