]>
Commit | Line | Data |
---|---|---|
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\TestCase; | |
11 | ||
12 | ||
13 | /** | |
14 | * Class UpdaterTest. | |
15 | * Runs unit tests against the updater class. | |
16 | */ | |
17 | class UpdaterTest extends TestCase | |
18 | { | |
19 | /** | |
20 | * @var string Path to test datastore. | |
21 | */ | |
22 | protected static $testDatastore = 'sandbox/datastore.php'; | |
23 | ||
24 | /** | |
25 | * @var string Config file path (without extension). | |
26 | */ | |
27 | protected static $configFile = 'sandbox/config'; | |
28 | ||
29 | /** | |
30 | * @var ConfigManager | |
31 | */ | |
32 | protected $conf; | |
33 | ||
34 | /** @var BookmarkServiceInterface */ | |
35 | protected $bookmarkService; | |
36 | ||
37 | /** @var \ReferenceLinkDB */ | |
38 | protected $refDB; | |
39 | ||
40 | /** @var Updater */ | |
41 | protected $updater; | |
42 | ||
43 | /** | |
44 | * Executed before each test. | |
45 | */ | |
46 | protected function setUp(): void | |
47 | { | |
48 | $mutex = new NoMutex(); | |
49 | $this->refDB = new \ReferenceLinkDB(); | |
50 | $this->refDB->write(self::$testDatastore); | |
51 | ||
52 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); | |
53 | $this->conf = new ConfigManager(self::$configFile); | |
54 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->createMock(History::class), $mutex, true); | |
55 | $this->updater = new Updater([], $this->bookmarkService, $this->conf, true); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Test UpdaterUtils::read_updates_file with an empty/missing file. | |
60 | */ | |
61 | public function testReadEmptyUpdatesFile() | |
62 | { | |
63 | $this->assertEquals(array(), UpdaterUtils::readUpdatesFile('')); | |
64 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | |
65 | touch($updatesFile); | |
66 | $this->assertEquals(array(), UpdaterUtils::readUpdatesFile($updatesFile)); | |
67 | unlink($updatesFile); | |
68 | } | |
69 | ||
70 | /** | |
71 | * Test read/write updates file. | |
72 | */ | |
73 | public function testReadWriteUpdatesFile() | |
74 | { | |
75 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | |
76 | $updatesMethods = array('m1', 'm2', 'm3'); | |
77 | ||
78 | UpdaterUtils::writeUpdatesFile($updatesFile, $updatesMethods); | |
79 | $readMethods = UpdaterUtils::readUpdatesFile($updatesFile); | |
80 | $this->assertEquals($readMethods, $updatesMethods); | |
81 | ||
82 | // Update | |
83 | $updatesMethods[] = 'm4'; | |
84 | UpdaterUtils::writeUpdatesFile($updatesFile, $updatesMethods); | |
85 | $readMethods = UpdaterUtils::readUpdatesFile($updatesFile); | |
86 | $this->assertEquals($readMethods, $updatesMethods); | |
87 | unlink($updatesFile); | |
88 | } | |
89 | ||
90 | /** | |
91 | * Test errors in UpdaterUtils::write_updates_file(): empty updates file. | |
92 | */ | |
93 | public function testWriteEmptyUpdatesFile() | |
94 | { | |
95 | $this->expectException(\Exception::class); | |
96 | $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/'); | |
97 | ||
98 | UpdaterUtils::writeUpdatesFile('', array('test')); | |
99 | } | |
100 | ||
101 | /** | |
102 | * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. | |
103 | */ | |
104 | public function testWriteUpdatesFileNotWritable() | |
105 | { | |
106 | $this->expectException(\Exception::class); | |
107 | $this->expectExceptionMessageRegExp('/Unable to write(.*)/'); | |
108 | ||
109 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | |
110 | touch($updatesFile); | |
111 | chmod($updatesFile, 0444); | |
112 | try { | |
113 | @UpdaterUtils::writeUpdatesFile($updatesFile, array('test')); | |
114 | } catch (Exception $e) { | |
115 | unlink($updatesFile); | |
116 | throw $e; | |
117 | } | |
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 | ); | |
133 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | |
134 | $this->assertEquals(array(), $updater->update()); | |
135 | ||
136 | $updater = new DummyUpdater(array(), array(), $this->conf, false); | |
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 | ); | |
151 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | |
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 | ||
167 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | |
168 | $this->assertEquals($expectedUpdate, $updater->update()); | |
169 | } | |
170 | ||
171 | /** | |
172 | * Test Update failed. | |
173 | */ | |
174 | public function testUpdateFailed() | |
175 | { | |
176 | $this->expectException(\Exception::class); | |
177 | ||
178 | $updates = array( | |
179 | 'updateMethodDummy1', | |
180 | 'updateMethodDummy2', | |
181 | 'updateMethodDummy3', | |
182 | ); | |
183 | ||
184 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | |
185 | $updater->update(); | |
186 | } | |
187 | ||
188 | public function testUpdateMethodRelativeHomeLinkRename(): void | |
189 | { | |
190 | $this->updater->setBasePath('/subfolder'); | |
191 | $this->conf->set('general.header_link', '?'); | |
192 | ||
193 | $this->updater->updateMethodRelativeHomeLink(); | |
194 | ||
195 | static::assertSame('/subfolder/', $this->conf->get('general.header_link')); | |
196 | } | |
197 | ||
198 | public function testUpdateMethodRelativeHomeLinkDoNotRename(): void | |
199 | { | |
200 | $this->conf->set('general.header_link', '~/my-blog'); | |
201 | ||
202 | $this->updater->updateMethodRelativeHomeLink(); | |
203 | ||
204 | static::assertSame('~/my-blog', $this->conf->get('general.header_link')); | |
205 | } | |
206 | ||
207 | public function testUpdateMethodMigrateExistingNotesUrl(): void | |
208 | { | |
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()); | |
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()); | |
222 | } | |
223 | } |