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 * @var string Path to test datastore.
19 protected static $testDatastore = 'sandbox/datastore.php';
22 * Executed before each test.
24 public function setUp()
26 self
::$configFields = array(
30 'timezone' => 'Europe/Paris',
32 'titleLink' => 'titleLink',
34 'disablesessionprotection' => false,
35 'privateLinkByDefault' => false,
37 'CONFIG_FILE' => 'tests/Updater/config.php',
38 'DATADIR' => 'tests/Updater',
39 'PAGECACHE' => 'sandbox/pagecache',
40 'config1' => 'config1data',
41 'config2' => 'config2data',
47 * Executed after each test.
51 public function tearDown()
53 if (is_file(self
::$configFields['config']['CONFIG_FILE'])) {
54 unlink(self
::$configFields['config']['CONFIG_FILE']);
57 if (is_file(self
::$configFields['config']['DATADIR'] . '/options.php')) {
58 unlink(self
::$configFields['config']['DATADIR'] . '/options.php');
61 if (is_file(self
::$configFields['config']['DATADIR'] . '/updates.json')) {
62 unlink(self
::$configFields['config']['DATADIR'] . '/updates.json');
67 * Test read_updates_file with an empty/missing file.
69 public function testReadEmptyUpdatesFile()
71 $this->assertEquals(array(), read_updates_file(''));
72 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.json';
74 $this->assertEquals(array(), read_updates_file($updatesFile));
78 * Test read/write updates file.
80 public function testReadWriteUpdatesFile()
82 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.json';
83 $updatesMethods = array('m1', 'm2', 'm3');
85 write_updates_file($updatesFile, $updatesMethods);
86 $readMethods = read_updates_file($updatesFile);
87 $this->assertEquals($readMethods, $updatesMethods);
90 $updatesMethods[] = 'm4';
91 write_updates_file($updatesFile, $updatesMethods);
92 $readMethods = read_updates_file($updatesFile);
93 $this->assertEquals($readMethods, $updatesMethods);
97 * Test errors in write_updates_file(): empty updates file.
99 * @expectedException Exception
100 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
102 public function testWriteEmptyUpdatesFile()
104 write_updates_file('', array('test'));
108 * Test errors in write_updates_file(): not writable updates file.
110 * @expectedException Exception
111 * @expectedExceptionMessageRegExp /Unable to write(.*)/
113 public function testWriteUpdatesFileNotWritable()
115 $updatesFile = self
::$configFields['config']['DATADIR'] . '/updates.json';
117 chmod($updatesFile, 0444);
118 @write_updates_file($updatesFile, array('test'));
122 * Test the update() method, with no update to run.
123 * 1. Everything already run.
124 * 2. User is logged out.
126 public function testNoUpdates()
129 'updateMethodDummy1',
130 'updateMethodDummy2',
131 'updateMethodDummy3',
132 'updateMethodException',
134 $updater = new DummyUpdater($updates, array(), array(), true);
135 $this->assertEquals(array(), $updater->update());
137 $updater = new DummyUpdater(array(), array(), array(), false);
138 $this->assertEquals(array(), $updater->update());
142 * Test the update() method, with all updates to run (except the failing one).
144 public function testUpdatesFirstTime()
146 $updates = array('updateMethodException',);
147 $expectedUpdates = array(
148 'updateMethodDummy1',
149 'updateMethodDummy2',
150 'updateMethodDummy3',
152 $updater = new DummyUpdater($updates, array(), array(), true);
153 $this->assertEquals($expectedUpdates, $updater->update());
157 * Test the update() method, only one update to run.
159 public function testOneUpdate()
162 'updateMethodDummy1',
163 'updateMethodDummy3',
164 'updateMethodException',
166 $expectedUpdate = array('updateMethodDummy2');
168 $updater = new DummyUpdater($updates, array(), array(), true);
169 $this->assertEquals($expectedUpdate, $updater->update());
173 * Test Update failed.
175 * @expectedException UpdaterException
177 public function testUpdateFailed()
180 'updateMethodDummy1',
181 'updateMethodDummy2',
182 'updateMethodDummy3',
185 $updater = new DummyUpdater($updates, array(), array(), true);
190 * Test update mergeDeprecatedConfig:
191 * 1. init a config file.
192 * 2. init a options.php file with update value.
194 * 4. check updated value in config file.
196 public function testUpdateMergeDeprecatedConfig()
199 writeConfig(self
::$configFields, true);
200 $configCopy = self
::$configFields;
201 $invert = !$configCopy['privateLinkByDefault'];
202 $configCopy['privateLinkByDefault'] = $invert;
204 // Use writeConfig to create a options.php
205 $configCopy['config']['CONFIG_FILE'] = 'tests/Updater/options.php';
206 writeConfig($configCopy, true);
208 $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE']));
211 $updater = new Updater(array(), self
::$configFields, array(), true);
212 $updater->updateMethodMergeDeprecatedConfigFile();
214 // make sure updated field is changed
215 include self
::$configFields['config']['CONFIG_FILE'];
216 $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']);
217 $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE']));
221 * Test mergeDeprecatedConfig in without options file.
223 public function testMergeDeprecatedConfigNoFile()
225 writeConfig(self
::$configFields, true);
227 $updater = new Updater(array(), self
::$configFields, array(), true);
228 $updater->updateMethodMergeDeprecatedConfigFile();
230 include self
::$configFields['config']['CONFIG_FILE'];
231 $this->assertEquals(self
::$configFields['login'], $GLOBALS['login']);
234 public function testRenameDashTags()
236 $refDB = new ReferenceLinkDB();
237 $refDB->write(self
::$testDatastore);
238 $linkDB = new LinkDB(self
::$testDatastore, true, false);
239 $this->assertEmpty($linkDB->filter(LinkFilter
::$FILTER_TAG, 'exclude'));
240 $updater = new Updater(array(), self
::$configFields, $linkDB, true);
241 $updater->updateMethodRenameDashTags();
242 var_dump($linkDB->filter(LinkFilter
::$FILTER_TAG, 'exclude'));
243 $this->assertNotEmpty($linkDB->filter(LinkFilter
::$FILTER_TAG, 'exclude'));