Skip to content

Commit a2ac8e3

Browse files
authored
Merge pull request #62647 from nextcloud/fix/setObjectIdsForTag-dispatches-once
fix(systemTags): setObjectIdsForTag() dispatches once
2 parents a2d57d3 + c11c616 commit a2ac8e3

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
@@ -358,16 +358,6 @@ public function setObjectIdsForTag(string $tagId, string $objectType, array $obj
358358
if (!empty($addedObjectIds)) {
359359
$this->dispatcher->dispatchTyped(new TagAssignedEvent($objectType, array_map(fn ($objectId) => (string)$objectId, $addedObjectIds), [(int)$tagId]));
360360
}
361-
362-
// Dispatch unassign events for removed object ids
363-
foreach ($removedObjectIds as $objectId) {
364-
$this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
365-
MapperEvent::EVENT_UNASSIGN,
366-
$objectType,
367-
(string)$objectId,
368-
[(int)$tagId]
369-
));
370-
}
371361
}
372362

373363
/**

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)