]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/Updater/UpdaterTest.php
0d0ad92220cac1fef8fc954e645066854c7f555a
[github/shaarli/Shaarli.git] / tests / Updater / UpdaterTest.php
1 <?php
2
3 require_once 'application/config/ConfigManager.php';
4 require_once 'tests/Updater/DummyUpdater.php';
5
6 /**
7 * Class UpdaterTest.
8 * Runs unit tests against the Updater class.
9 */
10 class UpdaterTest extends PHPUnit_Framework_TestCase
11 {
12 /**
13 * @var string Path to test datastore.
14 */
15 protected static $testDatastore = 'sandbox/datastore.php';
16
17 /**
18 * @var string Config file path (without extension).
19 */
20 protected static $configFile = 'tests/utils/config/configJson';
21
22 /**
23 * @var ConfigManager
24 */
25 protected $conf;
26
27 /**
28 * Executed before each test.
29 */
30 public function setUp()
31 {
32 $this->conf = new ConfigManager(self::$configFile);
33 }
34
35 /**
36 * Test read_updates_file with an empty/missing file.
37 */
38 public function testReadEmptyUpdatesFile()
39 {
40 $this->assertEquals(array(), read_updates_file(''));
41 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
42 touch($updatesFile);
43 $this->assertEquals(array(), read_updates_file($updatesFile));
44 unlink($updatesFile);
45 }
46
47 /**
48 * Test read/write updates file.
49 */
50 public function testReadWriteUpdatesFile()
51 {
52 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
53 $updatesMethods = array('m1', 'm2', 'm3');
54
55 write_updates_file($updatesFile, $updatesMethods);
56 $readMethods = read_updates_file($updatesFile);
57 $this->assertEquals($readMethods, $updatesMethods);
58
59 // Update
60 $updatesMethods[] = 'm4';
61 write_updates_file($updatesFile, $updatesMethods);
62 $readMethods = read_updates_file($updatesFile);
63 $this->assertEquals($readMethods, $updatesMethods);
64 unlink($updatesFile);
65 }
66
67 /**
68 * Test errors in write_updates_file(): empty updates file.
69 *
70 * @expectedException Exception
71 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
72 */
73 public function testWriteEmptyUpdatesFile()
74 {
75 write_updates_file('', array('test'));
76 }
77
78 /**
79 * Test errors in write_updates_file(): not writable updates file.
80 *
81 * @expectedException Exception
82 * @expectedExceptionMessageRegExp /Unable to write(.*)/
83 */
84 public function testWriteUpdatesFileNotWritable()
85 {
86 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
87 touch($updatesFile);
88 chmod($updatesFile, 0444);
89 try {
90 @write_updates_file($updatesFile, array('test'));
91 } catch (Exception $e) {
92 unlink($updatesFile);
93 throw $e;
94 }
95 }
96
97 /**
98 * Test the update() method, with no update to run.
99 * 1. Everything already run.
100 * 2. User is logged out.
101 */
102 public function testNoUpdates()
103 {
104 $updates = array(
105 'updateMethodDummy1',
106 'updateMethodDummy2',
107 'updateMethodDummy3',
108 'updateMethodException',
109 );
110 $updater = new DummyUpdater($updates, array(), $this->conf, true);
111 $this->assertEquals(array(), $updater->update());
112
113 $updater = new DummyUpdater(array(), array(), $this->conf, false);
114 $this->assertEquals(array(), $updater->update());
115 }
116
117 /**
118 * Test the update() method, with all updates to run (except the failing one).
119 */
120 public function testUpdatesFirstTime()
121 {
122 $updates = array('updateMethodException',);
123 $expectedUpdates = array(
124 'updateMethodDummy1',
125 'updateMethodDummy2',
126 'updateMethodDummy3',
127 );
128 $updater = new DummyUpdater($updates, array(), $this->conf, true);
129 $this->assertEquals($expectedUpdates, $updater->update());
130 }
131
132 /**
133 * Test the update() method, only one update to run.
134 */
135 public function testOneUpdate()
136 {
137 $updates = array(
138 'updateMethodDummy1',
139 'updateMethodDummy3',
140 'updateMethodException',
141 );
142 $expectedUpdate = array('updateMethodDummy2');
143
144 $updater = new DummyUpdater($updates, array(), $this->conf, true);
145 $this->assertEquals($expectedUpdate, $updater->update());
146 }
147
148 /**
149 * Test Update failed.
150 *
151 * @expectedException UpdaterException
152 */
153 public function testUpdateFailed()
154 {
155 $updates = array(
156 'updateMethodDummy1',
157 'updateMethodDummy2',
158 'updateMethodDummy3',
159 );
160
161 $updater = new DummyUpdater($updates, array(), $this->conf, true);
162 $updater->update();
163 }
164
165 /**
166 * Test update mergeDeprecatedConfig:
167 * 1. init a config file.
168 * 2. init a options.php file with update value.
169 * 3. merge.
170 * 4. check updated value in config file.
171 */
172 public function testUpdateMergeDeprecatedConfig()
173 {
174 $this->conf->setConfigFile('tests/utils/config/configPhp');
175 $this->conf->reset();
176
177 $optionsFile = 'tests/Updater/options.php';
178 $options = '<?php
179 $GLOBALS[\'privateLinkByDefault\'] = true;';
180 file_put_contents($optionsFile, $options);
181
182 // tmp config file.
183 $this->conf->setConfigFile('tests/Updater/config');
184
185 // merge configs
186 $updater = new Updater(array(), array(), $this->conf, true);
187 // This writes a new config file in tests/Updater/config.php
188 $updater->updateMethodMergeDeprecatedConfigFile();
189
190 // make sure updated field is changed
191 $this->conf->reload();
192 $this->assertTrue($this->conf->get('privacy.default_private_links'));
193 $this->assertFalse(is_file($optionsFile));
194 // Delete the generated file.
195 unlink($this->conf->getConfigFileExt());
196 }
197
198 /**
199 * Test mergeDeprecatedConfig in without options file.
200 */
201 public function testMergeDeprecatedConfigNoFile()
202 {
203 $updater = new Updater(array(), array(), $this->conf, true);
204 $updater->updateMethodMergeDeprecatedConfigFile();
205
206 $this->assertEquals('root', $this->conf->get('credentials.login'));
207 }
208
209 /**
210 * Test renameDashTags update method.
211 */
212 public function testRenameDashTags()
213 {
214 $refDB = new ReferenceLinkDB();
215 $refDB->write(self::$testDatastore);
216 $linkDB = new LinkDB(self::$testDatastore, true, false);
217 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
218 $updater = new Updater(array(), $linkDB, $this->conf, true);
219 $updater->updateMethodRenameDashTags();
220 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
221 }
222
223 /**
224 * Convert old PHP config file to JSON config.
225 */
226 public function testConfigToJson()
227 {
228 $configFile = 'tests/utils/config/configPhp';
229 $this->conf->setConfigFile($configFile);
230 $this->conf->reset();
231
232 // The ConfigIO is initialized with ConfigPhp.
233 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
234
235 $updater = new Updater(array(), array(), $this->conf, false);
236 $done = $updater->updateMethodConfigToJson();
237 $this->assertTrue($done);
238
239 // The ConfigIO has been updated to ConfigJson.
240 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
241 $this->assertTrue(file_exists($this->conf->getConfigFileExt()));
242
243 // Check JSON config data.
244 $this->conf->reload();
245 $this->assertEquals('root', $this->conf->get('credentials.login'));
246 $this->assertEquals('lala', $this->conf->get('redirector.url'));
247 $this->assertEquals('data/datastore.php', $this->conf->get('resource.datastore'));
248 $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
249
250 rename($configFile . '.save.php', $configFile . '.php');
251 unlink($this->conf->getConfigFileExt());
252 }
253
254 /**
255 * Launch config conversion update with an existing JSON file => nothing to do.
256 */
257 public function testConfigToJsonNothingToDo()
258 {
259 $filetime = filemtime($this->conf->getConfigFileExt());
260 $updater = new Updater(array(), array(), $this->conf, false);
261 $done = $updater->updateMethodConfigToJson();
262 $this->assertTrue($done);
263 $expected = filemtime($this->conf->getConfigFileExt());
264 $this->assertEquals($expected, $filetime);
265 }
266
267 /**
268 * Test escapeUnescapedConfig with valid data.
269 */
270 public function testEscapeConfig()
271 {
272 $sandbox = 'sandbox/config';
273 copy(self::$configFile .'.json.php', $sandbox .'.json.php');
274 $this->conf = new ConfigManager($sandbox);
275 $title = '<script>alert("title");</script>';
276 $headerLink = '<script>alert("header_link");</script>';
277 $redirectorUrl = '<script>alert("redirector");</script>';
278 $this->conf->set('general.title', $title);
279 $this->conf->set('general.header_link', $headerLink);
280 $this->conf->set('redirector.url', $redirectorUrl);
281 $updater = new Updater(array(), array(), $this->conf, true);
282 $done = $updater->updateMethodEscapeUnescapedConfig();
283 $this->assertTrue($done);
284 $this->conf->reload();
285 $this->assertEquals(escape($title), $this->conf->get('general.title'));
286 $this->assertEquals(escape($headerLink), $this->conf->get('general.header_link'));
287 $this->assertEquals(escape($redirectorUrl), $this->conf->get('redirector.url'));
288 unlink($sandbox .'.json.php');
289 }
290 }