Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/private/Talk/Broker.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,20 @@ public function deleteConversation(string $id): void {

$this->backend->deleteConversation($id);
}

public function isAllowedToCreateConversations(): bool {
if (!$this->isEnabledForUser()) {
return false;
}

return $this->backend->isAllowedToCreateConversations();
}

public function isEnabledForUser(): bool {
if (!$this->hasBackend()) {
return false;
}

return $this->backend->isEnabledForUser();
}
}
20 changes: 20 additions & 0 deletions lib/public/Talk/IBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,24 @@ public function createConversation(string $name,
* @since 26.0.0
*/
public function deleteConversation(string $id): void;

/**
* Check if the logged-in user is allowed to create conversations
*
* Also returns false when no backend is available
*
* @return bool
* @since 34.0.0
*/
public function isAllowedToCreateConversations(): bool;

/**
* Check if the Talk backend is enabled for the logged-in user
*
* Also returns false when no backend is available
*
* @return bool
* @since 34.0.0
*/
public function isEnabledForUser(): bool;
}
18 changes: 18 additions & 0 deletions lib/public/Talk/ITalkBackend.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,22 @@ public function createConversation(string $name,
* @since 26.0.0
*/
public function deleteConversation(string $id): void;

/**
* Check if the logged-in user is allowed to create conversations
*
* Also returns false when no backend is enabled for the user
*
* @return bool
* @since 34.0.0
*/
public function isAllowedToCreateConversations(): bool;

/**
* Check if the Talk backend is enabled for the logged-in user
*
* @return bool
* @since 34.0.0
*/
public function isEnabledForUser(): bool;
}
137 changes: 125 additions & 12 deletions tests/lib/Talk/BrokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OCP\IServerContainer;
use OCP\Talk\IConversationOptions;
use OCP\Talk\ITalkBackend;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LoggerInterface;
use RuntimeException;
use Test\TestCase;
Expand Down Expand Up @@ -51,7 +52,7 @@ public function testHasNoBackendCalledTooEarly(): void {
}

public function testHasNoBackend(): void {
$this->coordinator->expects(self::once())
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($this->createMock(RegistrationContext::class));

Expand All @@ -63,16 +64,16 @@ public function testHasNoBackend(): void {
public function testHasFaultyBackend(): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects(self::once())
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects(self::once())
$registrationContext->expects($this->once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$this->container->expects(self::once())
$this->container->expects($this->once())
->method('get')
->willThrowException(new QueryException());
$this->logger->expects(self::once())
$this->logger->expects($this->once())
->method('error');

self::assertFalse(
Expand All @@ -83,14 +84,14 @@ public function testHasFaultyBackend(): void {
public function testHasBackend(): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects(self::once())
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects(self::once())
$registrationContext->expects($this->once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$talkService = $this->createMock(ITalkBackend::class);
$this->container->expects(self::once())
$this->container->expects($this->once())
->method('get')
->with($fakeTalkServiceClass)
->willReturn($talkService);
Expand All @@ -110,19 +111,19 @@ public function testNewConversationOptions(): void {
public function testCreateConversation(): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects(self::once())
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects(self::once())
$registrationContext->expects($this->once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$talkService = $this->createMock(ITalkBackend::class);
$this->container->expects(self::once())
$this->container->expects($this->once())
->method('get')
->with($fakeTalkServiceClass)
->willReturn($talkService);
$options = $this->createMock(IConversationOptions::class);
$talkService->expects(self::once())
$talkService->expects($this->once())
->method('createConversation')
->with('Watercooler', [], $options);

Expand All @@ -132,4 +133,116 @@ public function testCreateConversation(): void {
$options
);
}

public function testIsEnabledForUserNoBackend(): void {
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($this->createMock(RegistrationContext::class));

self::assertFalse(
$this->broker->isEnabledForUser()
);
}

public static function dataIsEnabledForUser(): array {
return [
[true],
[false],
];
}

#[DataProvider('dataIsEnabledForUser')]
public function testIsEnabledForUser(bool $enabled): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects($this->once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$talkService = $this->createMock(ITalkBackend::class);
$this->container->expects($this->once())
->method('get')
->with($fakeTalkServiceClass)
->willReturn($talkService);
$talkService->expects($this->once())
->method('isEnabledForUser')
->willReturn($enabled);

self::assertSame(
$enabled,
$this->broker->isEnabledForUser()
);
}

public function testIsAllowedToCreateConversationsNoBackend(): void {
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($this->createMock(RegistrationContext::class));

self::assertFalse(
$this->broker->isAllowedToCreateConversations()
);
}

public function testIsAllowedToCreateConversationsBackendDisabled(): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects($this->once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$talkService = $this->createMock(ITalkBackend::class);
$this->container->expects($this->once())
->method('get')
->with($fakeTalkServiceClass)
->willReturn($talkService);
$talkService->expects($this->once())
->method('isEnabledForUser')
->willReturn(false);
$talkService->expects($this->never())
->method('isAllowedToCreateConversations');

self::assertFalse(
$this->broker->isAllowedToCreateConversations()
);
}

public static function dataIsAllowedToCreateConversations(): array {
return [
[true],
[false],
];
}

#[DataProvider('dataIsAllowedToCreateConversations')]
public function testIsAllowedToCreateConversations(bool $allowed): void {
$fakeTalkServiceClass = '\\OCA\\Spreed\\TalkBackend';
$registrationContext = $this->createMock(RegistrationContext::class);
$this->coordinator->expects($this->once())
->method('getRegistrationContext')
->willReturn($registrationContext);
$registrationContext->expects($this->once())
->method('getTalkBackendRegistration')
->willReturn(new ServiceRegistration('spreed', $fakeTalkServiceClass));
$talkService = $this->createMock(ITalkBackend::class);
$this->container->expects($this->once())
->method('get')
->with($fakeTalkServiceClass)
->willReturn($talkService);
$talkService->expects($this->once())
->method('isEnabledForUser')
->willReturn(true);
$talkService->expects($this->once())
->method('isAllowedToCreateConversations')
->willReturn($allowed);

self::assertSame(
$allowed,
$this->broker->isAllowedToCreateConversations()
);
}
}
Loading