Skip to content

Commit 1be6e36

Browse files
solracsfbackportbot[bot]
authored andcommitted
fix(systemTags): setObjectIdsForTag() dispatches once
Signed-off-by: Git'Fellow <12234510+solracsf@users.noreply.github.com>
1 parent 8077a8e commit 1be6e36

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

lib/private/SystemTag/SystemTagObjectMapper.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,16 +348,6 @@ public function setObjectIdsForTag(string $tagId, string $objectType, array $obj
348348
if (!empty($addedObjectIds)) {
349349
$this->dispatcher->dispatchTyped(new TagAssignedEvent($objectType, array_map(fn ($objectId) => (string)$objectId, $addedObjectIds), [(int)$tagId]));
350350
}
351-
352-
// Dispatch unassign events for removed object ids
353-
foreach ($removedObjectIds as $objectId) {
354-
$this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
355-
MapperEvent::EVENT_UNASSIGN,
356-
$objectType,
357-
(string)$objectId,
358-
[(int)$tagId]
359-
));
360-
}
361351
}
362352

363353
/**

tests/lib/SystemTag/SystemTagObjectMapperTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OCP\SystemTag\ISystemTag;
1919
use OCP\SystemTag\ISystemTagManager;
2020
use OCP\SystemTag\ISystemTagObjectMapper;
21+
use OCP\SystemTag\MapperEvent;
2122
use OCP\SystemTag\TagAssignedEvent;
2223
use OCP\SystemTag\TagNotFoundException;
2324
use OCP\SystemTag\TagUnassignedEvent;
@@ -258,6 +259,28 @@ public function testAssignUnassignTags(): void {
258259
], $tagIdMapping);
259260
}
260261

262+
public function testSetObjectIdsForTagDispatchesUnassignEventOncePerObject(): void {
263+
$unassignedObjectIds = [];
264+
$this->dispatcher->expects($this->any())->method('dispatch')->willReturnCallback(
265+
function (string $eventName, Event $event) use (&$unassignedObjectIds): void {
266+
if ($eventName === MapperEvent::EVENT_UNASSIGN) {
267+
$unassignedObjectIds[] = $event->getObjectId();
268+
}
269+
}
270+
);
271+
272+
// tag1 is assigned to objects '1' and '2', keep only '2'
273+
$this->tagMapper->setObjectIdsForTag((string)$this->tag1->getId(), 'testtype', ['2']);
274+
275+
$this->assertEquals(['1'], $unassignedObjectIds);
276+
277+
// same expectation when the new object list is empty
278+
$unassignedObjectIds = [];
279+
$this->tagMapper->setObjectIdsForTag((string)$this->tag1->getId(), 'testtype', []);
280+
281+
$this->assertEquals(['2'], $unassignedObjectIds);
282+
}
283+
261284
public function testReAssignUnassignTags(): void {
262285
// reassign tag1
263286
$this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]);

0 commit comments

Comments
 (0)