diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/ApplicationUtilsTest.php | 228 | ||||
-rw-r--r-- | tests/CacheTest.php | 17 | ||||
-rw-r--r-- | tests/CachedPageTest.php | 2 | ||||
-rw-r--r-- | tests/LinkDBTest.php | 25 | ||||
-rw-r--r-- | tests/UtilsTest.php | 37 | ||||
-rw-r--r-- | tests/plugins/PlugQrcodeTest.php | 4 |
6 files changed, 305 insertions, 8 deletions
diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index 01301e68..6064357d 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php | |||
@@ -5,12 +5,240 @@ | |||
5 | 5 | ||
6 | require_once 'application/ApplicationUtils.php'; | 6 | require_once 'application/ApplicationUtils.php'; |
7 | 7 | ||
8 | /** | ||
9 | * Fake ApplicationUtils class to avoid HTTP requests | ||
10 | */ | ||
11 | class FakeApplicationUtils extends ApplicationUtils | ||
12 | { | ||
13 | public static $VERSION_CODE = ''; | ||
14 | |||
15 | /** | ||
16 | * Toggle HTTP requests, allow overriding the version code | ||
17 | */ | ||
18 | public static function getLatestGitVersionCode($url, $timeout=0) | ||
19 | { | ||
20 | return self::$VERSION_CODE; | ||
21 | } | ||
22 | } | ||
23 | |||
8 | 24 | ||
9 | /** | 25 | /** |
10 | * Unitary tests for Shaarli utilities | 26 | * Unitary tests for Shaarli utilities |
11 | */ | 27 | */ |
12 | class ApplicationUtilsTest extends PHPUnit_Framework_TestCase | 28 | class ApplicationUtilsTest extends PHPUnit_Framework_TestCase |
13 | { | 29 | { |
30 | protected static $testUpdateFile = 'sandbox/update.txt'; | ||
31 | protected static $testVersion = '0.5.0'; | ||
32 | protected static $versionPattern = '/^\d+\.\d+\.\d+$/'; | ||
33 | |||
34 | /** | ||
35 | * Reset test data for each test | ||
36 | */ | ||
37 | public function setUp() | ||
38 | { | ||
39 | FakeApplicationUtils::$VERSION_CODE = ''; | ||
40 | if (file_exists(self::$testUpdateFile)) { | ||
41 | unlink(self::$testUpdateFile); | ||
42 | } | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Retrieve the latest version code available on Git | ||
47 | * | ||
48 | * Expected format: Semantic Versioning - major.minor.patch | ||
49 | */ | ||
50 | public function testGetLatestGitVersionCode() | ||
51 | { | ||
52 | $testTimeout = 10; | ||
53 | |||
54 | $this->assertEquals( | ||
55 | '0.5.4', | ||
56 | ApplicationUtils::getLatestGitVersionCode( | ||
57 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' | ||
58 | .'v0.5.4/shaarli_version.php', | ||
59 | $testTimeout | ||
60 | ) | ||
61 | ); | ||
62 | $this->assertRegexp( | ||
63 | self::$versionPattern, | ||
64 | ApplicationUtils::getLatestGitVersionCode( | ||
65 | 'https://raw.githubusercontent.com/shaarli/Shaarli/' | ||
66 | .'master/shaarli_version.php', | ||
67 | $testTimeout | ||
68 | ) | ||
69 | ); | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * Attempt to retrieve the latest version from an invalid URL | ||
74 | */ | ||
75 | public function testGetLatestGitVersionCodeInvalidUrl() | ||
76 | { | ||
77 | $this->assertFalse( | ||
78 | ApplicationUtils::getLatestGitVersionCode('htttp://null.io', 1) | ||
79 | ); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * Test update checks - the user is logged off | ||
84 | */ | ||
85 | public function testCheckUpdateLoggedOff() | ||
86 | { | ||
87 | $this->assertFalse( | ||
88 | ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, false) | ||
89 | ); | ||
90 | } | ||
91 | |||
92 | /** | ||
93 | * Test update checks - the user has disabled updates | ||
94 | */ | ||
95 | public function testCheckUpdateUserDisabled() | ||
96 | { | ||
97 | $this->assertFalse( | ||
98 | ApplicationUtils::checkUpdate(self::$testVersion, 'null', 0, false, true) | ||
99 | ); | ||
100 | } | ||
101 | |||
102 | /** | ||
103 | * A newer version is available | ||
104 | */ | ||
105 | public function testCheckUpdateNewVersionAvailable() | ||
106 | { | ||
107 | $newVersion = '1.8.3'; | ||
108 | FakeApplicationUtils::$VERSION_CODE = $newVersion; | ||
109 | |||
110 | $version = FakeApplicationUtils::checkUpdate( | ||
111 | self::$testVersion, | ||
112 | self::$testUpdateFile, | ||
113 | 100, | ||
114 | true, | ||
115 | true | ||
116 | ); | ||
117 | |||
118 | $this->assertEquals($newVersion, $version); | ||
119 | } | ||
120 | |||
121 | /** | ||
122 | * No available information about versions | ||
123 | */ | ||
124 | public function testCheckUpdateNewVersionUnavailable() | ||
125 | { | ||
126 | $version = FakeApplicationUtils::checkUpdate( | ||
127 | self::$testVersion, | ||
128 | self::$testUpdateFile, | ||
129 | 100, | ||
130 | true, | ||
131 | true | ||
132 | ); | ||
133 | |||
134 | $this->assertFalse($version); | ||
135 | } | ||
136 | |||
137 | /** | ||
138 | * Test update checks - invalid Git branch | ||
139 | * @expectedException Exception | ||
140 | * @expectedExceptionMessageRegExp /Invalid branch selected for updates/ | ||
141 | */ | ||
142 | public function testCheckUpdateInvalidGitBranch() | ||
143 | { | ||
144 | ApplicationUtils::checkUpdate('', 'null', 0, true, true, 'unstable'); | ||
145 | } | ||
146 | |||
147 | /** | ||
148 | * Shaarli is up-to-date | ||
149 | */ | ||
150 | public function testCheckUpdateNewVersionUpToDate() | ||
151 | { | ||
152 | FakeApplicationUtils::$VERSION_CODE = self::$testVersion; | ||
153 | |||
154 | $version = FakeApplicationUtils::checkUpdate( | ||
155 | self::$testVersion, | ||
156 | self::$testUpdateFile, | ||
157 | 100, | ||
158 | true, | ||
159 | true | ||
160 | ); | ||
161 | |||
162 | $this->assertFalse($version); | ||
163 | } | ||
164 | |||
165 | /** | ||
166 | * Time-traveller's Shaarli | ||
167 | */ | ||
168 | public function testCheckUpdateNewVersionMaartiMcFly() | ||
169 | { | ||
170 | FakeApplicationUtils::$VERSION_CODE = '0.4.1'; | ||
171 | |||
172 | $version = FakeApplicationUtils::checkUpdate( | ||
173 | self::$testVersion, | ||
174 | self::$testUpdateFile, | ||
175 | 100, | ||
176 | true, | ||
177 | true | ||
178 | ); | ||
179 | |||
180 | $this->assertFalse($version); | ||
181 | } | ||
182 | |||
183 | /** | ||
184 | * The version has been checked recently and Shaarli is up-to-date | ||
185 | */ | ||
186 | public function testCheckUpdateNewVersionTwiceUpToDate() | ||
187 | { | ||
188 | FakeApplicationUtils::$VERSION_CODE = self::$testVersion; | ||
189 | |||
190 | // Create the update file | ||
191 | $version = FakeApplicationUtils::checkUpdate( | ||
192 | self::$testVersion, | ||
193 | self::$testUpdateFile, | ||
194 | 100, | ||
195 | true, | ||
196 | true | ||
197 | ); | ||
198 | |||
199 | $this->assertFalse($version); | ||
200 | |||
201 | // Reuse the update file | ||
202 | $version = FakeApplicationUtils::checkUpdate( | ||
203 | self::$testVersion, | ||
204 | self::$testUpdateFile, | ||
205 | 100, | ||
206 | true, | ||
207 | true | ||
208 | ); | ||
209 | |||
210 | $this->assertFalse($version); | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * The version has been checked recently and Shaarli is outdated | ||
215 | */ | ||
216 | public function testCheckUpdateNewVersionTwiceOutdated() | ||
217 | { | ||
218 | $newVersion = '1.8.3'; | ||
219 | FakeApplicationUtils::$VERSION_CODE = $newVersion; | ||
220 | |||
221 | // Create the update file | ||
222 | $version = FakeApplicationUtils::checkUpdate( | ||
223 | self::$testVersion, | ||
224 | self::$testUpdateFile, | ||
225 | 100, | ||
226 | true, | ||
227 | true | ||
228 | ); | ||
229 | $this->assertEquals($newVersion, $version); | ||
230 | |||
231 | // Reuse the update file | ||
232 | $version = FakeApplicationUtils::checkUpdate( | ||
233 | self::$testVersion, | ||
234 | self::$testUpdateFile, | ||
235 | 100, | ||
236 | true, | ||
237 | true | ||
238 | ); | ||
239 | $this->assertEquals($newVersion, $version); | ||
240 | } | ||
241 | |||
14 | /** | 242 | /** |
15 | * Check supported PHP versions | 243 | * Check supported PHP versions |
16 | */ | 244 | */ |
diff --git a/tests/CacheTest.php b/tests/CacheTest.php index aa5395b0..26c43225 100644 --- a/tests/CacheTest.php +++ b/tests/CacheTest.php | |||
@@ -11,10 +11,10 @@ require_once 'application/Cache.php'; | |||
11 | /** | 11 | /** |
12 | * Unitary tests for cached pages | 12 | * Unitary tests for cached pages |
13 | */ | 13 | */ |
14 | class CachedTest extends PHPUnit_Framework_TestCase | 14 | class CacheTest extends PHPUnit_Framework_TestCase |
15 | { | 15 | { |
16 | // test cache directory | 16 | // test cache directory |
17 | protected static $testCacheDir = 'tests/dummycache'; | 17 | protected static $testCacheDir = 'sandbox/dummycache'; |
18 | 18 | ||
19 | // dummy cached file names / content | 19 | // dummy cached file names / content |
20 | protected static $pages = array('a', 'toto', 'd7b59c'); | 20 | protected static $pages = array('a', 'toto', 'd7b59c'); |
@@ -30,7 +30,7 @@ class CachedTest extends PHPUnit_Framework_TestCase | |||
30 | } else { | 30 | } else { |
31 | array_map('unlink', glob(self::$testCacheDir.'/*')); | 31 | array_map('unlink', glob(self::$testCacheDir.'/*')); |
32 | } | 32 | } |
33 | 33 | ||
34 | foreach (self::$pages as $page) { | 34 | foreach (self::$pages as $page) { |
35 | file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page); | 35 | file_put_contents(self::$testCacheDir.'/'.$page.'.cache', $page); |
36 | } | 36 | } |
@@ -38,6 +38,15 @@ class CachedTest extends PHPUnit_Framework_TestCase | |||
38 | } | 38 | } |
39 | 39 | ||
40 | /** | 40 | /** |
41 | * Remove dummycache folder after each tests. | ||
42 | */ | ||
43 | public function tearDown() | ||
44 | { | ||
45 | array_map('unlink', glob(self::$testCacheDir.'/*')); | ||
46 | rmdir(self::$testCacheDir); | ||
47 | } | ||
48 | |||
49 | /** | ||
41 | * Purge cached pages | 50 | * Purge cached pages |
42 | */ | 51 | */ |
43 | public function testPurgeCachedPages() | 52 | public function testPurgeCachedPages() |
@@ -56,7 +65,7 @@ class CachedTest extends PHPUnit_Framework_TestCase | |||
56 | public function testPurgeCachedPagesMissingDir() | 65 | public function testPurgeCachedPagesMissingDir() |
57 | { | 66 | { |
58 | $this->assertEquals( | 67 | $this->assertEquals( |
59 | 'Cannot purge tests/dummycache_missing: no directory', | 68 | 'Cannot purge sandbox/dummycache_missing: no directory', |
60 | purgeCachedPages(self::$testCacheDir.'_missing') | 69 | purgeCachedPages(self::$testCacheDir.'_missing') |
61 | ); | 70 | ); |
62 | } | 71 | } |
diff --git a/tests/CachedPageTest.php b/tests/CachedPageTest.php index e97af030..51565cd6 100644 --- a/tests/CachedPageTest.php +++ b/tests/CachedPageTest.php | |||
@@ -11,7 +11,7 @@ require_once 'application/CachedPage.php'; | |||
11 | class CachedPageTest extends PHPUnit_Framework_TestCase | 11 | class CachedPageTest extends PHPUnit_Framework_TestCase |
12 | { | 12 | { |
13 | // test cache directory | 13 | // test cache directory |
14 | protected static $testCacheDir = 'tests/pagecache'; | 14 | protected static $testCacheDir = 'sandbox/pagecache'; |
15 | protected static $url = 'http://shaar.li/?do=atom'; | 15 | protected static $url = 'http://shaar.li/?do=atom'; |
16 | protected static $filename; | 16 | protected static $filename; |
17 | 17 | ||
diff --git a/tests/LinkDBTest.php b/tests/LinkDBTest.php index 8929713d..7b22b270 100644 --- a/tests/LinkDBTest.php +++ b/tests/LinkDBTest.php | |||
@@ -16,7 +16,7 @@ require_once 'tests/utils/ReferenceLinkDB.php'; | |||
16 | class LinkDBTest extends PHPUnit_Framework_TestCase | 16 | class LinkDBTest extends PHPUnit_Framework_TestCase |
17 | { | 17 | { |
18 | // datastore to test write operations | 18 | // datastore to test write operations |
19 | protected static $testDatastore = 'tests/datastore.php'; | 19 | protected static $testDatastore = 'sandbox/datastore.php'; |
20 | protected static $refDB = null; | 20 | protected static $refDB = null; |
21 | protected static $publicLinkDB = null; | 21 | protected static $publicLinkDB = null; |
22 | protected static $privateLinkDB = null; | 22 | protected static $privateLinkDB = null; |
@@ -511,4 +511,27 @@ class LinkDBTest extends PHPUnit_Framework_TestCase | |||
511 | sizeof(self::$publicLinkDB->filterFullText('free software')) | 511 | sizeof(self::$publicLinkDB->filterFullText('free software')) |
512 | ); | 512 | ); |
513 | } | 513 | } |
514 | |||
515 | /** | ||
516 | * Test real_url without redirector. | ||
517 | */ | ||
518 | public function testLinkRealUrlWithoutRedirector() | ||
519 | { | ||
520 | $db = new LinkDB(self::$testDatastore, false, false); | ||
521 | foreach($db as $link) { | ||
522 | $this->assertEquals($link['url'], $link['real_url']); | ||
523 | } | ||
524 | } | ||
525 | |||
526 | /** | ||
527 | * Test real_url with redirector. | ||
528 | */ | ||
529 | public function testLinkRealUrlWithRedirector() | ||
530 | { | ||
531 | $redirector = 'http://redirector.to?'; | ||
532 | $db = new LinkDB(self::$testDatastore, false, false, $redirector); | ||
533 | foreach($db as $link) { | ||
534 | $this->assertStringStartsWith($redirector, $link['real_url']); | ||
535 | } | ||
536 | } | ||
514 | } | 537 | } |
diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php index 4847ea94..02eecda2 100644 --- a/tests/UtilsTest.php +++ b/tests/UtilsTest.php | |||
@@ -187,4 +187,41 @@ class UtilsTest extends PHPUnit_Framework_TestCase | |||
187 | is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=') | 187 | is_session_id_valid('c0ZqcWF3VFE2NmJBdm1HMVQ0ZHJ3UmZPbTFsNGhkNHI=') |
188 | ); | 188 | ); |
189 | } | 189 | } |
190 | |||
191 | /** | ||
192 | * Test text2clickable without a redirector being set. | ||
193 | */ | ||
194 | public function testText2clickableWithoutRedirector() | ||
195 | { | ||
196 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | ||
197 | $expectedText = 'stuff <a href="http://hello.there/is=someone#here">http://hello.there/is=someone#here</a> otherstuff'; | ||
198 | $processedText = text2clickable($text, ''); | ||
199 | $this->assertEquals($expectedText, $processedText); | ||
200 | } | ||
201 | |||
202 | /** | ||
203 | * Test text2clickable a redirector set. | ||
204 | */ | ||
205 | public function testText2clickableWithRedirector() | ||
206 | { | ||
207 | $text = 'stuff http://hello.there/is=someone#here otherstuff'; | ||
208 | $redirector = 'http://redirector.to'; | ||
209 | $expectedText = 'stuff <a href="'. | ||
210 | $redirector . | ||
211 | urlencode('http://hello.there/is=someone#here') . | ||
212 | '">http://hello.there/is=someone#here</a> otherstuff'; | ||
213 | $processedText = text2clickable($text, $redirector); | ||
214 | $this->assertEquals($expectedText, $processedText); | ||
215 | } | ||
216 | |||
217 | /** | ||
218 | * Test testSpace2nbsp. | ||
219 | */ | ||
220 | public function testSpace2nbsp() | ||
221 | { | ||
222 | $text = ' Are you thrilled by flags ?'. PHP_EOL .' Really?'; | ||
223 | $expectedText = ' Are you thrilled by flags ?'. PHP_EOL .' Really?'; | ||
224 | $processedText = space2nbsp($text); | ||
225 | $this->assertEquals($expectedText, $processedText); | ||
226 | } | ||
190 | } | 227 | } |
diff --git a/tests/plugins/PlugQrcodeTest.php b/tests/plugins/PlugQrcodeTest.php index 86dc7f29..c749fa86 100644 --- a/tests/plugins/PlugQrcodeTest.php +++ b/tests/plugins/PlugQrcodeTest.php | |||
@@ -30,7 +30,7 @@ class PlugQrcodeTest extends PHPUnit_Framework_TestCase | |||
30 | 'title' => $str, | 30 | 'title' => $str, |
31 | 'links' => array( | 31 | 'links' => array( |
32 | array( | 32 | array( |
33 | 'url' => $str, | 33 | 'real_url' => $str, |
34 | ) | 34 | ) |
35 | ) | 35 | ) |
36 | ); | 36 | ); |
@@ -39,7 +39,7 @@ class PlugQrcodeTest extends PHPUnit_Framework_TestCase | |||
39 | $link = $data['links'][0]; | 39 | $link = $data['links'][0]; |
40 | // data shouldn't be altered | 40 | // data shouldn't be altered |
41 | $this->assertEquals($str, $data['title']); | 41 | $this->assertEquals($str, $data['title']); |
42 | $this->assertEquals($str, $link['url']); | 42 | $this->assertEquals($str, $link['real_url']); |
43 | 43 | ||
44 | // plugin data | 44 | // plugin data |
45 | $this->assertEquals(1, count($link['link_plugin'])); | 45 | $this->assertEquals(1, count($link['link_plugin'])); |