[Pkg-owncloud-commits] [owncloud] 09/46: strip whitespace from the beginning and end of the display name to avoid empty display names

David Prévot taffit at moszumanska.debian.org
Fri Oct 24 15:11:40 UTC 2014


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch master
in repository owncloud.

commit 6e3a7ea140a41c804f55c7f03c92f33a48e53c43
Author: Bjoern Schiessle <schiessle at owncloud.com>
Date:   Wed Oct 15 14:05:18 2014 +0200

    strip whitespace from the beginning and end of the display name to avoid empty display names
---
 lib/private/user/user.php | 14 ++++++++++++--
 tests/lib/user/user.php   | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 58 insertions(+), 2 deletions(-)

diff --git a/lib/private/user/user.php b/lib/private/user/user.php
index 993fb4c..452261a 100644
--- a/lib/private/user/user.php
+++ b/lib/private/user/user.php
@@ -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;
diff --git a/tests/lib/user/user.php b/tests/lib/user/user.php
index 3f90432..7a1db86 100644
--- a/tests/lib/user/user.php
+++ b/tests/lib/user/user.php
@@ -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

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-owncloud/owncloud.git



More information about the Pkg-owncloud-commits mailing list