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