]>
Commit | Line | Data |
---|---|---|
510377d2 | 1 | <?php |
bcf056c9 | 2 | namespace Shaarli\Updater; |
f24896b2 | 3 | |
bcf056c9 | 4 | use Exception; |
fd1ddad9 | 5 | use malkusch\lock\mutex\NoMutex; |
1a8ac737 A |
6 | use Shaarli\Bookmark\BookmarkFileService; |
7 | use Shaarli\Bookmark\BookmarkServiceInterface; | |
3c66e564 | 8 | use Shaarli\Config\ConfigManager; |
1a8ac737 | 9 | use Shaarli\History; |
bcba6bd3 | 10 | use Shaarli\Plugin\PluginManager; |
a5a9cf23 | 11 | use Shaarli\TestCase; |
510377d2 | 12 | |
510377d2 A |
13 | |
14 | /** | |
15 | * Class UpdaterTest. | |
bcf056c9 | 16 | * Runs unit tests against the updater class. |
510377d2 | 17 | */ |
f7f08cee | 18 | class UpdaterTest extends TestCase |
510377d2 | 19 | { |
21979ff1 A |
20 | /** |
21 | * @var string Path to test datastore. | |
22 | */ | |
23 | protected static $testDatastore = 'sandbox/datastore.php'; | |
24 | ||
684e662a | 25 | /** |
b74b96bf | 26 | * @var string Config file path (without extension). |
684e662a | 27 | */ |
28f26524 | 28 | protected static $configFile = 'sandbox/config'; |
684e662a A |
29 | |
30 | /** | |
31 | * @var ConfigManager | |
32 | */ | |
33 | protected $conf; | |
34 | ||
1a8ac737 A |
35 | /** @var BookmarkServiceInterface */ |
36 | protected $bookmarkService; | |
37 | ||
c4ad3d4f A |
38 | /** @var \ReferenceLinkDB */ |
39 | protected $refDB; | |
40 | ||
1a8ac737 A |
41 | /** @var Updater */ |
42 | protected $updater; | |
43 | ||
510377d2 A |
44 | /** |
45 | * Executed before each test. | |
46 | */ | |
8f60e120 | 47 | protected function setUp(): void |
510377d2 | 48 | { |
fd1ddad9 | 49 | $mutex = new NoMutex(); |
c4ad3d4f A |
50 | $this->refDB = new \ReferenceLinkDB(); |
51 | $this->refDB->write(self::$testDatastore); | |
52 | ||
28f26524 | 53 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); |
278d9ee2 | 54 | $this->conf = new ConfigManager(self::$configFile); |
bcba6bd3 A |
55 | $this->bookmarkService = new BookmarkFileService( |
56 | $this->conf, | |
57 | $this->createMock(PluginManager::class), | |
58 | $this->createMock(History::class), | |
59 | $mutex, | |
60 | true | |
61 | ); | |
1a8ac737 | 62 | $this->updater = new Updater([], $this->bookmarkService, $this->conf, true); |
510377d2 A |
63 | } |
64 | ||
65 | /** | |
e26e2060 | 66 | * Test UpdaterUtils::read_updates_file with an empty/missing file. |
510377d2 A |
67 | */ |
68 | public function testReadEmptyUpdatesFile() | |
69 | { | |
b99e00f7 | 70 | $this->assertEquals(array(), UpdaterUtils::readUpdatesFile('')); |
894a3c4b | 71 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 | 72 | touch($updatesFile); |
b99e00f7 | 73 | $this->assertEquals(array(), UpdaterUtils::readUpdatesFile($updatesFile)); |
da10377b | 74 | unlink($updatesFile); |
510377d2 A |
75 | } |
76 | ||
77 | /** | |
78 | * Test read/write updates file. | |
79 | */ | |
80 | public function testReadWriteUpdatesFile() | |
81 | { | |
894a3c4b | 82 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
83 | $updatesMethods = array('m1', 'm2', 'm3'); |
84 | ||
b99e00f7 A |
85 | UpdaterUtils::writeUpdatesFile($updatesFile, $updatesMethods); |
86 | $readMethods = UpdaterUtils::readUpdatesFile($updatesFile); | |
510377d2 A |
87 | $this->assertEquals($readMethods, $updatesMethods); |
88 | ||
89 | // Update | |
90 | $updatesMethods[] = 'm4'; | |
b99e00f7 A |
91 | UpdaterUtils::writeUpdatesFile($updatesFile, $updatesMethods); |
92 | $readMethods = UpdaterUtils::readUpdatesFile($updatesFile); | |
510377d2 | 93 | $this->assertEquals($readMethods, $updatesMethods); |
da10377b | 94 | unlink($updatesFile); |
510377d2 A |
95 | } |
96 | ||
97 | /** | |
e26e2060 | 98 | * Test errors in UpdaterUtils::write_updates_file(): empty updates file. |
510377d2 A |
99 | */ |
100 | public function testWriteEmptyUpdatesFile() | |
101 | { | |
f447edb7 | 102 | $this->expectException(\Exception::class); |
b1baca99 A |
103 | $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/'); |
104 | ||
b99e00f7 | 105 | UpdaterUtils::writeUpdatesFile('', array('test')); |
510377d2 A |
106 | } |
107 | ||
108 | /** | |
e26e2060 | 109 | * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. |
510377d2 A |
110 | */ |
111 | public function testWriteUpdatesFileNotWritable() | |
112 | { | |
f447edb7 | 113 | $this->expectException(\Exception::class); |
b1baca99 A |
114 | $this->expectExceptionMessageRegExp('/Unable to write(.*)/'); |
115 | ||
894a3c4b | 116 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
117 | touch($updatesFile); |
118 | chmod($updatesFile, 0444); | |
da10377b | 119 | try { |
b99e00f7 | 120 | @UpdaterUtils::writeUpdatesFile($updatesFile, array('test')); |
da10377b A |
121 | } catch (Exception $e) { |
122 | unlink($updatesFile); | |
123 | throw $e; | |
124 | } | |
510377d2 A |
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 | ); | |
278d9ee2 | 140 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
141 | $this->assertEquals(array(), $updater->update()); |
142 | ||
278d9ee2 | 143 | $updater = new DummyUpdater(array(), array(), $this->conf, false); |
510377d2 A |
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 | ); | |
278d9ee2 | 158 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
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 | ||
278d9ee2 | 174 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
175 | $this->assertEquals($expectedUpdate, $updater->update()); |
176 | } | |
177 | ||
178 | /** | |
179 | * Test Update failed. | |
510377d2 A |
180 | */ |
181 | public function testUpdateFailed() | |
182 | { | |
b1baca99 A |
183 | $this->expectException(\Exception::class); |
184 | ||
510377d2 A |
185 | $updates = array( |
186 | 'updateMethodDummy1', | |
187 | 'updateMethodDummy2', | |
188 | 'updateMethodDummy3', | |
189 | ); | |
190 | ||
278d9ee2 | 191 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
192 | $updater->update(); |
193 | } | |
1a8ac737 A |
194 | |
195 | public function testUpdateMethodRelativeHomeLinkRename(): void | |
196 | { | |
c4ad3d4f | 197 | $this->updater->setBasePath('/subfolder'); |
1a8ac737 | 198 | $this->conf->set('general.header_link', '?'); |
c4ad3d4f A |
199 | |
200 | $this->updater->updateMethodRelativeHomeLink(); | |
201 | ||
202 | static::assertSame('/subfolder/', $this->conf->get('general.header_link')); | |
203 | } | |
204 | ||
205 | public function testUpdateMethodRelativeHomeLinkDoNotRename(): void | |
206 | { | |
c4ad3d4f A |
207 | $this->conf->set('general.header_link', '~/my-blog'); |
208 | ||
1a8ac737 A |
209 | $this->updater->updateMethodRelativeHomeLink(); |
210 | ||
c4ad3d4f A |
211 | static::assertSame('~/my-blog', $this->conf->get('general.header_link')); |
212 | } | |
213 | ||
214 | public function testUpdateMethodMigrateExistingNotesUrl(): void | |
215 | { | |
c4ad3d4f A |
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()); | |
f7f08cee A |
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()); | |
1a8ac737 | 229 | } |
510377d2 | 230 | } |