]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/Updater/UpdaterTest.php
Replace $GLOBALS configuration with the configuration manager in the whole code base
[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.
24 */
25 protected static $configFile = 'tests/Updater/config.php';
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 = 'tests/Updater/config';
56 $this->conf = ConfigManager::getInstance();
57 foreach (self::$configFields as $key => $value) {
58 $this->conf->set($key, $value);
59 }
60 $this->conf->write(true);
61 }
62
63 /**
64 * Executed after each test.
65 *
66 * @return void
67 */
68 public function tearDown()
69 {
70 if (is_file(self::$configFile)) {
71 unlink(self::$configFile);
72 }
73
74 if (is_file(self::$configFields['config']['DATADIR'] . '/options.php')) {
75 unlink(self::$configFields['config']['DATADIR'] . '/options.php');
76 }
77
78 if (is_file(self::$configFields['config']['DATADIR'] . '/updates.txt')) {
79 unlink(self::$configFields['config']['DATADIR'] . '/updates.txt');
80 }
81 }
82
83 /**
84 * Test read_updates_file with an empty/missing file.
85 */
86 public function testReadEmptyUpdatesFile()
87 {
88 $this->assertEquals(array(), read_updates_file(''));
89 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt';
90 touch($updatesFile);
91 $this->assertEquals(array(), read_updates_file($updatesFile));
92 }
93
94 /**
95 * Test read/write updates file.
96 */
97 public function testReadWriteUpdatesFile()
98 {
99 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt';
100 $updatesMethods = array('m1', 'm2', 'm3');
101
102 write_updates_file($updatesFile, $updatesMethods);
103 $readMethods = read_updates_file($updatesFile);
104 $this->assertEquals($readMethods, $updatesMethods);
105
106 // Update
107 $updatesMethods[] = 'm4';
108 write_updates_file($updatesFile, $updatesMethods);
109 $readMethods = read_updates_file($updatesFile);
110 $this->assertEquals($readMethods, $updatesMethods);
111 }
112
113 /**
114 * Test errors in write_updates_file(): empty updates file.
115 *
116 * @expectedException Exception
117 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
118 */
119 public function testWriteEmptyUpdatesFile()
120 {
121 write_updates_file('', array('test'));
122 }
123
124 /**
125 * Test errors in write_updates_file(): not writable updates file.
126 *
127 * @expectedException Exception
128 * @expectedExceptionMessageRegExp /Unable to write(.*)/
129 */
130 public function testWriteUpdatesFileNotWritable()
131 {
132 $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt';
133 touch($updatesFile);
134 chmod($updatesFile, 0444);
135 @write_updates_file($updatesFile, array('test'));
136 }
137
138 /**
139 * Test the update() method, with no update to run.
140 * 1. Everything already run.
141 * 2. User is logged out.
142 */
143 public function testNoUpdates()
144 {
145 $updates = array(
146 'updateMethodDummy1',
147 'updateMethodDummy2',
148 'updateMethodDummy3',
149 'updateMethodException',
150 );
151 $updater = new DummyUpdater($updates, array(), true);
152 $this->assertEquals(array(), $updater->update());
153
154 $updater = new DummyUpdater(array(), array(), false);
155 $this->assertEquals(array(), $updater->update());
156 }
157
158 /**
159 * Test the update() method, with all updates to run (except the failing one).
160 */
161 public function testUpdatesFirstTime()
162 {
163 $updates = array('updateMethodException',);
164 $expectedUpdates = array(
165 'updateMethodDummy1',
166 'updateMethodDummy2',
167 'updateMethodDummy3',
168 );
169 $updater = new DummyUpdater($updates, array(), true);
170 $this->assertEquals($expectedUpdates, $updater->update());
171 }
172
173 /**
174 * Test the update() method, only one update to run.
175 */
176 public function testOneUpdate()
177 {
178 $updates = array(
179 'updateMethodDummy1',
180 'updateMethodDummy3',
181 'updateMethodException',
182 );
183 $expectedUpdate = array('updateMethodDummy2');
184
185 $updater = new DummyUpdater($updates, array(), true);
186 $this->assertEquals($expectedUpdate, $updater->update());
187 }
188
189 /**
190 * Test Update failed.
191 *
192 * @expectedException UpdaterException
193 */
194 public function testUpdateFailed()
195 {
196 $updates = array(
197 'updateMethodDummy1',
198 'updateMethodDummy2',
199 'updateMethodDummy3',
200 );
201
202 $updater = new DummyUpdater($updates, array(), true);
203 $updater->update();
204 }
205
206 /**
207 * Test update mergeDeprecatedConfig:
208 * 1. init a config file.
209 * 2. init a options.php file with update value.
210 * 3. merge.
211 * 4. check updated value in config file.
212 */
213 public function testUpdateMergeDeprecatedConfig()
214 {
215 // Use writeConfig to create a options.php
216 ConfigManager::$CONFIG_FILE = 'tests/Updater/options';
217 $invert = !$this->conf->get('privateLinkByDefault');
218 $this->conf->set('privateLinkByDefault', $invert);
219 $this->conf->write(true);
220
221 $optionsFile = 'tests/Updater/options.php';
222 $this->assertTrue(is_file($optionsFile));
223
224 ConfigManager::$CONFIG_FILE = 'tests/Updater/config';
225
226 // merge configs
227 $updater = new Updater(array(), array(), true);
228 $updater->updateMethodMergeDeprecatedConfigFile();
229
230 // make sure updated field is changed
231 $this->conf->reload();
232 $this->assertEquals($invert, $this->conf->get('privateLinkByDefault'));
233 $this->assertFalse(is_file($optionsFile));
234 }
235
236 /**
237 * Test mergeDeprecatedConfig in without options file.
238 */
239 public function testMergeDeprecatedConfigNoFile()
240 {
241 $updater = new Updater(array(), array(), true);
242 $updater->updateMethodMergeDeprecatedConfigFile();
243
244 $this->assertEquals(self::$configFields['login'], $this->conf->get('login'));
245 }
246
247 /**
248 * Test renameDashTags update method.
249 */
250 public function testRenameDashTags()
251 {
252 $refDB = new ReferenceLinkDB();
253 $refDB->write(self::$testDatastore);
254 $linkDB = new LinkDB(self::$testDatastore, true, false);
255 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
256 $updater = new Updater(array(), $linkDB, true);
257 $updater->updateMethodRenameDashTags();
258 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
259 }
260 }