6 require_once 'application/Utils.php';
7 require_once 'application/Languages.php';
11 * Unitary tests for Shaarli utilities
13 class UtilsTest
extends PHPUnit_Framework_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()
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()
41 date_default_timezone_set(self
::$defaultTimeZone);
45 * Resets test data before each test
47 protected function setUp()
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 * Log a message to a file - IPv4 client address
68 public function testLogmIp4()
70 $logMessage = 'IPv4 client connected';
71 logm(self
::$testLogFile, '127.0.0.1', $logMessage);
72 list($date, $ip, $message) = $this->getLastLogEntry();
74 $this->assertInstanceOf(
76 DateTime
::createFromFormat(self
::$dateFormat, $date)
79 filter_var($ip, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV4
) !== false
81 $this->assertEquals($logMessage, $message);
85 * Log a message to a file - IPv6 client address
87 public function testLogmIp6()
89 $logMessage = 'IPv6 client connected';
90 logm(self
::$testLogFile, '2001:db8::ff00:42:8329', $logMessage);
91 list($date, $ip, $message) = $this->getLastLogEntry();
93 $this->assertInstanceOf(
95 DateTime
::createFromFormat(self
::$dateFormat, $date)
98 filter_var($ip, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) !== false
100 $this->assertEquals($logMessage, $message);
104 * Represent a link by its hash
106 public function testSmallHash()
108 $this->assertEquals('CyAAJw', smallHash('http://test.io'));
109 $this->assertEquals(6, strlen(smallHash('https://github.com')));
113 * Look for a substring at the beginning of a string
115 public function testStartsWithCaseInsensitive()
117 $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
118 $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
122 * Look for a substring at the beginning of a string (case-sensitive)
124 public function testStartsWithCaseSensitive()
126 $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true));
127 $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true));
128 $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true));
132 * Look for a substring at the beginning of a string (Unicode)
134 public function testStartsWithSpecialChars()
136 $this->assertTrue(startsWith('å!ùµ', 'å!', false));
137 $this->assertTrue(startsWith('µ$åù', 'µ$', true));
141 * Look for a substring at the end of a string
143 public function testEndsWithCaseInsensitive()
145 $this->assertTrue(endsWith('Lorem ipsum
', 'ipsum
', false));
146 $this->assertTrue(endsWith('Lorem ipsum
', 'm IpsUM
', false));
150 * Look for a substring at the end of a string (case-sensitive)
152 public function testEndsWithCaseSensitive()
154 $this->assertTrue(endsWith('lorem Ipsum
', 'Ipsum
', true));
155 $this->assertFalse(endsWith('lorem Ipsum
', 'ipsum
', true));
156 $this->assertFalse(endsWith('lorem Ipsum
', 'M IPsuM
', true));
160 * Look for a substring at the end of a string (Unicode)
162 public function testEndsWithSpecialChars()
164 $this->assertTrue(endsWith('å
!ùµ
', 'ùµ
', false));
165 $this->assertTrue(endsWith('µ$åù
', 'åù
', true));
169 * Check valid date strings, according to a DateTime format
171 public function testCheckValidDateFormat()
173 $this->assertTrue(checkDateFormat('Ymd
', '20150627'));
174 $this->assertTrue(checkDateFormat('Y
-m
-d
', '2015-06-27'));
178 * Check erroneous date strings, according to a DateTime format
180 public function testCheckInvalidDateFormat()
182 $this->assertFalse(checkDateFormat('Ymd
', '2015'));
183 $this->assertFalse(checkDateFormat('Y
-m
-d
', '2015-06'));
184 $this->assertFalse(checkDateFormat('Ymd
', 'DeLorean
'));
188 * Test generate location with valid data.
190 public function testGenerateLocation()
192 $ref = 'http
://localhost/?test';
193 $this->assertEquals($ref, generateLocation($ref, 'localhost'));
194 $ref = 'http://localhost:8080/?test';
195 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
196 $ref = '?localreferer#hash';
197 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
201 * Test generate location - anti loop.
203 public function testGenerateLocationLoop()
205 $ref = 'http://localhost/?test';
206 $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
210 * Test generate location - from other domain.
212 public function testGenerateLocationOut()
214 $ref = 'http://somewebsite.com/?test';
215 $this->assertEquals('?', generateLocation($ref, 'localhost'));
220 * Test generateSecretApi.
222 public function testGenerateSecretApi()
224 $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar')));
228 * Test generateSecretApi with invalid parameters.
230 public function testGenerateSecretApiInvalid()
232 $this->assertFalse(generate_api_secret('', ''));
233 $this->assertFalse(generate_api_secret(false, false));
237 * Test normalize_spaces.
239 public function testNormalizeSpace()
241 $str = ' foo bar is important ';
242 $this->assertEquals('foo bar is important', normalize_spaces($str));
243 $this->assertEquals('foo', normalize_spaces('foo'));
244 $this->assertEquals('', normalize_spaces(''));
245 $this->assertEquals(null, normalize_spaces(null));
249 * Test arrays_combine
251 public function testCartesianProductGenerator()
253 $arr = [['ab', 'cd'], ['ef', 'gh'], ['ij', 'kl'], ['m']];
255 ['ab', 'ef', 'ij', 'm'],
256 ['ab', 'ef', 'kl', 'm'],
257 ['ab', 'gh', 'ij', 'm'],
258 ['ab', 'gh', 'kl', 'm'],
259 ['cd', 'ef', 'ij', 'm'],
260 ['cd', 'ef', 'kl', 'm'],
261 ['cd', 'gh', 'ij', 'm'],
262 ['cd', 'gh', 'kl', 'm'],
264 $this->assertEquals($expected, iterator_to_array(cartesian_product_generator($arr)));
268 * Test date_format() with invalid parameter.
270 public function testDateFormatInvalid()
272 $this->assertFalse(format_date([]));
273 $this->assertFalse(format_date(null));
277 * Test is_integer_mixed with valid values
279 public function testIsIntegerMixedValid()
281 $this->assertTrue(is_integer_mixed(12));
282 $this->assertTrue(is_integer_mixed('12'));
283 $this->assertTrue(is_integer_mixed(-12));
284 $this->assertTrue(is_integer_mixed('-12'));
285 $this->assertTrue(is_integer_mixed(0));
286 $this->assertTrue(is_integer_mixed('0'));
287 $this->assertTrue(is_integer_mixed(0x0a));
291 * Test is_integer_mixed with invalid values
293 public function testIsIntegerMixedInvalid()
295 $this->assertFalse(is_integer_mixed(true));
296 $this->assertFalse(is_integer_mixed(false));
297 $this->assertFalse(is_integer_mixed([]));
298 $this->assertFalse(is_integer_mixed(['test']));
299 $this->assertFalse(is_integer_mixed([12]));
300 $this->assertFalse(is_integer_mixed(new DateTime()));
301 $this->assertFalse(is_integer_mixed('0x0a'));
302 $this->assertFalse(is_integer_mixed('12k'));
303 $this->assertFalse(is_integer_mixed('k12'));
304 $this->assertFalse(is_integer_mixed(''));
310 public function testReturnBytes()
312 $this->assertEquals(2 * 1024, return_bytes('2k'));
313 $this->assertEquals(2 * 1024, return_bytes('2K'));
314 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2m'));
315 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2M'));
316 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2g'));
317 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2G'));
318 $this->assertEquals(374, return_bytes('374'));
319 $this->assertEquals(374, return_bytes(374));
320 $this->assertEquals(0, return_bytes('0'));
321 $this->assertEquals(0, return_bytes(0));
322 $this->assertEquals(-1, return_bytes('-1'));
323 $this->assertEquals(-1, return_bytes(-1));
324 $this->assertEquals('', return_bytes(''));
330 public function testHumanBytes()
332 $this->assertEquals('2'. t('kiB'), human_bytes(2 * 1024));
333 $this->assertEquals('2'. t('kiB'), human_bytes(strval(2 * 1024)));
334 $this->assertEquals('2'. t('MiB'), human_bytes(2 * (pow(1024, 2))));
335 $this->assertEquals('2'. t('MiB'), human_bytes(strval(2 * (pow(1024, 2)))));
336 $this->assertEquals('2'. t('GiB'), human_bytes(2 * (pow(1024, 3))));
337 $this->assertEquals('2'. t('GiB'), human_bytes(strval(2 * (pow(1024, 3)))));
338 $this->assertEquals('374'. t('B'), human_bytes(374));
339 $this->assertEquals('374'. t('B'), human_bytes('374'));
340 $this->assertEquals('232'. t('kiB'), human_bytes(237481));
341 $this->assertEquals(t('Unlimited'), human_bytes('0'));
342 $this->assertEquals(t('Unlimited'), human_bytes(0));
343 $this->assertEquals(t('Setting not set'), human_bytes(''));
347 * Test get_max_upload_size with formatting
349 public function testGetMaxUploadSize()
351 $this->assertEquals('1'. t('MiB'), get_max_upload_size(2097152, '1024k'));
352 $this->assertEquals('1'. t('MiB'), get_max_upload_size('1m', '2m'));
353 $this->assertEquals('100'. t('B'), get_max_upload_size(100, 100));
357 * Test get_max_upload_size without formatting
359 public function testGetMaxUploadSizeRaw()
361 $this->assertEquals('1048576', get_max_upload_size(2097152, '1024k', false));
362 $this->assertEquals('1048576', get_max_upload_size('1m', '2m', false));
363 $this->assertEquals('100', get_max_upload_size(100, 100, false));
367 * Test alphabetical_sort by value, not reversed, with php-intl.
369 public function testAlphabeticalSortByValue()
390 alphabetical_sort($arr);
391 $this->assertEquals($expected, $arr);
395 * Test alphabetical_sort by value, reversed, with php-intl.
397 public function testAlphabeticalSortByValueReversed()
418 alphabetical_sort($arr, true);
419 $this->assertEquals($expected, $arr);
423 * Test alphabetical_sort by keys, not reversed, with php-intl.
425 public function testAlphabeticalSortByKeys()
446 alphabetical_sort($arr, true, true);
447 $this->assertEquals($expected, $arr);
451 * Test alphabetical_sort by keys, reversed, with php-intl.
453 public function testAlphabeticalSortByKeysReversed()
474 alphabetical_sort($arr, true, true);
475 $this->assertEquals($expected, $arr);