From bc267794ead0feb0197641a396876d62ca3a8adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 12 Mar 2026 16:57:42 +0100 Subject: [PATCH] fix: Fix user in Tags class, do not depend upon session MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/TagManager.php | 4 +++- lib/private/Tags.php | 8 +++---- tests/lib/TagsTest.php | 44 +++++++++++++++++++++++++++----------- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/lib/private/TagManager.php b/lib/private/TagManager.php index f9d8d71a5aaf3..dacdfbe4d32ce 100644 --- a/lib/private/TagManager.php +++ b/lib/private/TagManager.php @@ -17,6 +17,7 @@ use OCP\IDBConnection; use OCP\ITagManager; use OCP\ITags; +use OCP\IUserManager; use OCP\IUserSession; use OCP\User\Events\UserDeletedEvent; use Psr\Log\LoggerInterface; @@ -29,6 +30,7 @@ class TagManager implements ITagManager, IEventListener { public function __construct( private TagMapper $mapper, private IUserSession $userSession, + private IUserManager $userManager, private IDBConnection $connection, private LoggerInterface $logger, private IEventDispatcher $dispatcher, @@ -59,7 +61,7 @@ public function load($type, $defaultTags = [], $includeShared = false, $userId = $userId = $this->userSession->getUser()->getUId(); } $userFolder = $this->rootFolder->getUserFolder($userId); - return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $this->dispatcher, $this->userSession, $userFolder, $defaultTags); + return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $this->dispatcher, $this->userManager, $userFolder, $defaultTags); } /** diff --git a/lib/private/Tags.php b/lib/private/Tags.php index 81c8e102f1176..8e94f3665e5e1 100644 --- a/lib/private/Tags.php +++ b/lib/private/Tags.php @@ -17,7 +17,7 @@ use OCP\Files\Folder; use OCP\IDBConnection; use OCP\ITags; -use OCP\IUserSession; +use OCP\IUserManager; use OCP\Share_Backend; use Psr\Log\LoggerInterface; @@ -65,7 +65,7 @@ public function __construct( private LoggerInterface $logger, private IDBConnection $db, private IEventDispatcher $dispatcher, - private IUserSession $userSession, + private IUserManager $userManager, private Folder $userFolder, array $defaultTags = [], ) { @@ -538,7 +538,7 @@ public function tagAs($objid, $tag, ?string $path = null) { } } - $this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $objid, $path)); + $this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userManager->getExistingUser($this->user), $objid, $path)); } return true; } @@ -583,7 +583,7 @@ public function unTag($objid, $tag, ?string $path = null) { } } - $this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $objid, $path)); + $this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userManager->getExistingUser($this->user), $objid, $path)); } return true; } diff --git a/tests/lib/TagsTest.php b/tests/lib/TagsTest.php index 4aeaeeed86eb4..aa3712d3b0cee 100644 --- a/tests/lib/TagsTest.php +++ b/tests/lib/TagsTest.php @@ -15,11 +15,11 @@ use OCP\Files\IRootFolder; use OCP\Files\Node; use OCP\IDBConnection; -use OCP\ITagManager; use OCP\IUser; use OCP\IUserManager; use OCP\IUserSession; use OCP\Server; +use PHPUnit\Framework\MockObject\MockObject; use Psr\Log\LoggerInterface; /** @@ -29,16 +29,13 @@ */ class TagsTest extends \Test\TestCase { protected $objectType; - /** @var IUser */ - protected $user; - /** @var IUserSession */ - protected $userSession; - protected $backupGlobals = false; - /** @var \OC\Tagging\TagMapper */ - protected $tagMapper; - /** @var ITagManager */ - protected $tagMgr; - protected IRootFolder $rootFolder; + protected IUser&MockObject $user; + protected IUserSession&MockObject $userSession; + protected IUserManager&MockObject $userManager; + protected IRootFolder&MockObject $rootFolder; + + protected TagMapper $tagMapper; + protected TagManager $tagMgr; protected function setUp(): void { parent::setUp(); @@ -51,6 +48,11 @@ protected function setUp(): void { $this->user = $this->createMock(IUser::class); $this->user->method('getUID') ->willReturn($userId); + $this->userManager = $this->createMock(IUserManager::class); + $this->userManager + ->expects($this->any()) + ->method('getExistingUser') + ->willReturn($this->user); $this->userSession = $this->createMock(IUserSession::class); $this->userSession ->expects($this->any()) @@ -71,7 +73,15 @@ protected function setUp(): void { $this->objectType = $this->getUniqueID('type_'); $this->tagMapper = new TagMapper(Server::get(IDBConnection::class)); - $this->tagMgr = new TagManager($this->tagMapper, $this->userSession, Server::get(IDBConnection::class), Server::get(LoggerInterface::class), Server::get(IEventDispatcher::class), $this->rootFolder); + $this->tagMgr = new TagManager( + $this->tagMapper, + $this->userSession, + $this->userManager, + Server::get(IDBConnection::class), + Server::get(LoggerInterface::class), + Server::get(IEventDispatcher::class), + $this->rootFolder + ); } protected function tearDown(): void { @@ -88,7 +98,15 @@ public function testTagManagerWithoutUserReturnsNull(): void { ->expects($this->any()) ->method('getUser') ->willReturn(null); - $this->tagMgr = new TagManager($this->tagMapper, $this->userSession, Server::get(IDBConnection::class), Server::get(LoggerInterface::class), Server::get(IEventDispatcher::class), $this->rootFolder); + $this->tagMgr = new TagManager( + $this->tagMapper, + $this->userSession, + $this->userManager, + Server::get(IDBConnection::class), + Server::get(LoggerInterface::class), + Server::get(IEventDispatcher::class), + $this->rootFolder + ); $this->assertNull($this->tagMgr->load($this->objectType)); }