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