diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/HttpUtils/ServerUrlTest.php | 32 | ||||
-rw-r--r-- | tests/LinkUtilsTest.php | 244 | ||||
-rw-r--r-- | tests/api/controllers/PostLinkTest.php | 6 | ||||
-rw-r--r-- | tests/config/ConfigPhpTest.php | 14 | ||||
-rw-r--r-- | tests/utils/config/emptyConfigPhp.php | 1 |
5 files changed, 275 insertions, 22 deletions
diff --git a/tests/HttpUtils/ServerUrlTest.php b/tests/HttpUtils/ServerUrlTest.php index dac02b3e..324b827a 100644 --- a/tests/HttpUtils/ServerUrlTest.php +++ b/tests/HttpUtils/ServerUrlTest.php | |||
@@ -186,4 +186,36 @@ class ServerUrlTest extends PHPUnit_Framework_TestCase | |||
186 | ) | 186 | ) |
187 | ); | 187 | ); |
188 | } | 188 | } |
189 | |||
190 | /** | ||
191 | * Misconfigured server (see #1022): Proxy HTTP but 443 | ||
192 | */ | ||
193 | public function testHttpWithPort433() | ||
194 | { | ||
195 | $this->assertEquals( | ||
196 | 'https://host.tld', | ||
197 | server_url( | ||
198 | array( | ||
199 | 'HTTPS' => 'Off', | ||
200 | 'SERVER_NAME' => 'host.tld', | ||
201 | 'SERVER_PORT' => '80', | ||
202 | 'HTTP_X_FORWARDED_PROTO' => 'http', | ||
203 | 'HTTP_X_FORWARDED_PORT' => '443' | ||
204 | ) | ||
205 | ) | ||
206 | ); | ||
207 | |||
208 | $this->assertEquals( | ||
209 | 'https://host.tld', | ||
210 | server_url( | ||
211 | array( | ||
212 | 'HTTPS' => 'Off', | ||
213 | 'SERVER_NAME' => 'host.tld', | ||
214 | 'SERVER_PORT' => '80', | ||
215 | 'HTTP_X_FORWARDED_PROTO' => 'https, http', | ||
216 | 'HTTP_X_FORWARDED_PORT' => '443, 80' | ||
217 | ) | ||
218 | ) | ||
219 | ); | ||
220 | } | ||
189 | } | 221 | } |
diff --git a/tests/LinkUtilsTest.php b/tests/LinkUtilsTest.php index 99679320..7fbd59b0 100644 --- a/tests/LinkUtilsTest.php +++ b/tests/LinkUtilsTest.php | |||
@@ -29,27 +29,13 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase | |||
29 | } | 29 | } |
30 | 30 | ||
31 | /** | 31 | /** |
32 | * Test get_charset() with all priorities. | ||
33 | */ | ||
34 | public function testGetCharset() | ||
35 | { | ||
36 | $headers = array('Content-Type' => 'text/html; charset=Headers'); | ||
37 | $html = '<html><meta>stuff</meta><meta charset="Html"/></html>'; | ||
38 | $default = 'default'; | ||
39 | $this->assertEquals('headers', get_charset($headers, $html, $default)); | ||
40 | $this->assertEquals('html', get_charset(array(), $html, $default)); | ||
41 | $this->assertEquals($default, get_charset(array(), '', $default)); | ||
42 | $this->assertEquals('utf-8', get_charset(array(), '')); | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Test headers_extract_charset() when the charset is found. | 32 | * Test headers_extract_charset() when the charset is found. |
47 | */ | 33 | */ |
48 | public function testHeadersExtractExistentCharset() | 34 | public function testHeadersExtractExistentCharset() |
49 | { | 35 | { |
50 | $charset = 'x-MacCroatian'; | 36 | $charset = 'x-MacCroatian'; |
51 | $headers = array('Content-Type' => 'text/html; charset='. $charset); | 37 | $headers = 'text/html; charset='. $charset; |
52 | $this->assertEquals(strtolower($charset), headers_extract_charset($headers)); | 38 | $this->assertEquals(strtolower($charset), header_extract_charset($headers)); |
53 | } | 39 | } |
54 | 40 | ||
55 | /** | 41 | /** |
@@ -57,11 +43,11 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase | |||
57 | */ | 43 | */ |
58 | public function testHeadersExtractNonExistentCharset() | 44 | public function testHeadersExtractNonExistentCharset() |
59 | { | 45 | { |
60 | $headers = array(); | 46 | $headers = ''; |
61 | $this->assertFalse(headers_extract_charset($headers)); | 47 | $this->assertFalse(header_extract_charset($headers)); |
62 | 48 | ||
63 | $headers = array('Content-Type' => 'text/html'); | 49 | $headers = 'text/html'; |
64 | $this->assertFalse(headers_extract_charset($headers)); | 50 | $this->assertFalse(header_extract_charset($headers)); |
65 | } | 51 | } |
66 | 52 | ||
67 | /** | 53 | /** |
@@ -86,6 +72,131 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase | |||
86 | } | 72 | } |
87 | 73 | ||
88 | /** | 74 | /** |
75 | * Test the download callback with valid value | ||
76 | */ | ||
77 | public function testCurlDownloadCallbackOk() | ||
78 | { | ||
79 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); | ||
80 | $data = [ | ||
81 | 'HTTP/1.1 200 OK', | ||
82 | 'Server: GitHub.com', | ||
83 | 'Date: Sat, 28 Oct 2017 12:01:33 GMT', | ||
84 | 'Content-Type: text/html; charset=utf-8', | ||
85 | 'Status: 200 OK', | ||
86 | 'end' => 'th=device-width"><title>Refactoring · GitHub</title><link rel="search" type="application/opensea', | ||
87 | '<title>ignored</title>', | ||
88 | ]; | ||
89 | foreach ($data as $key => $line) { | ||
90 | $ignore = null; | ||
91 | $expected = $key !== 'end' ? strlen($line) : false; | ||
92 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
93 | if ($expected === false) { | ||
94 | break; | ||
95 | } | ||
96 | } | ||
97 | $this->assertEquals('utf-8', $charset); | ||
98 | $this->assertEquals('Refactoring · GitHub', $title); | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Test the download callback with valid values and no charset | ||
103 | */ | ||
104 | public function testCurlDownloadCallbackOkNoCharset() | ||
105 | { | ||
106 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); | ||
107 | $data = [ | ||
108 | 'HTTP/1.1 200 OK', | ||
109 | 'end' => 'th=device-width"><title>Refactoring · GitHub</title><link rel="search" type="application/opensea', | ||
110 | '<title>ignored</title>', | ||
111 | ]; | ||
112 | foreach ($data as $key => $line) { | ||
113 | $ignore = null; | ||
114 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | ||
115 | } | ||
116 | $this->assertEmpty($charset); | ||
117 | $this->assertEquals('Refactoring · GitHub', $title); | ||
118 | } | ||
119 | |||
120 | /** | ||
121 | * Test the download callback with valid values and no charset | ||
122 | */ | ||
123 | public function testCurlDownloadCallbackOkHtmlCharset() | ||
124 | { | ||
125 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_no_charset'); | ||
126 | $data = [ | ||
127 | 'HTTP/1.1 200 OK', | ||
128 | '<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />', | ||
129 | 'end' => 'th=device-width"><title>Refactoring · GitHub</title><link rel="search" type="application/opensea', | ||
130 | '<title>ignored</title>', | ||
131 | ]; | ||
132 | foreach ($data as $key => $line) { | ||
133 | $ignore = null; | ||
134 | $expected = $key !== 'end' ? strlen($line) : false; | ||
135 | $this->assertEquals($expected, $callback($ignore, $line)); | ||
136 | if ($expected === false) { | ||
137 | break; | ||
138 | } | ||
139 | } | ||
140 | $this->assertEquals('utf-8', $charset); | ||
141 | $this->assertEquals('Refactoring · GitHub', $title); | ||
142 | } | ||
143 | |||
144 | /** | ||
145 | * Test the download callback with valid values and no title | ||
146 | */ | ||
147 | public function testCurlDownloadCallbackOkNoTitle() | ||
148 | { | ||
149 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ok'); | ||
150 | $data = [ | ||
151 | 'HTTP/1.1 200 OK', | ||
152 | 'end' => 'th=device-width">Refactoring · GitHub<link rel="search" type="application/opensea', | ||
153 | 'ignored', | ||
154 | ]; | ||
155 | foreach ($data as $key => $line) { | ||
156 | $ignore = null; | ||
157 | $this->assertEquals(strlen($line), $callback($ignore, $line)); | ||
158 | } | ||
159 | $this->assertEquals('utf-8', $charset); | ||
160 | $this->assertEmpty($title); | ||
161 | } | ||
162 | |||
163 | /** | ||
164 | * Test the download callback with an invalid content type. | ||
165 | */ | ||
166 | public function testCurlDownloadCallbackInvalidContentType() | ||
167 | { | ||
168 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_ct_ko'); | ||
169 | $ignore = null; | ||
170 | $this->assertFalse($callback($ignore, '')); | ||
171 | $this->assertEmpty($charset); | ||
172 | $this->assertEmpty($title); | ||
173 | } | ||
174 | |||
175 | /** | ||
176 | * Test the download callback with an invalid response code. | ||
177 | */ | ||
178 | public function testCurlDownloadCallbackInvalidResponseCode() | ||
179 | { | ||
180 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rc_ko'); | ||
181 | $ignore = null; | ||
182 | $this->assertFalse($callback($ignore, '')); | ||
183 | $this->assertEmpty($charset); | ||
184 | $this->assertEmpty($title); | ||
185 | } | ||
186 | |||
187 | /** | ||
188 | * Test the download callback with an invalid content type and response code. | ||
189 | */ | ||
190 | public function testCurlDownloadCallbackInvalidContentTypeAndResponseCode() | ||
191 | { | ||
192 | $callback = get_curl_download_callback($charset, $title, 'ut_curl_getinfo_rs_ct_ko'); | ||
193 | $ignore = null; | ||
194 | $this->assertFalse($callback($ignore, '')); | ||
195 | $this->assertEmpty($charset); | ||
196 | $this->assertEmpty($title); | ||
197 | } | ||
198 | |||
199 | /** | ||
89 | * Test count_private. | 200 | * Test count_private. |
90 | */ | 201 | */ |
91 | public function testCountPrivateLinks() | 202 | public function testCountPrivateLinks() |
@@ -207,3 +318,96 @@ class LinkUtilsTest extends PHPUnit_Framework_TestCase | |||
207 | return str_replace('$1', $hashtag, $hashtagLink); | 318 | return str_replace('$1', $hashtag, $hashtagLink); |
208 | } | 319 | } |
209 | } | 320 | } |
321 | |||
322 | // old style mock: PHPUnit doesn't allow function mock | ||
323 | |||
324 | /** | ||
325 | * Returns code 200 or html content type. | ||
326 | * | ||
327 | * @param resource $ch cURL resource | ||
328 | * @param int $type cURL info type | ||
329 | * | ||
330 | * @return int|string 200 or 'text/html' | ||
331 | */ | ||
332 | function ut_curl_getinfo_ok($ch, $type) | ||
333 | { | ||
334 | switch ($type) { | ||
335 | case CURLINFO_RESPONSE_CODE: | ||
336 | return 200; | ||
337 | case CURLINFO_CONTENT_TYPE: | ||
338 | return 'text/html; charset=utf-8'; | ||
339 | } | ||
340 | } | ||
341 | |||
342 | /** | ||
343 | * Returns code 200 or html content type without charset. | ||
344 | * | ||
345 | * @param resource $ch cURL resource | ||
346 | * @param int $type cURL info type | ||
347 | * | ||
348 | * @return int|string 200 or 'text/html' | ||
349 | */ | ||
350 | function ut_curl_getinfo_no_charset($ch, $type) | ||
351 | { | ||
352 | switch ($type) { | ||
353 | case CURLINFO_RESPONSE_CODE: | ||
354 | return 200; | ||
355 | case CURLINFO_CONTENT_TYPE: | ||
356 | return 'text/html'; | ||
357 | } | ||
358 | } | ||
359 | |||
360 | /** | ||
361 | * Invalid response code. | ||
362 | * | ||
363 | * @param resource $ch cURL resource | ||
364 | * @param int $type cURL info type | ||
365 | * | ||
366 | * @return int|string 404 or 'text/html' | ||
367 | */ | ||
368 | function ut_curl_getinfo_rc_ko($ch, $type) | ||
369 | { | ||
370 | switch ($type) { | ||
371 | case CURLINFO_RESPONSE_CODE: | ||
372 | return 404; | ||
373 | case CURLINFO_CONTENT_TYPE: | ||
374 | return 'text/html; charset=utf-8'; | ||
375 | } | ||
376 | } | ||
377 | |||
378 | /** | ||
379 | * Invalid content type. | ||
380 | * | ||
381 | * @param resource $ch cURL resource | ||
382 | * @param int $type cURL info type | ||
383 | * | ||
384 | * @return int|string 200 or 'text/plain' | ||
385 | */ | ||
386 | function ut_curl_getinfo_ct_ko($ch, $type) | ||
387 | { | ||
388 | switch ($type) { | ||
389 | case CURLINFO_RESPONSE_CODE: | ||
390 | return 200; | ||
391 | case CURLINFO_CONTENT_TYPE: | ||
392 | return 'text/plain'; | ||
393 | } | ||
394 | } | ||
395 | |||
396 | /** | ||
397 | * Invalid response code and content type. | ||
398 | * | ||
399 | * @param resource $ch cURL resource | ||
400 | * @param int $type cURL info type | ||
401 | * | ||
402 | * @return int|string 404 or 'text/plain' | ||
403 | */ | ||
404 | function ut_curl_getinfo_rs_ct_ko($ch, $type) | ||
405 | { | ||
406 | switch ($type) { | ||
407 | case CURLINFO_RESPONSE_CODE: | ||
408 | return 404; | ||
409 | case CURLINFO_CONTENT_TYPE: | ||
410 | return 'text/plain'; | ||
411 | } | ||
412 | } | ||
413 | |||
diff --git a/tests/api/controllers/PostLinkTest.php b/tests/api/controllers/PostLinkTest.php index 31954e39..100a9170 100644 --- a/tests/api/controllers/PostLinkTest.php +++ b/tests/api/controllers/PostLinkTest.php | |||
@@ -3,11 +3,13 @@ | |||
3 | namespace Shaarli\Api\Controllers; | 3 | namespace Shaarli\Api\Controllers; |
4 | 4 | ||
5 | 5 | ||
6 | use PHPUnit\Framework\TestCase; | ||
6 | use Shaarli\Config\ConfigManager; | 7 | use Shaarli\Config\ConfigManager; |
7 | use Slim\Container; | 8 | use Slim\Container; |
8 | use Slim\Http\Environment; | 9 | use Slim\Http\Environment; |
9 | use Slim\Http\Request; | 10 | use Slim\Http\Request; |
10 | use Slim\Http\Response; | 11 | use Slim\Http\Response; |
12 | use Slim\Router; | ||
11 | 13 | ||
12 | /** | 14 | /** |
13 | * Class PostLinkTest | 15 | * Class PostLinkTest |
@@ -16,7 +18,7 @@ use Slim\Http\Response; | |||
16 | * | 18 | * |
17 | * @package Shaarli\Api\Controllers | 19 | * @package Shaarli\Api\Controllers |
18 | */ | 20 | */ |
19 | class PostLinkTest extends \PHPUnit_Framework_TestCase | 21 | class PostLinkTest extends TestCase |
20 | { | 22 | { |
21 | /** | 23 | /** |
22 | * @var string datastore to test write operations | 24 | * @var string datastore to test write operations |
@@ -78,7 +80,7 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase | |||
78 | 80 | ||
79 | $this->controller = new Links($this->container); | 81 | $this->controller = new Links($this->container); |
80 | 82 | ||
81 | $mock = $this->getMock('\Slim\Router', ['relativePathFor']); | 83 | $mock = $this->createMock(Router::class); |
82 | $mock->expects($this->any()) | 84 | $mock->expects($this->any()) |
83 | ->method('relativePathFor') | 85 | ->method('relativePathFor') |
84 | ->willReturn('api/v1/links/1'); | 86 | ->willReturn('api/v1/links/1'); |
diff --git a/tests/config/ConfigPhpTest.php b/tests/config/ConfigPhpTest.php index abfbb305..be23eea1 100644 --- a/tests/config/ConfigPhpTest.php +++ b/tests/config/ConfigPhpTest.php | |||
@@ -37,6 +37,20 @@ class ConfigPhpTest extends \PHPUnit_Framework_TestCase | |||
37 | } | 37 | } |
38 | 38 | ||
39 | /** | 39 | /** |
40 | * Read an empty existent config file -> array with blank default values. | ||
41 | */ | ||
42 | public function testReadEmpty() | ||
43 | { | ||
44 | $dataFile = 'tests/utils/config/emptyConfigPhp.php'; | ||
45 | $conf = $this->configIO->read($dataFile); | ||
46 | $this->assertEmpty($conf['login']); | ||
47 | $this->assertEmpty($conf['title']); | ||
48 | $this->assertEmpty($conf['titleLink']); | ||
49 | $this->assertEmpty($conf['config']); | ||
50 | $this->assertEmpty($conf['plugins']); | ||
51 | } | ||
52 | |||
53 | /** | ||
40 | * Write a new config file. | 54 | * Write a new config file. |
41 | */ | 55 | */ |
42 | public function testWriteNew() | 56 | public function testWriteNew() |
diff --git a/tests/utils/config/emptyConfigPhp.php b/tests/utils/config/emptyConfigPhp.php new file mode 100644 index 00000000..b3d9bbc7 --- /dev/null +++ b/tests/utils/config/emptyConfigPhp.php | |||
@@ -0,0 +1 @@ | |||
<?php | |||