aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--apps/user_ldap/lib/User/User.php13
-rw-r--r--apps/user_ldap/tests/User/UserTest.php53
2 files changed, 58 insertions, 8 deletions
diff --git a/apps/user_ldap/lib/User/User.php b/apps/user_ldap/lib/User/User.php
index 32cf4812461..7c5edcad6b0 100644
--- a/apps/user_ldap/lib/User/User.php
+++ b/apps/user_ldap/lib/User/User.php
@@ -414,14 +414,23 @@ class User {
*
* @param string $displayName
* @param string $displayName2
- * @returns string the effective display name
+ * @return string the effective display name
*/
public function composeAndStoreDisplayName($displayName, $displayName2 = '') {
$displayName2 = strval($displayName2);
if($displayName2 !== '') {
$displayName .= ' (' . $displayName2 . ')';
}
- $this->store('displayName', $displayName);
+ $oldName = $this->config->getUserValue($this->uid, 'user_ldap', 'displayName', null);
+ if ($oldName !== $displayName) {
+ $this->store('displayName', $displayName);
+ $user = $this->userManager->get($this->getUsername());
+ if (!empty($oldName) && $user instanceof \OC\User\User) {
+ // if it was empty, it would be a new record, not a change emitting the trigger could
+ // potentially cause a UniqueConstraintViolationException, depending on some factors.
+ $user->triggerChange('displayName', $displayName);
+ }
+ }
return $displayName;
}
diff --git a/apps/user_ldap/tests/User/UserTest.php b/apps/user_ldap/tests/User/UserTest.php
index 29d2ba3829f..b97931983f7 100644
--- a/apps/user_ldap/tests/User/UserTest.php
+++ b/apps/user_ldap/tests/User/UserTest.php
@@ -1232,27 +1232,68 @@ class UserTest extends \Test\TestCase {
public function displayNameProvider() {
return [
- ['Roland Deschain', '', 'Roland Deschain'],
- ['Roland Deschain', null, 'Roland Deschain'],
- ['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)'],
+ ['Roland Deschain', '', 'Roland Deschain', false],
+ ['Roland Deschain', '', 'Roland Deschain', true],
+ ['Roland Deschain', null, 'Roland Deschain', false],
+ ['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', false],
+ ['Roland Deschain', 'gunslinger@darktower.com', 'Roland Deschain (gunslinger@darktower.com)', true],
];
}
/**
* @dataProvider displayNameProvider
*/
- public function testComposeAndStoreDisplayName($part1, $part2, $expected) {
+ public function testComposeAndStoreDisplayName($part1, $part2, $expected, $expectTriggerChange) {
list(, $config, $filesys, $image, $log, $avaMgr, $userMgr, $notiMgr) =
$this->getTestInstances();
+ $user = new User(
+ 'user', 'cn=user', $this->access, $config, $filesys, $image, $log, $avaMgr, $userMgr, $notiMgr);
+
$config->expects($this->once())
->method('setUserValue');
+ $oldName = $expectTriggerChange ? 'xxGunslingerxx' : null;
+ $config->expects($this->once())
+ ->method('getUserValue')
+ ->with($user->getUsername(), 'user_ldap', 'displayName', null)
+ ->willReturn($oldName);
+
+ $ncUserObj = $this->createMock(\OC\User\User::class);
+ if ($expectTriggerChange) {
+ $ncUserObj->expects($this->once())
+ ->method('triggerChange')
+ ->with('displayName', $expected);
+ } else {
+ $ncUserObj->expects($this->never())
+ ->method('triggerChange');
+ }
+ $userMgr->expects($this->once())
+ ->method('get')
+ ->willReturn($ncUserObj);
+
+ $displayName = $user->composeAndStoreDisplayName($part1, $part2);
+ $this->assertSame($expected, $displayName);
+ }
+
+ public function testComposeAndStoreDisplayNameNoOverwrite() {
+ list(, $config, $filesys, $image, $log, $avaMgr, $userMgr, $notiMgr) =
+ $this->getTestInstances();
$user = new User(
'user', 'cn=user', $this->access, $config, $filesys, $image, $log, $avaMgr, $userMgr, $notiMgr);
- $displayName = $user->composeAndStoreDisplayName($part1, $part2);
- $this->assertSame($expected, $displayName);
+ $displayName = 'Randall Flagg';
+ $config->expects($this->never())
+ ->method('setUserValue');
+ $config->expects($this->once())
+ ->method('getUserValue')
+ ->willReturn($displayName);
+
+ $userMgr->expects($this->never())
+ ->method('get'); // Implicit: no triggerChange can be called
+
+ $composedDisplayName = $user->composeAndStoreDisplayName($displayName);
+ $this->assertSame($composedDisplayName, $displayName);
}
public function testHandlePasswordExpiryWarningDefaultPolicy() {