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