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