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