]>
Commit | Line | Data |
---|---|---|
510377d2 A |
1 | <?php |
2 | ||
684e662a | 3 | require_once 'application/config/ConfigManager.php'; |
510377d2 A |
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 | ||
21979ff1 A |
17 | /** |
18 | * @var string Path to test datastore. | |
19 | */ | |
20 | protected static $testDatastore = 'sandbox/datastore.php'; | |
21 | ||
684e662a A |
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 | ||
510377d2 A |
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( | |
510377d2 | 48 | 'DATADIR' => 'tests/Updater', |
21979ff1 | 49 | 'PAGECACHE' => 'sandbox/pagecache', |
510377d2 A |
50 | 'config1' => 'config1data', |
51 | 'config2' => 'config2data', | |
52 | ) | |
53 | ); | |
684e662a A |
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); | |
510377d2 A |
61 | } |
62 | ||
63 | /** | |
64 | * Executed after each test. | |
65 | * | |
66 | * @return void | |
67 | */ | |
68 | public function tearDown() | |
69 | { | |
684e662a A |
70 | if (is_file(self::$configFile)) { |
71 | unlink(self::$configFile); | |
510377d2 A |
72 | } |
73 | ||
74 | if (is_file(self::$configFields['config']['DATADIR'] . '/options.php')) { | |
75 | unlink(self::$configFields['config']['DATADIR'] . '/options.php'); | |
76 | } | |
77 | ||
684e662a A |
78 | if (is_file(self::$configFields['config']['DATADIR'] . '/updates.txt')) { |
79 | unlink(self::$configFields['config']['DATADIR'] . '/updates.txt'); | |
510377d2 A |
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('')); | |
684e662a | 89 | $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; |
510377d2 A |
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 | { | |
684e662a | 99 | $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; |
510377d2 A |
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 | { | |
684e662a | 132 | $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.txt'; |
510377d2 A |
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 | ); | |
684e662a | 151 | $updater = new DummyUpdater($updates, array(), true); |
510377d2 A |
152 | $this->assertEquals(array(), $updater->update()); |
153 | ||
684e662a | 154 | $updater = new DummyUpdater(array(), array(), false); |
510377d2 A |
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 | ); | |
684e662a | 169 | $updater = new DummyUpdater($updates, array(), true); |
510377d2 A |
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 | ||
684e662a | 185 | $updater = new DummyUpdater($updates, array(), true); |
510377d2 A |
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 | ||
684e662a | 202 | $updater = new DummyUpdater($updates, array(), true); |
510377d2 A |
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 | { | |
510377d2 | 215 | // Use writeConfig to create a options.php |
684e662a A |
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)); | |
510377d2 | 223 | |
684e662a | 224 | ConfigManager::$CONFIG_FILE = 'tests/Updater/config'; |
510377d2 A |
225 | |
226 | // merge configs | |
684e662a | 227 | $updater = new Updater(array(), array(), true); |
510377d2 A |
228 | $updater->updateMethodMergeDeprecatedConfigFile(); |
229 | ||
230 | // make sure updated field is changed | |
684e662a A |
231 | $this->conf->reload(); |
232 | $this->assertEquals($invert, $this->conf->get('privateLinkByDefault')); | |
233 | $this->assertFalse(is_file($optionsFile)); | |
510377d2 A |
234 | } |
235 | ||
236 | /** | |
237 | * Test mergeDeprecatedConfig in without options file. | |
238 | */ | |
239 | public function testMergeDeprecatedConfigNoFile() | |
240 | { | |
684e662a | 241 | $updater = new Updater(array(), array(), true); |
510377d2 A |
242 | $updater->updateMethodMergeDeprecatedConfigFile(); |
243 | ||
684e662a | 244 | $this->assertEquals(self::$configFields['login'], $this->conf->get('login')); |
510377d2 | 245 | } |
21979ff1 | 246 | |
684e662a A |
247 | /** |
248 | * Test renameDashTags update method. | |
249 | */ | |
21979ff1 A |
250 | public function testRenameDashTags() |
251 | { | |
252 | $refDB = new ReferenceLinkDB(); | |
253 | $refDB->write(self::$testDatastore); | |
254 | $linkDB = new LinkDB(self::$testDatastore, true, false); | |
528a6f8a | 255 | $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); |
684e662a | 256 | $updater = new Updater(array(), $linkDB, true); |
21979ff1 | 257 | $updater->updateMethodRenameDashTags(); |
528a6f8a | 258 | $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude'))); |
21979ff1 | 259 | } |
510377d2 | 260 | } |