3 require_once 'tests/Updater/DummyUpdater.php';
7 * Runs unit tests against the Updater class.
9 class UpdaterTest
extends PHPUnit_Framework_TestCase
12 * @var array Configuration input set.
14 private static $configFields;
17 * Executed before each test.
19 public function setUp()
21 self
::$configFields = array(
25 'timezone' => 'Europe/Paris',
27 'titleLink' => 'titleLink',
29 'disablesessionprotection' => false,
30 'privateLinkByDefault' => false,
32 'CONFIG_FILE' => 'tests/Updater/config.php',
33 'DATADIR' => 'tests/Updater',
34 'config1' => 'config1data',
35 'config2' => 'config2data',
41 * Executed after each test.
45 public function tearDown()
47 if (is_file(self
::$configFields['config']['CONFIG_FILE'])) {
48 unlink(self
::$configFields['config']['CONFIG_FILE']);
51 if (is_file(self
::$configFields['config']['DATADIR'] . '/options.php')) {
52 unlink(self
::$configFields['config']['DATADIR'] . '/options.php');
55 if (is_file(self
::$configFields['config']['DATADIR'] . '/updates.json')) {
56 unlink(self
::$configFields['config']['DATADIR'] . '/updates.json');
61 * Test read_updates_file with an empty/missing file.
63 public function testReadEmptyUpdatesFile()
65 $this->assertEquals(array(), read_updates_file(''));
66 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.json';
68 $this->assertEquals(array(), read_updates_file($updatesFile));
72 * Test read/write updates file.
74 public function testReadWriteUpdatesFile()
76 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.json';
77 $updatesMethods = array('m1', 'm2', 'm3');
79 write_updates_file($updatesFile, $updatesMethods);
80 $readMethods = read_updates_file($updatesFile);
81 $this->assertEquals($readMethods, $updatesMethods);
84 $updatesMethods[] = 'm4';
85 write_updates_file($updatesFile, $updatesMethods);
86 $readMethods = read_updates_file($updatesFile);
87 $this->assertEquals($readMethods, $updatesMethods);
91 * Test errors in write_updates_file(): empty updates file.
93 * @expectedException Exception
94 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
96 public function testWriteEmptyUpdatesFile()
98 write_updates_file('', array('test'));
102 * Test errors in write_updates_file(): not writable updates file.
104 * @expectedException Exception
105 * @expectedExceptionMessageRegExp /Unable to write(.*)/
107 public function testWriteUpdatesFileNotWritable()
109 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.json';
111 chmod($updatesFile, 0444);
112 @write_updates_file($updatesFile, array('test'));
116 * Test the update() method, with no update to run.
117 * 1. Everything already run.
118 * 2. User is logged out.
120 public function testNoUpdates()
123 'updateMethodDummy1',
124 'updateMethodDummy2',
125 'updateMethodDummy3',
126 'updateMethodException',
128 $updater = new DummyUpdater($updates, array(), array(), true);
129 $this->assertEquals(array(), $updater->update());
131 $updater = new DummyUpdater(array(), array(), array(), false);
132 $this->assertEquals(array(), $updater->update());
136 * Test the update() method, with all updates to run (except the failing one).
138 public function testUpdatesFirstTime()
140 $updates = array('updateMethodException',);
141 $expectedUpdates = array(
142 'updateMethodDummy1',
143 'updateMethodDummy2',
144 'updateMethodDummy3',
146 $updater = new DummyUpdater($updates, array(), array(), true);
147 $this->assertEquals($expectedUpdates, $updater->update());
151 * Test the update() method, only one update to run.
153 public function testOneUpdate()
156 'updateMethodDummy1',
157 'updateMethodDummy3',
158 'updateMethodException',
160 $expectedUpdate = array('updateMethodDummy2');
162 $updater = new DummyUpdater($updates, array(), array(), true);
163 $this->assertEquals($expectedUpdate, $updater->update());
167 * Test Update failed.
169 * @expectedException UpdaterException
171 public function testUpdateFailed()
174 'updateMethodDummy1',
175 'updateMethodDummy2',
176 'updateMethodDummy3',
179 $updater = new DummyUpdater($updates, array(), array(), true);
184 * Test update mergeDeprecatedConfig:
185 * 1. init a config file.
186 * 2. init a options.php file with update value.
188 * 4. check updated value in config file.
190 public function testUpdateMergeDeprecatedConfig()
193 writeConfig(self
::$configFields, true);
194 $configCopy = self
::$configFields;
195 $invert = !$configCopy['privateLinkByDefault'];
196 $configCopy['privateLinkByDefault'] = $invert;
198 // Use writeConfig to create a options.php
199 $configCopy['config']['CONFIG_FILE'] = 'tests/Updater/options.php';
200 writeConfig($configCopy, true);
202 $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE']));
205 $updater = new Updater(array(), self
::$configFields, array(), true);
206 $updater->updateMethodMergeDeprecatedConfigFile();
208 // make sure updated field is changed
209 include self
::$configFields['config']['CONFIG_FILE'];
210 $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']);
211 $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE']));
215 * Test mergeDeprecatedConfig in without options file.
217 public function testMergeDeprecatedConfigNoFile()
219 writeConfig(self
::$configFields, true);
221 $updater = new Updater(array(), self
::$configFields, array(), true);
222 $updater->updateMethodMergeDeprecatedConfigFile();
224 include self
::$configFields['config']['CONFIG_FILE'];
225 $this->assertEquals(self
::$configFields['login'], $GLOBALS['login']);