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
4 changes: 3 additions & 1 deletion lib/FilesHooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -799,8 +799,8 @@ protected function shareWithTeam(string $shareWith, Node $fileSource, string $fi
return;
}

$this->teamManager->startSuperSession();
try {
$this->teamManager->startSuperSession();
$team = $this->teamManager->getCircle($shareWith);
$members = $team->getInheritedMembers();
$members = array_filter($members, fn ($member) => $member->getUserType() === Member::TYPE_USER);
Expand All @@ -809,6 +809,8 @@ protected function shareWithTeam(string $shareWith, Node $fileSource, string $fi
$this->logger->debug('Fetching team members for share activity failed', ['exception' => $e]);
// error in teams app - setting users list to empty
$userIds = [];
} finally {
$this->teamManager->stopSession();
}

// Activity for user performing the share
Expand Down
7 changes: 5 additions & 2 deletions lib/NotificationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ public function prepare(INotification $notification, string $languageCode): INot
throw new AlreadyProcessedException();
}
$this->activityManager->setCurrentUserId($notification->getUser());
$event = $this->populateEvent($event, $languageCode);
$this->activityManager->setCurrentUserId(null);
try {
$event = $this->populateEvent($event, $languageCode);
} finally {
$this->activityManager->setCurrentUserId(null);
}

try {
return $this->getDisplayNotificationForEvent($event, $event->getObjectId());
Expand Down
25 changes: 25 additions & 0 deletions tests/FilesHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,31 @@ public static function dataAddNotificationsForUser(): array {
];
}

public function testAddNotificationsForUserSkipsInvalidEvent(): void {
$this->urlGenerator->method('linkToRouteAbsolute')
->willReturn('routeToFilesIndex');

$event = $this->createMock(IEvent::class);
$event->method('setApp')->willReturnSelf();
$event->method('setType')->willReturnSelf();
$event->method('setAffectedUser')->willReturnSelf();
$event->method('setTimestamp')->willReturnSelf();
$event->method('setSubject')
->willThrowException(new \InvalidArgumentException('invalid subject'));

$this->activityManager->expects($this->once())
->method('generateEvent')
->willReturn($event);

// A half-built event must never reach the stream or the mail queue
$this->data->expects($this->never())
->method('send');
$this->data->expects($this->never())
->method('storeMail');

self::invokePrivate($this->filesHooks, 'addNotificationsForUser', ['user1', 'subject', [], 42, '/file.txt', true, 3600, true, 'shared']);
}

#[DataProvider('dataAddNotificationsForUser')]
public function testAddNotificationsForUser(string $user, string $subject, array $parameter, int $fileId, string $path, string $urlPath, bool $isFile, bool $notification, bool $email, string $type, string $app, bool $sentEmail): void {
$this->urlGenerator->expects($this->once())
Expand Down
75 changes: 75 additions & 0 deletions tests/NotificationGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Activity\Tests;

use OCA\Activity\Data;
use OCA\Activity\NotificationGenerator;
use OCA\Activity\UserSettings;
use OCP\Activity\IEvent;
use OCP\Activity\IManager as ActivityManager;
use OCP\Activity\IProvider;
use OCP\IL10N;
use OCP\Notification\IManager as NotificationManager;
use OCP\Notification\INotification;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;

class NotificationGeneratorTest extends TestCase {
protected Data&MockObject $data;
protected ActivityManager&MockObject $activityManager;
protected NotificationGenerator $generator;

protected function setUp(): void {
parent::setUp();

$this->data = $this->createMock(Data::class);
$this->activityManager = $this->createMock(ActivityManager::class);

$this->generator = new NotificationGenerator(
$this->data,
$this->activityManager,
$this->createMock(NotificationManager::class),
$this->createMock(UserSettings::class),
$this->createMock(IL10N::class),
$this->createMock(LoggerInterface::class),
);
}

public function testCurrentUserIsResetWhenAProviderThrows(): void {
$notification = $this->createMock(INotification::class);
$notification->method('getObjectType')->willReturn('activity_notification');
$notification->method('getObjectId')->willReturn('42');
$notification->method('getUser')->willReturn('affected');

$event = $this->createMock(IEvent::class);
$event->method('getAffectedUser')->willReturn('affected');
$this->data->method('getById')->willReturn($event);

$provider = $this->createMock(IProvider::class);
$provider->method('parse')
->willThrowException(new \RuntimeException('provider exploded'));
$this->activityManager->method('getProviders')
->willReturn([$provider]);

$seenUsers = [];
$this->activityManager->method('setCurrentUserId')
->willReturnCallback(static function (?string $uid) use (&$seenUsers): void {
$seenUsers[] = $uid;
});

try {
$this->generator->prepare($notification, 'en');
$this->fail('Expected the provider exception to propagate');
} catch (\RuntimeException) {
}

// The identity must not survive an exploding provider
$this->assertSame(['affected', null], $seenUsers);
}
}
1 change: 1 addition & 0 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
</file>
<file src="lib/FilesHooks.php">
<UndefinedClass>
<code><![CDATA[$this->teamManager]]></code>
<code><![CDATA[$this->teamManager]]></code>
<code><![CDATA[$this->teamManager]]></code>
<code><![CDATA[Member]]></code>
Expand Down
Loading