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