fix removing single permissions from event (#1661)

This commit is contained in:
Kevin Papst
2020-04-24 19:02:21 +02:00
committed by GitHub
parent 0debc70ead
commit cbd7124cec
2 changed files with 15 additions and 8 deletions

View File

@@ -36,8 +36,8 @@ final class PermissionsEvent extends Event
public function removePermission(string $section, string $permission): PermissionsEvent
{
if (\array_key_exists($section, $this->sections)) {
if (\array_key_exists($permission, $this->sections[$section])) {
unset($this->sections[$section][$permission]);
if (($key = array_search($permission, $this->sections[$section])) !== false) {
unset($this->sections[$section][$key]);
}
}

View File

@@ -36,15 +36,22 @@ class PermissionsEventTest extends TestCase
self::assertFalse($sut->hasSection('foo'));
self::assertNull($sut->getSection('foo'));
$sut->addPermissions('bar', ['foo' => 123, 'hello' => 'world', 'test' => false]);
self::assertEquals(['bar' => ['foo' => 123, 'hello' => 'world', 'test' => false]], $sut->getPermissions());
$sut->addPermissions('bar', ['foo', 'hello', 'world', 'test']);
self::assertEquals(['bar' => ['foo', 'hello', 'world', 'test']], $sut->getPermissions());
self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'xxx'));
self::assertEquals(['bar' => ['foo' => 123, 'hello' => 'world', 'test' => false]], $sut->getPermissions());
self::assertEquals(['foo', 'hello', 'world', 'test'], array_values($sut->getSection('bar')));
self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'foo'));
self::assertEquals(['bar' => ['hello' => 'world', 'test' => false]], $sut->getPermissions());
self::assertEquals(['hello', 'world', 'test'], array_values($sut->getSection('bar')));
self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'test'));
self::assertEquals(['bar' => ['hello' => 'world']], $sut->getPermissions());
self::assertEquals(['hello', 'world'], array_values($sut->getSection('bar')));
self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'hello'));
self::assertEquals(['bar' => []], $sut->getPermissions());
self::assertEquals(['world'], array_values($sut->getSection('bar')));
self::assertInstanceOf(PermissionsEvent::class, $sut->removePermission('bar', 'world'));
self::assertEquals([], array_values($sut->getSection('bar')));
}
}