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
;
15 * Runs unit tests against the updater class.
17 class UpdaterTest
extends TestCase
20 * @var string Path to test datastore.
22 protected static $testDatastore = 'sandbox/datastore.php';
25 * @var string Config file path (without extension).
27 protected static $configFile = 'sandbox/config';
34 /** @var BookmarkServiceInterface */
35 protected $bookmarkService;
37 /** @var \ReferenceLinkDB */
44 * Executed before each test.
46 protected function setUp(): void
48 $mutex = new NoMutex();
49 $this->refDB
= new \
ReferenceLinkDB();
50 $this->refDB
->write(self
::$testDatastore);
52 copy('tests/utils/config/configJson.json.php', self
::$configFile .'.json.php');
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);
59 * Test UpdaterUtils::read_updates_file with an empty/missing file.
61 public function testReadEmptyUpdatesFile()
63 $this->assertEquals(array(), UpdaterUtils
::readUpdatesFile(''));
64 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
66 $this->assertEquals(array(), UpdaterUtils
::readUpdatesFile($updatesFile));
71 * Test read/write updates file.
73 public function testReadWriteUpdatesFile()
75 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
76 $updatesMethods = array('m1', 'm2', 'm3');
78 UpdaterUtils
::writeUpdatesFile($updatesFile, $updatesMethods);
79 $readMethods = UpdaterUtils
::readUpdatesFile($updatesFile);
80 $this->assertEquals($readMethods, $updatesMethods);
83 $updatesMethods[] = 'm4';
84 UpdaterUtils
::writeUpdatesFile($updatesFile, $updatesMethods);
85 $readMethods = UpdaterUtils
::readUpdatesFile($updatesFile);
86 $this->assertEquals($readMethods, $updatesMethods);
91 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
93 public function testWriteEmptyUpdatesFile()
95 $this->expectException(\Exception
::class);
96 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
98 UpdaterUtils
::writeUpdatesFile('', array('test'));
102 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
104 public function testWriteUpdatesFileNotWritable()
106 $this->expectException(\Exception
::class);
107 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
109 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
111 chmod($updatesFile, 0444);
113 @UpdaterUtils
::writeUpdatesFile($updatesFile, array('test'));
114 } catch (Exception
$e) {
115 unlink($updatesFile);
121 * Test the update() method, with no update to run.
122 * 1. Everything already run.
123 * 2. User is logged out.
125 public function testNoUpdates()
128 'updateMethodDummy1',
129 'updateMethodDummy2',
130 'updateMethodDummy3',
131 'updateMethodException',
133 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
134 $this->assertEquals(array(), $updater->update());
136 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
137 $this->assertEquals(array(), $updater->update());
141 * Test the update() method, with all updates to run (except the failing one).
143 public function testUpdatesFirstTime()
145 $updates = array('updateMethodException',);
146 $expectedUpdates = array(
147 'updateMethodDummy1',
148 'updateMethodDummy2',
149 'updateMethodDummy3',
151 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
152 $this->assertEquals($expectedUpdates, $updater->update());
156 * Test the update() method, only one update to run.
158 public function testOneUpdate()
161 'updateMethodDummy1',
162 'updateMethodDummy3',
163 'updateMethodException',
165 $expectedUpdate = array('updateMethodDummy2');
167 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
168 $this->assertEquals($expectedUpdate, $updater->update());
172 * Test Update failed.
174 public function testUpdateFailed()
176 $this->expectException(\Exception
::class);
179 'updateMethodDummy1',
180 'updateMethodDummy2',
181 'updateMethodDummy3',
184 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
188 public function testUpdateMethodRelativeHomeLinkRename(): void
190 $this->updater
->setBasePath('/subfolder');
191 $this->conf
->set('general.header_link', '?');
193 $this->updater
->updateMethodRelativeHomeLink();
195 static::assertSame('/subfolder/', $this->conf
->get('general.header_link'));
198 public function testUpdateMethodRelativeHomeLinkDoNotRename(): void
200 $this->conf
->set('general.header_link', '~/my-blog');
202 $this->updater
->updateMethodRelativeHomeLink();
204 static::assertSame('~/my-blog', $this->conf
->get('general.header_link'));
207 public function testUpdateMethodMigrateExistingNotesUrl(): void
209 $this->updater
->updateMethodMigrateExistingNotesUrl();
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());