Skip to content

Commit 57c57d7

Browse files
authored
Merge pull request #59918 from nextcloud/feat/check-if-talk-enabled
feat(ocp): expose whether talk is enabled for user
2 parents 5b33032 + 109eb0c commit 57c57d7

4 files changed

Lines changed: 179 additions & 12 deletions

File tree

lib/private/Talk/Broker.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,20 @@ public function deleteConversation(string $id): void {
9595

9696
$this->backend->deleteConversation($id);
9797
}
98+
99+
public function isAllowedToCreateConversations(): bool {
100+
if (!$this->isEnabledForUser()) {
101+
return false;
102+
}
103+
104+
return $this->backend->isAllowedToCreateConversations();
105+
}
106+
107+
public function isEnabledForUser(): bool {
108+
if (!$this->hasBackend()) {
109+
return false;
110+
}
111+
112+
return $this->backend->isEnabledForUser();
113+
}
98114
}

lib/public/Talk/IBroker.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,24 @@ public function createConversation(string $name,
6565
* @since 26.0.0
6666
*/
6767
public function deleteConversation(string $id): void;
68+
69+
/**
70+
* Check if the logged-in user is allowed to create conversations
71+
*
72+
* Also returns false when no backend is available
73+
*
74+
* @return bool
75+
* @since 34.0.0
76+
*/
77+
public function isAllowedToCreateConversations(): bool;
78+
79+
/**
80+
* Check if the Talk backend is enabled for the logged-in user
81+
*
82+
* Also returns false when no backend is available
83+
*
84+
* @return bool
85+
* @since 34.0.0
86+
*/
87+
public function isEnabledForUser(): bool;
6888
}

lib/public/Talk/ITalkBackend.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,22 @@ public function createConversation(string $name,
4242
* @since 26.0.0
4343
*/
4444
public function deleteConversation(string $id): void;
45+
46+
/**
47+
* Check if the logged-in user is allowed to create conversations
48+
*
49+
* Also returns false when no backend is enabled for the user
50+
*
51+
* @return bool
52+
* @since 34.0.0
53+
*/
54+
public function isAllowedToCreateConversations(): bool;
55+
56+
/**
57+
* Check if the Talk backend is enabled for the logged-in user
58+
*
59+
* @return bool
60+
* @since 34.0.0
61+
*/
62+
public function isEnabledForUser(): bool;
4563
}

tests/lib/Talk/BrokerTest.php

Lines changed: 125 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\IServerContainer;
1818
use OCP\Talk\IConversationOptions;
1919
use OCP\Talk\ITalkBackend;
20+
use PHPUnit\Framework\Attributes\DataProvider;
2021
use Psr\Log\LoggerInterface;
2122
use RuntimeException;
2223
use Test\TestCase;
@@ -52,7 +53,7 @@ public function testHasNoBackendCalledTooEarly(): void {
5253
}
5354

