2014-11-05 07:05:07 -05:00
< ? php
2024-05-29 05:32:54 -04:00
2025-05-28 03:50:46 -04:00
declare ( strict_types = 1 );
2014-11-05 07:05:07 -05:00
/**
2024-05-29 05:32:54 -04:00
* SPDX - FileCopyrightText : 2016 - 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX - FileCopyrightText : 2016 ownCloud , Inc .
* SPDX - License - Identifier : AGPL - 3.0 - only
2015-03-26 06:44:34 -04:00
*/
2016-05-12 11:14:59 -04:00
namespace OCA\User_LDAP\Tests\Mapping ;
2014-11-05 07:05:07 -05:00
2017-02-17 12:45:33 -05:00
use OCA\User_LDAP\Mapping\AbstractMapping ;
2025-08-14 12:52:37 -04:00
use OCP\IAppConfig ;
use OCP\ICacheFactory ;
2016-09-02 05:02:12 -04:00
use OCP\IDBConnection ;
2025-02-03 09:34:01 -05:00
use OCP\Server ;
2025-08-14 12:52:37 -04:00
use PHPUnit\Framework\MockObject\MockObject ;
2016-09-02 05:02:12 -04:00
2025-05-28 03:50:46 -04:00
abstract class AbstractMappingTestCase extends \Test\TestCase {
2025-08-14 12:52:37 -04:00
private ICacheFactory & MockObject $cacheFactoryMock ;
private IAppConfig & MockObject $configMock ;
abstract public function getMapper ( IDBConnection $dbMock , ICacheFactory $cacheFactory , IAppConfig $appConfig ) : AbstractMapping ;
protected function setUp () : void {
$this -> cacheFactoryMock = $this -> createMock ( ICacheFactory :: class );
$this -> configMock = $this -> createMock ( IAppConfig :: class );
}
2014-11-05 07:05:07 -05:00
/**
* kiss test on isColNameValid
*/
2024-09-15 16:32:31 -04:00
public function testIsColNameValid () : void {
2016-09-02 05:02:12 -04:00
$dbMock = $this -> createMock ( IDBConnection :: class );
2025-08-14 12:52:37 -04:00
$mapper = $this -> getMapper ( $dbMock , $this -> cacheFactoryMock , $this -> configMock );
2014-11-05 07:05:07 -05:00
$this -> assertTrue ( $mapper -> isColNameValid ( 'ldap_dn' ));
$this -> assertFalse ( $mapper -> isColNameValid ( 'foobar' ));
}
/**
* returns an array of test entries with dn , name and uuid as keys
* @ return array
*/
2025-05-28 03:50:46 -04:00
protected static function getTestData () : array {
return [
2020-03-26 04:30:18 -04:00
[
2014-11-05 07:05:07 -05:00
'dn' => 'uid=foobar,dc=example,dc=org' ,
'name' => 'Foobar' ,
'uuid' => '1111-AAAA-1234-CDEF' ,
2020-03-26 04:30:18 -04:00
],
[
2014-11-05 07:05:07 -05:00
'dn' => 'uid=barfoo,dc=example,dc=org' ,
'name' => 'Barfoo' ,
'uuid' => '2222-BBBB-1234-CDEF' ,
2020-03-26 04:30:18 -04:00
],
[
2014-11-05 07:05:07 -05:00
'dn' => 'uid=barabara,dc=example,dc=org' ,
'name' => 'BaraBara' ,
'uuid' => '3333-CCCC-1234-CDEF' ,
2020-03-26 04:30:18 -04:00
]
];
2014-11-05 07:05:07 -05:00
}
/**
* calls map () on the given mapper and asserts result for true
2024-10-10 06:40:31 -04:00
* @ param AbstractMapping $mapper
2014-11-05 07:05:07 -05:00
* @ param array $data
*/
2025-05-28 03:50:46 -04:00
protected function mapEntries ( AbstractMapping $mapper , array $data ) : void {
2020-04-10 08:19:56 -04:00
foreach ( $data as $entry ) {
2014-11-05 07:05:07 -05:00
$done = $mapper -> map ( $entry [ 'dn' ], $entry [ 'name' ], $entry [ 'uuid' ]);
$this -> assertTrue ( $done );
}
}
/**
2022-07-28 07:11:38 -04:00
* initializes environment for a test run and returns an array with
2014-11-05 07:05:07 -05:00
* test objects . Preparing environment means that all mappings are cleared
* first and then filled with test entries .
* @ return array 0 = \OCA\User_LDAP\Mapping\AbstractMapping , 1 = array of
2024-08-23 09:10:27 -04:00
* users or groups
2014-11-05 07:05:07 -05:00
*/
2025-05-28 03:50:46 -04:00
private function initTest () : array {
2025-02-03 09:34:01 -05:00
$dbc = Server :: get ( IDBConnection :: class );
2025-08-14 12:52:37 -04:00
$mapper = $this -> getMapper ( $dbc , $this -> cacheFactoryMock , $this -> configMock );
2014-11-05 07:05:07 -05:00
$data = $this -> getTestData ();
// make sure DB is pristine, then fill it with test entries
$mapper -> clear ();
$this -> mapEntries ( $mapper , $data );
2020-03-26 04:30:18 -04:00
return [ $mapper , $data ];
2014-11-05 07:05:07 -05:00
}
/**
* tests map () method with input that should result in not - mapping .
* Hint : successful mapping is tested inherently with mapEntries () .
*/
2024-09-15 16:32:31 -04:00
public function testMap () : void {
2021-01-12 04:15:48 -05:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 07:05:07 -05:00
// test that mapping will not happen when it shall not
2017-01-25 11:10:51 -05:00
$tooLongDN = 'uid=joann,ou=Secret Small Specialized Department,ou=Some Tremendously Important Department,ou=Another Very Important Department,ou=Pretty Meaningful Derpartment,ou=Quite Broad And General Department,ou=The Topmost Department,dc=hugelysuccessfulcompany,dc=com' ;
2020-03-26 04:30:18 -04:00
$paramKeys = [ '' , 'dn' , 'name' , 'uuid' , $tooLongDN ];
2020-04-10 08:19:56 -04:00
foreach ( $paramKeys as $key ) {
2014-11-05 07:05:07 -05:00
$failEntry = $data [ 0 ];
2020-04-10 08:19:56 -04:00
if ( ! empty ( $key )) {
2014-11-05 07:05:07 -05:00
$failEntry [ $key ] = 'do-not-get-mapped' ;
}
$isMapped = $mapper -> map ( $failEntry [ 'dn' ], $failEntry [ 'name' ], $failEntry [ 'uuid' ]);
$this -> assertFalse ( $isMapped );
}
}
/**
2016-04-12 06:52:51 -04:00
* tests unmap () for both successful and unsuccessful removing of
2014-11-05 07:05:07 -05:00
* mapping entries
*/
2024-09-15 16:32:31 -04:00
public function testUnmap () : void {
2021-01-12 04:15:48 -05:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 07:05:07 -05:00
2020-04-10 08:19:56 -04:00
foreach ( $data as $entry ) {
2021-11-22 08:57:08 -05:00
$fdnBefore = $mapper -> getDNByName ( $entry [ 'name' ]);
2014-11-05 07:05:07 -05:00
$result = $mapper -> unmap ( $entry [ 'name' ]);
2021-11-22 08:57:08 -05:00
$fdnAfter = $mapper -> getDNByName ( $entry [ 'name' ]);
2014-11-05 07:05:07 -05:00
$this -> assertTrue ( $result );
2021-11-22 08:57:08 -05:00
$this -> assertSame ( $fdnBefore , $entry [ 'dn' ]);
$this -> assertFalse ( $fdnAfter );
2014-11-05 07:05:07 -05:00
}
$result = $mapper -> unmap ( 'notAnEntry' );
$this -> assertFalse ( $result );
}
/**
* tests getDNByName (), getNameByDN () and getNameByUUID () for successful
* and unsuccessful requests .
*/
2024-09-15 16:32:31 -04:00
public function testGetMethods () : void {
2021-01-12 04:15:48 -05:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 07:05:07 -05:00
2020-04-10 08:19:56 -04:00
foreach ( $data as $entry ) {
2014-11-05 07:05:07 -05:00
$fdn = $mapper -> getDNByName ( $entry [ 'name' ]);
$this -> assertSame ( $fdn , $entry [ 'dn' ]);
}
$fdn = $mapper -> getDNByName ( 'nosuchname' );
$this -> assertFalse ( $fdn );
2020-04-10 08:19:56 -04:00
foreach ( $data as $entry ) {
2014-11-05 07:05:07 -05:00
$name = $mapper -> getNameByDN ( $entry [ 'dn' ]);
$this -> assertSame ( $name , $entry [ 'name' ]);
}
$name = $mapper -> getNameByDN ( 'nosuchdn' );
$this -> assertFalse ( $name );
2020-04-10 08:19:56 -04:00
foreach ( $data as $entry ) {
2014-11-05 07:05:07 -05:00
$name = $mapper -> getNameByUUID ( $entry [ 'uuid' ]);
$this -> assertSame ( $name , $entry [ 'name' ]);
}
$name = $mapper -> getNameByUUID ( 'nosuchuuid' );
$this -> assertFalse ( $name );
}
/**
* tests getNamesBySearch () for successful and unsuccessful requests .
*/
2024-09-15 16:32:31 -04:00
public function testSearch () : void {
2021-01-12 04:15:48 -05:00
[ $mapper ,] = $this -> initTest ();
2014-11-05 07:05:07 -05:00
2016-07-20 08:20:45 -04:00
$names = $mapper -> getNamesBySearch ( 'oo' , '%' , '%' );
2025-05-28 03:50:46 -04:00
$this -> assertIsArray ( $names );
2014-11-05 07:05:07 -05:00
$this -> assertSame ( 2 , count ( $names ));
2025-05-28 03:50:46 -04:00
$this -> assertContains ( 'Foobar' , $names );
$this -> assertContains ( 'Barfoo' , $names );
2014-11-05 07:05:07 -05:00
$names = $mapper -> getNamesBySearch ( 'nada' );
2025-05-28 03:50:46 -04:00
$this -> assertIsArray ( $names );
$this -> assertCount ( 0 , $names );
2014-11-05 07:05:07 -05:00
}
/**
* tests setDNbyUUID () for successful and unsuccessful update .
*/
2024-09-15 16:32:31 -04:00
public function testSetDNMethod () : void {
2021-01-12 04:15:48 -05:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 07:05:07 -05:00
$newDN = 'uid=modified,dc=example,dc=org' ;
$done = $mapper -> setDNbyUUID ( $newDN , $data [ 0 ][ 'uuid' ]);
$this -> assertTrue ( $done );
$fdn = $mapper -> getDNByName ( $data [ 0 ][ 'name' ]);
$this -> assertSame ( $fdn , $newDN );
$newDN = 'uid=notme,dc=example,dc=org' ;
$done = $mapper -> setDNbyUUID ( $newDN , 'iamnothere' );
$this -> assertFalse ( $done );
$name = $mapper -> getNameByDN ( $newDN );
$this -> assertFalse ( $name );
2017-02-17 12:45:33 -05:00
}
/**
* tests setUUIDbyDN () for successful and unsuccessful update .
*/
2024-09-15 16:32:31 -04:00
public function testSetUUIDMethod () : void {
2017-02-17 12:45:33 -05:00
/** @var AbstractMapping $mapper */
2021-01-12 04:15:48 -05:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 07:05:07 -05:00
2017-02-17 12:45:33 -05:00
$newUUID = 'ABC737-DEF754' ;
$done = $mapper -> setUUIDbyDN ( $newUUID , 'uid=notme,dc=example,dc=org' );
$this -> assertFalse ( $done );
$name = $mapper -> getNameByUUID ( $newUUID );
$this -> assertFalse ( $name );
$done = $mapper -> setUUIDbyDN ( $newUUID , $data [ 0 ][ 'dn' ]);
$this -> assertTrue ( $done );
$uuid = $mapper -> getUUIDByDN ( $data [ 0 ][ 'dn' ]);
$this -> assertSame ( $uuid , $newUUID );
2014-11-05 07:05:07 -05:00
}
/**
* tests clear () for successful update .
*/
2024-09-15 16:32:31 -04:00
public function testClear () : void {
2021-01-12 04:15:48 -05:00
[ $mapper , $data ] = $this -> initTest ();
2014-11-05 07:05:07 -05:00
$done = $mapper -> clear ();
$this -> assertTrue ( $done );
2020-04-10 08:19:56 -04:00
foreach ( $data as $entry ) {
2014-11-05 07:05:07 -05:00
$name = $mapper -> getNameByUUID ( $entry [ 'uuid' ]);
$this -> assertFalse ( $name );
}
}
2015-01-07 06:39:04 -05:00
2018-03-15 09:16:43 -04:00
/**
* tests clear () for successful update .
*/
2024-09-15 16:32:31 -04:00
public function testClearCb () : void {
2021-01-12 04:15:48 -05:00
[ $mapper , $data ] = $this -> initTest ();
2018-03-15 09:16:43 -04:00
$callbackCalls = 0 ;
$test = $this ;
2024-09-20 11:38:36 -04:00
$callback = function ( string $id ) use ( $test , & $callbackCalls ) : void {
2018-03-15 09:16:43 -04:00
$test -> assertTrue ( trim ( $id ) !== '' );
$callbackCalls ++ ;
};
$done = $mapper -> clearCb ( $callback , $callback );
$this -> assertTrue ( $done );
$this -> assertSame ( count ( $data ) * 2 , $callbackCalls );
2020-04-10 08:19:56 -04:00
foreach ( $data as $entry ) {
2018-03-15 09:16:43 -04:00
$name = $mapper -> getNameByUUID ( $entry [ 'uuid' ]);
$this -> assertFalse ( $name );
}
}
2015-01-07 06:39:04 -05:00
/**
* tests getList () method
*/
2024-09-15 16:32:31 -04:00
public function testList () : void {
2021-01-12 04:15:48 -05:00
[ $mapper , $data ] = $this -> initTest ();
2015-01-07 06:39:04 -05:00
// get all entries without specifying offset or limit
$results = $mapper -> getList ();
2025-05-28 03:50:46 -04:00
$this -> assertCount ( 3 , $results );
2015-01-07 06:39:04 -05:00
// get all-1 entries by specifying offset, and an high limit
// specifying only offset without limit will not work by underlying lib
$results = $mapper -> getList ( 1 , 999 );
2025-05-28 03:50:46 -04:00
$this -> assertCount ( count ( $data ) - 1 , $results );
2015-01-07 06:39:04 -05:00
// get first 2 entries by limit, but not offset
2022-02-03 16:59:23 -05:00
$results = $mapper -> getList ( 0 , 2 );
2025-05-28 03:50:46 -04:00
$this -> assertCount ( 2 , $results );
2015-01-07 06:39:04 -05:00
// get 2nd entry by specifying both offset and limit
$results = $mapper -> getList ( 1 , 1 );
2025-05-28 03:50:46 -04:00
$this -> assertCount ( 1 , $results );
2015-01-07 06:39:04 -05:00
}
2021-01-08 05:54:07 -05:00
2024-09-15 16:32:31 -04:00
public function testGetListOfIdsByDn () : void {
2021-01-08 05:54:07 -05:00
/** @var AbstractMapping $mapper */
2021-01-12 04:15:48 -05:00
[ $mapper ,] = $this -> initTest ();
2021-01-08 05:54:07 -05:00
$listOfDNs = [];
for ( $i = 0 ; $i < 66640 ; $i ++ ) {
// Postgres has a limit of 65535 values in a single IN list
$name = 'as_' . $i ;
$dn = 'uid=' . $name . ',dc=example,dc=org' ;
$listOfDNs [] = $dn ;
if ( $i % 20 === 0 ) {
$mapper -> map ( $dn , $name , 'fake-uuid-' . $i );
}
}
$result = $mapper -> getListOfIdsByDn ( $listOfDNs );
2025-05-28 03:50:46 -04:00
$this -> assertCount ( 66640 / 20 , $result );
2021-01-08 05:54:07 -05:00
}
2014-11-05 07:05:07 -05:00
}