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 * Assign reference data
24 public static function setUpBeforeClass()
26 self
::$sidHashes = ReferenceSessionIdHashes
::getHashes();
30 * Represent a link by its hash
32 public function testSmallHash()
34 $this->assertEquals('CyAAJw', smallHash('http://test.io'));
35 $this->assertEquals(6, strlen(smallHash('https://github.com')));
39 * Look for a substring at the beginning of a string
41 public function testStartsWithCaseInsensitive()
43 $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
44 $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
48 * Look for a substring at the beginning of a string (case-sensitive)
50 public function testStartsWithCaseSensitive()
52 $this->assertTrue(startsWith('Lorem ipsum', 'Lorem', true));
53 $this->assertFalse(startsWith('Lorem ipsum', 'lorem', true));
54 $this->assertFalse(startsWith('Lorem ipsum', 'LoReM i', true));
58 * Look for a substring at the beginning of a string (Unicode)
60 public function testStartsWithSpecialChars()
62 $this->assertTrue(startsWith('å!ùµ', 'å!', false));
63 $this->assertTrue(startsWith('µ$åù', 'µ$', true));
67 * Look for a substring at the end of a string
69 public function testEndsWithCaseInsensitive()
71 $this->assertTrue(endsWith('Lorem ipsum
', 'ipsum
', false));
72 $this->assertTrue(endsWith('Lorem ipsum
', 'm IpsUM
', false));
76 * Look for a substring at the end of a string (case-sensitive)
78 public function testEndsWithCaseSensitive()
80 $this->assertTrue(endsWith('lorem Ipsum
', 'Ipsum
', true));
81 $this->assertFalse(endsWith('lorem Ipsum
', 'ipsum
', true));
82 $this->assertFalse(endsWith('lorem Ipsum
', 'M IPsuM
', true));
86 * Look for a substring at the end of a string (Unicode)
88 public function testEndsWithSpecialChars()
90 $this->assertTrue(endsWith('å
!ùµ
', 'ùµ
', false));
91 $this->assertTrue(endsWith('µ$åù
', 'åù
', true));
95 * Check valid date strings, according to a DateTime format
97 public function testCheckValidDateFormat()
99 $this->assertTrue(checkDateFormat('Ymd
', '20150627'));
100 $this->assertTrue(checkDateFormat('Y
-m
-d
', '2015-06-27'));
104 * Check erroneous date strings, according to a DateTime format
106 public function testCheckInvalidDateFormat()
108 $this->assertFalse(checkDateFormat('Ymd
', '2015'));
109 $this->assertFalse(checkDateFormat('Y
-m
-d
', '2015-06'));
110 $this->assertFalse(checkDateFormat('Ymd
', 'DeLorean
'));
114 * Test generate location with valid data.
116 public function testGenerateLocation() {
117 $ref = 'http
://localhost/?test';
118 $this->assertEquals($ref, generateLocation($ref, 'localhost'));
119 $ref = 'http://localhost:8080/?test';
120 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
121 $ref = '?localreferer#hash';
122 $this->assertEquals($ref, generateLocation($ref, 'localhost:8080'));
126 * Test generate location - anti loop.
128 public function testGenerateLocationLoop() {
129 $ref = 'http://localhost/?test';
130 $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
134 * Test generate location - from other domain.
136 public function testGenerateLocationOut() {
137 $ref = 'http://somewebsite.com/?test';
138 $this->assertEquals('?', generateLocation($ref, 'localhost'));
142 * Check supported PHP versions
144 public function testCheckSupportedPHPVersion()
147 checkPHPVersion($minVersion, '5.4.32');
148 checkPHPVersion($minVersion, '5.5');
149 checkPHPVersion($minVersion, '5.6.10');
153 * Check a unsupported PHP version
154 * @expectedException Exception
155 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
157 public function testCheckSupportedPHPVersion51()
159 checkPHPVersion('5.3', '5.1.0');
163 * Check another unsupported PHP version
164 * @expectedException Exception
165 * @expectedExceptionMessageRegExp /Your PHP version is obsolete/
167 public function testCheckSupportedPHPVersion52()
169 checkPHPVersion('5.3', '5.2');
173 * Test is_session_id_valid with a valid ID - TEST ALL THE HASHES!
175 * This tests extensively covers all hash algorithms / bit representations
177 public function testIsAnyHashSessionIdValid()
179 foreach (self
::$sidHashes as $algo => $bpcs) {
180 foreach ($bpcs as $bpc => $hash) {
181 $this->assertTrue(is_session_id_valid($hash));
187 * Test is_session_id_valid with a valid ID - SHA-1 hashes
189 public function testIsSha1SessionIdValid()
191 $this->assertTrue(is_session_id_valid(sha1('shaarli')));
195 * Test is_session_id_valid with a valid ID - SHA-256 hashes
197 public function testIsSha256SessionIdValid()
199 $this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli')));
203 * Test is_session_id_valid with a valid ID - SHA-512 hashes
205 public function testIsSha512SessionIdValid()
207 $this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli')));
211 * Test is_session_id_valid with invalid IDs.
213 public function testIsSessionIdInvalid()
215 $this->assertFalse(is_session_id_valid(''));
216 $this->assertFalse(is_session_id_valid(array()));
218 is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')