Skip to content
This repository has been archived by the owner on Dec 11, 2020. It is now read-only.

Remove trailing dot in username if any #975

Merged
merged 4 commits into from
Feb 1, 2017
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
27 changes: 24 additions & 3 deletions src/Faker/Provider/Internet.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,18 @@ public function userName()
$format = static::randomElement(static::$userNameFormats);
$username = static::bothify($this->generator->parse($format));

return strtolower(static::transliterate($username));
$username = strtolower(static::transliterate($username));

// check if transliterate() didn't support the language and removed all letters
if (trim($username, '._') === '') {
throw new \Exception('userName failed with the selected locale. Try a different locale or activate the "intl" PHP extension.');
}

// clean possible trailing dots from first/last names
$username = str_replace('..', '.', $username);
$username = rtrim($username, '.');

return $username;
}
/**
* @example 'fY4èHdZv68'
Expand All @@ -120,7 +131,17 @@ public function domainWord()
{
$lastName = $this->generator->format('lastName');

return strtolower(static::transliterate($lastName));
$lastName = strtolower(static::transliterate($lastName));

// check if transliterate() didn't support the language and removed all letters
if (trim($lastName, '._') === '') {
throw new \Exception('domainWord failed with the selected locale. Try a different locale or activate the "intl" PHP extension.');
}

// clean possible trailing dot from last name
$lastName = rtrim($lastName, '.');

return $lastName;
}

/**
Expand Down Expand Up @@ -214,7 +235,7 @@ protected static function transliterate($string)
}

$transId = 'Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC;';
if (function_exists('transliterator_transliterate') && $transliterator = \Transliterator::create($transId)) {
if (class_exists('Transliterator') && $transliterator = \Transliterator::create($transId)) {
$transString = $transliterator->transliterate($string);
} else {
$transString = static::toAscii($string);
Expand Down
42 changes: 39 additions & 3 deletions test/Faker/Provider/InternetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,65 @@ public function localeDataProvider()
/**
* @link http://stackoverflow.com/questions/12026842/how-to-validate-an-email-address-in-php
*
* @requires PHP 5.4
* @dataProvider localeDataProvider
*/
public function testEmailIsValid($locale)
{
if ($locale !== 'en_US' && !class_exists('Transliterator')) {
$this->markTestSkipped('Transliterator class not available (intl extension)');
}

$this->loadLocalProviders($locale);
$pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}@)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';
$emailAddress = $this->faker->email();
$this->assertRegExp($pattern, $emailAddress);
}

/**
* @requires PHP 5.4
* @dataProvider localeDataProvider
*/
public function testUsernameIsValid($locale)
{
if ($locale !== 'en_US' && !class_exists('Transliterator')) {
$this->markTestSkipped('Transliterator class not available (intl extension)');
}

$this->loadLocalProviders($locale);
$pattern = '/^[A-Za-z0-9._]+$/';
$pattern = '/^[A-Za-z0-9]+([._][A-Za-z0-9]+)*$/';
$username = $this->faker->username();
$this->assertRegExp($pattern, $username);
}

/**
* @dataProvider localeDataProvider
*/
public function testDomainnameIsValid($locale)
{
if ($locale !== 'en_US' && !class_exists('Transliterator')) {
$this->markTestSkipped('Transliterator class not available (intl extension)');
}

$this->loadLocalProviders($locale);
$pattern = '/^[a-z]+(\.[a-z]+)+$/';
$domainName = $this->faker->domainName();
$this->assertRegExp($pattern, $domainName);
}

/**
* @dataProvider localeDataProvider
*/
public function testDomainwordIsValid($locale)
{
if ($locale !== 'en_US' && !class_exists('Transliterator')) {
$this->markTestSkipped('Transliterator class not available (intl extension)');
}

$this->loadLocalProviders($locale);
$pattern = '/^[a-z]+$/';
$domainWord = $this->faker->domainWord();
$this->assertRegExp($pattern, $domainWord);
}

public function loadLocalProviders($locale)
{
$providerPath = realpath(__DIR__ . '/../../../src/Faker/Provider');
Expand Down
4 changes: 4 additions & 0 deletions test/Faker/Provider/ProviderOverrideTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ public function testDateTime($locale = null)
*/
public function testInternet($locale = null)
{
if ($locale && $locale !== 'en_US' && !class_exists('Transliterator')) {
$this->markTestSkipped('Transliterator class not available (intl extension)');
}

$faker = Faker\Factory::create($locale);

$this->assertRegExp(static::TEST_STRING_REGEX, $faker->userName);
Expand Down