aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorFerdinand Thiessen <opensource@fthiessen.de>2025-02-23 19:08:10 +0100
committerFerdinand Thiessen <opensource@fthiessen.de>2025-02-24 15:16:27 +0100
commit08d33a9f571712c6ee0cfa40bcc23097e67fbea2 (patch)
treef8e39d0ec9c3c6189a83e142424281da203069eb /tests
parenta4ffc554d474e5daddbd533ade3f9dc94cc3c9b7 (diff)
downloadnextcloud-server-08d33a9f571712c6ee0cfa40bcc23097e67fbea2.tar.gz
nextcloud-server-08d33a9f571712c6ee0cfa40bcc23097e67fbea2.zip
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 <opensource@fthiessen.de>
Diffstat (limited to 'tests')
-rw-r--r--tests/lib/Repair/NC29/ValidateAccountPropertiesTest.php89
1 files changed, 89 insertions, 0 deletions
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 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OC\Repair\NC29;
+
+use InvalidArgumentException;
+use OCP\Accounts\IAccount;
+use OCP\Accounts\IAccountManager;
+use OCP\Accounts\IAccountProperty;
+use OCP\IUser;
+use OCP\IUserManager;
+use OCP\Migration\IOutput;
+use PHPUnit\Framework\MockObject\MockObject;
+use Psr\Log\LoggerInterface;
+use Test\TestCase;
+
+class ValidateAccountPropertiesTest extends TestCase {
+
+ private IUserManager&MockObject $userManager;
+ private IAccountManager&MockObject $accountManager;
+ private LoggerInterface&MockObject $logger;
+
+ private ValidateAccountProperties $repairStep;
+
+ protected function setUp(): void {
+ $this->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);
+ }
+}