5455
public function testHasNoBackend(): void {
55-
$this->coordinator->expects(self::once())
56+
$this->coordinator->expects($this->once())
5657
->method('getRegistrationContext')
5758
->willReturn($this->createMock(RegistrationContext::class));
5859

@@ -64,16 +65,16 @@ public function testHasNoBackend(): void {
6465
public function testHasFaultyBackend(): void {
6566
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
6667
$registrationContext = $this->createMock(RegistrationContext::class);
67-
$this->coordinator->expects(self::once())
68+
$this->coordinator->expects($this->once())
6869
->method('getRegistrationContext')
6970
->willReturn($registrationContext);
70-
$registrationContext->expects(self::once())
71+
$registrationContext->expects($this->once())
7172
->method('getTalkBackendRegistration')
7273
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
73-
$this->container->expects(self::once())
74+
$this->container->expects($this->once())
7475
->method('get')
7576
->willThrowException(new QueryException());
76-
$this->logger->expects(self::once())
77+
$this->logger->expects($this->once())
7778
->method('error');
7879

7980
self::assertFalse(
@@ -84,14 +85,14 @@ public function testHasFaultyBackend(): void {
8485
public function testHasBackend(): void {
8586
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
8687
$registrationContext = $this->createMock(RegistrationContext::class);
87-
$this->coordinator->expects(self::once())
88+
$this->coordinator->expects($this->once())
8889
->method('getRegistrationContext')
8990
->willReturn($registrationContext);
90-
$registrationContext->expects(self::once())
91+
$registrationContext->expects($this->once())
9192
->method('getTalkBackendRegistration')
9293
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
9394
$talkService = $this->createMock(ITalkBackend::class);
94-
$this->container->expects(self::once())
95+
$this->container->expects($this->once())
9596
->method('get')
9697
->with($fakeTalkServiceClass)
9798
->willReturn($talkService);
@@ -111,19 +112,19 @@ public function testNewConversationOptions(): void {
111112
public function testCreateConversation(): void {
112113
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
113114
$registrationContext = $this->createMock(RegistrationContext::class);
114-
$this->coordinator->expects(self::once())
115+
$this->coordinator->expects($this->once())
115116
->method('getRegistrationContext')
116117
->willReturn($registrationContext);
117-
$registrationContext->expects(self::once())
118+
$registrationContext->expects($this->once())
118119
->method('getTalkBackendRegistration')
119120
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
120121
$talkService = $this->createMock(ITalkBackend::class);
121-
$this->container->expects(self::once())
122+
$this->container->expects($this->once())
122123
->method('get')
123124
->with($fakeTalkServiceClass)
124125
->willReturn($talkService);
125126
$options = $this->createMock(IConversationOptions::class);
126-
$talkService->expects(self::once())
127+
$talkService->expects($this->once())
127128
->method('createConversation')
128129
->with('Watercooler', [], $options);
129130

@@ -133,4 +134,116 @@ public function testCreateConversation(): void {
133134
$options
134135
);
135136
}
137+
138+
public function testIsEnabledForUserNoBackend(): void {
139+
$this->coordinator->expects($this->once())
140+
->method('getRegistrationContext')
141+
->willReturn($this->createMock(RegistrationContext::class));
142+
143+
self::assertFalse(
144+
$this->broker->isEnabledForUser()
145+
);
146+
}
147+
148+
public static function dataIsEnabledForUser(): array {
149+
return [
150+
[true],
151+
[false],
152+
];
153+
}
154+
155+
#[DataProvider('dataIsEnabledForUser')]
156+
public function testIsEnabledForUser(bool $enabled): void {
157+
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
158+
$registrationContext = $this->createMock(RegistrationContext::class);
159+
$this->coordinator->expects($this->once())
160+
->method('getRegistrationContext')
161+
->willReturn($registrationContext);
162+
$registrationContext->expects($this->once())
163+
->method('getTalkBackendRegistration')
164+
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
165+
$talkService = $this->createMock(ITalkBackend::class);
166+
$this->container->expects($this->once())
167+
->method('get')
168+
->with($fakeTalkServiceClass)
169+
->willReturn($talkService);
170+
$talkService->expects($this->once())
171+
->method('isEnabledForUser')
172+
->willReturn($enabled);
173+
174+
self::assertSame(
175+
$enabled,
176+
$this->broker->isEnabledForUser()
177+
);
178+
}
179+
180+
public function testIsAllowedToCreateConversationsNoBackend(): void {
181+
$this->coordinator->expects($this->once())
182+
->method('getRegistrationContext')
183+
->willReturn($this->createMock(RegistrationContext::class));
184+
185+
self::assertFalse(
186+
$this->broker->isAllowedToCreateConversations()
187+
);
188+
}
189+
190+
public function testIsAllowedToCreateConversationsBackendDisabled(): void {
191+
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
192+
$registrationContext = $this->createMock(RegistrationContext::class);
193+
$this->coordinator->expects($this->once())
194+
->method('getRegistrationContext')
195+
->willReturn($registrationContext);
196+
$registrationContext->expects($this->once())
197+
->method('getTalkBackendRegistration')
198+
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
199+
$talkService = $this->createMock(ITalkBackend::class);
200+
$this->container->expects($this->once())
201+
->method('get')
202+
->with($fakeTalkServiceClass)
203+
->willReturn($talkService);
204+
$talkService->expects($this->once())
205+
->method('isEnabledForUser')
206+
->willReturn(false);
207+
$talkService->expects($this->never())
208+
->method('isAllowedToCreateConversations');
209+
210+
self::assertFalse(
211+
$this->broker->isAllowedToCreateConversations()
212+
);
213+
}
214+
215+
public static function dataIsAllowedToCreateConversations(): array {
216+
return [
217+
[true],
218+
[false],
219+
];
220+
}
221+
222+
#[DataProvider('dataIsAllowedToCreateConversations')]
223+
public function testIsAllowedToCreateConversations(bool $allowed): void {
224+
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
225+
$registrationContext = $this->createMock(RegistrationContext::class);
226+
$this->coordinator->expects($this->once())
227+
->method('getRegistrationContext')
228+
->willReturn($registrationContext);
229+
$registrationContext->expects($this->once())
230+
->method('getTalkBackendRegistration')
231+
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
232+
$talkService = $this->createMock(ITalkBackend::class);
233+
$this->container->expects($this->once())
234+
->method('get')
235+
->with($fakeTalkServiceClass)
236+
->willReturn($talkService);
237+
$talkService->expects($this->once())
238+
->method('isEnabledForUser')
239+
->willReturn(true);
240+
$talkService->expects($this->once())
241+
->method('isAllowedToCreateConversations')
242+
->willReturn($allowed);
243+
244+
self::assertSame(
245+
$allowed,
246+
$this->broker->isAllowedToCreateConversations()
247+
);
248+
}
136249
}

0 commit comments

Comments
 (0)