diff options
Diffstat (limited to 'tests/updater/UpdaterTest.php')
-rw-r--r-- | tests/updater/UpdaterTest.php | 79 |
1 files changed, 66 insertions, 13 deletions
diff --git a/tests/updater/UpdaterTest.php b/tests/updater/UpdaterTest.php index c689982b..47332544 100644 --- a/tests/updater/UpdaterTest.php +++ b/tests/updater/UpdaterTest.php | |||
@@ -2,17 +2,19 @@ | |||
2 | namespace Shaarli\Updater; | 2 | namespace Shaarli\Updater; |
3 | 3 | ||
4 | use Exception; | 4 | use Exception; |
5 | use malkusch\lock\mutex\NoMutex; | ||
6 | use Shaarli\Bookmark\BookmarkFileService; | ||
7 | use Shaarli\Bookmark\BookmarkServiceInterface; | ||
5 | use Shaarli\Config\ConfigManager; | 8 | use Shaarli\Config\ConfigManager; |
9 | use Shaarli\History; | ||
10 | use Shaarli\TestCase; | ||
6 | 11 | ||
7 | require_once 'tests/updater/DummyUpdater.php'; | ||
8 | require_once 'tests/utils/ReferenceLinkDB.php'; | ||
9 | require_once 'inc/rain.tpl.class.php'; | ||
10 | 12 | ||
11 | /** | 13 | /** |
12 | * Class UpdaterTest. | 14 | * Class UpdaterTest. |
13 | * Runs unit tests against the updater class. | 15 | * Runs unit tests against the updater class. |
14 | */ | 16 | */ |
15 | class UpdaterTest extends \PHPUnit\Framework\TestCase | 17 | class UpdaterTest extends TestCase |
16 | { | 18 | { |
17 | /** | 19 | /** |
18 | * @var string Path to test datastore. | 20 | * @var string Path to test datastore. |
@@ -29,13 +31,28 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
29 | */ | 31 | */ |
30 | protected $conf; | 32 | protected $conf; |
31 | 33 | ||
34 | /** @var BookmarkServiceInterface */ | ||
35 | protected $bookmarkService; | ||
36 | |||
37 | /** @var \ReferenceLinkDB */ | ||
38 | protected $refDB; | ||
39 | |||
40 | /** @var Updater */ | ||
41 | protected $updater; | ||
42 | |||
32 | /** | 43 | /** |
33 | * Executed before each test. | 44 | * Executed before each test. |
34 | */ | 45 | */ |
35 | public function setUp() | 46 | protected function setUp(): void |
36 | { | 47 | { |
48 | $mutex = new NoMutex(); | ||
49 | $this->refDB = new \ReferenceLinkDB(); | ||
50 | $this->refDB->write(self::$testDatastore); | ||
51 | |||
37 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); | 52 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); |
38 | $this->conf = new ConfigManager(self::$configFile); | 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); | ||
39 | } | 56 | } |
40 | 57 | ||
41 | /** | 58 | /** |
@@ -72,23 +89,23 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
72 | 89 | ||
73 | /** | 90 | /** |
74 | * Test errors in UpdaterUtils::write_updates_file(): empty updates file. | 91 | * Test errors in UpdaterUtils::write_updates_file(): empty updates file. |
75 | * | ||
76 | * @expectedException Exception | ||
77 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ | ||
78 | */ | 92 | */ |
79 | public function testWriteEmptyUpdatesFile() | 93 | public function testWriteEmptyUpdatesFile() |
80 | { | 94 | { |
95 | $this->expectException(\Exception::class); | ||
96 | $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/'); | ||
97 | |||
81 | UpdaterUtils::write_updates_file('', array('test')); | 98 | UpdaterUtils::write_updates_file('', array('test')); |
82 | } | 99 | } |
83 | 100 | ||
84 | /** | 101 | /** |
85 | * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. | 102 | * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. |
86 | * | ||
87 | * @expectedException Exception | ||
88 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ | ||
89 | */ | 103 | */ |
90 | public function testWriteUpdatesFileNotWritable() | 104 | public function testWriteUpdatesFileNotWritable() |
91 | { | 105 | { |
106 | $this->expectException(\Exception::class); | ||
107 | $this->expectExceptionMessageRegExp('/Unable to write(.*)/'); | ||
108 | |||
92 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; | 109 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
93 | touch($updatesFile); | 110 | touch($updatesFile); |
94 | chmod($updatesFile, 0444); | 111 | chmod($updatesFile, 0444); |
@@ -153,11 +170,11 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
153 | 170 | ||
154 | /** | 171 | /** |
155 | * Test Update failed. | 172 | * Test Update failed. |
156 | * | ||
157 | * @expectedException \Exception | ||
158 | */ | 173 | */ |
159 | public function testUpdateFailed() | 174 | public function testUpdateFailed() |
160 | { | 175 | { |
176 | $this->expectException(\Exception::class); | ||
177 | |||
161 | $updates = array( | 178 | $updates = array( |
162 | 'updateMethodDummy1', | 179 | 'updateMethodDummy1', |
163 | 'updateMethodDummy2', | 180 | 'updateMethodDummy2', |
@@ -167,4 +184,40 @@ class UpdaterTest extends \PHPUnit\Framework\TestCase | |||
167 | $updater = new DummyUpdater($updates, array(), $this->conf, true); | 184 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
168 | $updater->update(); | 185 | $updater->update(); |
169 | } | 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 | } | ||
170 | } | 223 | } |