aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Repair/NC29/SanitizeAccountPropertiesJobTest.php
blob: 2a4f6e9ecf10bc038a4cb02b616c54a5924c74d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?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\AppFramework\Utility\ITimeFactory;
use OCP\IUser;
use OCP\IUserManager;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;

class SanitizeAccountPropertiesJobTest extends TestCase {

	private IUserManager&MockObject $userManager;
	private IAccountManager&MockObject $accountManager;
	private LoggerInterface&MockObject $logger;

	private SanitizeAccountPropertiesJob $job;

	protected function setUp(): void {
		$this->userManager = $this->createMock(IUserManager::class);
		$this->accountManager = $this->createMock(IAccountManager::class);
		$this->logger = $this->createMock(LoggerInterface::class);

		$this->job = new SanitizeAccountPropertiesJob(
			$this->createMock(ITimeFactory::class),
			$this->userManager,
			$this->accountManager,
			$this->logger,
		);
	}

	public function testParallel() {
		self::assertFalse($this->job->getAllowParallelRuns());
	}

	public function testRun(): void {
		$users = [
			$this->createMock(IUser::class),
			$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);
		$account1->expects(self::once())
			->method('jsonSerialize')
			->willReturn([
				IAccountManager::PROPERTY_DISPLAYNAME => [],
				IAccountManager::PROPERTY_PHONE => [],
			]);

		$account2 = $this->createMock(IAccount::class);
		$account2->expects(self::never())
			->method('getProperty');
		$account2->expects(self::once())
			->method('jsonSerialize')
			->willReturn([
				IAccountManager::PROPERTY_DISPLAYNAME => [],
				IAccountManager::PROPERTY_PHONE => [],
			]);

		$account3 = $this->createMock(IAccount::class);
		$account3->expects(self::never())
			->method('getProperty');
		$account3->expects(self::once())
			->method('jsonSerialize')
			->willReturn([
				IAccountManager::PROPERTY_DISPLAYNAME => [],
			]);

		$this->accountManager
			->expects(self::exactly(3))
			->method('getAccount')
			->willReturnMap([
				[$users[0], $account1],
				[$users[1], $account2],
				[$users[2], $account3],
			]);
		$valid = false;
		$this->accountManager->expects(self::exactly(3))
			->method('updateAccount')
			->willReturnCallback(function (IAccount $account) use (&$account1, &$valid): void {
				if (!$valid && $account === $account1) {
					$valid = true;
					throw new InvalidArgumentException(IAccountManager::PROPERTY_PHONE);
				}
			});

		self::invokePrivate($this->job, 'run', [null]);
	}
}