]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/Updater/UpdaterTest.php
ConfigManager no longer uses singleton pattern
[github/shaarli/Shaarli.git] / tests / Updater / UpdaterTest.php
CommitLineData
510377d2
A
1<?php
2
684e662a 3require_once 'application/config/ConfigManager.php';
510377d2
A
4require_once 'tests/Updater/DummyUpdater.php';
5
6/**
7 * Class UpdaterTest.
8 * Runs unit tests against the Updater class.
9 */
10class UpdaterTest extends PHPUnit_Framework_TestCase
11{
21979ff1
A
12 /**
13 * @var string Path to test datastore.
14 */
15 protected static $testDatastore = 'sandbox/datastore.php';
16
684e662a 17 /**
b74b96bf 18 * @var string Config file path (without extension).
684e662a 19 */
da10377b 20 protected static $configFile = 'tests/utils/config/configJson';
684e662a
A
21
22 /**
23 * @var ConfigManager
24 */
25 protected $conf;
26
510377d2
A
27 /**
28 * Executed before each test.
29 */
30 public function setUp()
31 {
278d9ee2 32 $this->conf = new ConfigManager(self::$configFile);
510377d2
A
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(''));
da10377b 41 $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt';
510377d2
A
42 touch($updatesFile);
43 $this->assertEquals(array(), read_updates_file($updatesFile));
da10377b 44 unlink($updatesFile);
510377d2
A
45 }
46
47 /**
48 * Test read/write updates file.
49 */
50 public function testReadWriteUpdatesFile()
51 {
da10377b 52 $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt';
510377d2
A
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);
da10377b 64 unlink($updatesFile);
510377d2
A
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 {
da10377b 86 $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt';
510377d2
A
87 touch($updatesFile);
88 chmod($updatesFile, 0444);
da10377b
A
89 try {
90 @write_updates_file($updatesFile, array('test'));
91 } catch (Exception $e) {
92 unlink($updatesFile);
93 throw $e;
94 }
510377d2
A
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 );
278d9ee2 110 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
111 $this->assertEquals(array(), $updater->update());
112
278d9ee2 113 $updater = new DummyUpdater(array(), array(), $this->conf, false);
510377d2
A
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 );
278d9ee2 128 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
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
278d9ee2 144 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
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
278d9ee2 161 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
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 {
278d9ee2
A
174 $this->conf->setConfigFile('tests/utils/config/configPhp');
175 $this->conf->reset();
684e662a
A
176
177 $optionsFile = 'tests/Updater/options.php';
da10377b
A
178 $options = '<?php
179$GLOBALS[\'privateLinkByDefault\'] = true;';
180 file_put_contents($optionsFile, $options);
510377d2 181
da10377b 182 // tmp config file.
278d9ee2 183 $this->conf->setConfigFile('tests/Updater/config');
510377d2
A
184
185 // merge configs
278d9ee2 186 $updater = new Updater(array(), array(), $this->conf, true);
b74b96bf 187 // This writes a new config file in tests/Updater/config.php
510377d2
A
188 $updater->updateMethodMergeDeprecatedConfigFile();
189
190 // make sure updated field is changed
684e662a 191 $this->conf->reload();
da10377b 192 $this->assertTrue($this->conf->get('general.default_private_links'));
684e662a 193 $this->assertFalse(is_file($optionsFile));
b74b96bf 194 // Delete the generated file.
278d9ee2 195 unlink($this->conf->getConfigFileExt());
510377d2
A
196 }
197
198 /**
199 * Test mergeDeprecatedConfig in without options file.
200 */
201 public function testMergeDeprecatedConfigNoFile()
202 {
278d9ee2 203 $updater = new Updater(array(), array(), $this->conf, true);
510377d2
A
204 $updater->updateMethodMergeDeprecatedConfigFile();
205
da10377b 206 $this->assertEquals('root', $this->conf->get('credentials.login'));
510377d2 207 }
21979ff1 208
684e662a
A
209 /**
210 * Test renameDashTags update method.
211 */
21979ff1
A
212 public function testRenameDashTags()
213 {
214 $refDB = new ReferenceLinkDB();
215 $refDB->write(self::$testDatastore);
216 $linkDB = new LinkDB(self::$testDatastore, true, false);
528a6f8a 217 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
278d9ee2 218 $updater = new Updater(array(), $linkDB, $this->conf, true);
21979ff1 219 $updater->updateMethodRenameDashTags();
528a6f8a 220 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
21979ff1 221 }
b74b96bf
A
222
223 /**
224 * Convert old PHP config file to JSON config.
225 */
226 public function testConfigToJson()
227 {
228 $configFile = 'tests/utils/config/configPhp';
278d9ee2
A
229 $this->conf->setConfigFile($configFile);
230 $this->conf->reset();
b74b96bf
A
231
232 // The ConfigIO is initialized with ConfigPhp.
278d9ee2 233 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigPhp);
b74b96bf 234
278d9ee2 235 $updater = new Updater(array(), array(), $this->conf, false);
b74b96bf
A
236 $done = $updater->updateMethodConfigToJson();
237 $this->assertTrue($done);
238
239 // The ConfigIO has been updated to ConfigJson.
278d9ee2
A
240 $this->assertTrue($this->conf->getConfigIO() instanceof ConfigJson);
241 $this->assertTrue(file_exists($this->conf->getConfigFileExt()));
b74b96bf
A
242
243 // Check JSON config data.
278d9ee2
A
244 $this->conf->reload();
245 $this->assertEquals('root', $this->conf->get('credentials.login'));
246 $this->assertEquals('lala', $this->conf->get('extras.redirector'));
247 $this->assertEquals('data/datastore.php', $this->conf->get('path.datastore'));
248 $this->assertEquals('1', $this->conf->get('plugins.WALLABAG_VERSION'));
b74b96bf
A
249
250 rename($configFile . '.save.php', $configFile . '.php');
278d9ee2 251 unlink($this->conf->getConfigFileExt());
b74b96bf
A
252 }
253
254 /**
255 * Launch config conversion update with an existing JSON file => nothing to do.
256 */
257 public function testConfigToJsonNothingToDo()
258 {
278d9ee2
A
259 $filetime = filemtime($this->conf->getConfigFileExt());
260 $updater = new Updater(array(), array(), $this->conf, false);
b74b96bf
A
261 $done = $updater->updateMethodConfigToJson();
262 $this->assertTrue($done);
278d9ee2 263 $expected = filemtime($this->conf->getConfigFileExt());
b74b96bf
A
264 $this->assertEquals($expected, $filetime);
265 }
510377d2 266}