diff options
author | ArthurHoaro <arthur@hoa.ro> | 2016-01-12 19:50:48 +0100 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2016-02-15 20:30:24 +0100 |
commit | 510377d2cb4b12d1a421e8a88bd7edb86f223451 (patch) | |
tree | 6cea29c199fc1b29ccfb78f902313019f6f9d95e /tests/Updater/UpdaterTest.php | |
parent | 268a2e52659964fb7d033a1bb4d1490bf8cc49bf (diff) | |
download | Shaarli-510377d2cb4b12d1a421e8a88bd7edb86f223451.tar.gz Shaarli-510377d2cb4b12d1a421e8a88bd7edb86f223451.tar.zst Shaarli-510377d2cb4b12d1a421e8a88bd7edb86f223451.zip |
Introduce the Updater class which
* contains methods designed to be run once.
* is able to upgrade the datastore or the configuration.
* is based on methods names, stored in a text file with ';' separator (updates.txt).
* begins with existing function 'mergeDeprecatedConfigFile()' (options.php).
Diffstat (limited to 'tests/Updater/UpdaterTest.php')
-rw-r--r-- | tests/Updater/UpdaterTest.php | 227 |
1 files changed, 227 insertions, 0 deletions
diff --git a/tests/Updater/UpdaterTest.php b/tests/Updater/UpdaterTest.php new file mode 100644 index 00000000..63ed5e03 --- /dev/null +++ b/tests/Updater/UpdaterTest.php | |||
@@ -0,0 +1,227 @@ | |||
1 | <?php | ||
2 | |||
3 | require_once 'tests/Updater/DummyUpdater.php'; | ||
4 | |||
5 | /** | ||
6 | * Class UpdaterTest. | ||
7 | * Runs unit tests against the Updater class. | ||
8 | */ | ||
9 | class UpdaterTest extends PHPUnit_Framework_TestCase | ||
10 | { | ||
11 | /** | ||
12 | * @var array Configuration input set. | ||
13 | */ | ||
14 | private static $configFields; | ||
15 | |||
16 | /** | ||
17 | * Executed before each test. | ||
18 | */ | ||
19 | public function setUp() | ||
20 | { | ||
21 | self::$configFields = array( | ||
22 | 'login' => 'login', | ||
23 | 'hash' => 'hash', | ||
24 | 'salt' => 'salt', | ||
25 | 'timezone' => 'Europe/Paris', | ||
26 | 'title' => 'title', | ||
27 | 'titleLink' => 'titleLink', | ||
28 | 'redirector' => '', | ||
29 | 'disablesessionprotection' => false, | ||
30 | 'privateLinkByDefault' => false, | ||
31 | 'config' => array( | ||
32 | 'CONFIG_FILE' => 'tests/Updater/config.php', | ||
33 | 'DATADIR' => 'tests/Updater', | ||
34 | 'config1' => 'config1data', | ||
35 | 'config2' => 'config2data', | ||
36 | ) | ||
37 | ); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Executed after each test. | ||
42 | * | ||
43 | * @return void | ||
44 | */ | ||
45 | public function tearDown() | ||
46 | { | ||
47 | if (is_file(self::$configFields['config']['CONFIG_FILE'])) { | ||
48 | unlink(self::$configFields['config']['CONFIG_FILE']); | ||
49 | } | ||
50 | |||
51 | if (is_file(self::$configFields['config']['DATADIR'] . '/options.php')) { | ||
52 | unlink(self::$configFields['config']['DATADIR'] . '/options.php'); | ||
53 | } | ||
54 | |||
55 | if (is_file(self::$configFields['config']['DATADIR'] . '/updates.json')) { | ||
56 | unlink(self::$configFields['config']['DATADIR'] . '/updates.json'); | ||
57 | } | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Test read_updates_file with an empty/missing file. | ||
62 | */ | ||
63 | public function testReadEmptyUpdatesFile() | ||
64 | { | ||
65 | $this->assertEquals(array(), read_updates_file('')); | ||
66 | $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; | ||
67 | touch($updatesFile); | ||
68 | $this->assertEquals(array(), read_updates_file($updatesFile)); | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * Test read/write updates file. | ||
73 | */ | ||
74 | public function testReadWriteUpdatesFile() | ||
75 | { | ||
76 | $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; | ||
77 | $updatesMethods = array('m1', 'm2', 'm3'); | ||
78 | |||
79 | write_updates_file($updatesFile, $updatesMethods); | ||
80 | $readMethods = read_updates_file($updatesFile); | ||
81 | $this->assertEquals($readMethods, $updatesMethods); | ||
82 | |||
83 | // Update | ||
84 | $updatesMethods[] = 'm4'; | ||
85 | write_updates_file($updatesFile, $updatesMethods); | ||
86 | $readMethods = read_updates_file($updatesFile); | ||
87 | $this->assertEquals($readMethods, $updatesMethods); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Test errors in write_updates_file(): empty updates file. | ||
92 | * | ||
93 | * @expectedException Exception | ||
94 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ | ||
95 | */ | ||
96 | public function testWriteEmptyUpdatesFile() | ||
97 | { | ||
98 | write_updates_file('', array('test')); | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * Test errors in write_updates_file(): not writable updates file. | ||
103 | * | ||
104 | * @expectedException Exception | ||
105 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ | ||
106 | */ | ||
107 | public function testWriteUpdatesFileNotWritable() | ||
108 | { | ||
109 | $updatesFile = self::$configFields['config']['DATADIR'] . '/updates.json'; | ||
110 | touch($updatesFile); | ||
111 | chmod($updatesFile, 0444); | ||
112 | @write_updates_file($updatesFile, array('test')); | ||
113 | } | ||
114 | |||
115 | /** | ||
116 | * Test the update() method, with no update to run. | ||
117 | * 1. Everything already run. | ||
118 | * 2. User is logged out. | ||
119 | */ | ||
120 | public function testNoUpdates() | ||
121 | { | ||
122 | $updates = array( | ||
123 | 'updateMethodDummy1', | ||
124 | 'updateMethodDummy2', | ||
125 | 'updateMethodDummy3', | ||
126 | 'updateMethodException', | ||
127 | ); | ||
128 | $updater = new DummyUpdater($updates, array(), array(), true); | ||
129 | $this->assertEquals(array(), $updater->update()); | ||
130 | |||
131 | $updater = new DummyUpdater(array(), array(), array(), false); | ||
132 | $this->assertEquals(array(), $updater->update()); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * Test the update() method, with all updates to run (except the failing one). | ||
137 | */ | ||
138 | public function testUpdatesFirstTime() | ||
139 | { | ||
140 | $updates = array('updateMethodException',); | ||
141 | $expectedUpdates = array( | ||
142 | 'updateMethodDummy1', | ||
143 | 'updateMethodDummy2', | ||
144 | 'updateMethodDummy3', | ||
145 | ); | ||
146 | $updater = new DummyUpdater($updates, array(), array(), true); | ||
147 | $this->assertEquals($expectedUpdates, $updater->update()); | ||
148 | } | ||
149 | |||
150 | /** | ||
151 | * Test the update() method, only one update to run. | ||
152 | */ | ||
153 | public function testOneUpdate() | ||
154 | { | ||
155 | $updates = array( | ||
156 | 'updateMethodDummy1', | ||
157 | 'updateMethodDummy3', | ||
158 | 'updateMethodException', | ||
159 | ); | ||
160 | $expectedUpdate = array('updateMethodDummy2'); | ||
161 | |||
162 | $updater = new DummyUpdater($updates, array(), array(), true); | ||
163 | $this->assertEquals($expectedUpdate, $updater->update()); | ||
164 | } | ||
165 | |||
166 | /** | ||
167 | * Test Update failed. | ||
168 | * | ||
169 | * @expectedException UpdaterException | ||
170 | */ | ||
171 | public function testUpdateFailed() | ||
172 | { | ||
173 | $updates = array( | ||
174 | 'updateMethodDummy1', | ||
175 | 'updateMethodDummy2', | ||
176 | 'updateMethodDummy3', | ||
177 | ); | ||
178 | |||
179 | $updater = new DummyUpdater($updates, array(), array(), true); | ||
180 | $updater->update(); | ||
181 | } | ||
182 | |||
183 | /** | ||
184 | * Test update mergeDeprecatedConfig: | ||
185 | * 1. init a config file. | ||
186 | * 2. init a options.php file with update value. | ||
187 | * 3. merge. | ||
188 | * 4. check updated value in config file. | ||
189 | */ | ||
190 | public function testUpdateMergeDeprecatedConfig() | ||
191 | { | ||
192 | // init | ||
193 | writeConfig(self::$configFields, true); | ||
194 | $configCopy = self::$configFields; | ||
195 | $invert = !$configCopy['privateLinkByDefault']; | ||
196 | $configCopy['privateLinkByDefault'] = $invert; | ||
197 | |||
198 | // Use writeConfig to create a options.php | ||
199 | $configCopy['config']['CONFIG_FILE'] = 'tests/Updater/options.php'; | ||
200 | writeConfig($configCopy, true); | ||
201 | |||
202 | $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE'])); | ||
203 | |||
204 | // merge configs | ||
205 | $updater = new Updater(array(), self::$configFields, array(), true); | ||
206 | $updater->updateMethodMergeDeprecatedConfigFile(); | ||
207 | |||
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'])); | ||
212 | } | ||
213 | |||
214 | /** | ||
215 | * Test mergeDeprecatedConfig in without options file. | ||
216 | */ | ||
217 | public function testMergeDeprecatedConfigNoFile() | ||
218 | { | ||
219 | writeConfig(self::$configFields, true); | ||
220 | |||
221 | $updater = new Updater(array(), self::$configFields, array(), true); | ||
222 | $updater->updateMethodMergeDeprecatedConfigFile(); | ||
223 | |||
224 | include self::$configFields['config']['CONFIG_FILE']; | ||
225 | $this->assertEquals(self::$configFields['login'], $GLOBALS['login']); | ||
226 | } | ||
227 | } | ||