6 require_once 'application/Utils.php';
7 require_once 'tests/utils/ReferenceSessionIdHashes.php';
9 // Initialize reference data before PHPUnit starts a session
10 ReferenceSessionIdHashes
::genAllHashes();
14 * Unitary tests for Shaarli utilities
16 class UtilsTest
extends PHPUnit_Framework_TestCase
19 protected static $sidHashes = null;
22 protected static $testLogFile = 'tests.log';
24 // Expected log date format
25 protected static $dateFormat = 'Y/m/d H:i:s';
29 * Assign reference data
31 public static function setUpBeforeClass()
33 self
::$sidHashes = ReferenceSessionIdHashes
::getHashes();
37 * Resets test data before each test
39 protected function setUp()
41 if (file_exists(self
::$testLogFile)) {
42 unlink(self
::$testLogFile);
47 * Returns a list of the elements from the last logged entry
49 * @return list (date, ip address, message)
51 protected function getLastLogEntry()
53 $logFile = file(self
::$testLogFile);
54 return explode(' - ', trim(array_pop($logFile), PHP_EOL
));
58 * Log a message to a file - IPv4 client address
60 public function testLogmIp4()
62 $logMessage = 'IPv4 client connected';
63 logm(self
::$testLogFile, '127.0.0.1', $logMessage);
64 list($date, $ip, $message) = $this->getLastLogEntry();
66 $this->assertInstanceOf(
68 DateTime
::createFromFormat(self
::$dateFormat, $date)
71 filter_var($ip, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV4
) !== false
73 $this->assertEquals($logMessage, $message);
77 * Log a message to a file - IPv6 client address
79 public function testLogmIp6()
81 $logMessage = 'IPv6 client connected';
82 logm(self
::$testLogFile, '2001:db8::ff00:42:8329', $logMessage);
83 list($date, $ip, $message) = $this->getLastLogEntry();
85 $this->assertInstanceOf(
87 DateTime
::createFromFormat(self
::$dateFormat, $date)
90 filter_var($ip, FILTER_VALIDATE_IP
, FILTER_FLAG_IPV6
) !== false
92 $this->assertEquals($logMessage, $message);
96 * Represent a link by its hash
98 public function testSmallHash()
100 $this->assertEquals('CyAAJw', smallHash('http://test.io'));
101 $this->assertEquals(6, strlen(smallHash('https://github.com')));
105 * Look for a substring at the beginning of a string
107 public function testStartsWithCaseInsensitive()
109 $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
110 $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
114 * Look for a substring at the beginning of a string (case-sensitive)
116 public function testStartsWithCaseSensitive()
118 $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true));
119 $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true));
120 $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true));
124 * Look for a substring at the beginning of a string (Unicode)
126 public function testStartsWithSpecialChars()
128 $this->assertTrue(startsWith('å!ùµ', 'å!', false));
129 $this->assertTrue(startsWith('µ$åù', 'µ$', true));
133 * Look for a substring at the end of a string
135 public function testEndsWithCaseInsensitive()
137 $this->assertTrue(endsWith('Lorem ipsum
', 'ipsum
', false));
138 $this->assertTrue(endsWith('Lorem ipsum
', 'm IpsUM
', false));
142 * Look for a substring at the end of a string (case-sensitive)
144 public function testEndsWithCaseSensitive()
146 $this->assertTrue(endsWith('lorem Ipsum
', 'Ipsum
', true));
147 $this->assertFalse(endsWith('lorem Ipsum
', 'ipsum
', true));
148 $this->assertFalse(endsWith('lorem Ipsum
', 'M IPsuM
', true));
152 * Look for a substring at the end of a string (Unicode)
154 public function testEndsWithSpecialChars()
156 $this->assertTrue(endsWith('å
!ùµ
', 'ùµ
', false));
157 $this->assertTrue(endsWith('µ$åù
', 'åù
', true));
161 * Check valid date strings, according to a DateTime format
163 public function testCheckValidDateFormat()
165 $this->assertTrue(checkDateFormat('Ymd
', '20150627'));
166 $this->assertTrue(checkDateFormat('Y
-m
-d
', '2015-06-27'));
170 * Check erroneous date strings, according to a DateTime format
172 public function testCheckInvalidDateFormat()
174 $this->assertFalse(checkDateFormat('Ymd
', '2015'));
175 $this->assertFalse(checkDateFormat('Y
-m
-d
', '2015-06'));
176 $this->assertFalse(checkDateFormat('Ymd
', 'DeLorean
'));
180 * Test generate location with valid data.
182 public function testGenerateLocation() {
183 $ref = 'http
://localhost/?test';
184 $this->assertEquals($ref, generateLocation($ref, 'localhost'));
185 $ref = 'http://localhost:8080/?test';
186 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
187 $ref = '?localreferer#hash';
188 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
192 * Test generate location - anti loop.
194 public function testGenerateLocationLoop() {
195 $ref = 'http://localhost/?test';
196 $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
200 * Test generate location - from other domain.
202 public function testGenerateLocationOut() {
203 $ref = 'http://somewebsite.com/?test';
204 $this->assertEquals('?', generateLocation($ref, 'localhost'));
208 * Test is_session_id_valid with a valid ID - TEST ALL THE HASHES!
210 * This tests extensively covers all hash algorithms / bit representations
212 public function testIsAnyHashSessionIdValid()
214 foreach (self
::$sidHashes as $algo => $bpcs) {
215 foreach ($bpcs as $bpc => $hash) {
216 $this->assertTrue(is_session_id_valid($hash));
222 * Test is_session_id_valid with a valid ID - SHA-1 hashes
224 public function testIsSha1SessionIdValid()
226 $this->assertTrue(is_session_id_valid(sha1('shaarli')));
230 * Test is_session_id_valid with a valid ID - SHA-256 hashes
232 public function testIsSha256SessionIdValid()
234 $this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli')));
238 * Test is_session_id_valid with a valid ID - SHA-512 hashes
240 public function testIsSha512SessionIdValid()
242 $this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli')));
246 * Test is_session_id_valid with invalid IDs.
248 public function testIsSessionIdInvalid()
250 $this->assertFalse(is_session_id_valid(''));
251 $this->assertFalse(is_session_id_valid(array()));
253 is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
258 * Test text2clickable without a redirector being set.
260 public function testText2clickableWithoutRedirector()
262 $text = 'stuff http://hello.there/is=someone#here otherstuff';
263 $expectedText = 'stuff <a href="http://hello.there/is=someone#here">http://hello.there/is=someone#here</a> otherstuff';
264 $processedText = text2clickable($text, '');
265 $this->assertEquals($expectedText, $processedText);
269 * Test text2clickable a redirector set.
271 public function testText2clickableWithRedirector()
273 $text = 'stuff http://hello.there/is=someone#here otherstuff';
274 $redirector = 'http://redirector.to';
275 $expectedText = 'stuff <a href="'.
277 urlencode('http://hello.there/is=someone#here') .
278 '">http://hello.there/is=someone#here</a> otherstuff';
279 $processedText = text2clickable($text, $redirector);
280 $this->assertEquals($expectedText, $processedText);
284 * Test testSpace2nbsp.
286 public function testSpace2nbsp()
288 $text = ' Are you thrilled by flags ?'. PHP_EOL
.' Really?';
289 $expectedText = ' Are you thrilled by flags ?'. PHP_EOL
.' Really?';
290 $processedText = space2nbsp($text);
291 $this->assertEquals($expectedText, $processedText);