From 2877c1027a05ab516439f4917cfc25c84b2bef10 Mon Sep 17 00:00:00 2001
From: Dries Buytaert <dries@buytaert.net>
Date: Thu, 26 Jun 2008 19:47:02 +0000
Subject: [PATCH] - Patch #266488 by Damien Tournoud, nbz, Heine, MadHarold, et
 al: clean-up user_validate_name(), allow astrophes, removed some cruft and
 made the tests more compact.

---
 modules/user/user.test | 75 ++++++++++++++++++++----------------------
 1 file changed, 36 insertions(+), 39 deletions(-)

diff --git a/modules/user/user.test b/modules/user/user.test
index 2272a17d7f70..784735fa8ac7 100644
--- a/modules/user/user.test
+++ b/modules/user/user.test
@@ -116,47 +116,44 @@ class UserValidationTestCase extends DrupalWebTestCase {
   }
 
   // Username validation.
-  function testMinLengthName() {
-    $name = '';
-    $result = user_validate_name($name);
-    $this->assertNotNull($result, 'Excessively short username');
-  }
-
-  function testValidCharsName() {
-    $name = 'ab/';
-    $result = user_validate_name($name);
-    $this->assertNotNull($result, 'Invalid chars in username');
-  }
-
-  function testMaxLengthName() {
-    $name = str_repeat('a', 61);
-    $result = user_validate_name($name);
-    $this->assertNotNull($result, 'Excessively long username');
-  }
-
-  function testValidName() {
-    $name = 'abc';
-    $result = user_validate_name($name);
-    $this->assertNull($result, 'Valid username');
-  }
-
-  // Mail validation.
-  function testMinLengthMail() {
-    $name = '';
-    $result = user_validate_mail($name);
-    $this->assertNotNull($result, 'Empty mail');
-  }
-
-  function testInValidMail() {
-    $name = 'abc';
-    $result = user_validate_mail($name);
-    $this->assertNotNull($result, 'Invalid mail');
+  function testUsernames() {
+    $test_cases = array( // '<username>' => array('<description>', 'assert<testName>'),
+      'foo'                    => array('Valid username', 'assertNull'),
+      'FOO'                    => array('Valid username', 'assertNull'),
+      'Foo O\'Bar'             => array('Valid username', 'assertNull'),
+      'foo@bar'                => array('Valid username', 'assertNull'),
+      'foo@example.com'        => array('Valid username', 'assertNull'),
+      'foo@-example.com'       => array('Valid username', 'assertNull'), // invalid domains are allowed in usernames
+      'þòøÇߪř€'               => array('Valid username', 'assertNull'),
+      'ᚠᛇᚻ᛫ᛒᛦᚦ'                => array('Valid UTF8 username', 'assertNull'), // runes
+      ' foo'                   => array('Username that starts with a space', 'assertNotNull'),
+      'foo '                   => array('Username that ends with a space', 'assertNotNull'),
+      'foo  bar'               => array('Username that contains 2 spaces \'&nbsp;&nbsp;\'', 'assertNotNull'),
+      ''                       => array('Empty username', 'assertNotNull'),
+      'foo/'                   => array('Invalid chars in username', 'assertNotNull'),
+      'foo' . chr(0) . 'bar'   => array('chr(0) in username', 'assertNotNull'), // NULL
+      'foo' . chr(13) . 'bar'  => array('chr(13) in username', 'assertNotNull'), // CR
+      str_repeat('x', USERNAME_MAX_LENGTH + 1) => array('Excessively long username', 'assertNotNull'),
+    );
+    foreach ($test_cases as $name => $test_case) {
+      list($description, $test) = $test_case;
+      $result = user_validate_name($name);
+      $this->$test($result, $description . ' ('. $name . '). %s');
+    }
   }
 
-  function testValidMail() {
-    $name = 'absdsdsdc@dsdsde.com';
-    $result = user_validate_mail($name);
-    $this->assertNull($result, 'Valid mail');
+  // Mail validation. More extensive tests can be found at common.test
+  function testMailAddresses() {
+    $test_cases = array( // '<username>' => array('<description>', 'assert<testName>'),
+      ''                => array('Empty mail address', 'assertNotNull'),
+      'foo'             => array('Invalid mail address', 'assertNotNull'),
+      'foo@example.com' => array('Valid mail address', 'assertNull'),
+    );
+    foreach ($test_cases as $name => $test_case) {
+      list($description, $test) = $test_case;
+      $result = user_validate_mail($name);
+      $this->$test($result, $description . ' (' . $name . '). %s');
+    }
   }
 }
 
-- 
GitLab