Fix unit tests

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2020-02-07 10:51:22 +01:00 committed by Roeland Jago Douma
parent f4918f208e
commit 85153e8923
No known key found for this signature in database
GPG Key ID: F941078878347C0C
1 changed files with 75 additions and 56 deletions

View File

@ -29,11 +29,15 @@ use OCA\Files\Activity\Provider;
use OCP\Activity\IEvent;
use OCP\Activity\IEventMerger;
use OCP\Activity\IManager;
use OCP\Contacts\IManager as IContactsManager;
use OCP\Federation\ICloudId;
use OCP\Federation\ICloudIdManager;
use OCP\Files\IRootFolder;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\L10N\IFactory;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
/**
@ -43,17 +47,21 @@ use Test\TestCase;
*/
class ProviderTest extends TestCase {
/** @var IFactory|\PHPUnit_Framework_MockObject_MockObject */
/** @var IFactory|MockObject */
protected $l10nFactory;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
/** @var IURLGenerator|MockObject */
protected $url;
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var IManager|MockObject */
protected $activityManager;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
/** @var IUserManager|MockObject */
protected $userManager;
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
/** @var IRootFolder|MockObject */
protected $rootFolder;
/** @var IEventMerger|\PHPUnit_Framework_MockObject_MockObject */
/** @var ICloudIdManager|MockObject */
protected $cloudIdManager;
/** @var IContactsManager|MockObject */
protected $contactsManager;
/** @var IEventMerger|MockObject */
protected $eventMerger;
protected function setUp(): void {
@ -64,12 +72,14 @@ class ProviderTest extends TestCase {
$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|\PHPUnit_Framework_MockObject_MockObject
* @return Provider|MockObject
*/
protected function getProvider(array $methods = []) {
if (!empty($methods)) {
@ -80,6 +90,8 @@ class ProviderTest extends TestCase {
$this->activityManager,
$this->userManager,
$this->rootFolder,
$this->cloudIdManager,
$this->contactsManager,
$this->eventMerger,
])
->setMethods($methods)
@ -91,6 +103,8 @@ class ProviderTest extends TestCase {
$this->activityManager,
$this->userManager,
$this->rootFolder,
$this->cloudIdManager,
$this->contactsManager,
$this->eventMerger
);
}
@ -99,7 +113,7 @@ class ProviderTest extends TestCase {
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
['/Foo/Bar.txt', 128, 128, 'Bar.txt', 'Foo/Bar.txt'], // Legacy from ownCloud 8.2 and before
];
}
@ -137,7 +151,7 @@ class ProviderTest extends TestCase {
$this->assertSame('link-' . $id, $result['link']);
}
public function testGetFileThrows() {
$this->expectException(\InvalidArgumentException::class);
@ -147,71 +161,76 @@ class ProviderTest extends TestCase {
public function dataGetUser() {
return [
['test', [], false, 'Test'],
['foo', ['admin' => 'Admin'], false, 'Bar'],
['admin', ['admin' => 'Administrator'], true, 'Administrator'],
['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 array $cache
* @param bool $cacheHit
* @param string $name
* @param string|null $userDisplayName
* @param array|null $cloudIdData
* @param array $expected
*/
public function testGetUser($uid, $cache, $cacheHit, $name) {
$provider = $this->getProvider(['getDisplayName']);
self::invokePrivate($provider, 'displayNames', [$cache]);
if (!$cacheHit) {
$provider->expects($this->once())
->method('getDisplayName')
->with($uid)
->willReturn($name);
} else {
$provider->expects($this->never())
->method('getDisplayName');
}
$result = self::invokePrivate($provider, 'getUser', [$uid]);
$this->assertSame('user', $result['type']);
$this->assertSame($uid, $result['id']);
$this->assertSame($name, $result['name']);
}
public function dataGetDisplayName() {
return [
['test', true, 'Test'],
['foo', false, 'foo'],
];
}
/**
* @dataProvider dataGetDisplayName
* @param string $uid
* @param string $name
*/
public function testGetDisplayNamer($uid, $validUser, $name) {
public function testGetUser(string $uid, ?string $userDisplayName, ?array $cloudIdData, array $expected): void {
$provider = $this->getProvider();
if ($validUser) {
if ($userDisplayName !== null) {
$user = $this->createMock(IUser::class);
$user->expects($this->once())
->method('getUID')
->willReturn($uid);
$user->expects($this->once())
->method('getDisplayName')
->willReturn($name);
->willReturn($userDisplayName);
$this->userManager->expects($this->once())
->method('get')
->with($uid)
->willReturn($user);
} else {
$this->userManager->expects($this->once())
->method('get')
}
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(null);
->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([]);
}
}
$this->assertSame($name, self::invokePrivate($provider, 'getDisplayName', [$uid]));
$result = self::invokePrivate($provider, 'getUser', [$uid]);
$this->assertEquals($expected, $result);
}
}