Merge pull request #58990 from mosi-kha/fix/group-displayname-event-oldvalue

fix(group): pass previous display name in GroupChangedEvent
This commit is contained in:
Louis 2026-03-26 16:07:50 +01:00 committed by GitHub
commit 309d12abe1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View file

@ -75,11 +75,12 @@ class Group implements IGroup {
$displayName = trim($displayName);
if ($displayName !== '') {
$this->dispatcher->dispatchTyped(new BeforeGroupChangedEvent($this, 'displayName', $displayName, $this->displayName));
$oldDisplayName = $this->displayName;
foreach ($this->backends as $backend) {
if (($backend instanceof ISetDisplayNameBackend)
&& $backend->setDisplayName($this->gid, $displayName)) {
$this->displayName = $displayName;
$this->dispatcher->dispatchTyped(new GroupChangedEvent($this, 'displayName', $displayName, ''));
$this->dispatcher->dispatchTyped(new GroupChangedEvent($this, 'displayName', $displayName, $oldDisplayName));
return true;
}
}

View file

@ -11,6 +11,8 @@ namespace Test\Group;
use OC\Group\Group;
use OC\User\User;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Group\Events\BeforeGroupChangedEvent;
use OCP\Group\Events\GroupChangedEvent;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
@ -457,6 +459,41 @@ class GroupTest extends \Test\TestCase {
$this->assertSame(false, $users);
}
public function testSetDisplayNameDispatchesOldValue(): void {
$backend = $this->getMockBuilder('OC\Group\Database')
->disableOriginalConstructor()
->getMock();
$userManager = $this->getUserManager();
$dispatcher = $this->createMock(IEventDispatcher::class);
$invocation = 0;
$dispatcher->expects($this->exactly(2))
->method('dispatchTyped')
->willReturnCallback(function ($event) use (&$invocation): void {
$invocation++;
if ($invocation === 1) {
$this->assertInstanceOf(BeforeGroupChangedEvent::class, $event);
$this->assertSame('displayName', $event->getFeature());
$this->assertSame('New Name', $event->getValue());
$this->assertSame('Old Name', $event->getOldValue());
return;
}
$this->assertInstanceOf(GroupChangedEvent::class, $event);
$this->assertSame('displayName', $event->getFeature());
$this->assertSame('New Name', $event->getValue());
$this->assertSame('Old Name', $event->getOldValue());
});
$backend->expects($this->once())
->method('setDisplayName')
->with('group1', 'New Name')
->willReturn(true);
$group = new Group('group1', [$backend], $dispatcher, $userManager, null, 'Old Name');
$this->assertTrue($group->setDisplayName('New Name'));
}
public function testDelete(): void {
$backend = $this->getMockBuilder('OC\Group\Database')
->disableOriginalConstructor()