Skip to content

Commit a790f4c

Browse files
committed
fix: Fix user in Tags class, do not depend upon session
Signed-off-by: Côme Chilliet <[email protected]>
1 parent 1564846 commit a790f4c

File tree

3 files changed

+38
-18
lines changed

3 files changed

+38
-18
lines changed

lib/private/TagManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use OCP\IDBConnection;
1818
use OCP\ITagManager;
1919
use OCP\ITags;
20+
use OCP\IUserManager;
2021
use OCP\IUserSession;
2122
use OCP\User\Events\UserDeletedEvent;
2223
use Psr\Log\LoggerInterface;
@@ -29,6 +30,7 @@ class TagManager implements ITagManager, IEventListener {
2930
public function __construct(
3031
private TagMapper $mapper,
3132
private IUserSession $userSession,
33+
private IUserManager $userManager,
3234
private IDBConnection $connection,
3335
private LoggerInterface $logger,
3436
private IEventDispatcher $dispatcher,
@@ -59,7 +61,7 @@ public function load($type, $defaultTags = [], $includeShared = false, $userId =
5961
$userId = $this->userSession->getUser()->getUId();
6062
}
6163
$userFolder = $this->rootFolder->getUserFolder($userId);
62-
return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $this->dispatcher, $this->userSession, $userFolder, $defaultTags);
64+
return new Tags($this->mapper, $userId, $type, $this->logger, $this->connection, $this->dispatcher, $this->userManager, $userFolder, $defaultTags);
6365
}
6466

6567
/**

lib/private/Tags.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use OCP\Files\Folder;
1818
use OCP\IDBConnection;
1919
use OCP\ITags;
20-
use OCP\IUserSession;
20+
use OCP\IUserManager;
2121
use OCP\Share_Backend;
2222
use Psr\Log\LoggerInterface;
2323

@@ -65,7 +65,7 @@ public function __construct(
6565
private LoggerInterface $logger,
6666
private IDBConnection $db,
6767
private IEventDispatcher $dispatcher,
68-
private IUserSession $userSession,
68+
private IUserManager $userManager,
6969
private Folder $userFolder,
7070
array $defaultTags = [],
7171
) {
@@ -538,7 +538,7 @@ public function tagAs($objid, $tag, ?string $path = null) {
538538
}
539539
}
540540

541-
$this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userSession->getUser(), $objid, $path));
541+
$this->dispatcher->dispatchTyped(new NodeAddedToFavorite($this->userManager->getExistingUser($this->user), $objid, $path));
542542
}
543543
return true;
544544
}
@@ -583,7 +583,7 @@ public function unTag($objid, $tag, ?string $path = null) {
583583
}
584584
}
585585

586-
$this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userSession->getUser(), $objid, $path));
586+
$this->dispatcher->dispatchTyped(new NodeRemovedFromFavorite($this->userManager->getExistingUser($this->user), $objid, $path));
587587
}
588588
return true;
589589
}

tests/lib/TagsTest.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
use OCP\Files\IRootFolder;
1616
use OCP\Files\Node;
1717
use OCP\IDBConnection;
18-
use OCP\ITagManager;
1918
use OCP\IUser;
2019
use OCP\IUserManager;
2120
use OCP\IUserSession;
2221
use OCP\Server;
22+
use PHPUnit\Framework\MockObject\MockObject;
2323
use Psr\Log\LoggerInterface;
2424

2525
/**
@@ -28,16 +28,13 @@
2828
#[\PHPUnit\Framework\Attributes\Group('DB')]
2929
class TagsTest extends \Test\TestCase {
3030
protected $objectType;
31-
/** @var IUser */
32-
protected $user;
33-
/** @var IUserSession */
34-
protected $userSession;
35-
protected $backupGlobals = false;
36-
/** @var \OC\Tagging\TagMapper */
37-
protected $tagMapper;
38-
/** @var ITagManager */
39-
protected $tagMgr;
40-
protected IRootFolder $rootFolder;
31+
protected IUser&MockObject $user;
32+
protected IUserSession&MockObject $userSession;
33+
protected IUserManager&MockObject $userManager;
34+
protected IRootFolder&MockObject $rootFolder;
35+
36+
protected TagMapper $tagMapper;
37+
protected TagManager $tagMgr;
4138

4239
protected function setUp(): void {
4340
parent::setUp();
@@ -50,6 +47,11 @@ protected function setUp(): void {
5047
$this->user = $this->createMock(IUser::class);
5148
$this->user->method('getUID')
5249
->willReturn($userId);
50+
$this->userManager = $this->createMock(IUserManager::class);
51+
$this->userManager
52+
->expects($this->any())
53+
->method('getExistingUser')
54+
->willReturn($this->user);
5355
$this->userSession = $this->createMock(IUserSession::class);
5456
$this->userSession
5557
->expects($this->any())
@@ -70,7 +72,15 @@ protected function setUp(): void {
7072

7173
$this->objectType = $this->getUniqueID('type_');
7274
$this->tagMapper = new TagMapper(Server::get(IDBConnection::class));
73-
$this->tagMgr = new TagManager($this->tagMapper, $this->userSession, Server::get(IDBConnection::class), Server::get(LoggerInterface::class), Server::get(IEventDispatcher::class), $this->rootFolder);
75+
$this->tagMgr = new TagManager(
76+
$this->tagMapper,
77+
$this->userSession,
78+
$this->userManager,
79+
Server::get(IDBConnection::class),
80+
Server::get(LoggerInterface::class),
81+
Server::get(IEventDispatcher::class),
82+
$this->rootFolder
83+
);
7484
}
7585

7686
protected function tearDown(): void {
@@ -87,7 +97,15 @@ public function testTagManagerWithoutUserReturnsNull(): void {
8797
->expects($this->any())
8898
->method('getUser')
8999
->willReturn(null);
90-
$this->tagMgr = new TagManager($this->tagMapper, $this->userSession, Server::get(IDBConnection::class), Server::get(LoggerInterface::class), Server::get(IEventDispatcher::class), $this->rootFolder);
100+
$this->tagMgr = new TagManager(
101+
$this->tagMapper,
102+
$this->userSession,
103+
$this->userManager,
104+
Server::get(IDBConnection::class),
105+
Server::get(LoggerInterface::class),
106+
Server::get(IEventDispatcher::class),
107+
$this->rootFolder
108+
);
91109
$this->assertNull($this->tagMgr->load($this->objectType));
92110
}
93111

0 commit comments

Comments
 (0)