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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Remote;
use OC\Profile\Actions\FediverseAction;
use OCP\Accounts\IAccount;
use OCP\Accounts\IAccountManager;
use OCP\Accounts\IAccountProperty;
use OCP\Accounts\PropertyDoesNotExistException;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\L10N\IFactory;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class FediverseActionTest extends TestCase {
private FediverseAction $action;
private IAccountManager&MockObject $accountManager;
private IL10N&MockObject $l10n;
private IURLGenerator&MockObject $urlGenerator;
protected function setUp(): void {
parent::setUp();
$this->accountManager = $this->createMock(IAccountManager::class);
$this->l10n = $this->createMock(IL10N::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class);
$factory = $this->createMock(IFactory::class);
$factory->method('get')
->with('lib')
->willReturn($this->l10n);
$this->action = new FediverseAction(
$this->accountManager,
$factory,
$this->urlGenerator,
);
}
public function testGetActionId(): void {
self::assertEquals(
IAccountManager::PROPERTY_FEDIVERSE,
$this->action->getId(),
);
}
public function testGetAppId(): void {
self::assertEquals(
'core',
$this->action->getAppId(),
);
}
public function testGetDisplayId(): void {
$this->l10n->expects(self::once())
->method('t')
->with('Fediverse')
->willReturn('Translated fediverse');
self::assertEquals(
'Translated fediverse',
$this->action->getDisplayId(),
);
}
public function testGetPriority(): void {
self::assertEquals(
50,
$this->action->getPriority(),
);
}
public function testGetIcon(): void {
$this->urlGenerator->expects(self::once())
->method('getAbsoluteURL')
->with('the-image-path')
->willReturn('resolved-image-path');
$this->urlGenerator->expects(self::once())
->method('imagePath')
->with('core', 'actions/mastodon.svg')
->willReturn('the-image-path');
self::assertEquals(
'resolved-image-path',
$this->action->getIcon(),
);
}
public static function dataGetTitle(): array {
return [
['the-user@example.com'],
['@the-user@example.com'],
];
}
/** @dataProvider dataGetTitle */
public function testGetTitle(string $value): void {
$property = $this->createMock(IAccountProperty::class);
$property->method('getValue')
->willReturn($value);
$user = $this->createMock(IUser::class);
$account = $this->createMock(IAccount::class);
$account->method('getProperty')
->with(IAccountManager::PROPERTY_FEDIVERSE)
->willReturn($property);
$this->accountManager->method('getAccount')
->with($user)
->willReturn($account);
$this->l10n->expects(self::once())
->method('t')
->with(self::anything(), ['@the-user@example.com'])
->willReturn('Translated title');
// Preload and test
$this->action->preload($user);
self::assertEquals(
'Translated title',
$this->action->getTitle(),
);
}
public static function dataGetTarget(): array {
return [
['', null],
[null, null],
['user@example.com', 'https://example.com/@user'],
['@user@example.com', 'https://example.com/@user'],
['@user@social.example.com', 'https://social.example.com/@user'],
// invalid stuff
['@user', null],
['@example.com', null],
['@@example.com', null],
// evil stuff
['user@example.com/evil.exe', null],
['@user@example.com/evil.exe', null],
['user/evil.exe@example.com', null],
['@user/evil.exe@example.com', null],
];
}
/** @dataProvider dataGetTarget */
public function testGetTarget(?string $value, ?string $expected): void {
$user = $this->createMock(IUser::class);
$account = $this->createMock(IAccount::class);
$this->accountManager->method('getAccount')
->with($user)
->willReturn($account);
if ($value === null) {
// Property does not exist so throw
$account->method('getProperty')
->with(IAccountManager::PROPERTY_FEDIVERSE)
->willThrowException(new PropertyDoesNotExistException(IAccountManager::PROPERTY_FEDIVERSE));
} else {
$property = $this->createMock(IAccountProperty::class);
$property->method('getValue')
->willReturn($value);
$account->method('getProperty')
->willReturn($property);
}
// Preload and test
$this->action->preload($user);
self::assertEquals(
$expected,
$this->action->getTarget(),
);
}
}
|