]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/Updater/UpdaterTest.php
Refactor filter in LinkDB
[github/shaarli/Shaarli.git] / tests / Updater / UpdaterTest.php
CommitLineData
510377d2
A
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
21979ff1
A
16 /**
17 * @var string Path to test datastore.
18 */
19 protected static $testDatastore = 'sandbox/datastore.php';
20
510377d2
A
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',
21979ff1 39 'PAGECACHE' => 'sandbox/pagecache',
510377d2
A
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 }
21979ff1
A
233
234 public function testRenameDashTags()
235 {
236 $refDB = new ReferenceLinkDB();
237 $refDB->write(self::$testDatastore);
238 $linkDB = new LinkDB(self::$testDatastore, true, false);
528a6f8a 239 $this->assertEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
21979ff1
A
240 $updater = new Updater(array(), self::$configFields, $linkDB, true);
241 $updater->updateMethodRenameDashTags();
528a6f8a 242 $this->assertNotEmpty($linkDB->filterSearch(array('searchtags' => 'exclude')));
21979ff1 243 }
510377d2 244}