]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/updater/UpdaterTest.php
c801d451fecc526bc8de258e4701a54a26f5d605
[github/shaarli/Shaarli.git] / tests / updater / UpdaterTest.php
1 <?php
2 namespace Shaarli\Updater;
3
4 use Exception;
5 use Shaarli\Bookmark\BookmarkFileService;
6 use Shaarli\Bookmark\BookmarkServiceInterface;
7 use Shaarli\Config\ConfigManager;
8 use Shaarli\History;
9
10
11 /**
12 * Class UpdaterTest.
13 * Runs unit tests against the updater class.
14 */
15 class UpdaterTest extends \PHPUnit\Framework\TestCase
16 {
17 /**
18 * @var string Path to test datastore.
19 */
20 protected static $testDatastore = 'sandbox/datastore.php';
21
22 /**
23 * @var string Config file path (without extension).
24 */
25 protected static $configFile = 'sandbox/config';
26
27 /**
28 * @var ConfigManager
29 */
30 protected $conf;
31
32 /** @var BookmarkServiceInterface */
33 protected $bookmarkService;
34
35 /** @var \ReferenceLinkDB */
36 protected $refDB;
37
38 /** @var Updater */
39 protected $updater;
40
41 /**
42 * Executed before each test.
43 */
44 public function setUp()
45 {
46 $this->refDB = new \ReferenceLinkDB();
47 $this->refDB->write(self::$testDatastore);
48
49 copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php');
50 $this->conf = new ConfigManager(self::$configFile);
51 $this->bookmarkService = new BookmarkFileService($this->conf, $this->createMock(History::class), true);
52 $this->updater = new Updater([], $this->bookmarkService, $this->conf, true);
53 }
54
55 /**
56 * Test UpdaterUtils::read_updates_file with an empty/missing file.
57 */
58 public function testReadEmptyUpdatesFile()
59 {
60 $this->assertEquals(array(), UpdaterUtils::read_updates_file(''));
61 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
62 touch($updatesFile);
63 $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile));
64 unlink($updatesFile);
65 }
66
67 /**
68 * Test read/write updates file.
69 */
70 public function testReadWriteUpdatesFile()
71 {
72 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
73 $updatesMethods = array('m1', 'm2', 'm3');
74
75 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
76 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
77 $this->assertEquals($readMethods, $updatesMethods);
78
79 // Update
80 $updatesMethods[] = 'm4';
81 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
82 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
83 $this->assertEquals($readMethods, $updatesMethods);
84 unlink($updatesFile);
85 }
86
87 /**
88 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
89 *
90 * @expectedException Exception
91 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
92 */
93 public function testWriteEmptyUpdatesFile()
94 {
95 UpdaterUtils::write_updates_file('', array('test'));
96 }
97
98 /**
99 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
100 *
101 * @expectedException Exception
102 * @expectedExceptionMessageRegExp /Unable to write(.*)/
103 */
104 public function testWriteUpdatesFileNotWritable()
105 {
106 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
107 touch($updatesFile);
108 chmod($updatesFile, 0444);
109 try {
110 @UpdaterUtils::write_updates_file($updatesFile, array('test'));
111 } catch (Exception $e) {
112 unlink($updatesFile);
113 throw $e;
114 }
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 );
130 $updater = new DummyUpdater($updates, array(), $this->conf, true);
131 $this->assertEquals(array(), $updater->update());
132
133 $updater = new DummyUpdater(array(), array(), $this->conf, false);
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 );
148 $updater = new DummyUpdater($updates, array(), $this->conf, true);
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
164 $updater = new DummyUpdater($updates, array(), $this->conf, true);
165 $this->assertEquals($expectedUpdate, $updater->update());
166 }
167
168 /**
169 * Test Update failed.
170 *
171 * @expectedException \Exception
172 */
173 public function testUpdateFailed()
174 {
175 $updates = array(
176 'updateMethodDummy1',
177 'updateMethodDummy2',
178 'updateMethodDummy3',
179 );
180
181 $updater = new DummyUpdater($updates, array(), $this->conf, true);
182 $updater->update();
183 }
184
185 public function testUpdateMethodRelativeHomeLinkRename(): void
186 {
187 $this->updater->setBasePath('/subfolder');
188 $this->conf->set('general.header_link', '?');
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
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->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());
222 }
223 }