]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/Updater/UpdaterTest.php
Remove remaining settings initialization in index.php
[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 {
b74b96bf
A
32 ConfigManager::$CONFIG_FILE = self::$configFile;
33 $this->conf = ConfigManager::reset();
510377d2
A
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(''));
da10377b 42 $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt';
510377d2
A
43 touch($updatesFile);
44 $this->assertEquals(array(), read_updates_file($updatesFile));
da10377b 45 unlink($updatesFile);
510377d2
A
46 }
47
48 /**
49 * Test read/write updates file.
50 */
51 public function testReadWriteUpdatesFile()
52 {
da10377b 53 $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt';
510377d2
A
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);
da10377b 65 unlink($updatesFile);
510377d2
A
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 {
da10377b 87 $updatesFile = $this->conf->get('path.data_dir') . '/updates.txt';
510377d2
A
88 touch($updatesFile);
89 chmod($updatesFile, 0444);
da10377b
A
90 try {
91 @write_updates_file($updatesFile, array('test'));
92 } catch (Exception $e) {
93 unlink($updatesFile);
94 throw $e;
95 }
510377d2
A
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 );
684e662a 111 $updater = new DummyUpdater($updates, array(), true);
510377d2
A
112 $this->assertEquals(array(), $updater->update());
113
684e662a 114 $updater = new DummyUpdater(array(), array(), false);
510377d2
A
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 );
684e662a 129 $updater = new DummyUpdater($updates, array(), true);
510377d2
A
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
684e662a 145 $updater = new DummyUpdater($updates, array(), true);
510377d2
A
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
684e662a 162 $updater = new DummyUpdater($updates, array(), true);
510377d2
A
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 {
da10377b
A
175 ConfigManager::$CONFIG_FILE = 'tests/utils/config/configPhp';
176 $this->conf = $this->conf->reset();
684e662a
A
177
178 $optionsFile = 'tests/Updater/options.php';
da10377b
A
179 $options = '<?php
180$GLOBALS[\'privateLinkByDefault\'] = true;';
181 file_put_contents($optionsFile, $options);
510377d2 182
da10377b 183 // tmp config file.
684e662a 184 ConfigManager::$CONFIG_FILE = 'tests/Updater/config';
510377d2
A
185
186 // merge configs
684e662a 187 $updater = new Updater(array(), array(), true);
b74b96bf 188 // This writes a new config file in tests/Updater/config.php
510377d2
A
189 $updater->updateMethodMergeDeprecatedConfigFile();
190
191 // make sure updated field is changed
684e662a 192 $this->conf->reload();
da10377b 193 $this->assertTrue($this->conf->get('general.default_private_links'));
684e662a 194 $this->assertFalse(is_file($optionsFile));
b74b96bf
A
195 // Delete the generated file.
196 unlink($this->conf->getConfigFile());
510377d2
A
197 }
198
199 /**
200 * Test mergeDeprecatedConfig in without options file.
201 */
202 public function testMergeDeprecatedConfigNoFile()
203 {
684e662a 204 $updater = new Updater(array(), array(), true);
510377d2
A
205 $updater->updateMethodMergeDeprecatedConfigFile();
206
da10377b 207 $this->assertEquals('root', $this->conf->get('credentials.login'));
510377d2 208 }
21979ff1 209
684e662a
A
210 /**
211 * Test renameDashTags update method.
212 */
21979ff1
A
213 public function testRenameDashTags()
214 {
215 $refDB = new ReferenceLinkDB();
216 $refDB->write(self::$testDatastore);
217 $linkDB = new LinkDB(self::$testDatastore, true, false);
528a6f8a 218 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
684e662a 219 $updater = new Updater(array(), $linkDB, true);
21979ff1 220 $updater->updateMethodRenameDashTags();
528a6f8a 221 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
21979ff1 222 }
b74b96bf
A
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();
da10377b
A
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'));
b74b96bf
A
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 {
da10377b 260 $filetime = filemtime($this->conf->getConfigFile());
b74b96bf
A
261 $updater = new Updater(array(), array(), false);
262 $done = $updater->updateMethodConfigToJson();
263 $this->assertTrue($done);
da10377b 264 $expected = filemtime($this->conf->getConfigFile());
b74b96bf
A
265 $this->assertEquals($expected, $filetime);
266 }
510377d2 267}