l10nFactory = $this->createMock(IFactory::class); $this->url = $this->createMock(IURLGenerator::class); $this->activityManager = $this->createMock(IManager::class); $this->userManager = $this->createMock(IUserManager::class); $this->rootFolder = $this->createMock(IRootFolder::class); $this->cloudIdManager = $this->createMock(ICloudIdManager::class); $this->contactsManager = $this->createMock(IContactsManager::class); $this->eventMerger = $this->createMock(IEventMerger::class); } /** * @param string[] $methods * @return Provider|MockObject */ protected function getProvider(array $methods = []) { if (!empty($methods)) { return $this->getMockBuilder(Provider::class) ->setConstructorArgs([ $this->l10nFactory, $this->url, $this->activityManager, $this->userManager, $this->rootFolder, $this->cloudIdManager, $this->contactsManager, $this->eventMerger, ]) ->setMethods($methods) ->getMock(); } return new Provider( $this->l10nFactory, $this->url, $this->activityManager, $this->userManager, $this->rootFolder, $this->cloudIdManager, $this->contactsManager, $this->eventMerger ); } public function dataGetFile() { return [ [[42 => '/FortyTwo.txt'], null, '42', 'FortyTwo.txt', 'FortyTwo.txt'], [['23' => '/Twenty/Three.txt'], null, '23', 'Three.txt', 'Twenty/Three.txt'], ['/Foo/Bar.txt', 128, 128, 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before ]; } /** * @dataProvider dataGetFile * @param mixed $parameter * @param mixed $eventId * @param int $id * @param string $name * @param string $path */ public function testGetFile($parameter, $eventId, $id, $name, $path) { $provider = $this->getProvider(); if ($eventId !== null) { $event = $this->createMock(IEvent::class); $event->expects($this->once()) ->method('getObjectId') ->willReturn($eventId); } else { $event = null; } $this->url->expects($this->once()) ->method('linkToRouteAbsolute') ->with('files.viewcontroller.showFile', ['fileid' => $id]) ->willReturn('link-' . $id); $result = self::invokePrivate($provider, 'getFile', [$parameter, $event]); $this->assertSame('file', $result['type']); $this->assertSame($id, $result['id']); $this->assertSame($name, $result['name']); $this->assertSame($path, $result['path']); $this->assertSame('link-' . $id, $result['link']); } public function testGetFileThrows() { $this->expectException(\InvalidArgumentException::class); $provider = $this->getProvider(); self::invokePrivate($provider, 'getFile', ['/Foo/Bar.txt', null]); } public function dataGetUser() { return [ ['test', 'Test user', null, ['type' => 'user', 'id' => 'test', 'name' => 'Test user']], ['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => null], ['type' => 'user', 'id' => 'test', 'name' => 'test@localhost', 'server' => 'localhost']], ['test@http://localhost', null, ['user' => 'test', 'displayId' => 'test@localhost', 'remote' => 'localhost', 'name' => 'Remote user'], ['type' => 'user', 'id' => 'test', 'name' => 'Remote user (test@localhost)', 'server' => 'localhost']], ['test', null, null, ['type' => 'user', 'id' => 'test', 'name' => 'test']], ]; } /** * @dataProvider dataGetUser * @param string $uid * @param string|null $userDisplayName * @param array|null $cloudIdData * @param array $expected */ public function testGetUser(string $uid, ?string $userDisplayName, ?array $cloudIdData, array $expected): void { $provider = $this->getProvider(); if ($userDisplayName !== null) { $this->userManager->expects($this->once()) ->method('getDisplayName') ->with($uid) ->willReturn($userDisplayName); } if ($cloudIdData !== null) { $this->cloudIdManager->expects($this->once()) ->method('isValidCloudId') ->willReturn(true); $cloudId = $this->createMock(ICloudId::class); $cloudId->expects($this->once()) ->method('getUser') ->willReturn($cloudIdData['user']); $cloudId->expects($this->once()) ->method('getDisplayId') ->willReturn($cloudIdData['displayId']); $cloudId->expects($this->once()) ->method('getRemote') ->willReturn($cloudIdData['remote']); $this->cloudIdManager->expects($this->once()) ->method('resolveCloudId') ->with($uid) ->willReturn($cloudId); if ($cloudIdData['name'] !== null) { $this->contactsManager->expects($this->once()) ->method('search') ->with($cloudIdData['displayId'], ['CLOUD']) ->willReturn([ [ 'CLOUD' => $cloudIdData['displayId'], 'FN' => $cloudIdData['name'], ] ]); } else { $this->contactsManager->expects($this->once()) ->method('search') ->with($cloudIdData['displayId'], ['CLOUD']) ->willReturn([]); } } $result = self::invokePrivate($provider, 'getUser', [$uid]); $this->assertEquals($expected, $result); } }