2 namespace Shaarli\Updater
;
5 use Shaarli\Bookmark\BookmarkFileService
;
6 use Shaarli\Bookmark\BookmarkServiceInterface
;
7 use Shaarli\Config\ConfigManager
;
13 * Runs unit tests against the updater class.
15 class UpdaterTest
extends \PHPUnit\Framework\TestCase
18 * @var string Path to test datastore.
20 protected static $testDatastore = 'sandbox/datastore.php';
23 * @var string Config file path (without extension).
25 protected static $configFile = 'sandbox/config';
32 /** @var BookmarkServiceInterface */
33 protected $bookmarkService;
35 /** @var \ReferenceLinkDB */
42 * Executed before each test.
44 public function setUp()
46 $this->refDB
= new \
ReferenceLinkDB();
47 $this->refDB
->write(self
::$testDatastore);
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);
56 * Test UpdaterUtils::read_updates_file with an empty/missing file.
58 public function testReadEmptyUpdatesFile()
60 $this->assertEquals(array(), UpdaterUtils
::read_updates_file(''));
61 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
63 $this->assertEquals(array(), UpdaterUtils
::read_updates_file($updatesFile));
68 * Test read/write updates file.
70 public function testReadWriteUpdatesFile()
72 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
73 $updatesMethods = array('m1', 'm2', 'm3');
75 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
76 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
77 $this->assertEquals($readMethods, $updatesMethods);
80 $updatesMethods[] = 'm4';
81 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
82 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
83 $this->assertEquals($readMethods, $updatesMethods);
88 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
90 * @expectedException Exception
91 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
93 public function testWriteEmptyUpdatesFile()
95 UpdaterUtils
::write_updates_file('', array('test'));
99 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
101 * @expectedException Exception
102 * @expectedExceptionMessageRegExp /Unable to write(.*)/
104 public function testWriteUpdatesFileNotWritable()
106 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
108 chmod($updatesFile, 0444);
110 @UpdaterUtils
::write_updates_file($updatesFile, array('test'));
111 } catch (Exception
$e) {
112 unlink($updatesFile);
118 * Test the update() method, with no update to run.
119 * 1. Everything already run.
120 * 2. User is logged out.
122 public function testNoUpdates()
125 'updateMethodDummy1',
126 'updateMethodDummy2',
127 'updateMethodDummy3',
128 'updateMethodException',
130 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
131 $this->assertEquals(array(), $updater->update());
133 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
134 $this->assertEquals(array(), $updater->update());
138 * Test the update() method, with all updates to run (except the failing one).
140 public function testUpdatesFirstTime()
142 $updates = array('updateMethodException',);
143 $expectedUpdates = array(
144 'updateMethodDummy1',
145 'updateMethodDummy2',
146 'updateMethodDummy3',
148 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
149 $this->assertEquals($expectedUpdates, $updater->update());
153 * Test the update() method, only one update to run.
155 public function testOneUpdate()
158 'updateMethodDummy1',
159 'updateMethodDummy3',
160 'updateMethodException',
162 $expectedUpdate = array('updateMethodDummy2');
164 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
165 $this->assertEquals($expectedUpdate, $updater->update());
169 * Test Update failed.
171 * @expectedException \Exception
173 public function testUpdateFailed()
176 'updateMethodDummy1',
177 'updateMethodDummy2',
178 'updateMethodDummy3',
181 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
185 public function testUpdateMethodRelativeHomeLinkRename(): void
187 $this->updater
->setBasePath('/subfolder');
188 $this->conf
->set('general.header_link', '?');
190 $this->updater
->updateMethodRelativeHomeLink();
192 static::assertSame('/subfolder/', $this->conf
->get('general.header_link'));
195 public function testUpdateMethodRelativeHomeLinkDoNotRename(): void
197 $this->updater
->setBasePath('/subfolder');
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
->setBasePath('/subfolder');
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('/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());