]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/UtilsTest.php
Merge pull request #1698 from ArthurHoaro/feature/plugins-search-filter
[github/shaarli/Shaarli.git] / tests / UtilsTest.php
CommitLineData
ca74886f
V
1<?php
2/**
3 * Utilities' tests
4 */
5
6require_once 'application/Utils.php';
84315a3b 7require_once 'application/Languages.php';
68bc2135 8
ca74886f
V
9
10/**
11 * Unitary tests for Shaarli utilities
12 */
a5a9cf23 13class UtilsTest extends \Shaarli\TestCase
ca74886f 14{
1abe6555
V
15 // Log file
16 protected static $testLogFile = 'tests.log';
17
18 // Expected log date format
478ce8af 19 protected static $dateFormat = 'Y/m/d H:i:s';
52b50310
A
20
21 /**
22 * @var string Save the current timezone.
23 */
24 protected static $defaultTimeZone;
25
68bc2135
V
26 /**
27 * Assign reference data
28 */
8f60e120 29 public static function setUpBeforeClass(): void
68bc2135 30 {
52b50310
A
31 self::$defaultTimeZone = date_default_timezone_get();
32 // Timezone without DST for test consistency
33 date_default_timezone_set('Africa/Nairobi');
34 }
35
36 /**
37 * Reset the timezone
38 */
8f60e120 39 public static function tearDownAfterClass(): void
52b50310
A
40 {
41 date_default_timezone_set(self::$defaultTimeZone);
68bc2135
V
42 }
43
1abe6555
V
44 /**
45 * Resets test data before each test
46 */
8f60e120 47 protected function setUp(): void
1abe6555
V
48 {
49 if (file_exists(self::$testLogFile)) {
50 unlink(self::$testLogFile);
51 }
52 }
53
54 /**
55 * Returns a list of the elements from the last logged entry
56 *
57 * @return list (date, ip address, message)
58 */
59 protected function getLastLogEntry()
60 {
61 $logFile = file(self::$testLogFile);
aa7f7b3e 62 return explode(' - ', trim(array_pop($logFile), PHP_EOL));
1abe6555
V
63 }
64
65 /**
b38a1b02 66 * Format a log a message - IPv4 client address
1abe6555 67 */
b38a1b02 68 public function testFormatLogIp4()
1abe6555 69 {
b38a1b02
A
70 $message = 'IPv4 client connected';
71 $log = format_log($message, '127.0.0.1');
1abe6555 72
b38a1b02 73 static::assertSame('- 127.0.0.1 - IPv4 client connected', $log);
1abe6555
V
74 }
75
76 /**
b38a1b02 77 * Format a log a message - IPv6 client address
1abe6555 78 */
b38a1b02 79 public function testFormatLogIp6()
1abe6555 80 {
b38a1b02
A
81 $message = 'IPv6 client connected';
82 $log = format_log($message, '2001:db8::ff00:42:8329');
1abe6555 83
b38a1b02 84 static::assertSame('- 2001:db8::ff00:42:8329 - IPv6 client connected', $log);
1abe6555
V
85 }
86
ca74886f
V
87 /**
88 * Represent a link by its hash
89 */
90 public function testSmallHash()
91 {
92 $this->assertEquals('CyAAJw', smallHash('http://test.io'));
93 $this->assertEquals(6, strlen(smallHash('https://github.com')));
94 }
95
96 /**
97 * Look for a substring at the beginning of a string
98 */
99 public function testStartsWithCaseInsensitive()
100 {
101 $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
102 $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
103 }
104
105 /**
106 * Look for a substring at the beginning of a string (case-sensitive)
107 */
108 public function testStartsWithCaseSensitive()
109 {
110 $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true));
111 $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true));
112 $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true));
113 }
114
115 /**
116 * Look for a substring at the beginning of a string (Unicode)
117 */
118 public function testStartsWithSpecialChars()
119 {
120 $this->assertTrue(startsWith('å!ùµ', 'å!', false));
121 $this->assertTrue(startsWith('µ$åù', 'µ$', true));
122 }
123
124 /**
125 * Look for a substring at the end of a string
126 */
127 public function testEndsWithCaseInsensitive()
128 {
129 $this->assertTrue(endsWith('Lorem ipsum', 'ipsum', false));
130 $this->assertTrue(endsWith('Lorem ipsum', 'm IpsUM', false));
131 }
132
133 /**
134 * Look for a substring at the end of a string (case-sensitive)
135 */
136 public function testEndsWithCaseSensitive()
137 {
138 $this->assertTrue(endsWith('lorem Ipsum', 'Ipsum', true));
139 $this->assertFalse(endsWith('lorem Ipsum', 'ipsum', true));
140 $this->assertFalse(endsWith('lorem Ipsum', 'M IPsuM', true));
141 }
142
143 /**
144 * Look for a substring at the end of a string (Unicode)
145 */
146 public function testEndsWithSpecialChars()
147 {
148 $this->assertTrue(endsWith('å!ùµ', 'ùµ', false));
149 $this->assertTrue(endsWith('µ$åù', 'åù', true));
150 }
9186ab95
V
151
152 /**
153 * Check valid date strings, according to a DateTime format
154 */
155 public function testCheckValidDateFormat()
156 {
157 $this->assertTrue(checkDateFormat('Ymd', '20150627'));
158 $this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27'));
159 }
160
161 /**
162 * Check erroneous date strings, according to a DateTime format
163 */
164 public function testCheckInvalidDateFormat()
165 {
166 $this->assertFalse(checkDateFormat('Ymd', '2015'));
167 $this->assertFalse(checkDateFormat('Y-m-d', '2015-06'));
168 $this->assertFalse(checkDateFormat('Ymd', 'DeLorean'));
169 }
775803a0
A
170
171 /**
172 * Test generate location with valid data.
173 */
067c2dd8
V
174 public function testGenerateLocation()
175 {
775803a0
A
176 $ref = 'http://localhost/?test';
177 $this->assertEquals($ref, generateLocation($ref, 'localhost'));
178 $ref = 'http://localhost:8080/?test';
179 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
d01c2342
A
180 $ref = '?localreferer#hash';
181 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
775803a0
A
182 }
183
184 /**
185 * Test generate location - anti loop.
186 */
067c2dd8
V
187 public function testGenerateLocationLoop()
188 {
775803a0 189 $ref = 'http://localhost/?test';
9e4cc28e 190 $this->assertEquals('./?', generateLocation($ref, 'localhost', array('test')));
775803a0
A
191 }
192
193 /**
194 * Test generate location - from other domain.
195 */
067c2dd8
V
196 public function testGenerateLocationOut()
197 {
775803a0 198 $ref = 'http://somewebsite.com/?test';
9e4cc28e 199 $this->assertEquals('./?', generateLocation($ref, 'localhost'));
775803a0 200 }
d1e2f8e5 201
68bc2135 202
cbfdcff2
A
203 /**
204 * Test generateSecretApi.
205 */
206 public function testGenerateSecretApi()
207 {
208 $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar')));
209 }
210
211 /**
212 * Test generateSecretApi with invalid parameters.
213 */
214 public function testGenerateSecretApiInvalid()
215 {
216 $this->assertFalse(generate_api_secret('', ''));
217 $this->assertFalse(generate_api_secret(false, false));
218 }
b3051a6a
A
219
220 /**
221 * Test normalize_spaces.
222 */
223 public function testNormalizeSpace()
224 {
225 $str = ' foo bar is important ';
226 $this->assertEquals('foo bar is important', normalize_spaces($str));
227 $this->assertEquals('foo', normalize_spaces('foo'));
228 $this->assertEquals('', normalize_spaces(''));
229 $this->assertEquals(null, normalize_spaces(null));
230 }
1255a42c
A
231
232 /**
233 * Test arrays_combine
234 */
52b50310 235 public function testCartesianProductGenerator()
1255a42c
A
236 {
237 $arr = [['ab', 'cd'], ['ef', 'gh'], ['ij', 'kl'], ['m']];
238 $expected = [
52b50310
A
239 ['ab', 'ef', 'ij', 'm'],
240 ['ab', 'ef', 'kl', 'm'],
241 ['ab', 'gh', 'ij', 'm'],
242 ['ab', 'gh', 'kl', 'm'],
243 ['cd', 'ef', 'ij', 'm'],
244 ['cd', 'ef', 'kl', 'm'],
245 ['cd', 'gh', 'ij', 'm'],
246 ['cd', 'gh', 'kl', 'm'],
1255a42c 247 ];
52b50310 248 $this->assertEquals($expected, iterator_to_array(cartesian_product_generator($arr)));
1255a42c
A
249 }
250
52b50310
A
251 /**
252 * Test date_format() with invalid parameter.
253 */
254 public function testDateFormatInvalid()
255 {
256 $this->assertFalse(format_date([]));
257 $this->assertFalse(format_date(null));
258 }
84315a3b
A
259
260 /**
261 * Test is_integer_mixed with valid values
262 */
263 public function testIsIntegerMixedValid()
264 {
265 $this->assertTrue(is_integer_mixed(12));
266 $this->assertTrue(is_integer_mixed('12'));
267 $this->assertTrue(is_integer_mixed(-12));
268 $this->assertTrue(is_integer_mixed('-12'));
269 $this->assertTrue(is_integer_mixed(0));
270 $this->assertTrue(is_integer_mixed('0'));
271 $this->assertTrue(is_integer_mixed(0x0a));
272 }
273
274 /**
275 * Test is_integer_mixed with invalid values
276 */
277 public function testIsIntegerMixedInvalid()
278 {
279 $this->assertFalse(is_integer_mixed(true));
280 $this->assertFalse(is_integer_mixed(false));
281 $this->assertFalse(is_integer_mixed([]));
282 $this->assertFalse(is_integer_mixed(['test']));
283 $this->assertFalse(is_integer_mixed([12]));
284 $this->assertFalse(is_integer_mixed(new DateTime()));
285 $this->assertFalse(is_integer_mixed('0x0a'));
286 $this->assertFalse(is_integer_mixed('12k'));
287 $this->assertFalse(is_integer_mixed('k12'));
288 $this->assertFalse(is_integer_mixed(''));
289 }
290
291 /**
292 * Test return_bytes
293 */
294 public function testReturnBytes()
295 {
296 $this->assertEquals(2 * 1024, return_bytes('2k'));
297 $this->assertEquals(2 * 1024, return_bytes('2K'));
b68134ac
A
298 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2m'));
299 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2M'));
300 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2g'));
301 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2G'));
84315a3b
A
302 $this->assertEquals(374, return_bytes('374'));
303 $this->assertEquals(374, return_bytes(374));
304 $this->assertEquals(0, return_bytes('0'));
305 $this->assertEquals(0, return_bytes(0));
306 $this->assertEquals(-1, return_bytes('-1'));
307 $this->assertEquals(-1, return_bytes(-1));
308 $this->assertEquals('', return_bytes(''));
309 }
310
311 /**
312 * Test human_bytes
313 */
314 public function testHumanBytes()
315 {
12266213
A
316 $this->assertEquals('2'. t('kiB'), human_bytes(2 * 1024));
317 $this->assertEquals('2'. t('kiB'), human_bytes(strval(2 * 1024)));
318 $this->assertEquals('2'. t('MiB'), human_bytes(2 * (pow(1024, 2))));
319 $this->assertEquals('2'. t('MiB'), human_bytes(strval(2 * (pow(1024, 2)))));
320 $this->assertEquals('2'. t('GiB'), human_bytes(2 * (pow(1024, 3))));
321 $this->assertEquals('2'. t('GiB'), human_bytes(strval(2 * (pow(1024, 3)))));
322 $this->assertEquals('374'. t('B'), human_bytes(374));
323 $this->assertEquals('374'. t('B'), human_bytes('374'));
324 $this->assertEquals('232'. t('kiB'), human_bytes(237481));
325 $this->assertEquals(t('Unlimited'), human_bytes('0'));
326 $this->assertEquals(t('Unlimited'), human_bytes(0));
327 $this->assertEquals(t('Setting not set'), human_bytes(''));
84315a3b
A
328 }
329
330 /**
6a19124a 331 * Test get_max_upload_size with formatting
84315a3b
A
332 */
333 public function testGetMaxUploadSize()
334 {
12266213
A
335 $this->assertEquals('1'. t('MiB'), get_max_upload_size(2097152, '1024k'));
336 $this->assertEquals('1'. t('MiB'), get_max_upload_size('1m', '2m'));
337 $this->assertEquals('100'. t('B'), get_max_upload_size(100, 100));
84315a3b 338 }
6a19124a
A
339
340 /**
341 * Test get_max_upload_size without formatting
342 */
343 public function testGetMaxUploadSizeRaw()
344 {
345 $this->assertEquals('1048576', get_max_upload_size(2097152, '1024k', false));
346 $this->assertEquals('1048576', get_max_upload_size('1m', '2m', false));
347 $this->assertEquals('100', get_max_upload_size(100, 100, false));
348 }
aa4797ba
A
349
350 /**
351 * Test alphabetical_sort by value, not reversed, with php-intl.
352 */
353 public function testAlphabeticalSortByValue()
354 {
355 $arr = [
356 'zZz',
357 'éee',
358 'éae',
359 'eee',
360 'A',
361 'a',
362 'zzz',
363 ];
364 $expected = [
365 'a',
366 'A',
367 'éae',
368 'eee',
369 'éee',
370 'zzz',
371 'zZz',
372 ];
373
374 alphabetical_sort($arr);
375 $this->assertEquals($expected, $arr);
376 }
377
378 /**
379 * Test alphabetical_sort by value, reversed, with php-intl.
380 */
381 public function testAlphabeticalSortByValueReversed()
382 {
383 $arr = [
384 'zZz',
385 'éee',
386 'éae',
387 'eee',
388 'A',
389 'a',
390 'zzz',
391 ];
392 $expected = [
393 'zZz',
394 'zzz',
395 'éee',
396 'eee',
397 'éae',
398 'A',
399 'a',
400 ];
401
402 alphabetical_sort($arr, true);
403 $this->assertEquals($expected, $arr);
404 }
405
406 /**
407 * Test alphabetical_sort by keys, not reversed, with php-intl.
408 */
409 public function testAlphabeticalSortByKeys()
410 {
411 $arr = [
412 'zZz' => true,
413 'éee' => true,
414 'éae' => true,
415 'eee' => true,
416 'A' => true,
417 'a' => true,
418 'zzz' => true,
419 ];
420 $expected = [
421 'a' => true,
422 'A' => true,
423 'éae' => true,
424 'eee' => true,
425 'éee' => true,
426 'zzz' => true,
427 'zZz' => true,
428 ];
429
430 alphabetical_sort($arr, true, true);
431 $this->assertEquals($expected, $arr);
432 }
433
434 /**
435 * Test alphabetical_sort by keys, reversed, with php-intl.
436 */
437 public function testAlphabeticalSortByKeysReversed()
438 {
439 $arr = [
440 'zZz' => true,
441 'éee' => true,
442 'éae' => true,
443 'eee' => true,
444 'A' => true,
445 'a' => true,
446 'zzz' => true,
447 ];
448 $expected = [
449 'zZz' => true,
450 'zzz' => true,
451 'éee' => true,
452 'eee' => true,
453 'éae' => true,
454 'A' => true,
455 'a' => true,
456 ];
457
458 alphabetical_sort($arr, true, true);
459 $this->assertEquals($expected, $arr);
460 }
ca74886f 461}