Skip to content

Commit ac719ba

Browse files
Merge pull request #346 from nextcloud/bugfix/noid/dont-add-empty-entries-to-objects-array
Don't add empty entries to the objects array
2 parents 471ada3 + 18578ae commit ac719ba

2 files changed

Lines changed: 48 additions & 5 deletions

File tree

lib/GroupHelper.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,14 +219,15 @@ protected function eventToArray(IEvent $event, $id) {
219219
* @param IEvent $event
220220
* @return array
221221
*/
222-
protected function getObjectsFromChildren(IEvent $event) {
222+
protected function getObjectsFromChildren(IEvent $event): array {
223223
$child = $event->getChildEvent();
224+
$objects = [];
224225
if ($child instanceof IEvent) {
225226
$objects = $this->getObjectsFromChildren($child);
227+
}
228+
if ($event->getObjectId() !== 0 || $event->getObjectName() !== '') {
226229
$objects[$event->getObjectId()] = $event->getObjectName();
227-
return $objects;
228-
} else {
229-
return [$event->getObjectId() => $event->getObjectName()];
230230
}
231+
return $objects;
231232
}
232233
}

tests/GroupHelperTest.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use OCP\IL10N;
3333
use OCP\ILogger;
3434
use OCP\RichObjectStrings\IValidator;
35+
use PHPUnit\Framework\MockObject\MockObject;
3536

3637
class GroupHelperTest extends TestCase {
3738
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
@@ -202,7 +203,48 @@ public function testGetEventFromArray(array $activity) {
202203
->willReturn($event);
203204

204205
$helper = $this->getHelper();
205-
$instance = $this->invokePrivate($helper, 'arrayToEvent', [$activity]);
206+
$instance = self::invokePrivate($helper, 'arrayToEvent', [$activity]);
206207
$this->assertSame($event, $instance);
207208
}
209+
210+
211+
protected function createEvent(array $data): IEvent {
212+
/** @var IEvent|MockObject $event */
213+
$event = $this->createMock(IEvent::class);
214+
$event->expects($this->atLeastOnce())
215+
->method('getObjectId')
216+
->willReturn($data['id']);
217+
$event->expects($this->atLeastOnce())
218+
->method('getObjectName')
219+
->willReturn($data['name']);
220+
221+
if (isset($data['child'])) {
222+
$event->expects($this->once())
223+
->method('getChildEvent')
224+
->willReturn($this->createEvent($data['child']));
225+
}
226+
227+
return $event;
228+
}
229+
230+
public function dataGetObjectsFromChildren() {
231+
return [
232+
[['id' => 0, 'name' => ''], []],
233+
[['id' => 12, 'name' => ''], [12 => '']],
234+
[['id' => 0, 'name' => 'zero'], [0 => 'zero']],
235+
[['id' => 12, 'name' => 'twelve'], [12 => 'twelve']],
236+
[['id' => 12, 'name' => 'twelve', 'child' => ['id' => 11, 'name' => 'eleven', 'child' => ['id' => 10, 'name' => 'ten']]], [10 => 'ten', 11 => 'eleven', 12 => 'twelve']],
237+
];
238+
}
239+
240+
/**
241+
* @dataProvider dataGetObjectsFromChildren
242+
* @param array $data
243+
* @param array $expected
244+
*/
245+
public function testGetObjectsFromChildren(array $data, array $expected) {
246+
$event = $this->createEvent($data);
247+
$helper = $this->getHelper();
248+
$this->assertSame($expected, self::invokePrivate($helper, 'getObjectsFromChildren', [$event]));
249+
}
208250
}

0 commit comments

Comments
 (0)