Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
O
our_own_cloud_project
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
die_coolen_jungs
our_own_cloud_project
Commits
239bff57
Commit
239bff57
authored
10 years ago
by
Björn Schießle
Browse files
Options
Downloads
Patches
Plain Diff
strip whitespace from the beginning and end of the display name to avoid empty display names
parent
6fa03870
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
lib/private/user/user.php
+12
-2
12 additions, 2 deletions
lib/private/user/user.php
tests/lib/user/user.php
+46
-0
46 additions, 0 deletions
tests/lib/user/user.php
with
58 additions
and
2 deletions
lib/private/user/user.php
+
12
−
2
View file @
239bff57
...
...
@@ -89,8 +89,17 @@ class User implements IUser {
*/
public
function
getDisplayName
()
{
if
(
!
isset
(
$this
->
displayName
))
{
$displayName
=
''
;
if
(
$this
->
backend
and
$this
->
backend
->
implementsActions
(
OC_USER_BACKEND_GET_DISPLAYNAME
))
{
$this
->
displayName
=
$this
->
backend
->
getDisplayName
(
$this
->
uid
);
// get display name and strip whitespace from the beginning and end of it
$backendDisplayName
=
$this
->
backend
->
getDisplayName
(
$this
->
uid
);
if
(
is_string
(
$backendDisplayName
))
{
$displayName
=
trim
(
$backendDisplayName
);
}
}
if
(
!
empty
(
$displayName
))
{
$this
->
displayName
=
$displayName
;
}
else
{
$this
->
displayName
=
$this
->
uid
;
}
...
...
@@ -105,7 +114,8 @@ class User implements IUser {
* @return bool
*/
public
function
setDisplayName
(
$displayName
)
{
if
(
$this
->
canChangeDisplayName
())
{
$displayName
=
trim
(
$displayName
);
if
(
$this
->
canChangeDisplayName
()
&&
!
empty
(
$displayName
))
{
$this
->
displayName
=
$displayName
;
$result
=
$this
->
backend
->
setDisplayName
(
$this
->
uid
,
$displayName
);
return
$result
!==
false
;
...
...
This diff is collapsed.
Click to expand it.
tests/lib/user/user.php
+
46
−
0
View file @
239bff57
...
...
@@ -32,6 +32,28 @@ class User extends \PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'Foo'
,
$user
->
getDisplayName
());
}
/**
* if the display name contain whitespaces only, we expect the uid as result
*/
public
function
testDisplayNameEmpty
()
{
/**
* @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
*/
$backend
=
$this
->
getMock
(
'\OC_User_Backend'
);
$backend
->
expects
(
$this
->
once
())
->
method
(
'getDisplayName'
)
->
with
(
$this
->
equalTo
(
'foo'
))
->
will
(
$this
->
returnValue
(
' '
));
$backend
->
expects
(
$this
->
any
())
->
method
(
'implementsActions'
)
->
with
(
$this
->
equalTo
(
\OC_USER_BACKEND_GET_DISPLAYNAME
))
->
will
(
$this
->
returnValue
(
true
));
$user
=
new
\OC\User\User
(
'foo'
,
$backend
);
$this
->
assertEquals
(
'foo'
,
$user
->
getDisplayName
());
}
public
function
testDisplayNameNotSupported
()
{
/**
* @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
...
...
@@ -305,6 +327,30 @@ class User extends \PHPUnit_Framework_TestCase {
$this
->
assertEquals
(
'Foo'
,
$user
->
getDisplayName
());
}
/**
* don't allow display names containing whitespaces only
*/
public
function
testSetDisplayNameEmpty
()
{
/**
* @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
*/
$backend
=
$this
->
getMock
(
'\OC_User_Database'
);
$backend
->
expects
(
$this
->
any
())
->
method
(
'implementsActions'
)
->
will
(
$this
->
returnCallback
(
function
(
$actions
)
{
if
(
$actions
===
\OC_USER_BACKEND_SET_DISPLAYNAME
)
{
return
true
;
}
else
{
return
false
;
}
}));
$user
=
new
\OC\User\User
(
'foo'
,
$backend
);
$this
->
assertFalse
(
$user
->
setDisplayName
(
' '
));
$this
->
assertEquals
(
'foo'
,
$user
->
getDisplayName
());
}
public
function
testSetDisplayNameNotSupported
()
{
/**
* @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment