diff options
Diffstat (limited to 'tests/ApplicationUtilsTest.php')
-rw-r--r-- | tests/ApplicationUtilsTest.php | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/tests/ApplicationUtilsTest.php b/tests/ApplicationUtilsTest.php index 01301e68..437c21fd 100644 --- a/tests/ApplicationUtilsTest.php +++ b/tests/ApplicationUtilsTest.php | |||
@@ -5,12 +5,230 @@ | |||
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', 0) | ||
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 testCheckUpdateNewVersionNew() | ||
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 | * Shaarli is up-to-date | ||
139 | */ | ||
140 | public function testCheckUpdateNewVersionUpToDate() | ||
141 | { | ||
142 | FakeApplicationUtils::$VERSION_CODE = self::$testVersion; | ||
143 | |||
144 | $version = FakeApplicationUtils::checkUpdate( | ||
145 | self::$testVersion, | ||
146 | self::$testUpdateFile, | ||
147 | 100, | ||
148 | true, | ||
149 | true | ||
150 | ); | ||
151 | |||
152 | $this->assertFalse($version); | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * Time-traveller's Shaarli | ||
157 | */ | ||
158 | public function testCheckUpdateNewVersionMaartiMcFly() | ||
159 | { | ||
160 | FakeApplicationUtils::$VERSION_CODE = '0.4.1'; | ||
161 | |||
162 | $version = FakeApplicationUtils::checkUpdate( | ||
163 | self::$testVersion, | ||
164 | self::$testUpdateFile, | ||
165 | 100, | ||
166 | true, | ||
167 | true | ||
168 | ); | ||
169 | |||
170 | $this->assertFalse($version); | ||
171 | } | ||
172 | |||
173 | /** | ||
174 | * The version has been checked recently and Shaarli is up-to-date | ||
175 | */ | ||
176 | public function testCheckUpdateNewVersionTwiceUpToDate() | ||
177 | { | ||
178 | FakeApplicationUtils::$VERSION_CODE = self::$testVersion; | ||
179 | |||
180 | // Create the update file | ||
181 | $version = FakeApplicationUtils::checkUpdate( | ||
182 | self::$testVersion, | ||
183 | self::$testUpdateFile, | ||
184 | 100, | ||
185 | true, | ||
186 | true | ||
187 | ); | ||
188 | |||
189 | $this->assertFalse($version); | ||
190 | |||
191 | // Reuse the update file | ||
192 | $version = FakeApplicationUtils::checkUpdate( | ||
193 | self::$testVersion, | ||
194 | self::$testUpdateFile, | ||
195 | 100, | ||
196 | true, | ||
197 | true | ||
198 | ); | ||
199 | |||
200 | $this->assertFalse($version); | ||
201 | } | ||
202 | |||
203 | /** | ||
204 | * The version has been checked recently and Shaarli is outdated | ||
205 | */ | ||
206 | public function testCheckUpdateNewVersionTwiceOutdated() | ||
207 | { | ||
208 | $newVersion = '1.8.3'; | ||
209 | FakeApplicationUtils::$VERSION_CODE = $newVersion; | ||
210 | |||
211 | // Create the update file | ||
212 | $version = FakeApplicationUtils::checkUpdate( | ||
213 | self::$testVersion, | ||
214 | self::$testUpdateFile, | ||
215 | 100, | ||
216 | true, | ||
217 | true | ||
218 | ); | ||
219 | $this->assertEquals($newVersion, $version); | ||
220 | |||
221 | // Reuse 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 | |||
14 | /** | 232 | /** |
15 | * Check supported PHP versions | 233 | * Check supported PHP versions |
16 | */ | 234 | */ |