]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/UtilsTest.php
Merge pull request #842 from ArthurHoaro/cleanup/remove-riy-plugin
[github/shaarli/Shaarli.git] / tests / UtilsTest.php
CommitLineData
ca74886f
V
1<?php
2/**
3 * Utilities' tests
4 */
5
6require_once 'application/Utils.php';
68bc2135
V
7require_once 'tests/utils/ReferenceSessionIdHashes.php';
8
9// Initialize reference data before PHPUnit starts a session
10ReferenceSessionIdHashes::genAllHashes();
11
ca74886f
V
12
13/**
14 * Unitary tests for Shaarli utilities
15 */
16class UtilsTest extends PHPUnit_Framework_TestCase
17{
68bc2135
V
18 // Session ID hashes
19 protected static $sidHashes = null;
20
1abe6555
V
21 // Log file
22 protected static $testLogFile = 'tests.log';
23
24 // Expected log date format
478ce8af 25 protected static $dateFormat = 'Y/m/d H:i:s';
52b50310
A
26
27 /**
28 * @var string Save the current timezone.
29 */
30 protected static $defaultTimeZone;
31
1abe6555 32
68bc2135
V
33 /**
34 * Assign reference data
35 */
36 public static function setUpBeforeClass()
37 {
38 self::$sidHashes = ReferenceSessionIdHashes::getHashes();
52b50310
A
39 self::$defaultTimeZone = date_default_timezone_get();
40 // Timezone without DST for test consistency
41 date_default_timezone_set('Africa/Nairobi');
42 }
43
44 /**
45 * Reset the timezone
46 */
47 public static function tearDownAfterClass()
48 {
49 date_default_timezone_set(self::$defaultTimeZone);
68bc2135
V
50 }
51
1abe6555
V
52 /**
53 * Resets test data before each test
54 */
55 protected function setUp()
56 {
57 if (file_exists(self::$testLogFile)) {
58 unlink(self::$testLogFile);
59 }
60 }
61
62 /**
63 * Returns a list of the elements from the last logged entry
64 *
65 * @return list (date, ip address, message)
66 */
67 protected function getLastLogEntry()
68 {
69 $logFile = file(self::$testLogFile);
aa7f7b3e 70 return explode(' - ', trim(array_pop($logFile), PHP_EOL));
1abe6555
V
71 }
72
73 /**
74 * Log a message to a file - IPv4 client address
75 */
76 public function testLogmIp4()
77 {
78 $logMessage = 'IPv4 client connected';
79 logm(self::$testLogFile, '127.0.0.1', $logMessage);
80 list($date, $ip, $message) = $this->getLastLogEntry();
81
82 $this->assertInstanceOf(
83 'DateTime',
84 DateTime::createFromFormat(self::$dateFormat, $date)
85 );
86 $this->assertTrue(
87 filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false
88 );
89 $this->assertEquals($logMessage, $message);
90 }
91
92 /**
93 * Log a message to a file - IPv6 client address
94 */
95 public function testLogmIp6()
96 {
97 $logMessage = 'IPv6 client connected';
98 logm(self::$testLogFile, '2001:db8::ff00:42:8329', $logMessage);
99 list($date, $ip, $message) = $this->getLastLogEntry();
100
101 $this->assertInstanceOf(
102 'DateTime',
103 DateTime::createFromFormat(self::$dateFormat, $date)
104 );
105 $this->assertTrue(
106 filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false
107 );
108 $this->assertEquals($logMessage, $message);
109 }
110
ca74886f
V
111 /**
112 * Represent a link by its hash
113 */
114 public function testSmallHash()
115 {
116 $this->assertEquals('CyAAJw', smallHash('http://test.io'));
117 $this->assertEquals(6, strlen(smallHash('https://github.com')));
118 }
119
120 /**
121 * Look for a substring at the beginning of a string
122 */
123 public function testStartsWithCaseInsensitive()
124 {
125 $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
126 $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
127 }
128
129 /**
130 * Look for a substring at the beginning of a string (case-sensitive)
131 */
132 public function testStartsWithCaseSensitive()
133 {
134 $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true));
135 $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true));
136 $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true));
137 }
138
139 /**
140 * Look for a substring at the beginning of a string (Unicode)
141 */
142 public function testStartsWithSpecialChars()
143 {
144 $this->assertTrue(startsWith('å!ùµ', 'å!', false));
145 $this->assertTrue(startsWith('µ$åù', 'µ$', true));
146 }
147
148 /**
149 * Look for a substring at the end of a string
150 */
151 public function testEndsWithCaseInsensitive()
152 {
153 $this->assertTrue(endsWith('Lorem ipsum', 'ipsum', false));
154 $this->assertTrue(endsWith('Lorem ipsum', 'm IpsUM', false));
155 }
156
157 /**
158 * Look for a substring at the end of a string (case-sensitive)
159 */
160 public function testEndsWithCaseSensitive()
161 {
162 $this->assertTrue(endsWith('lorem Ipsum', 'Ipsum', true));
163 $this->assertFalse(endsWith('lorem Ipsum', 'ipsum', true));
164 $this->assertFalse(endsWith('lorem Ipsum', 'M IPsuM', true));
165 }
166
167 /**
168 * Look for a substring at the end of a string (Unicode)
169 */
170 public function testEndsWithSpecialChars()
171 {
172 $this->assertTrue(endsWith('å!ùµ', 'ùµ', false));
173 $this->assertTrue(endsWith('µ$åù', 'åù', true));
174 }
9186ab95
V
175
176 /**
177 * Check valid date strings, according to a DateTime format
178 */
179 public function testCheckValidDateFormat()
180 {
181 $this->assertTrue(checkDateFormat('Ymd', '20150627'));
182 $this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27'));
183 }
184
185 /**
186 * Check erroneous date strings, according to a DateTime format
187 */
188 public function testCheckInvalidDateFormat()
189 {
190 $this->assertFalse(checkDateFormat('Ymd', '2015'));
191 $this->assertFalse(checkDateFormat('Y-m-d', '2015-06'));
192 $this->assertFalse(checkDateFormat('Ymd', 'DeLorean'));
193 }
775803a0
A
194
195 /**
196 * Test generate location with valid data.
197 */
198 public function testGenerateLocation() {
199 $ref = 'http://localhost/?test';
200 $this->assertEquals($ref, generateLocation($ref, 'localhost'));
201 $ref = 'http://localhost:8080/?test';
202 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
d01c2342
A
203 $ref = '?localreferer#hash';
204 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
775803a0
A
205 }
206
207 /**
208 * Test generate location - anti loop.
209 */
210 public function testGenerateLocationLoop() {
211 $ref = 'http://localhost/?test';
d1e2f8e5 212 $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
775803a0
A
213 }
214
215 /**
216 * Test generate location - from other domain.
217 */
218 public function testGenerateLocationOut() {
219 $ref = 'http://somewebsite.com/?test';
220 $this->assertEquals('?', generateLocation($ref, 'localhost'));
221 }
d1e2f8e5 222
06b6660a 223 /**
68bc2135
V
224 * Test is_session_id_valid with a valid ID - TEST ALL THE HASHES!
225 *
226 * This tests extensively covers all hash algorithms / bit representations
227 */
228 public function testIsAnyHashSessionIdValid()
229 {
230 foreach (self::$sidHashes as $algo => $bpcs) {
231 foreach ($bpcs as $bpc => $hash) {
232 $this->assertTrue(is_session_id_valid($hash));
233 }
234 }
235 }
236
237 /**
238 * Test is_session_id_valid with a valid ID - SHA-1 hashes
239 */
240 public function testIsSha1SessionIdValid()
241 {
242 $this->assertTrue(is_session_id_valid(sha1('shaarli')));
243 }
244
245 /**
246 * Test is_session_id_valid with a valid ID - SHA-256 hashes
247 */
248 public function testIsSha256SessionIdValid()
249 {
250 $this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli')));
251 }
252
253 /**
254 * Test is_session_id_valid with a valid ID - SHA-512 hashes
06b6660a 255 */
68bc2135 256 public function testIsSha512SessionIdValid()
06b6660a 257 {
68bc2135 258 $this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli')));
06b6660a
A
259 }
260
261 /**
262 * Test is_session_id_valid with invalid IDs.
263 */
264 public function testIsSessionIdInvalid()
265 {
266 $this->assertFalse(is_session_id_valid(''));
267 $this->assertFalse(is_session_id_valid(array()));
68bc2135
V
268 $this->assertFalse(
269 is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
270 );
06b6660a 271 }
b3051a6a 272
cbfdcff2
A
273 /**
274 * Test generateSecretApi.
275 */
276 public function testGenerateSecretApi()
277 {
278 $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar')));
279 }
280
281 /**
282 * Test generateSecretApi with invalid parameters.
283 */
284 public function testGenerateSecretApiInvalid()
285 {
286 $this->assertFalse(generate_api_secret('', ''));
287 $this->assertFalse(generate_api_secret(false, false));
288 }
b3051a6a
A
289
290 /**
291 * Test normalize_spaces.
292 */
293 public function testNormalizeSpace()
294 {
295 $str = ' foo bar is important ';
296 $this->assertEquals('foo bar is important', normalize_spaces($str));
297 $this->assertEquals('foo', normalize_spaces('foo'));
298 $this->assertEquals('', normalize_spaces(''));
299 $this->assertEquals(null, normalize_spaces(null));
300 }
1255a42c
A
301
302 /**
303 * Test arrays_combine
304 */
52b50310 305 public function testCartesianProductGenerator()
1255a42c
A
306 {
307 $arr = [['ab', 'cd'], ['ef', 'gh'], ['ij', 'kl'], ['m']];
308 $expected = [
52b50310
A
309 ['ab', 'ef', 'ij', 'm'],
310 ['ab', 'ef', 'kl', 'm'],
311 ['ab', 'gh', 'ij', 'm'],
312 ['ab', 'gh', 'kl', 'm'],
313 ['cd', 'ef', 'ij', 'm'],
314 ['cd', 'ef', 'kl', 'm'],
315 ['cd', 'gh', 'ij', 'm'],
316 ['cd', 'gh', 'kl', 'm'],
1255a42c 317 ];
52b50310 318 $this->assertEquals($expected, iterator_to_array(cartesian_product_generator($arr)));
1255a42c
A
319 }
320
52b50310
A
321 /**
322 * Test date_format() with invalid parameter.
323 */
324 public function testDateFormatInvalid()
325 {
326 $this->assertFalse(format_date([]));
327 $this->assertFalse(format_date(null));
328 }
ca74886f 329}