]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/NicknameTest.php
Fix ticket #2914: Realtime no longer tells you it's got a message queued up when...
[quix0rs-gnu-social.git] / tests / NicknameTest.php
1 <?php
2
3 if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
4     print "This script must be run from the command line\n";
5     exit();
6 }
7
8 define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
9 define('STATUSNET', true);
10 define('LACONICA', true);
11
12 require_once INSTALLDIR . '/lib/common.php';
13
14 /**
15  * Test cases for nickname validity and normalization.
16  */
17 class NicknameTest extends PHPUnit_Framework_TestCase
18 {
19     /**
20      * Basic test using Nickname::normalize()
21      *
22      * @dataProvider provider
23      */
24     public function testBasic($input, $expected, $expectedException=null)
25     {
26         $exception = null;
27         $normalized = false;
28         try {
29             $normalized = Nickname::normalize($input);
30         } catch (NicknameException $e) {
31             $exception = $e;
32         }
33
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());
39             } else {
40                 $this->assertTrue($normalized == false,
41                         "invalid input '$input' expected to fail");
42             }
43         } else {
44             $msg = "normalized input nickname '$input' expected to normalize to '$expected', got ";
45             if ($exception) {
46                 $msg .= get_class($exception) . ': ' . $exception->getMessage();
47             } else {
48                 $msg .= "'$normalized'";
49             }
50             $this->assertEquals($expected, $normalized, $msg);
51         }
52     }
53
54     /**
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)
58      *
59      * @dataProvider provider
60      */
61     public function testAtReply($input, $expected, $expectedException=null)
62     {
63         if ($expected == false) {
64             // nothing to do
65         } else {
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]));
70         }
71     }
72
73     static public function provider()
74     {
75         return array(
76                      array('evan', 'evan'),
77
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'),
88
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'),
95
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,
99                      // though.
100                      array('évan', false, 'NicknameInvalidException'), // so far...
101                      array('Évan', false, 'NicknameInvalidException'), // so far...
102
103                      // Length checks
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
109                      );
110     }
111 }