|  | 
|  | 1 | +<?php | 
|  | 2 | + | 
|  | 3 | +declare(strict_types=1); | 
|  | 4 | + | 
|  | 5 | +/** | 
|  | 6 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors | 
|  | 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later | 
|  | 8 | + */ | 
|  | 9 | +namespace OC\Repair\NC29; | 
|  | 10 | + | 
|  | 11 | +use InvalidArgumentException; | 
|  | 12 | +use OCP\Accounts\IAccount; | 
|  | 13 | +use OCP\Accounts\IAccountManager; | 
|  | 14 | +use OCP\Accounts\IAccountProperty; | 
|  | 15 | +use OCP\AppFramework\Utility\ITimeFactory; | 
|  | 16 | +use OCP\IUser; | 
|  | 17 | +use OCP\IUserManager; | 
|  | 18 | +use PHPUnit\Framework\MockObject\MockObject; | 
|  | 19 | +use Psr\Log\LoggerInterface; | 
|  | 20 | +use Test\TestCase; | 
|  | 21 | + | 
|  | 22 | +class SanitizeAccountPropertiesJobTest extends TestCase { | 
|  | 23 | + | 
|  | 24 | +	private IUserManager&MockObject $userManager; | 
|  | 25 | +	private IAccountManager&MockObject $accountManager; | 
|  | 26 | +	private LoggerInterface&MockObject $logger; | 
|  | 27 | +	 | 
|  | 28 | +	private SanitizeAccountPropertiesJob $job; | 
|  | 29 | + | 
|  | 30 | +	protected function setUp(): void { | 
|  | 31 | +		$this->userManager = $this->createMock(IUserManager::class); | 
|  | 32 | +		$this->accountManager = $this->createMock(IAccountManager::class); | 
|  | 33 | +		$this->logger = $this->createMock(LoggerInterface::class); | 
|  | 34 | + | 
|  | 35 | +		$this->job = new SanitizeAccountPropertiesJob( | 
|  | 36 | +			$this->createMock(ITimeFactory::class), | 
|  | 37 | +			$this->userManager, | 
|  | 38 | +			$this->accountManager, | 
|  | 39 | +			$this->logger, | 
|  | 40 | +		); | 
|  | 41 | +	} | 
|  | 42 | + | 
|  | 43 | +	public function testParallel() { | 
|  | 44 | +		self::assertFalse($this->job->getAllowParallelRuns()); | 
|  | 45 | +	} | 
|  | 46 | + | 
|  | 47 | +	public function testRun(): void { | 
|  | 48 | +		$users = [ | 
|  | 49 | +			$this->createMock(IUser::class), | 
|  | 50 | +			$this->createMock(IUser::class), | 
|  | 51 | +			$this->createMock(IUser::class), | 
|  | 52 | +		]; | 
|  | 53 | +		$this->userManager | 
|  | 54 | +			->expects(self::once()) | 
|  | 55 | +			->method('callForSeenUsers') | 
|  | 56 | +			->willReturnCallback(fn ($fn) => array_map($fn, $users)); | 
|  | 57 | + | 
|  | 58 | +		$property = $this->createMock(IAccountProperty::class); | 
|  | 59 | +		$property->expects(self::once())->method('getName')->willReturn(IAccountManager::PROPERTY_PHONE); | 
|  | 60 | +		$property->expects(self::once())->method('getScope')->willReturn(IAccountManager::SCOPE_LOCAL); | 
|  | 61 | + | 
|  | 62 | +		$account1 = $this->createMock(IAccount::class); | 
|  | 63 | +		$account1->expects(self::once()) | 
|  | 64 | +			->method('getProperty') | 
|  | 65 | +			->with(IAccountManager::PROPERTY_PHONE) | 
|  | 66 | +			->willReturn($property); | 
|  | 67 | +		$account1->expects(self::once()) | 
|  | 68 | +			->method('setProperty') | 
|  | 69 | +			->with(IAccountManager::PROPERTY_PHONE, '', IAccountManager::SCOPE_LOCAL, IAccountManager::NOT_VERIFIED); | 
|  | 70 | +		$account1->expects(self::once()) | 
|  | 71 | +			->method('jsonSerialize') | 
|  | 72 | +			->willReturn([ | 
|  | 73 | +				IAccountManager::PROPERTY_DISPLAYNAME => [], | 
|  | 74 | +				IAccountManager::PROPERTY_PHONE => [], | 
|  | 75 | +			]); | 
|  | 76 | + | 
|  | 77 | +		$account2 = $this->createMock(IAccount::class); | 
|  | 78 | +		$account2->expects(self::never()) | 
|  | 79 | +			->method('getProperty'); | 
|  | 80 | +		$account2->expects(self::once()) | 
|  | 81 | +			->method('jsonSerialize') | 
|  | 82 | +			->willReturn([ | 
|  | 83 | +				IAccountManager::PROPERTY_DISPLAYNAME => [], | 
|  | 84 | +				IAccountManager::PROPERTY_PHONE => [], | 
|  | 85 | +			]); | 
|  | 86 | + | 
|  | 87 | +		$account3 = $this->createMock(IAccount::class); | 
|  | 88 | +		$account3->expects(self::never()) | 
|  | 89 | +			->method('getProperty'); | 
|  | 90 | +		$account3->expects(self::once()) | 
|  | 91 | +			->method('jsonSerialize') | 
|  | 92 | +			->willReturn([ | 
|  | 93 | +				IAccountManager::PROPERTY_DISPLAYNAME => [], | 
|  | 94 | +			]); | 
|  | 95 | + | 
|  | 96 | +		$this->accountManager | 
|  | 97 | +			->expects(self::exactly(3)) | 
|  | 98 | +			->method('getAccount') | 
|  | 99 | +			->willReturnMap([ | 
|  | 100 | +				[$users[0], $account1], | 
|  | 101 | +				[$users[1], $account2], | 
|  | 102 | +				[$users[2], $account3], | 
|  | 103 | +			]); | 
|  | 104 | +		$valid = false; | 
|  | 105 | +		$this->accountManager->expects(self::exactly(3)) | 
|  | 106 | +			->method('updateAccount') | 
|  | 107 | +			->willReturnCallback(function (IAccount $account) use (&$account1, &$valid) { | 
|  | 108 | +				if (!$valid && $account === $account1) { | 
|  | 109 | +					$valid = true; | 
|  | 110 | +					throw new InvalidArgumentException(IAccountManager::PROPERTY_PHONE); | 
|  | 111 | +				} | 
|  | 112 | +			}); | 
|  | 113 | + | 
|  | 114 | +		self::invokePrivate($this->job, 'run', [null]); | 
|  | 115 | +	} | 
|  | 116 | +} | 
0 commit comments