]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/UtilsTest.php
Merge pull request #558 from ArthurHoaro/hashtag4
[github/shaarli/Shaarli.git] / tests / UtilsTest.php
1 <?php
2 /**
3 * Utilities' tests
4 */
5
6 require_once 'application/Utils.php';
7 require_once 'tests/utils/ReferenceSessionIdHashes.php';
8
9 // Initialize reference data before PHPUnit starts a session
10 ReferenceSessionIdHashes::genAllHashes();
11
12
13 /**
14 * Unitary tests for Shaarli utilities
15 */
16 class UtilsTest extends PHPUnit_Framework_TestCase
17 {
18 // Session ID hashes
19 protected static $sidHashes = null;
20
21 // Log file
22 protected static $testLogFile = 'tests.log';
23
24 // Expected log date format
25 protected static $dateFormat = 'Y/m/d H:i:s';
26
27
28 /**
29 * Assign reference data
30 */
31 public static function setUpBeforeClass()
32 {
33 self::$sidHashes = ReferenceSessionIdHashes::getHashes();
34 }
35
36 /**
37 * Resets test data before each test
38 */
39 protected function setUp()
40 {
41 if (file_exists(self::$testLogFile)) {
42 unlink(self::$testLogFile);
43 }
44 }
45
46 /**
47 * Returns a list of the elements from the last logged entry
48 *
49 * @return list (date, ip address, message)
50 */
51 protected function getLastLogEntry()
52 {
53 $logFile = file(self::$testLogFile);
54 return explode(' - ', trim(array_pop($logFile), PHP_EOL));
55 }
56
57 /**
58 * Log a message to a file - IPv4 client address
59 */
60 public function testLogmIp4()
61 {
62 $logMessage = 'IPv4 client connected';
63 logm(self::$testLogFile, '127.0.0.1', $logMessage);
64 list($date, $ip, $message) = $this->getLastLogEntry();
65
66 $this->assertInstanceOf(
67 'DateTime',
68 DateTime::createFromFormat(self::$dateFormat, $date)
69 );
70 $this->assertTrue(
71 filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false
72 );
73 $this->assertEquals($logMessage, $message);
74 }
75
76 /**
77 * Log a message to a file - IPv6 client address
78 */
79 public function testLogmIp6()
80 {
81 $logMessage = 'IPv6 client connected';
82 logm(self::$testLogFile, '2001:db8::ff00:42:8329', $logMessage);
83 list($date, $ip, $message) = $this->getLastLogEntry();
84
85 $this->assertInstanceOf(
86 'DateTime',
87 DateTime::createFromFormat(self::$dateFormat, $date)
88 );
89 $this->assertTrue(
90 filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false
91 );
92 $this->assertEquals($logMessage, $message);
93 }
94
95 /**
96 * Represent a link by its hash
97 */
98 public function testSmallHash()
99 {
100 $this->assertEquals('CyAAJw', smallHash('http://test.io'));
101 $this->assertEquals(6, strlen(smallHash('https://github.com')));
102 }
103
104 /**
105 * Look for a substring at the beginning of a string
106 */
107 public function testStartsWithCaseInsensitive()
108 {
109 $this->assertTrue(startsWith('Lorem ipsum', 'lorem', false));
110 $this->assertTrue(startsWith('Lorem ipsum', 'LoReM i', false));
111 }
112
113 /**
114 * Look for a substring at the beginning of a string (case-sensitive)
115 */
116 public function testStartsWithCaseSensitive()
117 {
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));
121 }
122
123 /**
124 * Look for a substring at the beginning of a string (Unicode)
125 */
126 public function testStartsWithSpecialChars()
127 {
128 $this->assertTrue(startsWith('å!ùµ', 'å!', false));
129 $this->assertTrue(startsWith('µ$åù', $', true));
130 }
131
132 /**
133 * Look for a substring at the end of a string
134 */
135 public function testEndsWithCaseInsensitive()
136 {
137 $this->assertTrue(endsWith('Lorem ipsum', 'ipsum', false));
138 $this->assertTrue(endsWith('Lorem ipsum', 'm IpsUM', false));
139 }
140
141 /**
142 * Look for a substring at the end of a string (case-sensitive)
143 */
144 public function testEndsWithCaseSensitive()
145 {
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));
149 }
150
151 /**
152 * Look for a substring at the end of a string (Unicode)
153 */
154 public function testEndsWithSpecialChars()
155 {
156 $this->assertTrue(endsWith('å!ùµ', 'ùµ', false));
157 $this->assertTrue(endsWith('µ$åù', 'åù', true));
158 }
159
160 /**
161 * Check valid date strings, according to a DateTime format
162 */
163 public function testCheckValidDateFormat()
164 {
165 $this->assertTrue(checkDateFormat('Ymd', '20150627'));
166 $this->assertTrue(checkDateFormat('Y-m-d', '2015-06-27'));
167 }
168
169 /**
170 * Check erroneous date strings, according to a DateTime format
171 */
172 public function testCheckInvalidDateFormat()
173 {
174 $this->assertFalse(checkDateFormat('Ymd', '2015'));
175 $this->assertFalse(checkDateFormat('Y-m-d', '2015-06'));
176 $this->assertFalse(checkDateFormat('Ymd', 'DeLorean'));
177 }
178
179 /**
180 * Test generate location with valid data.
181 */
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'));
189 }
190
191 /**
192 * Test generate location - anti loop.
193 */
194 public function testGenerateLocationLoop() {
195 $ref = 'http://localhost/?test';
196 $this->assertEquals('?', generateLocation($ref, 'localhost', array('test')));
197 }
198
199 /**
200 * Test generate location - from other domain.
201 */
202 public function testGenerateLocationOut() {
203 $ref = 'http://somewebsite.com/?test';
204 $this->assertEquals('?', generateLocation($ref, 'localhost'));
205 }
206
207 /**
208 * Test is_session_id_valid with a valid ID - TEST ALL THE HASHES!
209 *
210 * This tests extensively covers all hash algorithms / bit representations
211 */
212 public function testIsAnyHashSessionIdValid()
213 {
214 foreach (self::$sidHashes as $algo => $bpcs) {
215 foreach ($bpcs as $bpc => $hash) {
216 $this->assertTrue(is_session_id_valid($hash));
217 }
218 }
219 }
220
221 /**
222 * Test is_session_id_valid with a valid ID - SHA-1 hashes
223 */
224 public function testIsSha1SessionIdValid()
225 {
226 $this->assertTrue(is_session_id_valid(sha1('shaarli')));
227 }
228
229 /**
230 * Test is_session_id_valid with a valid ID - SHA-256 hashes
231 */
232 public function testIsSha256SessionIdValid()
233 {
234 $this->assertTrue(is_session_id_valid(hash('sha256', 'shaarli')));
235 }
236
237 /**
238 * Test is_session_id_valid with a valid ID - SHA-512 hashes
239 */
240 public function testIsSha512SessionIdValid()
241 {
242 $this->assertTrue(is_session_id_valid(hash('sha512', 'shaarli')));
243 }
244
245 /**
246 * Test is_session_id_valid with invalid IDs.
247 */
248 public function testIsSessionIdInvalid()
249 {
250 $this->assertFalse(is_session_id_valid(''));
251 $this->assertFalse(is_session_id_valid(array()));
252 $this->assertFalse(
253 is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=')
254 );
255 }
256 }