adjust tests

Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
This commit is contained in:
Arthur Schiwon 2017-11-03 17:40:05 +01:00
parent 59c05d5447
commit fc6b3902af
No known key found for this signature in database
GPG Key ID: 7424F1874854DF23
5 changed files with 413 additions and 403 deletions

View File

@ -353,8 +353,9 @@ class AccessTest extends TestCase {
$userMock->expects($this->never()) $userMock->expects($this->never())
->method('processAttributes'); ->method('processAttributes');
$this->userManager->expects($this->never()) $this->userManager->expects($this->any())
->method('get'); ->method('get')
->willReturn($this->createMock(User::class));
$this->c->expects($this->any()) $this->c->expects($this->any())
->method('getConfig') ->method('getConfig')
@ -402,10 +403,6 @@ class AccessTest extends TestCase {
->will($this->returnValue($userMock)); ->will($this->returnValue($userMock));
$configMock = $this->createMock(IConfig::class); $configMock = $this->createMock(IConfig::class);
$configMock->expects($this->once())
->method('getAppValue')
->with('core', 'backgroundjobs_mode', $this->anything())
->willReturn('ajax');
$this->c->expects($this->any()) $this->c->expects($this->any())
->method('getConfig') ->method('getConfig')

View File

@ -33,8 +33,11 @@
namespace OCA\User_LDAP\Tests; namespace OCA\User_LDAP\Tests;
use OCP\GroupInterface; use OCP\GroupInterface;
use OCA\User_LDAP\Access;
use OCA\User_LDAP\Connection;
use OCA\User_LDAP\Group_LDAP as GroupLDAP; use OCA\User_LDAP\Group_LDAP as GroupLDAP;
use OCA\User_LDAP\ILDAPWrapper; use OCA\User_LDAP\ILDAPWrapper;
use OCA\User_LDAP\User\Manager;
/** /**
* Class GroupLDAPTest * Class GroupLDAPTest
@ -44,6 +47,9 @@ use OCA\User_LDAP\ILDAPWrapper;
* @package OCA\User_LDAP\Tests * @package OCA\User_LDAP\Tests
*/ */
class Group_LDAPTest extends \Test\TestCase { class Group_LDAPTest extends \Test\TestCase {
/**
* @return \PHPUnit_Framework_MockObject_MockObject|Access
*/
private function getAccessMock() { private function getAccessMock() {
static $conMethods; static $conMethods;
static $accMethods; static $accMethods;
@ -57,14 +63,8 @@ class Group_LDAPTest extends \Test\TestCase {
->setMethods($conMethods) ->setMethods($conMethods)
->setConstructorArgs([$lw, null, null]) ->setConstructorArgs([$lw, null, null])
->getMock(); ->getMock();
$um = $this->getMockBuilder('\OCA\User_LDAP\User\Manager')
->disableOriginalConstructor() $access = $this->createMock(Access::class);
->getMock();
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$access = $this->getMockBuilder('\OCA\User_LDAP\Access')
->setMethods($accMethods)
->setConstructorArgs([$connector, $lw, $um, $helper])
->getMock();
$access->expects($this->any()) $access->expects($this->any())
->method('getConnection') ->method('getConnection')
@ -77,7 +77,12 @@ class Group_LDAPTest extends \Test\TestCase {
return $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')->getMock(); return $this->getMockBuilder('\OCA\User_LDAP\GroupPluginManager')->getMock();
} }
/**
* @param Access|\PHPUnit_Framework_MockObject_MockObject $access
*/
private function enableGroups($access) { private function enableGroups($access) {
$access->connection = $this->createMock(Connection::class);
$access->connection->expects($this->any()) $access->connection->expects($this->any())
->method('__get') ->method('__get')
->will($this->returnCallback(function($name) { ->will($this->returnCallback(function($name) {
@ -498,7 +503,6 @@ class Group_LDAPTest extends \Test\TestCase {
$access->connection->expects($this->any()) $access->connection->expects($this->any())
->method('getFromCache') ->method('getFromCache')
->will($this->returnValue(null)); ->will($this->returnValue(null));
$access->expects($this->any()) $access->expects($this->any())
->method('readAttribute') ->method('readAttribute')
->will($this->returnCallback(function($dn, $attr) { ->will($this->returnCallback(function($dn, $attr) {
@ -509,14 +513,13 @@ class Group_LDAPTest extends \Test\TestCase {
} }
return array(); return array();
})); }));
$access->expects($this->any()) $access->expects($this->any())
->method('groupname2dn') ->method('groupname2dn')
->will($this->returnValue('cn=foobar,dc=foo,dc=bar')); ->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
$access->expects($this->exactly(2)) $access->expects($this->exactly(2))
->method('nextcloudUserNames') ->method('nextcloudUserNames')
->willReturnOnConsecutiveCalls(['lisa', 'bart', 'kira', 'brad'], ['walle', 'dino', 'xenia']); ->willReturnOnConsecutiveCalls(['lisa', 'bart', 'kira', 'brad'], ['walle', 'dino', 'xenia']);
$access->userManager = $this->createMock(Manager::class);
$groupBackend = new GroupLDAP($access, $pluginManager); $groupBackend = new GroupLDAP($access, $pluginManager);
$users = $groupBackend->usersInGroup('foobar'); $users = $groupBackend->usersInGroup('foobar');
@ -537,7 +540,6 @@ class Group_LDAPTest extends \Test\TestCase {
$access->connection->expects($this->any()) $access->connection->expects($this->any())
->method('getFromCache') ->method('getFromCache')
->will($this->returnValue(null)); ->will($this->returnValue(null));
$access->expects($this->any()) $access->expects($this->any())
->method('readAttribute') ->method('readAttribute')
->will($this->returnCallback(function($dn, $attr) { ->will($this->returnCallback(function($dn, $attr) {
@ -546,14 +548,13 @@ class Group_LDAPTest extends \Test\TestCase {
} }
return array(); return array();
})); }));
$access->expects($this->any()) $access->expects($this->any())
->method('groupname2dn') ->method('groupname2dn')
->will($this->returnValue('cn=foobar,dc=foo,dc=bar')); ->will($this->returnValue('cn=foobar,dc=foo,dc=bar'));
$access->expects($this->once()) $access->expects($this->once())
->method('nextcloudUserNames') ->method('nextcloudUserNames')
->will($this->returnValue(array('lisa', 'bart', 'kira', 'brad'))); ->will($this->returnValue(array('lisa', 'bart', 'kira', 'brad')));
$access->userManager = $this->createMock(Manager::class);
$groupBackend = new GroupLDAP($access, $pluginManager); $groupBackend = new GroupLDAP($access, $pluginManager);
$users = $groupBackend->usersInGroup('foobar'); $users = $groupBackend->usersInGroup('foobar');
@ -635,6 +636,7 @@ class Group_LDAPTest extends \Test\TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$pluginManager = $this->getPluginManagerMock(); $pluginManager = $this->getPluginManagerMock();
$access->connection = $this->createMock(Connection::class);
$access->connection->expects($this->any()) $access->connection->expects($this->any())
->method('__get') ->method('__get')
->will($this->returnCallback(function($name) { ->will($this->returnCallback(function($name) {
@ -671,6 +673,7 @@ class Group_LDAPTest extends \Test\TestCase {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$pluginManager = $this->getPluginManagerMock(); $pluginManager = $this->getPluginManagerMock();
$access->connection = $this->createMock(Connection::class);
$access->connection->expects($this->any()) $access->connection->expects($this->any())
->method('__get') ->method('__get')
->will($this->returnCallback(function($name) { ->will($this->returnCallback(function($name) {

File diff suppressed because it is too large Load Diff

View File

@ -81,12 +81,6 @@ class User_LDAPTest extends TestCase {
* @return \PHPUnit_Framework_MockObject_MockObject|Access * @return \PHPUnit_Framework_MockObject_MockObject|Access
*/ */
private function getAccessMock() { private function getAccessMock() {
$lw = $this->createMock(ILDAPWrapper::class);
$connector = $this->getMockBuilder(Connection::class)
->setMethodsExcept(['getConnection'])
->setConstructorArgs([$lw, null, null])
->getMock();
$this->configMock = $this->createMock(IConfig::class); $this->configMock = $this->createMock(IConfig::class);
$this->offlineUser = $this->createMock(OfflineUser::class); $this->offlineUser = $this->createMock(OfflineUser::class);
@ -106,18 +100,16 @@ class User_LDAPTest extends TestCase {
]) ])
->getMock(); ->getMock();
$um->expects($this->any()) /** @var Connection|\PHPUnit_Framework_MockObject_MockObject $connection */
->method('getDeletedUser') $connection = $this->createMock(Connection::class);
->will($this->returnValue($this->offlineUser));
$helper = new Helper(\OC::$server->getConfig()); /** @var Manager|\PHPUnit_Framework_MockObject_MockObject $userManager */
$userManager = $this->createMock(Manager::class);
$access = $this->getMockBuilder(Access::class) /** @var Access|\PHPUnit_Framework_MockObject_MockObject $access */
->setMethodsExcept(['getConnection']) $access = $this->createMock(Access::class);
->setConstructorArgs([$connector, $lw, $um, $helper]) $access->connection = $connection;
->getMock(); $access->userManager = $userManager;
$um->setLdapAccess($access);
return $access; return $access;
} }
@ -211,11 +203,17 @@ class User_LDAPTest extends TestCase {
} }
public function testCheckPasswordUidReturn() { public function testCheckPasswordUidReturn() {
$access = $this->getAccessMock(); $user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('gunslinger');
$access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access); $this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
->willReturn($user);
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = $backend->checkPassword('roland', 'dt19'); $result = $backend->checkPassword('roland', 'dt19');
@ -246,11 +244,12 @@ class User_LDAPTest extends TestCase {
public function testCheckPasswordNoDisplayName() { public function testCheckPasswordNoDisplayName() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access, true); $this->prepareAccessForCheckPassword($access, true);
$access->expects($this->once())
->method('username2dn') $this->prepareAccessForCheckPassword($access);
->will($this->returnValue(false)); $access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn(null);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
@ -260,9 +259,17 @@ class User_LDAPTest extends TestCase {
} }
public function testCheckPasswordPublicAPI() { public function testCheckPasswordPublicAPI() {
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('gunslinger');
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$this->prepareAccessForCheckPassword($access); $this->prepareAccessForCheckPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
->method('get')
->willReturn($user);
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$result = \OCP\User::checkPassword('roland', 'dt19'); $result = \OCP\User::checkPassword('roland', 'dt19');
@ -308,6 +315,9 @@ class User_LDAPTest extends TestCase {
$access->expects($this->once()) $access->expects($this->once())
->method('getUserMapper') ->method('getUserMapper')
->will($this->returnValue($mapping)); ->will($this->returnValue($mapping));
$access->connection->expects($this->any())
->method('getConnectionResource')
->willReturn('this is an ldap link');
$this->configMock->expects($this->any()) $this->configMock->expects($this->any())
->method('getUserValue') ->method('getUserValue')
@ -320,6 +330,9 @@ class User_LDAPTest extends TestCase {
$this->offlineUser->expects($this->once()) $this->offlineUser->expects($this->once())
->method('getOCName') ->method('getOCName')
->willReturn($uid); ->willReturn($uid);
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->offlineUser);
$backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->configMock, $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
@ -516,6 +529,11 @@ class User_LDAPTest extends TestCase {
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$access->expects($this->any()) $access->expects($this->any())
->method('readAttribute') ->method('readAttribute')
->will($this->returnCallback(function($dn) { ->will($this->returnCallback(function($dn) {
@ -524,6 +542,12 @@ class User_LDAPTest extends TestCase {
} }
return false; return false;
})); }));
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$access->expects($this->any())
->method('getUserMapper')
->willReturn($this->createMock(UserMapping::class));
//test for existing user //test for existing user
$result = $backend->userExists('gunslinger'); $result = $backend->userExists('gunslinger');
@ -547,8 +571,13 @@ class User_LDAPTest extends TestCase {
return false; return false;
})); }));
$access->userManager = $this->createMock(Manager::class);
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->createMock(User::class));
//test for deleted user //test for deleted user
$result = $backend->userExists('formerUser'); $backend->userExists('formerUser');
} }
public function testUserExistsForNeverExisting() { public function testUserExistsForNeverExisting() {
@ -576,6 +605,11 @@ class User_LDAPTest extends TestCase {
$this->prepareMockForUserExists($access); $this->prepareMockForUserExists($access);
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$access->expects($this->any()) $access->expects($this->any())
->method('readAttribute') ->method('readAttribute')
->will($this->returnCallback(function($dn) { ->will($this->returnCallback(function($dn) {
@ -584,6 +618,12 @@ class User_LDAPTest extends TestCase {
} }
return false; return false;
})); }));
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$access->expects($this->any())
->method('getUserMapper')
->willReturn($this->createMock(UserMapping::class));
//test for existing user //test for existing user
$result = \OCP\User::userExists('gunslinger'); $result = \OCP\User::userExists('gunslinger');
@ -607,9 +647,13 @@ class User_LDAPTest extends TestCase {
} }
return false; return false;
})); }));
$access->userManager = $this->createMock(Manager::class);
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->createMock(User::class));
//test for deleted user //test for deleted user
$result = \OCP\User::userExists('formerUser'); \OCP\User::userExists('formerUser');
} }
public function testUserExistsPublicAPIForNeverExisting() { public function testUserExistsPublicAPIForNeverExisting() {
@ -672,7 +716,22 @@ class User_LDAPTest extends TestCase {
} }
})); }));
//absolut path $user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('gunslinger');
$user->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$user->expects($this->any())
->method('getHomePath')
->willReturn('/tmp/rolandshome/');
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
//absolute path
$result = $backend->getHome('gunslinger'); $result = $backend->getHome('gunslinger');
$this->assertEquals('/tmp/rolandshome/', $result); $this->assertEquals('/tmp/rolandshome/', $result);
} }
@ -687,10 +746,6 @@ class User_LDAPTest extends TestCase {
$dataDir = \OC::$server->getConfig()->getSystemValue( $dataDir = \OC::$server->getConfig()->getSystemValue(
'datadirectory', \OC::$SERVERROOT.'/data'); 'datadirectory', \OC::$SERVERROOT.'/data');
$this->configMock->expects($this->once())
->method('getSystemValue')
->will($this->returnValue($dataDir));
$access->connection->expects($this->any()) $access->connection->expects($this->any())
->method('__get') ->method('__get')
->will($this->returnCallback(function($name) { ->will($this->returnCallback(function($name) {
@ -715,6 +770,21 @@ class User_LDAPTest extends TestCase {
} }
})); }));
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('ladyofshadows');
$user->expects($this->any())
->method('getDN')
->willReturn('dnOfLadyOfShadows,dc=test');
$user->expects($this->any())
->method('getHomePath')
->willReturn($dataDir.'/susannah/');
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
$result = $backend->getHome('ladyofshadows'); $result = $backend->getHome('ladyofshadows');
$this->assertEquals($dataDir.'/susannah/', $result); $this->assertEquals($dataDir.'/susannah/', $result);
} }
@ -745,6 +815,18 @@ class User_LDAPTest extends TestCase {
} }
})); }));
$user = $this->createMock(User::class);
$user->expects($this->any())
->method('getUsername')
->willReturn('newyorker');
$user->expects($this->any())
->method('getHomePath')
->willThrowException(new \Exception());
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($user);
//no path at all triggers OC default behaviour //no path at all triggers OC default behaviour
$result = $backend->getHome('newyorker'); $result = $backend->getHome('newyorker');
$this->assertFalse($result); $this->assertFalse($result);
@ -786,6 +868,10 @@ class User_LDAPTest extends TestCase {
$this->offlineUser->expects($this->never()) $this->offlineUser->expects($this->never())
->method('getHomePath'); ->method('getHomePath');
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->offlineUser);
$backend->getHome($uid); $backend->getHome($uid);
} }
@ -852,14 +938,6 @@ class User_LDAPTest extends TestCase {
} }
})); }));
$userMapper = $this->getMockBuilder('\OCA\User_LDAP\Mapping\UserMapping')
->disableOriginalConstructor()
->getMock();
$access->expects($this->any())
->method('getUserMapper')
->will($this->returnValue($userMapper));
$access->method('fetchUsersByLoginName') $access->method('fetchUsersByLoginName')
->willReturn([]); ->willReturn([]);
} }
@ -876,6 +954,42 @@ class User_LDAPTest extends TestCase {
return true; return true;
})); }));
$user1 = $this->createMock(User::class);
$user1->expects($this->once())
->method('composeAndStoreDisplayName')
->willReturn('Roland Deschain');
$user1->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$user2 = $this->createMock(User::class);
$user2->expects($this->never())
->method('composeAndStoreDisplayName');
$user2->expects($this->any())
->method('getDN')
->willReturn('another DN');
$mapper = $this->createMock(UserMapping::class);
$mapper->expects($this->any())
->method('getUUIDByDN')
->willReturnCallback(function($dn) { return $dn; });
$access->userManager->expects($this->any())
->method('get')
->willReturnCallback(function($uid) use ($user1, $user2) {
if($uid === 'gunslinger') {
return $user1;
} else if($uid === 'newyorker') {
return $user2;
}
});
$access->expects($this->any())
->method('getUserMapper')
->willReturn($mapper);
$access->expects($this->any())
->method('getUserDnByUuid')
->willReturnCallback(function($uuid) { return $uuid . '1'; });
//with displayName //with displayName
$result = $backend->getDisplayName('gunslinger'); $result = $backend->getDisplayName('gunslinger');
$this->assertEquals('Roland Deschain', $result); $this->assertEquals('Roland Deschain', $result);
@ -919,6 +1033,42 @@ class User_LDAPTest extends TestCase {
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$user1 = $this->createMock(User::class);
$user1->expects($this->once())
->method('composeAndStoreDisplayName')
->willReturn('Roland Deschain');
$user1->expects($this->any())
->method('getDN')
->willReturn('dnOfRoland,dc=test');
$user2 = $this->createMock(User::class);
$user2->expects($this->never())
->method('composeAndStoreDisplayName');
$user2->expects($this->any())
->method('getDN')
->willReturn('another DN');
$mapper = $this->createMock(UserMapping::class);
$mapper->expects($this->any())
->method('getUUIDByDN')
->willReturnCallback(function($dn) { return $dn; });
$access->userManager->expects($this->any())
->method('get')
->willReturnCallback(function($uid) use ($user1, $user2) {
if($uid === 'gunslinger') {
return $user1;
} else if($uid === 'newyorker') {
return $user2;
}
});
$access->expects($this->any())
->method('getUserMapper')
->willReturn($mapper);
$access->expects($this->any())
->method('getUserDnByUuid')
->willReturnCallback(function($uuid) { return $uuid . '1'; });
//with displayName //with displayName
$result = \OCP\User::getDisplayName('gunslinger'); $result = \OCP\User::getDisplayName('gunslinger');
$this->assertEquals('Roland Deschain', $result); $this->assertEquals('Roland Deschain', $result);
@ -1028,11 +1178,11 @@ class User_LDAPTest extends TestCase {
->method('fetchUsersByLoginName') ->method('fetchUsersByLoginName')
->with($this->equalTo($loginName)) ->with($this->equalTo($loginName))
->willReturn([['dn' => [$dn]]]); ->willReturn([['dn' => [$dn]]]);
$access->expects($this->once()) $access->expects($this->any())
->method('stringResemblesDN') ->method('stringResemblesDN')
->with($this->equalTo($dn)) ->with($this->equalTo($dn))
->willReturn(true); ->willReturn(true);
$access->expects($this->once()) $access->expects($this->any())
->method('dn2username') ->method('dn2username')
->with($this->equalTo($dn)) ->with($this->equalTo($dn))
->willReturn($username); ->willReturn($username);
@ -1046,6 +1196,15 @@ class User_LDAPTest extends TestCase {
->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username)); ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username));
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
$user->expects($this->any())
->method('getUsername')
->willReturn('alice');
$access->userManager->expects($this->atLeastOnce())
->method('get')
->with($dn)
->willReturn($user);
$name = $backend->loginName2UserName($loginName); $name = $backend->loginName2UserName($loginName);
$this->assertSame($username, $name); $this->assertSame($username, $name);
@ -1084,7 +1243,6 @@ class User_LDAPTest extends TestCase {
public function testLoginName2UserNameOfflineUser() { public function testLoginName2UserNameOfflineUser() {
$loginName = 'Alice'; $loginName = 'Alice';
$username = 'alice';
$dn = 'uid=alice,dc=what,dc=ever'; $dn = 'uid=alice,dc=what,dc=ever';
$offlineUser = $this->getMockBuilder(OfflineUser::class) $offlineUser = $this->getMockBuilder(OfflineUser::class)
@ -1096,13 +1254,6 @@ class User_LDAPTest extends TestCase {
->method('fetchUsersByLoginName') ->method('fetchUsersByLoginName')
->with($this->equalTo($loginName)) ->with($this->equalTo($loginName))
->willReturn([['dn' => [$dn]]]); ->willReturn([['dn' => [$dn]]]);
$access->expects($this->once())
->method('stringResemblesDN')
->with($this->equalTo($dn))
->willReturn(true);
$access->expects($this->once())
->method('dn2username')
->willReturn(false); // this is fake, but allows us to force-enter the OfflineUser path
$access->connection->expects($this->exactly(2)) $access->connection->expects($this->exactly(2))
->method('getFromCache') ->method('getFromCache')
@ -1112,14 +1263,10 @@ class User_LDAPTest extends TestCase {
->method('writeToCache') ->method('writeToCache')
->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo(false)); ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo(false));
$access->userManager->expects($this->once()) $access->userManager->expects($this->any())
->method('getDeletedUser') ->method('get')
->will($this->returnValue($offlineUser)); ->with($dn)
->willReturn($offlineUser);
//$config = $this->createMock(IConfig::class);
$this->configMock->expects($this->once())
->method('getUserValue')
->willReturn(1);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
$name = $backend->loginName2UserName($loginName); $name = $backend->loginName2UserName($loginName);
@ -1192,6 +1339,10 @@ class User_LDAPTest extends TestCase {
} }
return true; return true;
})); }));
$access->userManager->expects($this->atLeastOnce())
->method('get')
->willReturn($this->createMock(User::class));
} }
/** /**
@ -1213,6 +1364,10 @@ class User_LDAPTest extends TestCase {
$this->prepareAccessForSetPassword($access); $this->prepareAccessForSetPassword($access);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());
$access->userManager->expects($this->any())
->method('get')
->willReturn($this->createMock(User::class));
\OC_User::useBackend($backend); \OC_User::useBackend($backend);
$this->assertTrue(\OC_User::setPassword('roland', 'dt12234$')); $this->assertTrue(\OC_User::setPassword('roland', 'dt12234$'));
@ -1220,6 +1375,9 @@ class User_LDAPTest extends TestCase {
public function testSetPasswordValidDisabled() { public function testSetPasswordValidDisabled() {
$access = $this->getAccessMock(); $access = $this->getAccessMock();
$access->userManager->expects($this->any())
->method('get')
->willReturn($this->createMock(User::class));
$this->prepareAccessForSetPassword($access, false); $this->prepareAccessForSetPassword($access, false);
$backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock()); $backend = new UserLDAP($access, $this->createMock(IConfig::class), $this->createMock(INotificationManager::class), $this->createMock(Session::class), $this->getDefaultPluginManagerMock());

View File

@ -28,8 +28,11 @@
namespace OCA\User_LDAP\Tests; namespace OCA\User_LDAP\Tests;
use OCA\User_LDAP\Access;
use OCA\User_LDAP\Configuration;
use OCA\User_LDAP\ILDAPWrapper; use OCA\User_LDAP\ILDAPWrapper;
use \OCA\User_LDAP\Wizard; use \OCA\User_LDAP\Wizard;
use Test\TestCase;
/** /**
* Class Test_Wizard * Class Test_Wizard
@ -38,7 +41,7 @@ use \OCA\User_LDAP\Wizard;
* *
* @package OCA\User_LDAP\Tests * @package OCA\User_LDAP\Tests
*/ */
class WizardTest extends \Test\TestCase { class WizardTest extends TestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
//we need to make sure the consts are defined, otherwise tests will fail //we need to make sure the consts are defined, otherwise tests will fail
@ -62,30 +65,22 @@ class WizardTest extends \Test\TestCase {
$connMethods = get_class_methods('\OCA\User_LDAP\Connection'); $connMethods = get_class_methods('\OCA\User_LDAP\Connection');
$accMethods = get_class_methods('\OCA\User_LDAP\Access'); $accMethods = get_class_methods('\OCA\User_LDAP\Access');
} }
/** @var ILDAPWrapper|\PHPUnit_Framework_MockObject_MockObject $lw */
$lw = $this->createMock(ILDAPWrapper::class); $lw = $this->createMock(ILDAPWrapper::class);
$conf = $this->getMockBuilder('\OCA\User_LDAP\Configuration')
/** @var Configuration|\PHPUnit_Framework_MockObject_MockObject $conf */
$conf = $this->getMockBuilder(Configuration::class)
->setMethods($confMethods) ->setMethods($confMethods)
->setConstructorArgs([$lw, null, null]) ->setConstructorArgs([$lw, null, null])
->getMock(); ->getMock();
$connector = $this->getMockBuilder('\OCA\User_LDAP\Connection') /** @var Access|\PHPUnit_Framework_MockObject_MockObject $access */
->setMethods($connMethods) $access = $this->createMock(Access::class);
->setConstructorArgs([$lw, null, null])
->getMock();
$um = $this->getMockBuilder('\OCA\User_LDAP\User\Manager')
->disableOriginalConstructor()
->getMock();
$helper = new \OCA\User_LDAP\Helper(\OC::$server->getConfig());
$access = $this->getMockBuilder('\OCA\User_LDAP\Access')
->setMethods($accMethods)
->setConstructorArgs([$connector, $lw, $um, $helper])
->getMock();
return array(new Wizard($conf, $lw, $access), $conf, $lw, $access); return array(new Wizard($conf, $lw, $access), $conf, $lw, $access);
} }
private function prepareLdapWrapperForConnections(&$ldap) { private function prepareLdapWrapperForConnections(\PHPUnit_Framework_MockObject_MockObject &$ldap) {
$ldap->expects($this->once()) $ldap->expects($this->once())
->method('connect') ->method('connect')
//dummy value, usually invalid //dummy value, usually invalid