Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions apps/user_ldap/lib/Mapping/AbstractMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,13 @@ public function getDNByName(string $name): string|false {
$dn = array_search($name, $this->cache, true);
if ($dn === false) {
$dn = $this->localNameToDnCache?->get($name);
if ($dn === null) {
// a miss is not cached, the CLI may map the name without updating the local cache
if (!is_string($dn)) {
$dn = $this->getXbyY('ldap_dn', 'owncloud_name', $name);
if ($dn !== false) {
$this->cache[$dn] = $name;
$this->localNameToDnCache?->set($name, $dn, self::LOCAL_CACHE_TTL);
}
$this->localNameToDnCache?->set($name, $dn, self::LOCAL_CACHE_TTL);
}
}
return $dn ?? false;
Expand Down
34 changes: 33 additions & 1 deletion apps/user_ldap/tests/Mapping/AbstractMappingTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace OCA\User_LDAP\Tests\Mapping;

use OC\Memcache\ArrayCache;
use OCA\User_LDAP\Mapping\AbstractMapping;
use OCP\IAppConfig;
use OCP\ICacheFactory;
Expand All @@ -22,7 +23,7 @@ abstract class AbstractMappingTestCase extends \Test\TestCase {

private IAppConfig&MockObject $configMock;

abstract public function getMapper(IDBConnection $dbMock, ICacheFactory $cacheFactory, IAppConfig $appConfig): AbstractMapping;
abstract public function getMapper(IDBConnection $dbMock, ICacheFactory $cacheFactory, IAppConfig $appConfig, bool $isCLI = true): AbstractMapping;

protected function setUp(): void {
$this->cacheFactoryMock = $this->createMock(ICacheFactory::class);
Expand Down Expand Up @@ -94,6 +95,37 @@ private function initTest(): array {
return [$mapper, $data];
}

/**
* A name looked up before it is mapped must be found once the CLI maps it,
* because the CLI does not write to the local cache of web requests.
*/
public function testGetDNByNameFindsNameMappedAfterLookup(): void {
$localCache = new ArrayCache();
$cacheFactory = $this->createMock(ICacheFactory::class);
$cacheFactory->method('isLocalCacheAvailable')->willReturn(true);
$cacheFactory->method('createLocal')->willReturn($localCache);
$config = $this->createMock(IAppConfig::class);
$config->method('getValueString')->willReturn('yes');
$dbc = Server::get(IDBConnection::class);
$data = $this->getTestData();

$webMapper = $this->getMapper($dbc, $cacheFactory, $config, false);
$webMapper->clear();
$this->assertFalse($webMapper->getDNByName($data[0]['name']));
$this->assertFalse($localCache->hasKey($data[0]['name']));
// a cached miss
$localCache->set($data[1]['name'], false);

$cliMapper = $this->getMapper($dbc, $this->cacheFactoryMock, $this->configMock);
$this->mapEntries($cliMapper, [$data[0], $data[1]]);

$webMapper = $this->getMapper($dbc, $cacheFactory, $config, false);
$this->assertSame($data[0]['dn'], $webMapper->getDNByName($data[0]['name']));
$this->assertSame($data[1]['dn'], $webMapper->getDNByName($data[1]['name']));
$this->assertSame($data[0]['dn'], $localCache->get($data[0]['name']));
$this->assertSame($data[1]['dn'], $localCache->get($data[1]['name']));
}

/**
* tests map() method with input that should result in not-mapping.
* Hint: successful mapping is tested inherently with mapEntries().
Expand Down
4 changes: 2 additions & 2 deletions apps/user_ldap/tests/Mapping/GroupMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
class GroupMappingTest extends AbstractMappingTestCase {
public function getMapper(IDBConnection $dbMock, ICacheFactory $cacheFactory, IAppConfig $appConfig): GroupMapping {
return new GroupMapping($dbMock, $cacheFactory, $appConfig, true);
public function getMapper(IDBConnection $dbMock, ICacheFactory $cacheFactory, IAppConfig $appConfig, bool $isCLI = true): GroupMapping {
return new GroupMapping($dbMock, $cacheFactory, $appConfig, $isCLI);
}
}
4 changes: 2 additions & 2 deletions apps/user_ldap/tests/Mapping/UserMappingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
#[\PHPUnit\Framework\Attributes\Group(name: 'DB')]
class UserMappingTest extends AbstractMappingTestCase {
public function getMapper(IDBConnection $dbMock, ICacheFactory $cacheFactory, IAppConfig $appConfig): UserMapping {
return new UserMapping($dbMock, $cacheFactory, $appConfig, true, $this->createMock(IAssertion::class), $this->createMock(IRequest::class));
public function getMapper(IDBConnection $dbMock, ICacheFactory $cacheFactory, IAppConfig $appConfig, bool $isCLI = true): UserMapping {
return new UserMapping($dbMock, $cacheFactory, $appConfig, $isCLI, $this->createMock(IAssertion::class), $this->createMock(IRequest::class));
}
}
Loading