aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/ConfigTest.php42
-rw-r--r--tests/LinkDBTest.php25
-rw-r--r--tests/LinkFilterTest.php16
-rw-r--r--tests/Updater/DummyUpdater.php68
-rw-r--r--tests/Updater/UpdaterTest.php244
-rw-r--r--tests/utils/ReferenceLinkDB.php4
6 files changed, 353 insertions, 46 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/LinkDBTest.php b/tests/LinkDBTest.php
index 3b1a2057..765f771e 100644
--- a/tests/LinkDBTest.php
+++ b/tests/LinkDBTest.php
@@ -276,7 +276,8 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
276 'media' => 1, 276 'media' => 1,
277 'software' => 1, 277 'software' => 1,
278 'stallman' => 1, 278 'stallman' => 1,
279 'free' => 1 279 'free' => 1,
280 '-exclude' => 1,
280 ), 281 ),
281 self::$publicLinkDB->allTags() 282 self::$publicLinkDB->allTags()
282 ); 283 );
@@ -295,7 +296,9 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
295 'html' => 1, 296 'html' => 1,
296 'w3c' => 1, 297 'w3c' => 1,
297 'css' => 1, 298 'css' => 1,
298 'Mercurial' => 1 299 'Mercurial' => 1,
300 '-exclude' => 1,
301 '.hidden' => 1,
299 ), 302 ),
300 self::$privateLinkDB->allTags() 303 self::$privateLinkDB->allTags()
301 ); 304 );
@@ -347,4 +350,22 @@ class LinkDBTest extends PHPUnit_Framework_TestCase
347 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false)) 350 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
348 ); 351 );
349 } 352 }
353
354 /**
355 * Test hidden tags feature:
356 * tags starting with a dot '.' are only visible when logged in.
357 */
358 public function testHiddenTags()
359 {
360 $tags = '.hidden';
361 $this->assertEquals(
362 1,
363 count(self::$privateLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
364 );
365
366 $this->assertEquals(
367 0,
368 count(self::$publicLinkDB->filter(LinkFilter::$FILTER_TAG, $tags, true, false))
369 );
370 }
350} 371}
diff --git a/tests/LinkFilterTest.php b/tests/LinkFilterTest.php
index 5fb2423f..164af0d4 100644
--- a/tests/LinkFilterTest.php
+++ b/tests/LinkFilterTest.php
@@ -254,4 +254,20 @@ class LinkFilterTest extends PHPUnit_Framework_TestCase
254 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free software')) 254 count(self::$linkFilter->filter(LinkFilter::$FILTER_TEXT, 'free software'))
255 ); 255 );
256 } 256 }
257
258 /**
259 * Tag search with exclusion.
260 */
261 public function testTagFilterWithExclusion()
262 {
263 $this->assertEquals(
264 1,
265 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, 'gnu -free'))
266 );
267
268 $this->assertEquals(
269 5,
270 count(self::$linkFilter->filter(LinkFilter::$FILTER_TAG, '-free'))
271 );
272 }
257} 273}
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}
diff --git a/tests/utils/ReferenceLinkDB.php b/tests/utils/ReferenceLinkDB.php
index 011317ef..da3e8c65 100644
--- a/tests/utils/ReferenceLinkDB.php
+++ b/tests/utils/ReferenceLinkDB.php
@@ -19,7 +19,7 @@ class ReferenceLinkDB
19 'Richard Stallman and the Free Software Revolution', 19 'Richard Stallman and the Free Software Revolution',
20 0, 20 0,
21 '20150310_114633', 21 '20150310_114633',
22 'free gnu software stallman' 22 'free gnu software stallman -exclude'
23 ); 23 );
24 24
25 $this->addLink( 25 $this->addLink(
@@ -28,7 +28,7 @@ class ReferenceLinkDB
28 'A free software media publishing platform', 28 'A free software media publishing platform',
29 0, 29 0,
30 '20130614_184135', 30 '20130614_184135',
31 'gnu media web' 31 'gnu media web .hidden'
32 ); 32 );
33 33
34 $this->addLink( 34 $this->addLink(