diff options
author | Arthur <arthur@hoa.ro> | 2016-02-15 20:36:42 +0100 |
---|---|---|
committer | Arthur <arthur@hoa.ro> | 2016-02-15 20:36:42 +0100 |
commit | 854ea372553d6ef8174cae320801e5ff3a101a87 (patch) | |
tree | da9185d7c5d733219ca4638b512b18f2d9a55174 /tests | |
parent | 2865118ca44661c7535aaf7b83e3b9222bc1f9a6 (diff) | |
parent | 510377d2cb4b12d1a421e8a88bd7edb86f223451 (diff) | |
download | Shaarli-854ea372553d6ef8174cae320801e5ff3a101a87.tar.gz Shaarli-854ea372553d6ef8174cae320801e5ff3a101a87.tar.zst Shaarli-854ea372553d6ef8174cae320801e5ff3a101a87.zip |
Merge pull request #442 from ArthurHoaro/updater
Introduce the Updater class which
Diffstat (limited to 'tests')
-rw-r--r-- | tests/ConfigTest.php | 42 | ||||
-rw-r--r-- | tests/Updater/DummyUpdater.php | 68 | ||||
-rw-r--r-- | tests/Updater/UpdaterTest.php | 227 |
3 files changed, 295 insertions, 42 deletions
diff --git a/tests/ConfigTest.php b/tests/ConfigTest.php index 492ddd3b..7200aae6 100644 --- a/tests/ConfigTest.php +++ b/tests/ConfigTest.php | |||
@@ -134,48 +134,6 @@ class ConfigTest extends PHPUnit_Framework_TestCase | |||
134 | } | 134 | } |
135 | 135 | ||
136 | /** | 136 | /** |
137 | * Test mergeDeprecatedConfig while being logged in: | ||
138 | * 1. init a config file. | ||
139 | * 2. init a options.php file with update value. | ||
140 | * 3. merge. | ||
141 | * 4. check updated value in config file. | ||
142 | */ | ||
143 | public function testMergeDeprecatedConfig() | ||
144 | { | ||
145 | // init | ||
146 | writeConfig(self::$configFields, true); | ||
147 | $configCopy = self::$configFields; | ||
148 | $invert = !$configCopy['privateLinkByDefault']; | ||
149 | $configCopy['privateLinkByDefault'] = $invert; | ||
150 | |||
151 | // Use writeConfig to create a options.php | ||
152 | $configCopy['config']['CONFIG_FILE'] = 'tests/options.php'; | ||
153 | writeConfig($configCopy, true); | ||
154 | |||
155 | $this->assertTrue(is_file($configCopy['config']['CONFIG_FILE'])); | ||
156 | |||
157 | // merge configs | ||
158 | mergeDeprecatedConfig(self::$configFields, true); | ||
159 | |||
160 | // make sure updated field is changed | ||
161 | include self::$configFields['config']['CONFIG_FILE']; | ||
162 | $this->assertEquals($invert, $GLOBALS['privateLinkByDefault']); | ||
163 | $this->assertFalse(is_file($configCopy['config']['CONFIG_FILE'])); | ||
164 | } | ||
165 | |||
166 | /** | ||
167 | * Test mergeDeprecatedConfig while being logged in without options file. | ||
168 | */ | ||
169 | public function testMergeDeprecatedConfigNoFile() | ||
170 | { | ||
171 | writeConfig(self::$configFields, true); | ||
172 | mergeDeprecatedConfig(self::$configFields, true); | ||
173 | |||
174 | include self::$configFields['config']['CONFIG_FILE']; | ||
175 | $this->assertEquals(self::$configFields['login'], $GLOBALS['login']); | ||
176 | } | ||
177 | |||
178 | /** | ||
179 | * Test save_plugin_config with valid data. | 137 | * Test save_plugin_config with valid data. |
180 | * | 138 | * |
181 | * @throws PluginConfigOrderException | 139 | * @throws PluginConfigOrderException |
diff --git a/tests/Updater/DummyUpdater.php b/tests/Updater/DummyUpdater.php new file mode 100644 index 00000000..e9ef2aaa --- /dev/null +++ b/tests/Updater/DummyUpdater.php | |||
@@ -0,0 +1,68 @@ | |||
1 | <?php | ||
2 | |||
3 | require_once 'application/Updater.php'; | ||
4 | |||
5 | /** | ||
6 | * Class DummyUpdater. | ||
7 | * Extends Updater to add update method designed for unit tests. | ||
8 | */ | ||
9 | class DummyUpdater extends Updater | ||
10 | { | ||
11 | /** | ||
12 | * Object constructor. | ||
13 | * | ||
14 | * @param array $doneUpdates Updates which are already done. | ||
15 | * @param array $config Shaarli's configuration array. | ||
16 | * @param LinkDB $linkDB LinkDB instance. | ||
17 | * @param boolean $isLoggedIn True if the user is logged in. | ||
18 | */ | ||
19 | public function __construct($doneUpdates, $config, $linkDB, $isLoggedIn) | ||
20 | { | ||
21 | parent::__construct($doneUpdates, $config, $linkDB, $isLoggedIn); | ||
22 | |||
23 | // Retrieve all update methods. | ||
24 | // For unit test, only retrieve final methods, | ||
25 | $class = new ReflectionClass($this); | ||
26 | $this->methods = $class->getMethods(ReflectionMethod::IS_FINAL); | ||
27 | } | ||
28 | |||
29 | /** | ||
30 | * Update method 1. | ||
31 | * | ||
32 | * @return bool true. | ||
33 | */ | ||
34 | private final function updateMethodDummy1() | ||
35 | { | ||
36 | return true; | ||
37 | } | ||
38 | |||
39 | /** | ||
40 | * Update method 2. | ||
41 | * | ||
42 | * @return bool true. | ||
43 | */ | ||
44 | private final function updateMethodDummy2() | ||
45 | { | ||
46 | return true; | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * Update method 3. | ||
51 | * | ||
52 | * @return bool true. | ||
53 | */ | ||
54 | private final function updateMethodDummy3() | ||
55 | { | ||
56 | return true; | ||
57 | } | ||
58 | |||
59 | /** | ||
60 | * Update method 4, raise an exception. | ||
61 | * | ||
62 | * @throws Exception error. | ||
63 | */ | ||
64 | private final function updateMethodException() | ||
65 | { | ||
66 | throw new Exception('whatever'); | ||
67 | } | ||
68 | } | ||
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 | } | ||