2 namespace Shaarli\Updater
;
5 use malkusch\lock\mutex\NoMutex
;
6 use Shaarli\Bookmark\BookmarkFileService
;
7 use Shaarli\Bookmark\BookmarkServiceInterface
;
8 use Shaarli\Config\ConfigManager
;
10 use Shaarli\Plugin\PluginManager
;
16 * Runs unit tests against the updater class.
18 class UpdaterTest
extends TestCase
21 * @var string Path to test datastore.
23 protected static $testDatastore = 'sandbox/datastore.php';
26 * @var string Config file path (without extension).
28 protected static $configFile = 'sandbox/config';
35 /** @var BookmarkServiceInterface */
36 protected $bookmarkService;
38 /** @var \ReferenceLinkDB */
45 * Executed before each test.
47 protected function setUp(): void
49 $mutex = new NoMutex();
50 $this->refDB
= new \
ReferenceLinkDB();
51 $this->refDB
->write(self
::$testDatastore);
53 copy('tests/utils/config/configJson.json.php', self
::$configFile .'.json.php');
54 $this->conf
= new ConfigManager(self
::$configFile);
55 $this->bookmarkService
= new BookmarkFileService(
57 $this->createMock(PluginManager
::class),
58 $this->createMock(History
::class),
62 $this->updater
= new Updater([], $this->bookmarkService
, $this->conf
, true);
66 * Test UpdaterUtils::read_updates_file with an empty/missing file.
68 public function testReadEmptyUpdatesFile()
70 $this->assertEquals(array(), UpdaterUtils
::readUpdatesFile(''));
71 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
73 $this->assertEquals(array(), UpdaterUtils
::readUpdatesFile($updatesFile));
78 * Test read/write updates file.
80 public function testReadWriteUpdatesFile()
82 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
83 $updatesMethods = array('m1', 'm2', 'm3');
85 UpdaterUtils
::writeUpdatesFile($updatesFile, $updatesMethods);
86 $readMethods = UpdaterUtils
::readUpdatesFile($updatesFile);
87 $this->assertEquals($readMethods, $updatesMethods);
90 $updatesMethods[] = 'm4';
91 UpdaterUtils
::writeUpdatesFile($updatesFile, $updatesMethods);
92 $readMethods = UpdaterUtils
::readUpdatesFile($updatesFile);
93 $this->assertEquals($readMethods, $updatesMethods);
98 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
100 public function testWriteEmptyUpdatesFile()
102 $this->expectException(\Exception
::class);
103 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
105 UpdaterUtils
::writeUpdatesFile('', array('test'));
109 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
111 public function testWriteUpdatesFileNotWritable()
113 $this->expectException(\Exception
::class);
114 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
116 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
118 chmod($updatesFile, 0444);
120 @UpdaterUtils
::writeUpdatesFile($updatesFile, array('test'));
121 } catch (Exception
$e) {
122 unlink($updatesFile);
128 * Test the update() method, with no update to run.
129 * 1. Everything already run.
130 * 2. User is logged out.
132 public function testNoUpdates()
135 'updateMethodDummy1',
136 'updateMethodDummy2',
137 'updateMethodDummy3',
138 'updateMethodException',
140 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
141 $this->assertEquals(array(), $updater->update());
143 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
144 $this->assertEquals(array(), $updater->update());
148 * Test the update() method, with all updates to run (except the failing one).
150 public function testUpdatesFirstTime()
152 $updates = array('updateMethodException',);
153 $expectedUpdates = array(
154 'updateMethodDummy1',
155 'updateMethodDummy2',
156 'updateMethodDummy3',
158 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
159 $this->assertEquals($expectedUpdates, $updater->update());
163 * Test the update() method, only one update to run.
165 public function testOneUpdate()
168 'updateMethodDummy1',
169 'updateMethodDummy3',
170 'updateMethodException',
172 $expectedUpdate = array('updateMethodDummy2');
174 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
175 $this->assertEquals($expectedUpdate, $updater->update());
179 * Test Update failed.
181 public function testUpdateFailed()
183 $this->expectException(\Exception
::class);
186 'updateMethodDummy1',
187 'updateMethodDummy2',
188 'updateMethodDummy3',
191 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
195 public function testUpdateMethodRelativeHomeLinkRename(): void
197 $this->updater
->setBasePath('/subfolder');
198 $this->conf
->set('general.header_link', '?');
200 $this->updater
->updateMethodRelativeHomeLink();
202 static::assertSame('/subfolder/', $this->conf
->get('general.header_link'));
205 public function testUpdateMethodRelativeHomeLinkDoNotRename(): void
207 $this->conf
->set('general.header_link', '~/my-blog');
209 $this->updater
->updateMethodRelativeHomeLink();
211 static::assertSame('~/my-blog', $this->conf
->get('general.header_link'));
214 public function testUpdateMethodMigrateExistingNotesUrl(): void
216 $this->updater
->updateMethodMigrateExistingNotesUrl();
218 static::assertSame($this->refDB
->getLinks()[0]->getUrl(), $this->bookmarkService
->get(0)->getUrl());
219 static::assertSame($this->refDB
->getLinks()[1]->getUrl(), $this->bookmarkService
->get(1)->getUrl());
220 static::assertSame($this->refDB
->getLinks()[4]->getUrl(), $this->bookmarkService
->get(4)->getUrl());
221 static::assertSame($this->refDB
->getLinks()[6]->getUrl(), $this->bookmarkService
->get(6)->getUrl());
222 static::assertSame($this->refDB
->getLinks()[7]->getUrl(), $this->bookmarkService
->get(7)->getUrl());
223 static::assertSame($this->refDB
->getLinks()[8]->getUrl(), $this->bookmarkService
->get(8)->getUrl());
224 static::assertSame($this->refDB
->getLinks()[9]->getUrl(), $this->bookmarkService
->get(9)->getUrl());
225 static::assertSame('/shaare/WDWyig', $this->bookmarkService
->get(42)->getUrl());
226 static::assertSame('/shaare/WDWyig', $this->bookmarkService
->get(41)->getUrl());
227 static::assertSame('/shaare/0gCTjQ', $this->bookmarkService
->get(10)->getUrl());
228 static::assertSame('/shaare/PCRizQ', $this->bookmarkService
->get(11)->getUrl());