]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/updater/UpdaterTest.php
New plugin hook: ability to add custom filters to Shaarli search engine
[github/shaarli/Shaarli.git] / tests / updater / UpdaterTest.php
1 <?php
2 namespace Shaarli\Updater;
3
4 use Exception;
5 use malkusch\lock\mutex\NoMutex;
6 use Shaarli\Bookmark\BookmarkFileService;
7 use Shaarli\Bookmark\BookmarkServiceInterface;
8 use Shaarli\Config\ConfigManager;
9 use Shaarli\History;
10 use Shaarli\Plugin\PluginManager;
11 use Shaarli\TestCase;
12
13
14 /**
15 * Class UpdaterTest.
16 * Runs unit tests against the updater class.
17 */
18 class UpdaterTest extends TestCase
19 {
20 /**
21 * @var string Path to test datastore.
22 */
23 protected static $testDatastore = 'sandbox/datastore.php';
24
25 /**
26 * @var string Config file path (without extension).
27 */
28 protected static $configFile = 'sandbox/config';
29
30 /**
31 * @var ConfigManager
32 */
33 protected $conf;
34
35 /** @var BookmarkServiceInterface */
36 protected $bookmarkService;
37
38 /** @var \ReferenceLinkDB */
39 protected $refDB;
40
41 /** @var Updater */
42 protected $updater;
43
44 /**
45 * Executed before each test.
46 */
47 protected function setUp(): void
48 {
49 $mutex = new NoMutex();
50 $this->refDB = new \ReferenceLinkDB();
51 $this->refDB->write(self::$testDatastore);
52
53 copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php');
54 $this->conf = new ConfigManager(self::$configFile);
55 $this->bookmarkService = new BookmarkFileService(
56 $this->conf,
57 $this->createMock(PluginManager::class),
58 $this->createMock(History::class),
59 $mutex,
60 true
61 );
62 $this->updater = new Updater([], $this->bookmarkService, $this->conf, true);
63 }
64
65 /**
66 * Test UpdaterUtils::read_updates_file with an empty/missing file.
67 */
68 public function testReadEmptyUpdatesFile()
69 {
70 $this->assertEquals(array(), UpdaterUtils::readUpdatesFile(''));
71 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
72 touch($updatesFile);
73 $this->assertEquals(array(), UpdaterUtils::readUpdatesFile($updatesFile));
74 unlink($updatesFile);
75 }
76
77 /**
78 * Test read/write updates file.
79 */
80 public function testReadWriteUpdatesFile()
81 {
82 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
83 $updatesMethods = array('m1', 'm2', 'm3');
84
85 UpdaterUtils::writeUpdatesFile($updatesFile, $updatesMethods);
86 $readMethods = UpdaterUtils::readUpdatesFile($updatesFile);
87 $this->assertEquals($readMethods, $updatesMethods);
88
89 // Update
90 $updatesMethods[] = 'm4';
91 UpdaterUtils::writeUpdatesFile($updatesFile, $updatesMethods);
92 $readMethods = UpdaterUtils::readUpdatesFile($updatesFile);
93 $this->assertEquals($readMethods, $updatesMethods);
94 unlink($updatesFile);
95 }
96
97 /**
98 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
99 */
100 public function testWriteEmptyUpdatesFile()
101 {
102 $this->expectException(\Exception::class);
103 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
104
105 UpdaterUtils::writeUpdatesFile('', array('test'));
106 }
107
108 /**
109 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
110 */
111 public function testWriteUpdatesFileNotWritable()
112 {
113 $this->expectException(\Exception::class);
114 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
115
116 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
117 touch($updatesFile);
118 chmod($updatesFile, 0444);
119 try {
120 @UpdaterUtils::writeUpdatesFile($updatesFile, array('test'));
121 } catch (Exception $e) {
122 unlink($updatesFile);
123 throw $e;
124 }
125 }
126
127 /**
128 * Test the update() method, with no update to run.
129 * 1. Everything already run.
130 * 2. User is logged out.
131 */
132 public function testNoUpdates()
133 {
134 $updates = array(
135 'updateMethodDummy1',
136 'updateMethodDummy2',
137 'updateMethodDummy3',
138 'updateMethodException',
139 );
140 $updater = new DummyUpdater($updates, array(), $this->conf, true);
141 $this->assertEquals(array(), $updater->update());
142
143 $updater = new DummyUpdater(array(), array(), $this->conf, false);
144 $this->assertEquals(array(), $updater->update());
145 }
146
147 /**
148 * Test the update() method, with all updates to run (except the failing one).
149 */
150 public function testUpdatesFirstTime()
151 {
152 $updates = array('updateMethodException',);
153 $expectedUpdates = array(
154 'updateMethodDummy1',
155 'updateMethodDummy2',
156 'updateMethodDummy3',
157 );
158 $updater = new DummyUpdater($updates, array(), $this->conf, true);
159 $this->assertEquals($expectedUpdates, $updater->update());
160 }
161
162 /**
163 * Test the update() method, only one update to run.
164 */
165 public function testOneUpdate()
166 {
167 $updates = array(
168 'updateMethodDummy1',
169 'updateMethodDummy3',
170 'updateMethodException',
171 );
172 $expectedUpdate = array('updateMethodDummy2');
173
174 $updater = new DummyUpdater($updates, array(), $this->conf, true);
175 $this->assertEquals($expectedUpdate, $updater->update());
176 }
177
178 /**
179 * Test Update failed.
180 */
181 public function testUpdateFailed()
182 {
183 $this->expectException(\Exception::class);
184
185 $updates = array(
186 'updateMethodDummy1',
187 'updateMethodDummy2',
188 'updateMethodDummy3',
189 );
190
191 $updater = new DummyUpdater($updates, array(), $this->conf, true);
192 $updater->update();
193 }
194
195 public function testUpdateMethodRelativeHomeLinkRename(): void
196 {
197 $this->updater->setBasePath('/subfolder');
198 $this->conf->set('general.header_link', '?');
199
200 $this->updater->updateMethodRelativeHomeLink();
201
202 static::assertSame('/subfolder/', $this->conf->get('general.header_link'));
203 }
204
205 public function testUpdateMethodRelativeHomeLinkDoNotRename(): void
206 {
207 $this->conf->set('general.header_link', '~/my-blog');
208
209 $this->updater->updateMethodRelativeHomeLink();
210
211 static::assertSame('~/my-blog', $this->conf->get('general.header_link'));
212 }
213
214 public function testUpdateMethodMigrateExistingNotesUrl(): void
215 {
216 $this->updater->updateMethodMigrateExistingNotesUrl();
217
218 static::assertSame($this->refDB->getLinks()[0]->getUrl(), $this->bookmarkService->get(0)->getUrl());
219 static::assertSame($this->refDB->getLinks()[1]->getUrl(), $this->bookmarkService->get(1)->getUrl());
220 static::assertSame($this->refDB->getLinks()[4]->getUrl(), $this->bookmarkService->get(4)->getUrl());
221 static::assertSame($this->refDB->getLinks()[6]->getUrl(), $this->bookmarkService->get(6)->getUrl());
222 static::assertSame($this->refDB->getLinks()[7]->getUrl(), $this->bookmarkService->get(7)->getUrl());
223 static::assertSame($this->refDB->getLinks()[8]->getUrl(), $this->bookmarkService->get(8)->getUrl());
224 static::assertSame($this->refDB->getLinks()[9]->getUrl(), $this->bookmarkService->get(9)->getUrl());
225 static::assertSame('/shaare/WDWyig', $this->bookmarkService->get(42)->getUrl());
226 static::assertSame('/shaare/WDWyig', $this->bookmarkService->get(41)->getUrl());
227 static::assertSame('/shaare/0gCTjQ', $this->bookmarkService->get(10)->getUrl());
228 static::assertSame('/shaare/PCRizQ', $this->bookmarkService->get(11)->getUrl());
229 }
230 }