2 namespace Shaarli\Updater
;
5 use Shaarli\Bookmark\BookmarkFileService
;
6 use Shaarli\Bookmark\BookmarkServiceInterface
;
7 use Shaarli\Config\ConfigManager
;
14 * Runs unit tests against the updater class.
16 class UpdaterTest
extends TestCase
19 * @var string Path to test datastore.
21 protected static $testDatastore = 'sandbox/datastore.php';
24 * @var string Config file path (without extension).
26 protected static $configFile = 'sandbox/config';
33 /** @var BookmarkServiceInterface */
34 protected $bookmarkService;
36 /** @var \ReferenceLinkDB */
43 * Executed before each test.
45 protected function setUp(): void
47 $this->refDB
= new \
ReferenceLinkDB();
48 $this->refDB
->write(self
::$testDatastore);
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);
57 * Test UpdaterUtils::read_updates_file with an empty/missing file.
59 public function testReadEmptyUpdatesFile()
61 $this->assertEquals(array(), UpdaterUtils
::read_updates_file(''));
62 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
64 $this->assertEquals(array(), UpdaterUtils
::read_updates_file($updatesFile));
69 * Test read/write updates file.
71 public function testReadWriteUpdatesFile()
73 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
74 $updatesMethods = array('m1', 'm2', 'm3');
76 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
77 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
78 $this->assertEquals($readMethods, $updatesMethods);
81 $updatesMethods[] = 'm4';
82 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
83 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
84 $this->assertEquals($readMethods, $updatesMethods);
89 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
91 public function testWriteEmptyUpdatesFile()
93 $this->expectException(\Exception
::class);
94 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
96 UpdaterUtils
::write_updates_file('', array('test'));
100 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
102 public function testWriteUpdatesFileNotWritable()
104 $this->expectException(\Exception
::class);
105 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
107 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
109 chmod($updatesFile, 0444);
111 @UpdaterUtils
::write_updates_file($updatesFile, array('test'));
112 } catch (Exception
$e) {
113 unlink($updatesFile);
119 * Test the update() method, with no update to run.
120 * 1. Everything already run.
121 * 2. User is logged out.
123 public function testNoUpdates()
126 'updateMethodDummy1',
127 'updateMethodDummy2',
128 'updateMethodDummy3',
129 'updateMethodException',
131 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
132 $this->assertEquals(array(), $updater->update());
134 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
135 $this->assertEquals(array(), $updater->update());
139 * Test the update() method, with all updates to run (except the failing one).
141 public function testUpdatesFirstTime()
143 $updates = array('updateMethodException',);
144 $expectedUpdates = array(
145 'updateMethodDummy1',
146 'updateMethodDummy2',
147 'updateMethodDummy3',
149 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
150 $this->assertEquals($expectedUpdates, $updater->update());
154 * Test the update() method, only one update to run.
156 public function testOneUpdate()
159 'updateMethodDummy1',
160 'updateMethodDummy3',
161 'updateMethodException',
163 $expectedUpdate = array('updateMethodDummy2');
165 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
166 $this->assertEquals($expectedUpdate, $updater->update());
170 * Test Update failed.
172 public function testUpdateFailed()
174 $this->expectException(\Exception
::class);
177 'updateMethodDummy1',
178 'updateMethodDummy2',
179 'updateMethodDummy3',
182 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
186 public function testUpdateMethodRelativeHomeLinkRename(): void
188 $this->updater
->setBasePath('/subfolder');
189 $this->conf
->set('general.header_link', '?');
191 $this->updater
->updateMethodRelativeHomeLink();
193 static::assertSame('/subfolder/', $this->conf
->get('general.header_link'));
196 public function testUpdateMethodRelativeHomeLinkDoNotRename(): void
198 $this->conf
->set('general.header_link', '~/my-blog');
200 $this->updater
->updateMethodRelativeHomeLink();
202 static::assertSame('~/my-blog', $this->conf
->get('general.header_link'));
205 public function testUpdateMethodMigrateExistingNotesUrl(): void
207 $this->updater
->updateMethodMigrateExistingNotesUrl();
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());