]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - tests/NicknameTest.php
Use the profile URI when linking instead of URL
[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('GNUSOCIAL', true);
10 define('STATUSNET', true);  // compatibility
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                 if ($exception) {
37                     $stuff = get_class($exception) . ': ' . $exception->getMessage();
38                 } else {
39                     $stuff = var_export($exception, true);
40                 }
41                 $this->assertTrue($exception && $exception instanceof $expectedException,
42                         "invalid input '$input' expected to fail with $expectedException, " .
43                         "got $stuff");
44             } else {
45                 $this->assertTrue($normalized == false,
46                         "invalid input '$input' expected to fail");
47             }
48         } else {
49             $msg = "normalized input nickname '$input' expected to normalize to '$expected', got ";
50             if ($exception) {
51                 $msg .= get_class($exception) . ': ' . $exception->getMessage();
52             } else {
53                 $msg .= "'$normalized'";
54             }
55             $this->assertEquals($expected, $normalized, $msg);
56         }
57     }
58
59     /**
60      * Test on the regex matching used in common_find_mentions
61      * (testing on the full notice rendering is difficult as it needs
62      * to be able to pull from global state)
63      *
64      * @dataProvider provider
65      */
66     public function testAtReply($input, $expected, $expectedException=null)
67     {
68         if ($expected == false) {
69             // nothing to do
70         } else {
71             $text = "@{$input} awesome! :)";
72             $matches = common_find_mentions_raw($text);
73             $this->assertEquals(1, count($matches));
74             $this->assertEquals($expected, Nickname::normalize($matches[0][0]));
75         }
76     }
77
78     static public function provider()
79     {
80         return array(
81                      array('evan', 'evan'),
82
83                      // Case and underscore variants
84                      array('Evan', 'evan'),
85                      array('EVAN', 'evan'),
86                      array('ev_an', 'evan'),
87                      array('E__V_an', 'evan'),
88                      array('evan1', 'evan1'),
89                      array('evan_1', 'evan1'),
90                      array('0x20', '0x20'),
91                      array('1234', '1234'), // should this be allowed though? :)
92                      array('12__34', '1234'),
93
94                      // Some (currently) invalid chars...
95                      array('^#@&^#@', false, 'NicknameInvalidException'), // all invalid :D
96                      array('ev.an', false, 'NicknameInvalidException'),
97                      array('ev/an', false, 'NicknameInvalidException'),
98                      array('ev an', false, 'NicknameInvalidException'),
99                      array('ev-an', false, 'NicknameInvalidException'),
100
101                      // Non-ASCII letters; currently not allowed, in future
102                      // we'll add them at least with conversion to ASCII.
103                      // Not much use until we have storage of display names,
104                      // though.
105                      array('évan', false, 'NicknameInvalidException'), // so far...
106                      array('Évan', false, 'NicknameInvalidException'), // so far...
107
108                      // Length checks
109                      array('', false, 'NicknameEmptyException'),
110                      array('___', false, 'NicknameEmptyException'),
111                      array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', 'eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee'), // 64 chars
112                      array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee_', false, 'NicknameTooLongException'), // the _ is too long...
113                      array('eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', false, 'NicknameTooLongException'), // 65 chars -- too long
114                      );
115     }
116 }