2013-09-11 20:51:45 +04:00
< ? php
/**
2016-07-21 17:49:16 +03:00
* @ copyright Copyright ( c ) 2016 , ownCloud , Inc .
*
2016-05-26 20:56:05 +03:00
* @ author Arthur Schiwon < blizzz @ arthur - schiwon . de >
2016-07-21 17:49:16 +03:00
* @ author Joas Schilling < coding @ schilljs . com >
2015-06-25 12:43:55 +03:00
* @ author Jörn Friedrich Dreyer < jfd @ butonic . de >
2016-05-26 20:56:05 +03:00
* @ author Lukas Reschke < lukas @ statuscode . ch >
2015-03-26 13:44:34 +03:00
* @ author Morris Jobke < hey @ morrisjobke . de >
2016-01-12 17:02:16 +03:00
* @ author Robin McCorkell < robin @ mccorkell . me . uk >
2017-11-06 17:56:42 +03:00
* @ author Roeland Jago Douma < roeland @ famdouma . nl >
2016-08-30 12:43:29 +03:00
* @ author Roger Szabo < roger . szabo @ web . de >
2017-11-06 17:56:42 +03:00
* @ author Thomas Müller < thomas . mueller @ tmit . eu >
* @ author Vinicius Cubas Brand < vinicius @ eita . org . br >
2015-03-26 13:44:34 +03:00
*
* @ license AGPL - 3.0
*
* This code is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License , version 3 ,
* as published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License , version 3 ,
2019-12-03 21:57:53 +03:00
* along with this program . If not , see < http :// www . gnu . org / licenses />
2015-03-26 13:44:34 +03:00
*
*/
2015-02-26 13:37:37 +03:00
2016-05-12 12:01:29 +03:00
namespace OCA\User_LDAP\Tests ;
2013-09-11 20:51:45 +04:00
2019-11-22 22:52:10 +03:00
use OC\HintException ;
2017-11-02 15:40:38 +03:00
use OC\User\Backend ;
2017-09-15 17:01:54 +03:00
use OC\User\Session ;
2016-10-06 23:52:45 +03:00
use OCA\User_LDAP\Access ;
use OCA\User_LDAP\Connection ;
2019-06-14 18:20:42 +03:00
use OCA\User_LDAP\Mapping\AbstractMapping ;
2017-07-13 15:32:52 +03:00
use OCA\User_LDAP\Mapping\UserMapping ;
2016-10-06 23:52:45 +03:00
use OCA\User_LDAP\User\Manager ;
2016-10-07 02:39:57 +03:00
use OCA\User_LDAP\User\OfflineUser ;
2016-11-23 21:58:43 +03:00
use OCA\User_LDAP\User\User ;
use OCA\User_LDAP\User_LDAP ;
2019-11-22 22:52:10 +03:00
use OCA\User_LDAP\User_LDAP as UserLDAP ;
2018-07-02 18:38:16 +03:00
use OCA\User_LDAP\UserPluginManager ;
2016-09-02 12:02:12 +03:00
use OCP\IConfig ;
2017-09-15 17:01:54 +03:00
use OCP\IUser ;
2017-03-31 10:16:22 +03:00
use OCP\Notification\IManager as INotificationManager ;
2019-11-22 22:52:10 +03:00
use Test\TestCase ;
2013-09-11 20:51:45 +04:00
2015-11-25 18:58:54 +03:00
/**
* Class Test_User_Ldap_Direct
*
* @ group DB
*
2016-05-12 12:01:29 +03:00
* @ package OCA\User_LDAP\Tests
2015-11-25 18:58:54 +03:00
*/
2016-10-06 23:52:45 +03:00
class User_LDAPTest extends TestCase {
2018-07-02 18:38:16 +03:00
/** @var User_LDAP */
2013-09-11 20:51:45 +04:00
protected $backend ;
2018-07-02 18:38:16 +03:00
/** @var Access|\PHPUnit_Framework_MockObject_MockObject */
2014-04-01 14:28:23 +04:00
protected $access ;
2017-07-13 15:32:52 +03:00
/** @var OfflineUser|\PHPUnit_Framework_MockObject_MockObject */
protected $offlineUser ;
2018-07-02 18:38:16 +03:00
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
protected $config ;
/** @var INotificationManager|\PHPUnit_Framework_MockObject_MockObject */
protected $notificationManager ;
/** @var Session|\PHPUnit_Framework_MockObject_MockObject */
protected $session ;
/** @var UserPluginManager|\PHPUnit_Framework_MockObject_MockObject */
protected $pluginManager ;
/** @var Connection|\PHPUnit_Framework_MockObject_MockObject */
protected $connection ;
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject */
protected $userManager ;
2013-09-11 20:51:45 +04:00
2019-11-21 18:40:38 +03:00
protected function setUp () : void {
2014-11-11 00:28:12 +03:00
parent :: setUp ();
2013-09-11 20:51:45 +04:00
\OC_User :: clearBackends ();
2017-03-03 10:24:27 +03:00
\OC :: $server -> getGroupManager () -> clearBackends ();
2014-04-01 14:28:23 +04:00
2018-07-02 18:38:16 +03:00
$this -> connection = $this -> createMock ( Connection :: class );
$this -> userManager = $this -> createMock ( Manager :: class );
$this -> access = $this -> createMock ( Access :: class );
$this -> access -> connection = $this -> connection ;
$this -> access -> userManager = $this -> userManager ;
$this -> config = $this -> createMock ( IConfig :: class );
$this -> notificationManager = $this -> createMock ( INotificationManager :: class );
// Cannot use IUserSession because of private listen() methods
$this -> session = $this -> createMock ( Session :: class );
$this -> pluginManager = $this -> createMock ( UserPluginManager :: class );
$this -> backend = new User_LDAP (
$this -> access ,
$this -> config ,
$this -> notificationManager ,
$this -> session ,
$this -> pluginManager
);
2017-11-02 15:40:38 +03:00
}
2018-07-02 18:38:16 +03:00
private function prepareMockForUserExists () {
$this -> access -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( 'username2dn' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $uid ) {
2013-09-11 20:51:45 +04:00
switch ( $uid ) {
case 'gunslinger' :
2014-04-01 14:28:23 +04:00
return 'dnOfRoland,dc=test' ;
2013-09-11 20:51:45 +04:00
break ;
case 'formerUser' :
2014-04-01 14:28:23 +04:00
return 'dnOfFormerUser,dc=test' ;
2013-09-11 20:51:45 +04:00
break ;
case 'newyorker' :
2014-04-01 14:28:23 +04:00
return 'dnOfNewYorker,dc=test' ;
2013-09-11 20:51:45 +04:00
break ;
case 'ladyofshadows' :
2014-04-01 14:28:23 +04:00
return 'dnOfLadyOfShadows,dc=test' ;
2013-09-11 20:51:45 +04:00
break ;
2014-05-11 17:24:42 +04:00
default :
2013-09-11 20:51:45 +04:00
return false ;
}
2020-03-26 00:21:27 +03:00
});
2017-08-09 17:43:40 +03:00
2018-07-02 18:38:16 +03:00
$this -> access -> method ( 'fetchUsersByLoginName' )
2017-08-09 17:43:40 +03:00
-> willReturn ([]);
2013-09-11 20:51:45 +04:00
}
2013-09-26 00:14:02 +04:00
/**
2014-05-19 19:50:53 +04:00
* Prepares the Access mock for checkPassword tests
2016-05-12 17:42:57 +03:00
* @ param bool $noDisplayName
2013-09-26 00:14:02 +04:00
* @ return void
*/
2018-07-02 18:38:16 +03:00
private function prepareAccessForCheckPassword ( $noDisplayName = false ) {
$this -> connection -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( '__get' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $name ) {
2013-09-11 20:51:45 +04:00
if ( $name === 'ldapLoginFilter' ) {
return '%uid' ;
}
return null ;
2020-03-26 00:21:27 +03:00
});
2013-09-11 20:51:45 +04:00
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( 'fetchListOfUsers' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $filter ) {
2013-09-11 20:51:45 +04:00
if ( $filter === 'roland' ) {
2015-10-09 15:30:49 +03:00
return array ( array ( 'dn' => [ 'dnOfRoland,dc=test' ]));
2013-09-11 20:51:45 +04:00
}
return array ();
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-04-09 13:00:11 +03:00
-> method ( 'fetchUsersByLoginName' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $uid ) {
2015-04-09 13:00:11 +03:00
if ( $uid === 'roland' ) {
2015-10-09 15:30:49 +03:00
return array ( array ( 'dn' => [ 'dnOfRoland,dc=test' ]));
2015-04-09 13:00:11 +03:00
}
return array ();
2020-03-26 00:21:27 +03:00
});
2015-04-09 13:00:11 +03:00
2014-10-27 17:58:23 +03:00
$retVal = 'gunslinger' ;
if ( $noDisplayName === true ) {
$retVal = false ;
}
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( 'dn2username' )
2014-04-01 14:28:23 +04:00
-> with ( $this -> equalTo ( 'dnOfRoland,dc=test' ))
2020-03-26 00:21:27 +03:00
-> willReturn ( $retVal );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2014-08-12 18:13:17 +04:00
-> method ( 'stringResemblesDN' )
-> with ( $this -> equalTo ( 'dnOfRoland,dc=test' ))
2020-03-26 00:21:27 +03:00
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( 'areCredentialsValid' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $dn , $pwd ) {
2013-09-11 20:51:45 +04:00
if ( $pwd === 'dt19' ) {
return true ;
}
return false ;
2020-03-26 00:21:27 +03:00
});
2013-09-26 00:14:02 +04:00
}
2014-02-20 17:05:45 +04:00
public function testCheckPasswordUidReturn () {
2017-11-03 19:40:05 +03:00
$user = $this -> createMock ( User :: class );
$user -> expects ( $this -> any ())
-> method ( 'getUsername' )
-> willReturn ( 'gunslinger' );
2014-02-20 17:05:45 +04:00
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForCheckPassword ();
$this -> userManager -> expects ( $this -> any ())
2017-11-07 15:19:08 +03:00
-> method ( 'get' )
-> willReturn ( $user );
2017-11-02 15:40:38 +03:00
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2017-11-03 19:40:05 +03:00
2013-09-26 00:14:02 +04:00
\OC_User :: useBackend ( $backend );
2013-09-11 20:51:45 +04:00
$result = $backend -> checkPassword ( 'roland' , 'dt19' );
$this -> assertEquals ( 'gunslinger' , $result );
2014-02-20 17:05:45 +04:00
}
public function testCheckPasswordWrongPassword () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForCheckPassword ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-02-20 17:05:45 +04:00
\OC_User :: useBackend ( $backend );
2013-09-11 20:51:45 +04:00
$result = $backend -> checkPassword ( 'roland' , 'wrong' );
$this -> assertFalse ( $result );
2014-02-20 17:05:45 +04:00
}
public function testCheckPasswordWrongUser () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForCheckPassword ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-02-20 17:05:45 +04:00
\OC_User :: useBackend ( $backend );
2013-09-11 20:51:45 +04:00
$result = $backend -> checkPassword ( 'mallory' , 'evil' );
$this -> assertFalse ( $result );
}
2014-10-27 17:58:23 +03:00
public function testCheckPasswordNoDisplayName () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForCheckPassword ( true );
2017-11-03 19:40:05 +03:00
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForCheckPassword ();
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( null );
2014-10-27 17:58:23 +03:00
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-10-27 17:58:23 +03:00
\OC_User :: useBackend ( $backend );
$result = $backend -> checkPassword ( 'roland' , 'dt19' );
$this -> assertFalse ( $result );
}
2013-09-26 00:14:02 +04:00
public function testCheckPasswordPublicAPI () {
2017-11-03 19:40:05 +03:00
$user = $this -> createMock ( User :: class );
$user -> expects ( $this -> any ())
-> method ( 'getUsername' )
-> willReturn ( 'gunslinger' );
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForCheckPassword ();
$this -> userManager -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( $user );
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2013-09-26 00:14:02 +04:00
\OC_User :: useBackend ( $backend );
2013-09-11 20:51:45 +04:00
2018-01-16 15:27:45 +03:00
$user = \OC :: $server -> getUserManager () -> checkPassword ( 'roland' , 'dt19' );
$result = false ;
if ( $user !== false ) {
$result = $user -> getUID ();
}
2013-09-26 00:14:02 +04:00
$this -> assertEquals ( 'gunslinger' , $result );
2014-02-20 17:05:45 +04:00
}
public function testCheckPasswordPublicAPIWrongPassword () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForCheckPassword ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-02-20 17:05:45 +04:00
\OC_User :: useBackend ( $backend );
2013-09-26 00:14:02 +04:00
2018-01-16 15:27:45 +03:00
$user = \OC :: $server -> getUserManager () -> checkPassword ( 'roland' , 'wrong' );
$result = false ;
if ( $user !== false ) {
$result = $user -> getUID ();
}
2013-09-26 00:14:02 +04:00
$this -> assertFalse ( $result );
2014-02-20 17:05:45 +04:00
}
public function testCheckPasswordPublicAPIWrongUser () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForCheckPassword ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-02-20 17:05:45 +04:00
\OC_User :: useBackend ( $backend );
2013-09-26 00:14:02 +04:00
2018-01-16 15:27:45 +03:00
$user = \OC :: $server -> getUserManager () -> checkPassword ( 'mallory' , 'evil' );
$result = false ;
if ( $user !== false ) {
$result = $user -> getUID ();
}
2013-09-26 00:14:02 +04:00
$this -> assertFalse ( $result );
}
2014-08-21 19:59:13 +04:00
public function testDeleteUserCancel () {
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-08-21 19:59:13 +04:00
$result = $backend -> deleteUser ( 'notme' );
$this -> assertFalse ( $result );
}
public function testDeleteUserSuccess () {
2017-07-13 15:32:52 +03:00
$uid = 'jeremy' ;
$home = '/var/vhome/jdings/' ;
$mapping = $this -> createMock ( UserMapping :: class );
2014-12-20 18:09:04 +03:00
$mapping -> expects ( $this -> once ())
-> method ( 'unmap' )
2020-03-26 00:21:27 +03:00
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> once ())
2014-12-20 18:09:04 +03:00
-> method ( 'getUserMapper' )
2020-03-26 00:21:27 +03:00
-> willReturn ( $mapping );
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'getConnectionResource' )
-> willReturn ( 'this is an ldap link' );
2014-12-20 18:09:04 +03:00
2018-07-02 18:38:16 +03:00
$this -> config -> expects ( $this -> any ())
2015-01-07 01:28:49 +03:00
-> method ( 'getUserValue' )
2017-07-13 15:32:52 +03:00
-> with ( $uid , 'user_ldap' , 'isDeleted' )
-> willReturn ( '1' );
2014-08-21 19:59:13 +04:00
2018-07-02 18:38:16 +03:00
$offlineUser = $this -> createMock ( OfflineUser :: class );
$offlineUser -> expects ( $this -> once ())
2017-07-13 15:32:52 +03:00
-> method ( 'getHomePath' )
-> willReturn ( $home );
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
2018-07-02 18:38:16 +03:00
-> willReturn ( $offlineUser );
2014-08-21 19:59:13 +04:00
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2017-09-15 17:01:54 +03:00
2017-07-13 15:32:52 +03:00
$result = $backend -> deleteUser ( $uid );
$this -> assertTrue ( $result );
2018-07-02 18:38:16 +03:00
/** @noinspection PhpUnhandledExceptionInspection */
2017-07-13 15:32:52 +03:00
$this -> assertSame ( $backend -> getHome ( $uid ), $home );
2014-08-21 19:59:13 +04:00
}
2017-11-02 15:40:38 +03:00
public function testDeleteUserWithPlugin () {
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'canDeleteUser' )
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'deleteUser' )
-> with ( 'uid' )
2019-06-26 15:18:28 +03:00
-> willReturn ( true );
$this -> config -> expects ( $this -> once ())
-> method ( 'getUserValue' )
-> with ( 'uid' , 'user_ldap' , 'isDeleted' , 0 )
-> willReturn ( 1 );
$mapper = $this -> createMock ( UserMapping :: class );
$mapper -> expects ( $this -> once ())
-> method ( 'unmap' )
-> with ( 'uid' );
$this -> access -> expects ( $this -> atLeastOnce ())
-> method ( 'getUserMapper' )
-> willReturn ( $mapper );
$this -> userManager -> expects ( $this -> once ())
-> method ( 'invalidate' )
-> with ( 'uid' );
2017-11-02 15:40:38 +03:00
2019-06-26 15:18:28 +03:00
$this -> assertEquals ( true , $this -> backend -> deleteUser ( 'uid' ));
2017-11-02 15:40:38 +03:00
}
2013-09-26 00:14:02 +04:00
/**
2014-05-19 19:50:53 +04:00
* Prepares the Access mock for getUsers tests
2013-09-26 00:14:02 +04:00
*/
2018-07-02 18:38:16 +03:00
private function prepareAccessForGetUsers () {
$this -> access -> expects ( $this -> once ())
2014-02-20 17:05:45 +04:00
-> method ( 'escapeFilterPart' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $search ) {
2014-02-20 17:05:45 +04:00
return $search ;
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( 'getFilterPartForUserSearch' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $search ) {
2013-09-11 20:51:45 +04:00
return $search ;
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( 'combineFilterWithAnd' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $param ) {
2015-11-28 01:46:26 +03:00
return $param [ 2 ];
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( 'fetchListOfUsers' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $search , $a , $l , $o ) {
2013-09-11 20:51:45 +04:00
$users = array ( 'gunslinger' , 'newyorker' , 'ladyofshadows' );
if ( empty ( $search )) {
$result = $users ;
} else {
$result = array ();
foreach ( $users as $user ) {
if ( stripos ( $user , $search ) !== false ) {
$result [] = $user ;
}
}
}
if ( ! is_null ( $l ) || ! is_null ( $o )) {
$result = array_slice ( $result , $o , $l );
}
return $result ;
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2017-04-12 07:21:04 +03:00
-> method ( 'nextcloudUserNames' )
2020-03-26 00:21:27 +03:00
-> willReturnArgument ( 0 );
2018-07-02 18:38:16 +03:00
$this -> access -> method ( 'fetchUsersByLoginName' )
2017-08-09 17:43:40 +03:00
-> willReturn ([]);
2013-09-26 00:14:02 +04:00
}
2014-02-20 17:05:45 +04:00
public function testGetUsersNoParam () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2013-09-11 20:51:45 +04:00
$result = $backend -> getUsers ();
$this -> assertEquals ( 3 , count ( $result ));
2014-02-20 17:05:45 +04:00
}
public function testGetUsersLimitOffset () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2013-09-11 20:51:45 +04:00
$result = $backend -> getUsers ( '' , 1 , 2 );
$this -> assertEquals ( 1 , count ( $result ));
2014-02-20 17:05:45 +04:00
}
public function testGetUsersLimitOffset2 () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2013-09-11 20:51:45 +04:00
$result = $backend -> getUsers ( '' , 2 , 1 );
$this -> assertEquals ( 2 , count ( $result ));
2014-02-20 17:05:45 +04:00
}
public function testGetUsersSearchWithResult () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2013-09-11 20:51:45 +04:00
$result = $backend -> getUsers ( 'yo' );
$this -> assertEquals ( 2 , count ( $result ));
2014-02-20 17:05:45 +04:00
}
public function testGetUsersSearchEmptyResult () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2013-09-11 20:51:45 +04:00
$result = $backend -> getUsers ( 'nix' );
$this -> assertEquals ( 0 , count ( $result ));
}
2018-03-25 21:33:24 +03:00
private function getUsers ( $search = '' , $limit = null , $offset = null ) {
$users = \OC :: $server -> getUserManager () -> search ( $search , $limit , $offset );
2018-03-25 21:42:38 +03:00
$uids = array_map ( function ( IUser $user ) {
return $user -> getUID ();
}, $users );
2018-03-25 21:33:24 +03:00
return $uids ;
}
2014-02-20 17:05:45 +04:00
public function testGetUsersViaAPINoParam () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2013-09-26 00:14:02 +04:00
\OC_User :: useBackend ( $backend );
2018-03-25 21:33:24 +03:00
$result = $this -> getUsers ();
2013-09-26 00:14:02 +04:00
$this -> assertEquals ( 3 , count ( $result ));
2014-02-20 17:05:45 +04:00
}
public function testGetUsersViaAPILimitOffset () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-02-20 17:05:45 +04:00
\OC_User :: useBackend ( $backend );
2013-09-26 00:14:02 +04:00
2018-03-25 21:33:24 +03:00
$result = $this -> getUsers ( '' , 1 , 2 );
2013-09-26 00:14:02 +04:00
$this -> assertEquals ( 1 , count ( $result ));
2014-02-20 17:05:45 +04:00
}
public function testGetUsersViaAPILimitOffset2 () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-02-20 17:05:45 +04:00
\OC_User :: useBackend ( $backend );
2013-09-26 00:14:02 +04:00
2018-03-25 21:33:24 +03:00
$result = $this -> getUsers ( '' , 2 , 1 );
2013-09-26 00:14:02 +04:00
$this -> assertEquals ( 2 , count ( $result ));
2014-02-20 17:05:45 +04:00
}
public function testGetUsersViaAPISearchWithResult () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-02-20 17:05:45 +04:00
\OC_User :: useBackend ( $backend );
2013-09-26 00:14:02 +04:00
2018-03-25 21:33:24 +03:00
$result = $this -> getUsers ( 'yo' );
2013-09-26 00:14:02 +04:00
$this -> assertEquals ( 2 , count ( $result ));
2014-02-20 17:05:45 +04:00
}
public function testGetUsersViaAPISearchEmptyResult () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetUsers ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-02-20 17:05:45 +04:00
\OC_User :: useBackend ( $backend );
2013-09-26 00:14:02 +04:00
2018-03-25 21:33:24 +03:00
$result = $this -> getUsers ( 'nix' );
2013-09-26 00:14:02 +04:00
$this -> assertEquals ( 0 , count ( $result ));
}
2013-09-11 20:51:45 +04:00
public function testUserExists () {
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2013-09-11 20:51:45 +04:00
2017-11-03 19:40:05 +03:00
$user = $this -> createMock ( User :: class );
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( $user );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'getUserMapper' )
-> willReturn ( $this -> createMock ( UserMapping :: class ));
2013-09-11 20:51:45 +04:00
//test for existing user
2018-07-02 18:38:16 +03:00
/** @noinspection PhpUnhandledExceptionInspection */
2013-09-11 20:51:45 +04:00
$result = $backend -> userExists ( 'gunslinger' );
$this -> assertTrue ( $result );
2015-03-31 16:33:44 +03:00
}
public function testUserExistsForDeleted () {
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2015-03-31 16:33:44 +03:00
2018-08-15 17:11:07 +03:00
$mapper = $this -> createMock ( UserMapping :: class );
$mapper -> expects ( $this -> any ())
-> method ( 'getUUIDByDN' )
-> with ( 'dnOfFormerUser,dc=test' )
-> willReturn ( '45673458748' );
$this -> access -> expects ( $this -> any ())
-> method ( 'getUserMapper' )
-> willReturn ( $mapper );
$user = $this -> createMock ( User :: class );
2013-09-11 20:51:45 +04:00
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
2018-08-15 17:11:07 +03:00
-> willReturn ( $user );
2017-11-03 19:40:05 +03:00
2019-11-29 15:30:25 +03:00
//test for deleted user – always returns true as long as we have the user in DB
$this -> assertTrue ( $backend -> userExists ( 'formerUser' ));
2015-03-31 16:33:44 +03:00
}
public function testUserExistsForNeverExisting () {
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2015-03-31 16:33:44 +03:00
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( 'readAttribute' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $dn ) {
2015-03-31 16:33:44 +03:00
if ( $dn === 'dnOfRoland,dc=test' ) {
return array ();
}
return false ;
2020-03-26 00:21:27 +03:00
});
2013-09-11 20:51:45 +04:00
//test for never-existing user
2018-07-02 18:38:16 +03:00
/** @noinspection PhpUnhandledExceptionInspection */
2013-09-11 20:51:45 +04:00
$result = $backend -> userExists ( 'mallory' );
$this -> assertFalse ( $result );
}
2013-09-26 00:14:02 +04:00
public function testUserExistsPublicAPI () {
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2013-09-26 00:14:02 +04:00
\OC_User :: useBackend ( $backend );
2017-11-03 19:40:05 +03:00
$user = $this -> createMock ( User :: class );
$user -> expects ( $this -> any ())
-> method ( 'getDN' )
-> willReturn ( 'dnOfRoland,dc=test' );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( 'readAttribute' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $dn ) {
2015-03-31 16:33:44 +03:00
if ( $dn === 'dnOfRoland,dc=test' ) {
return array ();
}
return false ;
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( $user );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'getUserMapper' )
-> willReturn ( $this -> createMock ( UserMapping :: class ));
2013-09-26 00:14:02 +04:00
//test for existing user
2018-01-16 15:27:45 +03:00
$result = \OC :: $server -> getUserManager () -> userExists ( 'gunslinger' );
2013-09-26 00:14:02 +04:00
$this -> assertTrue ( $result );
2015-03-31 16:33:44 +03:00
}
2017-07-13 15:32:52 +03:00
public function testDeleteUserExisting () {
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2013-09-11 20:51:45 +04:00
2017-07-13 15:32:52 +03:00
//we do not support deleting existing users at all
2013-09-11 20:51:45 +04:00
$result = $backend -> deleteUser ( 'gunslinger' );
$this -> assertFalse ( $result );
}
2015-03-31 16:33:44 +03:00
public function testGetHomeAbsolutePath () {
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2013-09-11 20:51:45 +04:00
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( '__get' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $name ) {
2015-03-31 16:33:44 +03:00
if ( $name === 'homeFolderNamingRule' ) {
return 'attr:testAttribute' ;
}
return null ;
2020-03-26 00:21:27 +03:00
});
2013-09-11 20:51:45 +04:00
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( 'readAttribute' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $dn , $attr ) {
2015-03-31 16:33:44 +03:00
switch ( $dn ) {
case 'dnOfRoland,dc=test' :
if ( $attr === 'testAttribute' ) {
return array ( '/tmp/rolandshome/' );
}
return array ();
break ;
default :
return false ;
}
2020-03-26 00:21:27 +03:00
});
2013-09-11 20:51:45 +04:00
2017-11-03 19:40:05 +03:00
$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/' );
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( $user );
//absolute path
2018-07-02 18:38:16 +03:00
/** @noinspection PhpUnhandledExceptionInspection */
2013-09-11 20:51:45 +04:00
$result = $backend -> getHome ( 'gunslinger' );
$this -> assertEquals ( '/tmp/rolandshome/' , $result );
2015-03-31 16:33:44 +03:00
}
2015-04-09 15:03:30 +03:00
public function testGetHomeRelative () {
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2015-03-31 16:33:44 +03:00
2015-08-21 01:55:42 +03:00
$dataDir = \OC :: $server -> getConfig () -> getSystemValue (
'datadirectory' , \OC :: $SERVERROOT . '/data' );
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( '__get' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $name ) {
2015-03-31 16:33:44 +03:00
if ( $name === 'homeFolderNamingRule' ) {
return 'attr:testAttribute' ;
}
return null ;
2020-03-26 00:21:27 +03:00
});
2013-09-11 20:51:45 +04:00
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( 'readAttribute' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $dn , $attr ) {
2015-03-31 16:33:44 +03:00
switch ( $dn ) {
case 'dnOfLadyOfShadows,dc=test' :
if ( $attr === 'testAttribute' ) {
return array ( 'susannah/' );
}
return array ();
break ;
default :
return false ;
}
2020-03-26 00:21:27 +03:00
});
2015-04-09 15:03:30 +03:00
2017-11-03 19:40:05 +03:00
$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/' );
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( $user );
2018-07-02 18:38:16 +03:00
/** @noinspection PhpUnhandledExceptionInspection */
2013-09-11 20:51:45 +04:00
$result = $backend -> getHome ( 'ladyofshadows' );
2015-08-21 01:55:42 +03:00
$this -> assertEquals ( $dataDir . '/susannah/' , $result );
2015-03-31 16:33:44 +03:00
}
2019-11-29 15:30:25 +03:00
2015-03-31 16:33:44 +03:00
public function testGetHomeNoPath () {
2019-11-27 17:27:18 +03:00
$this -> expectException ( \Exception :: class );
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2015-03-31 16:33:44 +03:00
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( '__get' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $name ) {
2015-03-31 16:33:44 +03:00
if ( $name === 'homeFolderNamingRule' ) {
return 'attr:testAttribute' ;
}
return null ;
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( 'readAttribute' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $dn , $attr ) {
2015-03-31 16:33:44 +03:00
switch ( $dn ) {
default :
return false ;
}
2020-03-26 00:21:27 +03:00
});
2018-08-15 17:11:07 +03:00
$this -> access -> connection -> expects ( $this -> any ())
-> method ( 'getFromCache' )
-> willReturnCallback ( function ( $key ) {
if ( $key === 'userExistsnewyorker' ) {
return true ;
}
return null ;
});
2013-09-11 20:51:45 +04:00
2017-11-03 19:40:05 +03:00
$user = $this -> createMock ( User :: class );
$user -> expects ( $this -> any ())
-> method ( 'getUsername' )
-> willReturn ( 'newyorker' );
$user -> expects ( $this -> any ())
-> method ( 'getHomePath' )
-> willThrowException ( new \Exception ());
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( $user );
2013-09-11 20:51:45 +04:00
//no path at all – triggers OC default behaviour
$result = $backend -> getHome ( 'newyorker' );
$this -> assertFalse ( $result );
}
2015-12-11 19:25:57 +03:00
public function testGetHomeDeletedUser () {
2017-07-13 15:32:52 +03:00
$uid = 'newyorker' ;
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2015-12-11 19:25:57 +03:00
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2015-12-11 19:25:57 +03:00
-> method ( '__get' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $name ) {
2015-12-11 19:25:57 +03:00
if ( $name === 'homeFolderNamingRule' ) {
return 'attr:testAttribute' ;
}
return null ;
2020-03-26 00:21:27 +03:00
});
2015-12-11 19:25:57 +03:00
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-12-11 19:25:57 +03:00
-> method ( 'readAttribute' )
2020-03-26 00:21:27 +03:00
-> willReturn ([]);
2015-12-11 19:25:57 +03:00
2017-07-13 15:32:52 +03:00
$userMapper = $this -> createMock ( UserMapping :: class );
2015-12-11 19:25:57 +03:00
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-12-11 19:25:57 +03:00
-> method ( 'getUserMapper' )
2020-03-26 00:21:27 +03:00
-> willReturn ( $userMapper );
2015-12-11 19:25:57 +03:00
2018-07-02 18:38:16 +03:00
$this -> config -> expects ( $this -> any ())
2015-12-11 19:25:57 +03:00
-> method ( 'getUserValue' )
2020-03-26 00:21:27 +03:00
-> willReturn ( true );
2015-12-11 19:25:57 +03:00
2018-07-02 18:38:16 +03:00
$offlineUser = $this -> createMock ( OfflineUser :: class );
2019-10-28 17:11:41 +03:00
$offlineUser -> expects ( $this -> atLeastOnce ())
-> method ( 'getHomePath' )
-> willReturn ( '' );
2017-07-13 15:32:52 +03:00
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
2018-07-02 18:38:16 +03:00
-> willReturn ( $offlineUser );
2017-11-03 19:40:05 +03:00
2019-10-28 17:11:41 +03:00
$result = $backend -> getHome ( $uid );
$this -> assertFalse ( $result );
2015-12-11 19:25:57 +03:00
}
2017-11-02 15:40:38 +03:00
public function testGetHomeWithPlugin () {
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: GET_HOME )
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'getHome' )
-> with ( 'uid' )
-> willReturn ( 'result' );
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2017-11-02 15:40:38 +03:00
-> method ( 'getFromCache' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $uid ) {
2017-11-02 15:40:38 +03:00
return true ;
2020-03-26 00:21:27 +03:00
});
2017-11-02 15:40:38 +03:00
2018-07-02 18:38:16 +03:00
/** @noinspection PhpUnhandledExceptionInspection */
$this -> assertEquals ( $this -> backend -> getHome ( 'uid' ), 'result' );
2017-11-02 15:40:38 +03:00
}
2018-07-02 18:38:16 +03:00
private function prepareAccessForGetDisplayName () {
$this -> connection -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( '__get' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $name ) {
2013-09-11 20:51:45 +04:00
if ( $name === 'ldapUserDisplayName' ) {
return 'displayname' ;
}
return null ;
2020-03-26 00:21:27 +03:00
});
2013-09-11 20:51:45 +04:00
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2013-09-11 20:51:45 +04:00
-> method ( 'readAttribute' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $dn , $attr ) {
2013-09-11 20:51:45 +04:00
switch ( $dn ) {
2014-04-01 14:28:23 +04:00
case 'dnOfRoland,dc=test' :
2013-09-11 20:51:45 +04:00
if ( $attr === 'displayname' ) {
return array ( 'Roland Deschain' );
}
return array ();
break ;
default :
return false ;
}
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> method ( 'fetchUsersByLoginName' )
2017-08-09 17:43:40 +03:00
-> willReturn ([]);
2013-09-26 00:14:02 +04:00
}
public function testGetDisplayName () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetDisplayName ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2013-09-11 20:51:45 +04:00
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( 'getConnectionResource' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function () {
2015-03-31 16:33:44 +03:00
return true ;
2020-03-26 00:21:27 +03:00
});
2015-03-31 16:33:44 +03:00
2017-11-03 19:40:05 +03:00
$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 ; });
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturnCallback ( function ( $uid ) use ( $user1 , $user2 ) {
if ( $uid === 'gunslinger' ) {
return $user1 ;
} else if ( $uid === 'newyorker' ) {
return $user2 ;
}
2018-07-02 18:38:16 +03:00
return null ;
2017-11-03 19:40:05 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'getUserMapper' )
-> willReturn ( $mapper );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'getUserDnByUuid' )
-> willReturnCallback ( function ( $uuid ) { return $uuid . '1' ; });
2013-09-11 20:51:45 +04:00
//with displayName
$result = $backend -> getDisplayName ( 'gunslinger' );
$this -> assertEquals ( 'Roland Deschain' , $result );
//empty displayname retrieved
$result = $backend -> getDisplayName ( 'newyorker' );
$this -> assertEquals ( null , $result );
}
2013-09-26 00:14:02 +04:00
public function testGetDisplayNamePublicAPI () {
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( 'username2dn' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $uid ) {
2015-03-31 16:33:44 +03:00
switch ( $uid ) {
case 'gunslinger' :
return 'dnOfRoland,dc=test' ;
break ;
case 'formerUser' :
return 'dnOfFormerUser,dc=test' ;
break ;
case 'newyorker' :
return 'dnOfNewYorker,dc=test' ;
break ;
case 'ladyofshadows' :
return 'dnOfLadyOfShadows,dc=test' ;
break ;
default :
return false ;
}
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForGetDisplayName ();
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> prepareMockForUserExists ();
2015-03-31 16:33:44 +03:00
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2015-03-31 16:33:44 +03:00
-> method ( 'getConnectionResource' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function () {
2015-03-31 16:33:44 +03:00
return true ;
2020-03-26 00:21:27 +03:00
});
2015-03-31 16:33:44 +03:00
2013-09-26 00:14:02 +04:00
\OC_User :: useBackend ( $backend );
2017-11-03 19:40:05 +03:00
$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 ; });
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturnCallback ( function ( $uid ) use ( $user1 , $user2 ) {
if ( $uid === 'gunslinger' ) {
return $user1 ;
} else if ( $uid === 'newyorker' ) {
return $user2 ;
}
2018-07-02 18:38:16 +03:00
return null ;
2017-11-03 19:40:05 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'getUserMapper' )
-> willReturn ( $mapper );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'getUserDnByUuid' )
-> willReturnCallback ( function ( $uuid ) { return $uuid . '1' ; });
2013-09-26 00:14:02 +04:00
//with displayName
2018-03-25 21:42:03 +03:00
$result = \OC :: $server -> getUserManager () -> get ( 'gunslinger' ) -> getDisplayName ();
2013-09-26 00:14:02 +04:00
$this -> assertEquals ( 'Roland Deschain' , $result );
//empty displayname retrieved
2018-03-25 21:42:38 +03:00
$result = \OC :: $server -> getUserManager () -> get ( 'newyorker' ) === null ? 'newyorker' : \OC :: $server -> getUserManager () -> get ( 'newyorker' ) -> getDisplayName ();
2013-09-26 00:14:02 +04:00
$this -> assertEquals ( 'newyorker' , $result );
}
2017-11-02 15:40:38 +03:00
public function testGetDisplayNameWithPlugin () {
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: GET_DISPLAYNAME )
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'getDisplayName' )
-> with ( 'uid' )
-> willReturn ( 'result' );
2018-07-02 18:38:16 +03:00
$this -> assertEquals ( $this -> backend -> getDisplayName ( 'uid' ), 'result' );
2017-11-02 15:40:38 +03:00
}
2013-09-11 20:51:45 +04:00
//no test for getDisplayNames, because it just invokes getUsers and
//getDisplayName
2014-01-08 15:24:29 +04:00
public function testCountUsers () {
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> once ())
2014-01-08 15:24:29 +04:00
-> method ( 'countUsers' )
2020-03-26 00:21:27 +03:00
-> willReturn ( 5 );
2014-01-08 15:24:29 +04:00
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-01-08 15:24:29 +04:00
$result = $backend -> countUsers ();
$this -> assertEquals ( 5 , $result );
}
public function testCountUsersFailing () {
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> once ())
2014-01-08 15:24:29 +04:00
-> method ( 'countUsers' )
2020-03-26 00:21:27 +03:00
-> willReturn ( false );
2014-01-08 15:24:29 +04:00
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2014-01-08 15:24:29 +04:00
$result = $backend -> countUsers ();
$this -> assertFalse ( $result );
}
2016-10-07 02:39:57 +03:00
2017-11-02 15:40:38 +03:00
public function testCountUsersWithPlugin () {
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: COUNT_USERS )
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'countUsers' )
-> willReturn ( 42 );
2018-07-02 18:38:16 +03:00
$this -> assertEquals ( $this -> backend -> countUsers (), 42 );
2019-11-29 15:30:25 +03:00
}
2017-11-02 15:40:38 +03:00
2016-10-07 02:39:57 +03:00
public function testLoginName2UserNameSuccess () {
$loginName = 'Alice' ;
$username = 'alice' ;
$dn = 'uid=alice,dc=what,dc=ever' ;
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> once ())
2016-10-07 02:39:57 +03:00
-> method ( 'fetchUsersByLoginName' )
-> with ( $this -> equalTo ( $loginName ))
-> willReturn ([[ 'dn' => [ $dn ]]]);
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2016-10-07 02:39:57 +03:00
-> method ( 'stringResemblesDN' )
-> with ( $this -> equalTo ( $dn ))
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2016-10-07 02:39:57 +03:00
-> method ( 'dn2username' )
-> with ( $this -> equalTo ( $dn ))
-> willReturn ( $username );
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> exactly ( 2 ))
2016-10-07 02:39:57 +03:00
-> method ( 'getFromCache' )
-> with ( $this -> equalTo ( 'loginName2UserName-' . $loginName ))
-> willReturnOnConsecutiveCalls ( null , $username );
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> once ())
2016-10-07 02:39:57 +03:00
-> method ( 'writeToCache' )
-> with ( $this -> equalTo ( 'loginName2UserName-' . $loginName ), $this -> equalTo ( $username ));
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2017-11-07 15:19:08 +03:00
$user = $this -> createMock ( User :: class );
2017-11-03 19:40:05 +03:00
$user -> expects ( $this -> any ())
-> method ( 'getUsername' )
-> willReturn ( 'alice' );
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> atLeastOnce ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> with ( $dn )
-> willReturn ( $user );
2016-10-07 02:39:57 +03:00
$name = $backend -> loginName2UserName ( $loginName );
$this -> assertSame ( $username , $name );
// and once again to verify that caching works
$backend -> loginName2UserName ( $loginName );
}
public function testLoginName2UserNameNoUsersOnLDAP () {
$loginName = 'Loki' ;
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> once ())
2016-10-07 02:39:57 +03:00
-> method ( 'fetchUsersByLoginName' )
-> with ( $this -> equalTo ( $loginName ))
-> willReturn ([]);
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> never ())
2016-10-07 02:39:57 +03:00
-> method ( 'stringResemblesDN' );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> never ())
2016-10-07 02:39:57 +03:00
-> method ( 'dn2username' );
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> exactly ( 2 ))
2016-10-07 02:39:57 +03:00
-> method ( 'getFromCache' )
-> with ( $this -> equalTo ( 'loginName2UserName-' . $loginName ))
-> willReturnOnConsecutiveCalls ( null , false );
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> once ())
2016-10-07 02:39:57 +03:00
-> method ( 'writeToCache' )
-> with ( $this -> equalTo ( 'loginName2UserName-' . $loginName ), false );
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2016-10-07 02:39:57 +03:00
$name = $backend -> loginName2UserName ( $loginName );
$this -> assertSame ( false , $name );
// and once again to verify that caching works
$backend -> loginName2UserName ( $loginName );
}
public function testLoginName2UserNameOfflineUser () {
$loginName = 'Alice' ;
$dn = 'uid=alice,dc=what,dc=ever' ;
$offlineUser = $this -> getMockBuilder ( OfflineUser :: class )
-> disableOriginalConstructor ()
-> getMock ();
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> once ())
2016-10-07 02:39:57 +03:00
-> method ( 'fetchUsersByLoginName' )
-> with ( $this -> equalTo ( $loginName ))
-> willReturn ([[ 'dn' => [ $dn ]]]);
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> exactly ( 2 ))
2016-10-07 02:39:57 +03:00
-> method ( 'getFromCache' )
-> with ( $this -> equalTo ( 'loginName2UserName-' . $loginName ))
-> willReturnOnConsecutiveCalls ( null , false );
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> once ())
2016-10-07 02:39:57 +03:00
-> method ( 'writeToCache' )
-> with ( $this -> equalTo ( 'loginName2UserName-' . $loginName ), $this -> equalTo ( false ));
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> with ( $dn )
-> willReturn ( $offlineUser );
2016-10-07 02:39:57 +03:00
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2016-10-07 02:39:57 +03:00
$name = $backend -> loginName2UserName ( $loginName );
$this -> assertSame ( false , $name );
// and once again to verify that caching works
$backend -> loginName2UserName ( $loginName );
}
2018-07-02 18:38:16 +03:00
2016-08-30 12:43:29 +03:00
/**
* Prepares the Access mock for setPassword tests
2018-07-02 18:38:16 +03:00
*
* @ param bool $enablePasswordChange
2016-08-30 12:43:29 +03:00
*/
2018-07-02 18:38:16 +03:00
private function prepareAccessForSetPassword ( $enablePasswordChange = true ) {
$this -> connection -> expects ( $this -> any ())
2016-08-30 12:43:29 +03:00
-> method ( '__get' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $name ) use ( & $enablePasswordChange ) {
2016-08-30 12:43:29 +03:00
if ( $name === 'ldapLoginFilter' ) {
return '%uid' ;
}
if ( $name === 'turnOnPasswordChange' ) {
return $enablePasswordChange ? 1 : 0 ;
}
return null ;
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> connection -> expects ( $this -> any ())
2016-08-30 12:43:29 +03:00
-> method ( 'getFromCache' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $uid ) {
2016-08-30 12:43:29 +03:00
if ( $uid === 'userExists' . 'roland' ) {
return true ;
}
return null ;
2020-03-26 00:21:27 +03:00
});
2016-08-30 12:43:29 +03:00
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2016-08-30 12:43:29 +03:00
-> method ( 'fetchListOfUsers' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $filter ) {
2016-08-30 12:43:29 +03:00
if ( $filter === 'roland' ) {
return array ( array ( 'dn' => [ 'dnOfRoland,dc=test' ]));
}
return array ();
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2016-08-30 12:43:29 +03:00
-> method ( 'fetchUsersByLoginName' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $uid ) {
2016-08-30 12:43:29 +03:00
if ( $uid === 'roland' ) {
return array ( array ( 'dn' => [ 'dnOfRoland,dc=test' ]));
}
return array ();
2020-03-26 00:21:27 +03:00
});
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2016-08-30 12:43:29 +03:00
-> method ( 'dn2username' )
-> with ( $this -> equalTo ( 'dnOfRoland,dc=test' ))
2020-03-26 00:21:27 +03:00
-> willReturn ( 'roland' );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2016-08-30 12:43:29 +03:00
-> method ( 'stringResemblesDN' )
-> with ( $this -> equalTo ( 'dnOfRoland,dc=test' ))
2020-03-26 00:21:27 +03:00
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> access -> expects ( $this -> any ())
2016-08-30 12:43:29 +03:00
-> method ( 'setPassword' )
2020-03-26 00:21:27 +03:00
-> willReturnCallback ( function ( $uid , $password ) {
2016-08-30 12:43:29 +03:00
if ( strlen ( $password ) <= 5 ) {
throw new HintException ( 'Password fails quality checking policy' , '' , 19 );
}
return true ;
2020-03-26 00:21:27 +03:00
});
2016-08-30 12:43:29 +03:00
}
2019-11-29 15:30:25 +03:00
2016-08-30 12:43:29 +03:00
public function testSetPasswordInvalid () {
2019-11-27 17:27:18 +03:00
$this -> expectException ( \OC\HintException :: class );
$this -> expectExceptionMessage ( 'Password fails quality checking policy' );
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForSetPassword ( $this -> access );
$this -> userManager -> expects ( $this -> atLeastOnce ())
2018-01-03 23:44:30 +03:00
-> method ( 'get' )
-> willReturn ( $this -> createMock ( User :: class ));
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2016-08-30 12:43:29 +03:00
\OC_User :: useBackend ( $backend );
$this -> assertTrue ( \OC_User :: setPassword ( 'roland' , 'dt' ));
}
2019-11-29 15:30:25 +03:00
2016-08-30 12:43:29 +03:00
public function testSetPasswordValid () {
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForSetPassword ( $this -> access );
2016-08-30 12:43:29 +03:00
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> any ())
2017-11-07 15:19:08 +03:00
-> method ( 'get' )
-> willReturn ( $this -> createMock ( User :: class ));
2018-07-02 18:38:16 +03:00
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
$this -> userManager -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( $this -> createMock ( User :: class ));
2016-08-30 12:43:29 +03:00
\OC_User :: useBackend ( $backend );
$this -> assertTrue ( \OC_User :: setPassword ( 'roland' , 'dt12234$' ));
}
2017-03-31 10:16:22 +03:00
2016-08-30 12:43:29 +03:00
public function testSetPasswordValidDisabled () {
2018-07-02 18:38:16 +03:00
$this -> userManager -> expects ( $this -> any ())
2017-11-03 19:40:05 +03:00
-> method ( 'get' )
-> willReturn ( $this -> createMock ( User :: class ));
2016-08-30 12:43:29 +03:00
2018-07-02 18:38:16 +03:00
$this -> prepareAccessForSetPassword ( false );
$backend = new UserLDAP ( $this -> access , $this -> config , $this -> notificationManager , $this -> session , $this -> pluginManager );
2016-08-30 12:43:29 +03:00
\OC_User :: useBackend ( $backend );
$this -> assertFalse ( \OC_User :: setPassword ( 'roland' , 'dt12234$' ));
}
2016-11-23 21:58:43 +03:00
2019-11-29 15:30:25 +03:00
2016-11-23 21:58:43 +03:00
public function testSetPasswordWithInvalidUser () {
2019-11-27 17:27:18 +03:00
$this -> expectException ( \Exception :: class );
$this -> expectExceptionMessage ( 'LDAP setPassword: Could not get user object for uid NotExistingUser. Maybe the LDAP entry has no set display name attribute?' );
2018-07-02 18:38:16 +03:00
$this -> userManager
2016-11-23 21:58:43 +03:00
-> expects ( $this -> once ())
-> method ( 'get' )
-> with ( 'NotExistingUser' )
-> willReturn ( null );
2018-07-02 18:38:16 +03:00
$this -> backend -> setPassword ( 'NotExistingUser' , 'Password' );
2016-11-23 21:58:43 +03:00
}
public function testSetPasswordWithUsernameFalse () {
$user = $this -> createMock ( User :: class );
$user
-> expects ( $this -> once ())
-> method ( 'getUsername' )
-> willReturn ( false );
2018-07-02 18:38:16 +03:00
$this -> userManager
2016-11-23 21:58:43 +03:00
-> expects ( $this -> once ())
-> method ( 'get' )
-> with ( 'NotExistingUser' )
-> willReturn ( $user );
2018-07-02 18:38:16 +03:00
/** @noinspection PhpUnhandledExceptionInspection */
$this -> assertFalse ( $this -> backend -> setPassword ( 'NotExistingUser' , 'Password' ));
2016-11-23 21:58:43 +03:00
}
2017-11-02 15:40:38 +03:00
public function testSetPasswordWithPlugin () {
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: SET_PASSWORD )
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'setPassword' )
-> with ( 'uid' , 'password' )
-> willReturn ( 'result' );
2018-07-02 18:38:16 +03:00
/** @noinspection PhpUnhandledExceptionInspection */
$this -> assertEquals ( $this -> backend -> setPassword ( 'uid' , 'password' ), 'result' );
}
2017-11-02 15:40:38 +03:00
2018-07-03 01:38:25 +03:00
public function avatarDataProvider () {
return [
[ 'validImageData' , false ],
[ 'corruptImageData' , true ],
[ false , true ]
];
}
/** @dataProvider avatarDataProvider */
public function testCanChangeAvatar ( $imageData , $expected ) {
$isValidImage = strpos (( string ) $imageData , 'valid' ) === 0 ;
$user = $this -> createMock ( User :: class );
$user -> expects ( $this -> once ())
-> method ( 'getAvatarImage' )
-> willReturn ( $imageData );
$user -> expects ( $this -> atMost ( 1 ))
-> method ( 'updateAvatar' )
-> willReturn ( $isValidImage );
$this -> userManager -> expects ( $this -> atLeastOnce ())
-> method ( 'get' )
-> willReturn ( $user );
/** @noinspection PhpUnhandledExceptionInspection */
$this -> assertSame ( $expected , $this -> backend -> canChangeAvatar ( 'uid' ));
}
2017-11-02 15:40:38 +03:00
public function testCanChangeAvatarWithPlugin () {
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: PROVIDE_AVATAR )
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'canChangeAvatar' )
-> with ( 'uid' )
-> willReturn ( 'result' );
2018-07-02 18:38:16 +03:00
$this -> assertEquals ( $this -> backend -> canChangeAvatar ( 'uid' ), 'result' );
2017-11-02 15:40:38 +03:00
}
public function testSetDisplayNameWithPlugin () {
2019-06-18 01:20:09 +03:00
$newDisplayName = 'J. Baker' ;
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: SET_DISPLAYNAME )
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'setDisplayName' )
2019-06-18 01:20:09 +03:00
-> with ( 'uid' , $newDisplayName )
-> willReturn ( $newDisplayName );
$this -> access -> expects ( $this -> once ())
-> method ( 'cacheUserDisplayName' );
$this -> assertEquals ( $newDisplayName , $this -> backend -> setDisplayName ( 'uid' , $newDisplayName ));
}
2019-11-29 15:30:25 +03:00
2019-06-18 01:20:09 +03:00
public function testSetDisplayNameErrorWithPlugin () {
2019-11-27 17:27:18 +03:00
$this -> expectException ( \OC\HintException :: class );
2019-06-18 01:20:09 +03:00
$newDisplayName = 'J. Baker' ;
$this -> pluginManager -> expects ( $this -> once ())
-> method ( 'implementsActions' )
-> with ( Backend :: SET_DISPLAYNAME )
-> willReturn ( true );
$this -> pluginManager -> expects ( $this -> once ())
-> method ( 'setDisplayName' )
-> with ( 'uid' , $newDisplayName )
-> willThrowException ( new HintException ( 'something happned' ));
$this -> access -> expects ( $this -> never ())
-> method ( 'cacheUserDisplayName' );
2017-11-02 15:40:38 +03:00
2019-06-18 01:20:09 +03:00
$this -> backend -> setDisplayName ( 'uid' , $newDisplayName );
2017-11-02 15:40:38 +03:00
}
public function testSetDisplayNameFailing () {
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: SET_DISPLAYNAME )
-> willReturn ( false );
2019-06-18 01:20:09 +03:00
$this -> access -> expects ( $this -> never ())
-> method ( 'cacheUserDisplayName' );
2017-11-02 15:40:38 +03:00
2018-07-02 18:38:16 +03:00
$this -> assertFalse ( $this -> backend -> setDisplayName ( 'uid' , 'displayName' ));
2017-11-02 15:40:38 +03:00
}
public function testCreateUserWithPlugin () {
2019-06-14 18:20:42 +03:00
$uid = 'alien6372' ;
$uuid = '123-2345-36756-123-2345234-4431' ;
$pwd = 'passwørd' ;
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: CREATE_USER )
-> willReturn ( true );
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'createUser' )
2019-06-14 18:20:42 +03:00
-> with ( $uid , $pwd )
2017-11-02 15:40:38 +03:00
-> willReturn ( 'result' );
2019-06-14 18:20:42 +03:00
$this -> access -> expects ( $this -> atLeastOnce ())
-> method ( 'getUUID' )
-> willReturn ( $uuid );
$this -> access -> expects ( $this -> once ())
-> method ( 'mapAndAnnounceIfApplicable' )
-> with ( $this -> isInstanceOf ( AbstractMapping :: class ), $this -> anything (), $uid , $uuid , true );
$this -> access -> expects ( $this -> any ())
-> method ( 'getUserMapper' )
-> willReturn ( $this -> createMock ( AbstractMapping :: class ));
$this -> assertEquals ( $this -> backend -> createUser ( $uid , $pwd ), true );
2017-11-02 15:40:38 +03:00
}
public function testCreateUserFailing () {
2018-07-02 18:38:16 +03:00
$this -> pluginManager -> expects ( $this -> once ())
2017-11-02 15:40:38 +03:00
-> method ( 'implementsActions' )
-> with ( Backend :: CREATE_USER )
-> willReturn ( false );
2018-07-02 18:38:16 +03:00
$this -> assertFalse ( $this -> backend -> createUser ( 'uid' , 'password' ));
2017-11-02 15:40:38 +03:00
}
2018-07-05 12:29:19 +03:00
public function actionProvider () {
return [
[ 'ldapUserAvatarRule' , 'default' , Backend :: PROVIDE_AVATAR , true ] ,
[ 'ldapUserAvatarRule' , 'data:selfiePhoto' , Backend :: PROVIDE_AVATAR , true ],
[ 'ldapUserAvatarRule' , 'none' , Backend :: PROVIDE_AVATAR , false ],
[ 'turnOnPasswordChange' , 0 , Backend :: SET_PASSWORD , false ],
[ 'turnOnPasswordChange' , 1 , Backend :: SET_PASSWORD , true ],
];
}
/**
* @ dataProvider actionProvider
*/
public function testImplementsAction ( $configurable , $value , $actionCode , $expected ) {
$this -> pluginManager -> expects ( $this -> once ())
-> method ( 'getImplementedActions' )
-> willReturn ( 0 );
$this -> connection -> expects ( $this -> any ())
-> method ( '__get' )
-> willReturnMap ([
[ $configurable , $value ],
]);
$this -> assertSame ( $expected , $this -> backend -> implementsActions ( $actionCode ));
}
2014-05-13 15:29:25 +04:00
}