Skip to content

Commit 309d12a

Browse files
authored
Merge pull request #58990 from mosi-kha/fix/group-displayname-event-oldvalue
fix(group): pass previous display name in GroupChangedEvent
2 parents 7e9e126 + 3d6d38a commit 309d12a

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

lib/private/Group/Group.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,12 @@ public function setDisplayName(string $displayName): bool {
7575
$displayName = trim($displayName);
7676
if ($displayName !== '') {
7777
$this->dispatcher->dispatchTyped(new BeforeGroupChangedEvent($this, 'displayName', $displayName, $this->displayName));
78+
$oldDisplayName = $this->displayName;
7879
foreach ($this->backends as $backend) {
7980
if (($backend instanceof ISetDisplayNameBackend)
8081
&& $backend->setDisplayName($this->gid, $displayName)) {
8182
$this->displayName = $displayName;
82-
$this->dispatcher->dispatchTyped(new GroupChangedEvent($this, 'displayName', $displayName, ''));
83+
$this->dispatcher->dispatchTyped(new GroupChangedEvent($this, 'displayName', $displayName, $oldDisplayName));
8384
return true;
8485
}
8586
}

tests/lib/Group/GroupTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use OC\Group\Group;
1212
use OC\User\User;
1313
use OCP\EventDispatcher\IEventDispatcher;
14+
use OCP\Group\Events\BeforeGroupChangedEvent;
15+
use OCP\Group\Events\GroupChangedEvent;
1416
use OCP\IUser;
1517
use PHPUnit\Framework\MockObject\MockObject;
1618

@@ -457,6 +459,41 @@ public function testCountUsersNoMethod(): void {
457459
$this->assertSame(false, $users);
458460
}
459461

462+
public function testSetDisplayNameDispatchesOldValue(): void {
463+
$backend = $this->getMockBuilder('OC\Group\Database')
464+
->disableOriginalConstructor()
465+
->getMock();
466+
$userManager = $this->getUserManager();
467+
468+
$dispatcher = $this->createMock(IEventDispatcher::class);
469+
$invocation = 0;
470+
$dispatcher->expects($this->exactly(2))
471+
->method('dispatchTyped')
472+
->willReturnCallback(function ($event) use (&$invocation): void {
473+
$invocation++;
474+
if ($invocation === 1) {
475+
$this->assertInstanceOf(BeforeGroupChangedEvent::class, $event);
476+
$this->assertSame('displayName', $event->getFeature());
477+
$this->assertSame('New Name', $event->getValue());
478+
$this->assertSame('Old Name', $event->getOldValue());
479+
return;
480+
}
481+
482+
$this->assertInstanceOf(GroupChangedEvent::class, $event);
483+
$this->assertSame('displayName', $event->getFeature());
484+
$this->assertSame('New Name', $event->getValue());
485+
$this->assertSame('Old Name', $event->getOldValue());
486+
});
487+
488+
$backend->expects($this->once())
489+
->method('setDisplayName')
490+
->with('group1', 'New Name')
491+
->willReturn(true);
492+
493+
$group = new Group('group1', [$backend], $dispatcher, $userManager, null, 'Old Name');
494+
$this->assertTrue($group->setDisplayName('New Name'));
495+
}
496+
460497
public function testDelete(): void {
461498
$backend = $this->getMockBuilder('OC\Group\Database')
462499
->disableOriginalConstructor()

0 commit comments

Comments
 (0)