Fixed #5067 - account for only one name in generateFormattedNameFromFullName

This commit is contained in:
snipe 2018-02-22 14:10:58 -08:00
parent a85b38850c
commit cbd8409611
2 changed files with 28 additions and 10 deletions

View file

@ -330,19 +330,29 @@ class User extends SnipeModel implements AuthenticatableContract, CanResetPasswo
public static function generateFormattedNameFromFullName($format = 'filastname', $users_name) public static function generateFormattedNameFromFullName($format = 'filastname', $users_name)
{ {
list($first_name, $last_name) = explode(" ", $users_name, 2);
// Assume filastname by default // If there was only one name given
$username = str_slug(substr($first_name, 0, 1).$last_name); if (strpos($users_name, ' ') === false) {
$first_name = $users_name;
if ($format=='firstname.lastname') { $last_name = '';
$username = str_slug($first_name).'.'.str_slug($last_name); $username = $users_name;
} elseif ($format=='firstname_lastname') { } else {
$username = str_slug($first_name).'_'.str_slug($last_name);
} elseif ($format=='firstname') { list($first_name, $last_name) = explode(" ", $users_name, 2);
$username = str_slug($first_name);
// Assume filastname by default
$username = str_slug(substr($first_name, 0, 1).$last_name);
if ($format=='firstname.lastname') {
$username = str_slug($first_name).'.'.str_slug($last_name);
} elseif ($format=='firstname_lastname') {
$username = str_slug($first_name).'_'.str_slug($last_name);
} elseif ($format=='firstname') {
$username = str_slug($first_name);
}
} }
$user['first_name'] = $first_name; $user['first_name'] = $first_name;

View file

@ -71,5 +71,13 @@ class UserTest extends BaseTest
$this->assertEquals($expected_username, $user['username']); $this->assertEquals($expected_username, $user['username']);
} }
public function testSingleName()
{
$fullname = "Natalia";
$expected_username = 'natalia';
$user = User::generateFormattedNameFromFullName('firstname_lastname', $fullname);
$this->assertEquals($expected_username, $user['username']);
}
} }