3 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
4 print "This script must be run from the command line\n";
8 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
9 define('STATUSNET', true);
10 define('LACONICA', true);
12 require_once INSTALLDIR . '/lib/common.php';
15 * Test cases for nickname validity and normalization.
17 class NicknameTest extends PHPUnit_Framework_TestCase
20 * Basic test using Nickname::normalize()
22 * @dataProvider provider
24 public function testBasic($input, $expected, $expectedException=null)
29 $normalized = Nickname::normalize($input);
30 } catch (NicknameException $e) {
34 if ($expected === false) {
35 if ($expectedException) {
36 $this->assertTrue($exception && $exception instanceof $expectedException,
37 "invalid input '$input' expected to fail with $expectedException, " .
38 "got " . get_class($exception) . ': ' . $exception->getMessage());
40 $this->assertTrue($normalized == false,
41 "invalid input '$input' expected to fail");
44 $msg = "normalized input nickname '$input' expected to normalize to '$expected', got ";
46 $msg .= get_class($exception) . ': ' . $exception->getMessage();
48 $msg .= "'$normalized'";
50 $this->assertEquals($expected, $normalized, $msg);
55 * Test on the regex matching used in common_find_mentions
56 * (testing on the full notice rendering is difficult as it needs
57 * to be able to pull from global state)
59 * @dataProvider provider
61 public function testAtReply($input, $expected, $expectedException=null)
63 if ($expected == false) {
66 $text = "@{$input} awesome! :)";
67 $matches = common_find_mentions_raw($text);
68 $this->assertEquals(1, count($matches));
69 $this->assertEquals($expected, Nickname::normalize($matches[0][0]));
73 static public function provider()
76 array('evan', 'evan'),
78 // Case and underscore variants
79 array('Evan', 'evan'),
80 array('EVAN', 'evan'),
81 array('ev_an', 'evan'),
82 array('E__V_an', 'evan'),
83 array('evan1', 'evan1'),
84 array('evan_1', 'evan1'),
85 array('0x20', '0x20'),
86 array('1234', '1234'), // should this be allowed though? :)
87 array('12__34', '1234'),
89 // Some (currently) invalid chars...
90 array('^#@&^#@', false, 'NicknameInvalidException'), // all invalid :D
91 array('ev.an', false, 'NicknameInvalidException'),
92 array('ev/an', false, 'NicknameInvalidException'),
93 array('ev an', false, 'NicknameInvalidException'),
94 array('ev-an', false, 'NicknameInvalidException'),
96 // Non-ASCII letters; currently not allowed, in future
97 // we'll add them at least with conversion to ASCII.
98 // Not much use until we have storage of display names,
100 array('évan', false, 'NicknameInvalidException'), // so far...
101 array('Évan', false, 'NicknameInvalidException'), // so far...
104 array('', false, 'NicknameEmptyException'),
105 array('___', false, 'NicknameEmptyException'),
106 array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'), // 64 chars
107 array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee_', 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'), // the _ will be trimmed off, remaining valid
108 array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', false, 'NicknameTooLongException'), // 65 chars -- too long