]>
git.immae.eu Git - github/shaarli/Shaarli.git/blob - UtilsTest.php
59dca75f572f3d92948211e72e1487f365598b6c
6 require_once 'application/Utils.php';
7 require_once 'application/Languages.php';
11 * Unitary tests for Shaarli utilities
13 class UtilsTest
extends \Shaarli\TestCase
16 protected static $testLogFile = 'tests.log';
18 // Expected log date format
19 protected static $dateFormat = 'Y/m/d H:i:s';
22 * @var string Save the current timezone.
24 protected static $defaultTimeZone;
27 * Assign reference data
29 public static function setUpBeforeClass(): void
31 self
::$defaultTimeZone = date_default_timezone_get();
32 // Timezone without DST for test consistency
33 date_default_timezone_set('Africa/Nairobi');
39 public static function tearDownAfterClass(): void
41 date_default_timezone_set(self
::$defaultTimeZone);
45 * Resets test data before each test
47 protected function setUp(): void
49 if (file_exists(self
::$testLogFile)) {
50 unlink(self
::$testLogFile);
55 * Returns a list of the elements from the last logged entry
57 * @return list (date, ip address, message)
59 protected function getLastLogEntry()
61 $logFile = file(self
::$testLogFile);
62 return explode(' - ', trim(array_pop($logFile), PHP_EOL
));
66 * Format a log a message - IPv4 client address
68 public function testFormatLogIp4()
70 $message = 'IPv4 client connected';
71 $log = format_log($message, '127.0.0.1');
73 static::assertSame('- 127.0.0.1 - IPv4 client connected', $log);
77 * Format a log a message - IPv6 client address
79 public function testFormatLogIp6()
81 $message = 'IPv6 client connected';
82 $log = format_log($message, '2001:db8::ff00:42:8329');
84 static::assertSame('- 2001:db8::ff00:42:8329 - IPv6 client connected', $log);
88 * Represent a link by its hash
90 public function testSmallHash()
92 $this->assertEquals('CyAAJw', smallHash('http://test.io'));
93 $this->assertEquals(6, strlen(smallHash('https://github.com')));
97 * Look for a substring at the beginning of a string
99 public function testStartsWithCaseInsensitive()
101 $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
102 $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
106 * Look for a substring at the beginning of a string (case-sensitive)
108 public function testStartsWithCaseSensitive()
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));
116 * Look for a substring at the beginning of a string (Unicode)
118 public function testStartsWithSpecialChars()
120 $this->assertTrue(startsWith('å!ùµ', 'å!', false));
121 $this->assertTrue(startsWith('µ$åù', 'µ$', true));
125 * Look for a substring at the end of a string
127 public function testEndsWithCaseInsensitive()
129 $this->assertTrue(endsWith('Lorem ipsum
', 'ipsum
', false));
130 $this->assertTrue(endsWith('Lorem ipsum
', 'm IpsUM
', false));
134 * Look for a substring at the end of a string (case-sensitive)
136 public function testEndsWithCaseSensitive()
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));
144 * Look for a substring at the end of a string (Unicode)
146 public function testEndsWithSpecialChars()
148 $this->assertTrue(endsWith('å
!ùµ
', 'ùµ
', false));
149 $this->assertTrue(endsWith('µ$åù
', 'åù
', true));
153 * Check valid date strings, according to a DateTime format
155 public function testCheckValidDateFormat()
157 $this->assertTrue(checkDateFormat('Ymd
', '20150627'));
158 $this->assertTrue(checkDateFormat('Y
-m
-d
', '2015-06-27'));
162 * Check erroneous date strings, according to a DateTime format
164 public function testCheckInvalidDateFormat()
166 $this->assertFalse(checkDateFormat('Ymd
', '2015'));
167 $this->assertFalse(checkDateFormat('Y
-m
-d
', '2015-06'));
168 $this->assertFalse(checkDateFormat('Ymd
', 'DeLorean
'));
172 * Test generate location with valid data.
174 public function testGenerateLocation()
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'));
180 $ref = '?localreferer#hash';
181 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
185 * Test generate location - anti loop.
187 public function testGenerateLocationLoop()
189 $ref = 'http://localhost/?test';
190 $this->assertEquals('./?', generateLocation($ref, 'localhost', array('test')));
194 * Test generate location - from other domain.
196 public function testGenerateLocationOut()
198 $ref = 'http://somewebsite.com/?test';
199 $this->assertEquals('./?', generateLocation($ref, 'localhost'));
204 * Test generateSecretApi.
206 public function testGenerateSecretApi()
208 $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar')));
212 * Test generateSecretApi with invalid parameters.
214 public function testGenerateSecretApiInvalid()
216 $this->assertFalse(generate_api_secret('', ''));
217 $this->assertFalse(generate_api_secret(false, false));
221 * Test normalize_spaces.
223 public function testNormalizeSpace()
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));
233 * Test arrays_combine
235 public function testCartesianProductGenerator()
237 $arr = [['ab', 'cd'], ['ef', 'gh'], ['ij', 'kl'], ['m']];
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'],
248 $this->assertEquals($expected, iterator_to_array(cartesian_product_generator($arr)));
252 * Test date_format() with invalid parameter.
254 public function testDateFormatInvalid()
256 $this->assertFalse(format_date([]));
257 $this->assertFalse(format_date(null));
261 * Test is_integer_mixed with valid values
263 public function testIsIntegerMixedValid()
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));
275 * Test is_integer_mixed with invalid values
277 public function testIsIntegerMixedInvalid()
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(''));
294 public function testReturnBytes()
296 $this->assertEquals(2 * 1024, return_bytes('2k'));
297 $this->assertEquals(2 * 1024, return_bytes('2K'));
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'));
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(''));
314 public function testHumanBytes()
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(''));
331 * Test get_max_upload_size with formatting
333 public function testGetMaxUploadSize()
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));
341 * Test get_max_upload_size without formatting
343 public function testGetMaxUploadSizeRaw()
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));
351 * Test alphabetical_sort by value, not reversed, with php-intl.
353 public function testAlphabeticalSortByValue()
374 alphabetical_sort($arr);
375 $this->assertEquals($expected, $arr);
379 * Test alphabetical_sort by value, reversed, with php-intl.
381 public function testAlphabeticalSortByValueReversed()
402 alphabetical_sort($arr, true);
403 $this->assertEquals($expected, $arr);
407 * Test alphabetical_sort by keys, not reversed, with php-intl.
409 public function testAlphabeticalSortByKeys()
430 alphabetical_sort($arr, true, true);
431 $this->assertEquals($expected, $arr);
435 * Test alphabetical_sort by keys, reversed, with php-intl.
437 public function testAlphabeticalSortByKeysReversed()
458 alphabetical_sort($arr, true, true);
459 $this->assertEquals($expected, $arr);