]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | /** | |
3 | * Utilities' tests | |
4 | */ | |
5 | ||
6 | require_once 'application/Utils.php'; | |
7 | require_once 'tests/utils/ReferenceSessionIdHashes.php'; | |
8 | ||
9 | // Initialize reference data before PHPUnit starts a session | |
10 | ReferenceSessionIdHashes::genAllHashes(); | |
11 | ||
12 | ||
13 | /** | |
14 | * Unitary tests for Shaarli utilities | |
15 | */ | |
16 | class UtilsTest extends PHPUnit_Framework_TestCase | |
17 | { | |
18 | // Session ID hashes | |
19 | protected static $sidHashes = null; | |
20 | ||
21 | /** | |
22 | * Assign reference data | |
23 | */ | |
24 | public static function setUpBeforeClass() | |
25 | { | |
26 | self::$sidHashes = ReferenceSessionIdHashes::getHashes(); | |
27 | } | |
28 | ||
29 | /** | |
30 | * Represent a link by its hash | |
31 | */ | |
32 | public function testSmallHash() | |
33 | { | |
34 | $this->assertEquals('CyAAJw', smallHash('http://test.io')); | |
35 | $this->assertEquals(6, strlen(smallHash('https://github.com'))); | |
36 | } | |
37 | ||
38 | /** | |
39 | * Look for a substring at the beginning of a string | |
40 | */ | |
41 | public function testStartsWithCaseInsensitive() | |
42 | { | |
43 | $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false)); | |
44 | $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false)); | |
45 | } | |
46 | ||
47 | /** | |
48 | * Look for a substring at the beginning of a string (case-sensitive) | |
49 | */ | |
50 | public function testStartsWithCaseSensitive() | |
51 | { | |
52 | $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true)); | |
53 | $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true)); | |
54 | $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true)); | |
55 | } | |
56 | ||
57 | /** | |
58 | * Look for a substring at the beginning of a string (Unicode) | |
59 | */ | |
60 | public function testStartsWithSpecialChars() | |
61 | { | |
62 | $this->assertTrue(startsWith('å!ùµ', 'å!', false)); | |
63 | $this->assertTrue(startsWith('µ$åù', 'µ$', true)); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Look for a substring at the end of a string | |
68 | */ | |
69 | public function testEndsWithCaseInsensitive() | |
70 | { | |
71 | $this->assertTrue(endsWith('Lorem ipsum', 'ipsum', false)); | |
72 | $this->assertTrue(endsWith('Lorem ipsum', 'm IpsUM', false)); | |
73 | } | |
74 | ||
75 | /** | |
76 | * Look for a substring at the end of a string (case-sensitive) | |
77 | */ | |
78 | public function testEndsWithCaseSensitive() | |
79 | { | |
80 | $this->assertTrue(endsWith('lorem Ipsum', 'Ipsum', true)); | |
81 | $this->assertFalse(endsWith('lorem Ipsum', 'ipsum', true)); | |
82 | $this->assertFalse(endsWith('lorem Ipsum', 'M IPsuM', true)); | |
83 | } | |
84 | ||
85 | /** | |
86 | * Look for a substring at the end of a string (Unicode) | |
87 | */ | |
88 | public function testEndsWithSpecialChars() | |
89 | { | |
90 | $this->assertTrue(endsWith('å!ùµ', 'ùµ', false)); | |
91 | $this->assertTrue(endsWith('µ$åù', 'åù', true)); | |
92 | } | |
93 | ||
94 | /** | |
95 | * Check valid date strings, according to a DateTime format | |
96 | */ | |
97 | public function testCheckValidDateFormat() | |
98 | { | |
99 | $this->assertTrue(checkDateFormat('Ymd', '20150627')); | |
100 | $this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27')); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Check erroneous date strings, according to a DateTime format | |
105 | */ | |
106 | public function testCheckInvalidDateFormat() | |
107 | { | |
108 | $this->assertFalse(checkDateFormat('Ymd', '2015')); | |
109 | $this->assertFalse(checkDateFormat('Y-m-d', '2015-06')); | |
110 | $this->assertFalse(checkDateFormat('Ymd', 'DeLorean')); | |
111 | } | |
112 | ||
113 | /** | |
114 | * Test generate location with valid data. | |
115 | */ | |
116 | public function testGenerateLocation() { | |
117 | $ref = 'http://localhost/?test'; | |
118 | $this->assertEquals($ref, generateLocation($ref, 'localhost')); | |
119 | $ref = 'http://localhost:8080/?test'; | |
120 | $this->assertEquals($ref, generateLocation($ref, 'localhost:8080')); | |
121 | $ref = '?localreferer#hash'; | |
122 | $this->assertEquals($ref, generateLocation($ref, 'localhost:8080')); | |
123 | } | |
124 | ||
125 | /** | |
126 | * Test generate location - anti loop. | |
127 | */ | |
128 | public function testGenerateLocationLoop() { | |
129 | $ref = 'http://localhost/?test'; | |
130 | $this->assertEquals('?', generateLocation($ref, 'localhost', array('test'))); | |
131 | } | |
132 | ||
133 | /** | |
134 | * Test generate location - from other domain. | |
135 | */ | |
136 | public function testGenerateLocationOut() { | |
137 | $ref = 'http://somewebsite.com/?test'; | |
138 | $this->assertEquals('?', generateLocation($ref, 'localhost')); | |
139 | } | |
140 | ||
141 | /** | |
142 | * Test is_session_id_valid with a valid ID - TEST ALL THE HASHES! | |
143 | * | |
144 | * This tests extensively covers all hash algorithms / bit representations | |
145 | */ | |
146 | public function testIsAnyHashSessionIdValid() | |
147 | { | |
148 | foreach (self::$sidHashes as $algo => $bpcs) { | |
149 | foreach ($bpcs as $bpc => $hash) { | |
150 | $this->assertTrue(is_session_id_valid($hash)); | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | /** | |
156 | * Test is_session_id_valid with a valid ID - SHA-1 hashes | |
157 | */ | |
158 | public function testIsSha1SessionIdValid() | |
159 | { | |
160 | $this->assertTrue(is_session_id_valid(sha1('shaarli'))); | |
161 | } | |
162 | ||
163 | /** | |
164 | * Test is_session_id_valid with a valid ID - SHA-256 hashes | |
165 | */ | |
166 | public function testIsSha256SessionIdValid() | |
167 | { | |
168 | $this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli'))); | |
169 | } | |
170 | ||
171 | /** | |
172 | * Test is_session_id_valid with a valid ID - SHA-512 hashes | |
173 | */ | |
174 | public function testIsSha512SessionIdValid() | |
175 | { | |
176 | $this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli'))); | |
177 | } | |
178 | ||
179 | /** | |
180 | * Test is_session_id_valid with invalid IDs. | |
181 | */ | |
182 | public function testIsSessionIdInvalid() | |
183 | { | |
184 | $this->assertFalse(is_session_id_valid('')); | |
185 | $this->assertFalse(is_session_id_valid(array())); | |
186 | $this->assertFalse( | |
187 | is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=') | |
188 | ); | |
189 | } | |
190 | ||
191 | /** | |
192 | * Test text2clickable without a redirector being set. | |
193 | */ | |
194 | public function testText2clickableWithoutRedirector() | |
195 | { | |
196 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
197 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here">http://hello.there/is=someone#here</a> otherstuff'; | |
198 | $processedText = text2clickable($text, ''); | |
199 | $this->assertEquals($expectedText, $processedText); | |
200 | } | |
201 | ||
202 | /** | |
203 | * Test text2clickable a redirector set. | |
204 | */ | |
205 | public function testText2clickableWithRedirector() | |
206 | { | |
207 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | |
208 | $redirector = 'http://redirector.to'; | |
209 | $expectedText = 'stuff <a href="'. | |
210 | $redirector . | |
211 | urlencode('http://hello.there/is=someone#here') . | |
212 | '">http://hello.there/is=someone#here</a> otherstuff'; | |
213 | $processedText = text2clickable($text, $redirector); | |
214 | $this->assertEquals($expectedText, $processedText); | |
215 | } | |
216 | ||
217 | /** | |
218 | * Test testSpace2nbsp. | |
219 | */ | |
220 | public function testSpace2nbsp() | |
221 | { | |
222 | $text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?'; | |
223 | $expectedText = ' Are you thrilled by flags ?'. PHP_EOL .' Really?'; | |
224 | $processedText = space2nbsp($text); | |
225 | $this->assertEquals($expectedText, $processedText); | |
226 | } | |
227 | } |