Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions lib/private/SystemTag/SystemTagObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,16 +358,6 @@ public function setObjectIdsForTag(string $tagId, string $objectType, array $obj
if (!empty($addedObjectIds)) {
$this->dispatcher->dispatchTyped(new TagAssignedEvent($objectType, array_map(fn ($objectId) => (string)$objectId, $addedObjectIds), [(int)$tagId]));
}

// Dispatch unassign events for removed object ids
foreach ($removedObjectIds as $objectId) {
$this->dispatcher->dispatch(MapperEvent::EVENT_UNASSIGN, new MapperEvent(
MapperEvent::EVENT_UNASSIGN,
$objectType,
(string)$objectId,
[(int)$tagId]
));
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions tests/lib/SystemTag/SystemTagObjectMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use OCP\SystemTag\ISystemTag;
use OCP\SystemTag\ISystemTagManager;
use OCP\SystemTag\ISystemTagObjectMapper;
use OCP\SystemTag\MapperEvent;
use OCP\SystemTag\TagAssignedEvent;
use OCP\SystemTag\TagNotFoundException;
use OCP\SystemTag\TagUnassignedEvent;
Expand Down Expand Up @@ -258,6 +259,28 @@ public function testAssignUnassignTags(): void {
], $tagIdMapping);
}

public function testSetObjectIdsForTagDispatchesUnassignEventOncePerObject(): void {
$unassignedObjectIds = [];
$this->dispatcher->expects($this->any())->method('dispatch')->willReturnCallback(
function (string $eventName, Event $event) use (&$unassignedObjectIds): void {
if ($eventName === MapperEvent::EVENT_UNASSIGN) {
$unassignedObjectIds[] = $event->getObjectId();
}
}
);

// tag1 is assigned to objects '1' and '2', keep only '2'
$this->tagMapper->setObjectIdsForTag((string)$this->tag1->getId(), 'testtype', ['2']);

$this->assertEquals(['1'], $unassignedObjectIds);

// same expectation when the new object list is empty
$unassignedObjectIds = [];
$this->tagMapper->setObjectIdsForTag((string)$this->tag1->getId(), 'testtype', []);

$this->assertEquals(['2'], $unassignedObjectIds);
}

public function testReAssignUnassignTags(): void {
// reassign tag1
$this->tagMapper->assignTags('1', 'testtype', [$this->tag1->getId()]);
Expand Down
Loading