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 f117a3a7f7c85..fcca3d96b7ac1 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 9528fc291848b..6dc43300dfcff 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; /** @@ -28,16 +28,13 @@ #[\PHPUnit\Framework\Attributes\Group('DB')] 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(); @@ -50,6 +47,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()) @@ -70,7 +72,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 { @@ -87,7 +97,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)); }