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 * @expectedException Exception
93 public function testWriteEmptyUpdatesFile()
95 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
97 UpdaterUtils
::write_updates_file('', array('test'));
101 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
103 * @expectedException Exception
105 public function testWriteUpdatesFileNotWritable()
107 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
109 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
111 chmod($updatesFile, 0444);
113 @UpdaterUtils
::write_updates_file($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());