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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
<?php
/**
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Accounts;
use OC\Accounts\AccountManager;
use OC\Accounts\Hooks;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
/**
* Class HooksTest
*
* @package Test\Accounts
* @group DB
*/
class HooksTest extends TestCase {
/** @var LoggerInterface|MockObject */
private $logger;
/** @var AccountManager|MockObject */
private $accountManager;
/** @var Hooks */
private $hooks;
protected function setUp(): void {
parent::setUp();
$this->logger = $this->createMock(LoggerInterface::class);
$this->accountManager = $this->getMockBuilder(AccountManager::class)
->disableOriginalConstructor()->getMock();
$this->hooks = new Hooks($this->logger, $this->accountManager);
}
/**
* @dataProvider dataTestChangeUserHook
*
* @param $params
* @param $data
* @param $setEmail
* @param $setDisplayName
* @param $error
*/
public function testChangeUserHook($params, $data, $setEmail, $setDisplayName, $error): void {
if ($error) {
$this->accountManager->expects($this->never())->method('updateAccount');
} else {
$account = $this->createMock(IAccount::class);
$this->accountManager->expects($this->atLeastOnce())->method('getAccount')->willReturn($account);
if ($setEmail) {
$property = $this->createMock(IAccountProperty::class);
$property->expects($this->atLeastOnce())
->method('getValue')
->willReturn($data[IAccountManager::PROPERTY_EMAIL]['value']);
$property->expects($this->atLeastOnce())
->method('setValue')
->with($params['value']);
$account->expects($this->atLeastOnce())
->method('getProperty')
->with(IAccountManager::PROPERTY_EMAIL)
->willReturn($property);
$this->accountManager->expects($this->once())
->method('updateAccount')
->with($account);
} elseif ($setDisplayName) {
$property = $this->createMock(IAccountProperty::class);
$property->expects($this->atLeastOnce())
->method('getValue')
->willReturn($data[IAccountManager::PROPERTY_DISPLAYNAME]['value']);
$property->expects($this->atLeastOnce())
->method('setValue')
->with($params['value']);
$account->expects($this->atLeastOnce())
->method('getProperty')
->with(IAccountManager::PROPERTY_DISPLAYNAME)
->willReturn($property);
$this->accountManager->expects($this->once())
->method('updateAccount')
->with($account);
} else {
$this->accountManager->expects($this->never())->method('updateAccount');
}
}
$this->hooks->changeUserHook($params['user'], $params['feature'], $params['value']);
}
public function dataTestChangeUserHook() {
$user = $this->createMock(IUser::class);
return [
[
['user' => $user, 'feature' => '', 'value' => ''],
[
IAccountManager::PROPERTY_EMAIL => ['value' => ''],
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => '']
],
false, false, true
],
[
['user' => $user, 'feature' => 'foo', 'value' => 'bar'],
[
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
],
false, false, false
],
[
['user' => $user, 'feature' => 'eMailAddress', 'value' => 'newMail@example.com'],
[
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
],
true, false, false
],
[
['user' => $user, 'feature' => 'displayName', 'value' => 'newDisplayName'],
[
IAccountManager::PROPERTY_EMAIL => ['value' => 'oldMail@example.com'],
IAccountManager::PROPERTY_DISPLAYNAME => ['value' => 'oldDisplayName']
],
false, true, false
],
];
}
}
|