3 require_once 'application/config/ConfigManager.php';
4 require_once 'tests/Updater/DummyUpdater.php';
8 * Runs unit tests against the Updater class.
10 class UpdaterTest
extends PHPUnit_Framework_TestCase
13 * @var array Configuration input set.
15 private static $configFields;
18 * @var string Path to test datastore.
20 protected static $testDatastore = 'sandbox/datastore.php';
23 * @var string Config file path.
25 protected static $configFile = 'tests/Updater/config.php';
33 * Executed before each test.
35 public function setUp()
37 self
::$configFields = array(
41 'timezone' => 'Europe/Paris',
43 'titleLink' => 'titleLink',
45 'disablesessionprotection' => false,
46 'privateLinkByDefault' => false,
48 'DATADIR' => 'tests/Updater',
49 'PAGECACHE' => 'sandbox/pagecache',
50 'config1' => 'config1data',
51 'config2' => 'config2data',
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);
60 $this->conf
->write(true);
64 * Executed after each test.
68 public function tearDown()
70 if (is_file(self
::$configFile)) {
71 unlink(self
::$configFile);
74 if (is_file(self
::$configFields['config']['DATADIR'] . '/options.php')) {
75 unlink(self
::$configFields['config']['DATADIR'] . '/options.php');
78 if (is_file(self
::$configFields['config']['DATADIR'] . '/updates.txt')) {
79 unlink(self
::$configFields['config']['DATADIR'] . '/updates.txt');
84 * Test read_updates_file with an empty/missing file.
86 public function testReadEmptyUpdatesFile()
88 $this->assertEquals(array(), read_updates_file(''));
89 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.txt';
91 $this->assertEquals(array(), read_updates_file($updatesFile));
95 * Test read/write updates file.
97 public function testReadWriteUpdatesFile()
99 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.txt';
100 $updatesMethods = array('m1', 'm2', 'm3');
102 write_updates_file($updatesFile, $updatesMethods);
103 $readMethods = read_updates_file($updatesFile);
104 $this->assertEquals($readMethods, $updatesMethods);
107 $updatesMethods[] = 'm4';
108 write_updates_file($updatesFile, $updatesMethods);
109 $readMethods = read_updates_file($updatesFile);
110 $this->assertEquals($readMethods, $updatesMethods);
114 * Test errors in write_updates_file(): empty updates file.
116 * @expectedException Exception
117 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
119 public function testWriteEmptyUpdatesFile()
121 write_updates_file('', array('test'));
125 * Test errors in write_updates_file(): not writable updates file.
127 * @expectedException Exception
128 * @expectedExceptionMessageRegExp /Unable to write(.*)/
130 public function testWriteUpdatesFileNotWritable()
132 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.txt';
134 chmod($updatesFile, 0444);
135 @write_updates_file($updatesFile, array('test'));
139 * Test the update() method, with no update to run.
140 * 1. Everything already run.
141 * 2. User is logged out.
143 public function testNoUpdates()
146 'updateMethodDummy1',
147 'updateMethodDummy2',
148 'updateMethodDummy3',
149 'updateMethodException',
151 $updater = new DummyUpdater($updates, array(), true);
152 $this->assertEquals(array(), $updater->update());
154 $updater = new DummyUpdater(array(), array(), false);
155 $this->assertEquals(array(), $updater->update());
159 * Test the update() method, with all updates to run (except the failing one).
161 public function testUpdatesFirstTime()
163 $updates = array('updateMethodException',);
164 $expectedUpdates = array(
165 'updateMethodDummy1',
166 'updateMethodDummy2',
167 'updateMethodDummy3',
169 $updater = new DummyUpdater($updates, array(), true);
170 $this->assertEquals($expectedUpdates, $updater->update());
174 * Test the update() method, only one update to run.
176 public function testOneUpdate()
179 'updateMethodDummy1',
180 'updateMethodDummy3',
181 'updateMethodException',
183 $expectedUpdate = array('updateMethodDummy2');
185 $updater = new DummyUpdater($updates, array(), true);
186 $this->assertEquals($expectedUpdate, $updater->update());
190 * Test Update failed.
192 * @expectedException UpdaterException
194 public function testUpdateFailed()
197 'updateMethodDummy1',
198 'updateMethodDummy2',
199 'updateMethodDummy3',
202 $updater = new DummyUpdater($updates, array(), true);
207 * Test update mergeDeprecatedConfig:
208 * 1. init a config file.
209 * 2. init a options.php file with update value.
211 * 4. check updated value in config file.
213 public function testUpdateMergeDeprecatedConfig()
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);
221 $optionsFile = 'tests/Updater/options.php';
222 $this->assertTrue(is_file($optionsFile));
224 ConfigManager
::$CONFIG_FILE = 'tests/Updater/config';
227 $updater = new Updater(array(), array(), true);
228 $updater->updateMethodMergeDeprecatedConfigFile();
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));
237 * Test mergeDeprecatedConfig in without options file.
239 public function testMergeDeprecatedConfigNoFile()
241 $updater = new Updater(array(), array(), true);
242 $updater->updateMethodMergeDeprecatedConfigFile();
244 $this->assertEquals(self
::$configFields['login'], $this->conf
->get('login'));
248 * Test renameDashTags update method.
250 public function testRenameDashTags()
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')));