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() {
191 $ref = 'http
://localhost/?test';
192 $this->assertEquals($ref, generateLocation($ref, 'localhost'));
193 $ref = 'http://localhost:8080/?test';
194 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
195 $ref = '?localreferer#hash';
196 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
200 * Test generate location - anti loop.
202 public function testGenerateLocationLoop() {
203 $ref = 'http://localhost/?test';
204 $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
208 * Test generate location - from other domain.
210 public function testGenerateLocationOut() {
211 $ref = 'http://somewebsite.com/?test';
212 $this->assertEquals('?', generateLocation($ref, 'localhost'));
217 * Test generateSecretApi.
219 public function testGenerateSecretApi()
221 $this->assertEquals(12, strlen(generate_api_secret('foo', 'bar')));
225 * Test generateSecretApi with invalid parameters.
227 public function testGenerateSecretApiInvalid()
229 $this->assertFalse(generate_api_secret('', ''));
230 $this->assertFalse(generate_api_secret(false, false));
234 * Test normalize_spaces.
236 public function testNormalizeSpace()
238 $str = ' foo bar is important ';
239 $this->assertEquals('foo bar is important', normalize_spaces($str));
240 $this->assertEquals('foo', normalize_spaces('foo'));
241 $this->assertEquals('', normalize_spaces(''));
242 $this->assertEquals(null, normalize_spaces(null));
246 * Test arrays_combine
248 public function testCartesianProductGenerator()
250 $arr = [['ab', 'cd'], ['ef', 'gh'], ['ij', 'kl'], ['m']];
252 ['ab', 'ef', 'ij', 'm'],
253 ['ab', 'ef', 'kl', 'm'],
254 ['ab', 'gh', 'ij', 'm'],
255 ['ab', 'gh', 'kl', 'm'],
256 ['cd', 'ef', 'ij', 'm'],
257 ['cd', 'ef', 'kl', 'm'],
258 ['cd', 'gh', 'ij', 'm'],
259 ['cd', 'gh', 'kl', 'm'],
261 $this->assertEquals($expected, iterator_to_array(cartesian_product_generator($arr)));
265 * Test date_format() with invalid parameter.
267 public function testDateFormatInvalid()
269 $this->assertFalse(format_date([]));
270 $this->assertFalse(format_date(null));
274 * Test is_integer_mixed with valid values
276 public function testIsIntegerMixedValid()
278 $this->assertTrue(is_integer_mixed(12));
279 $this->assertTrue(is_integer_mixed('12'));
280 $this->assertTrue(is_integer_mixed(-12));
281 $this->assertTrue(is_integer_mixed('-12'));
282 $this->assertTrue(is_integer_mixed(0));
283 $this->assertTrue(is_integer_mixed('0'));
284 $this->assertTrue(is_integer_mixed(0x0a));
288 * Test is_integer_mixed with invalid values
290 public function testIsIntegerMixedInvalid()
292 $this->assertFalse(is_integer_mixed(true));
293 $this->assertFalse(is_integer_mixed(false));
294 $this->assertFalse(is_integer_mixed([]));
295 $this->assertFalse(is_integer_mixed(['test']));
296 $this->assertFalse(is_integer_mixed([12]));
297 $this->assertFalse(is_integer_mixed(new DateTime()));
298 $this->assertFalse(is_integer_mixed('0x0a'));
299 $this->assertFalse(is_integer_mixed('12k'));
300 $this->assertFalse(is_integer_mixed('k12'));
301 $this->assertFalse(is_integer_mixed(''));
307 public function testReturnBytes()
309 $this->assertEquals(2 * 1024, return_bytes('2k'));
310 $this->assertEquals(2 * 1024, return_bytes('2K'));
311 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2m'));
312 $this->assertEquals(2 * (pow(1024, 2)), return_bytes('2M'));
313 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2g'));
314 $this->assertEquals(2 * (pow(1024, 3)), return_bytes('2G'));
315 $this->assertEquals(374, return_bytes('374'));
316 $this->assertEquals(374, return_bytes(374));
317 $this->assertEquals(0, return_bytes('0'));
318 $this->assertEquals(0, return_bytes(0));
319 $this->assertEquals(-1, return_bytes('-1'));
320 $this->assertEquals(-1, return_bytes(-1));
321 $this->assertEquals('', return_bytes(''));
327 public function testHumanBytes()
329 $this->assertEquals('2'. t('kiB'), human_bytes(2 * 1024));
330 $this->assertEquals('2'. t('kiB'), human_bytes(strval(2 * 1024)));
331 $this->assertEquals('2'. t('MiB'), human_bytes(2 * (pow(1024, 2))));
332 $this->assertEquals('2'. t('MiB'), human_bytes(strval(2 * (pow(1024, 2)))));
333 $this->assertEquals('2'. t('GiB'), human_bytes(2 * (pow(1024, 3))));
334 $this->assertEquals('2'. t('GiB'), human_bytes(strval(2 * (pow(1024, 3)))));
335 $this->assertEquals('374'. t('B'), human_bytes(374));
336 $this->assertEquals('374'. t('B'), human_bytes('374'));
337 $this->assertEquals('232'. t('kiB'), human_bytes(237481));
338 $this->assertEquals(t('Unlimited'), human_bytes('0'));
339 $this->assertEquals(t('Unlimited'), human_bytes(0));
340 $this->assertEquals(t('Setting not set'), human_bytes(''));
344 * Test get_max_upload_size with formatting
346 public function testGetMaxUploadSize()
348 $this->assertEquals('1'. t('MiB'), get_max_upload_size(2097152, '1024k'));
349 $this->assertEquals('1'. t('MiB'), get_max_upload_size('1m', '2m'));
350 $this->assertEquals('100'. t('B'), get_max_upload_size(100, 100));
354 * Test get_max_upload_size without formatting
356 public function testGetMaxUploadSizeRaw()
358 $this->assertEquals('1048576', get_max_upload_size(2097152, '1024k', false));
359 $this->assertEquals('1048576', get_max_upload_size('1m', '2m', false));
360 $this->assertEquals('100', get_max_upload_size(100, 100, false));
364 * Test alphabetical_sort by value, not reversed, with php-intl.
366 public function testAlphabeticalSortByValue()
387 alphabetical_sort($arr);
388 $this->assertEquals($expected, $arr);
392 * Test alphabetical_sort by value, reversed, with php-intl.
394 public function testAlphabeticalSortByValueReversed()
415 alphabetical_sort($arr, true);
416 $this->assertEquals($expected, $arr);
420 * Test alphabetical_sort by keys, not reversed, with php-intl.
422 public function testAlphabeticalSortByKeys()
443 alphabetical_sort($arr, true, true);
444 $this->assertEquals($expected, $arr);
448 * Test alphabetical_sort by keys, reversed, with php-intl.
450 public function testAlphabeticalSortByKeysReversed()
471 alphabetical_sort($arr, true, true);
472 $this->assertEquals($expected, $arr);