2 namespace Shaarli\Updater
;
5 use Shaarli\Bookmark\BookmarkFileService
;
6 use Shaarli\Bookmark\BookmarkServiceInterface
;
7 use Shaarli\Config\ConfigManager
;
10 require_once 'tests/updater/DummyUpdater.php';
11 require_once 'tests/utils/ReferenceLinkDB.php';
12 require_once 'inc/rain.tpl.class.php';
16 * Runs unit tests against the updater class.
18 class UpdaterTest
extends \PHPUnit\Framework\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;
42 * Executed before each test.
44 public function setUp()
46 copy('tests/utils/config/configJson.json.php', self
::$configFile .'.json.php');
47 $this->conf
= new ConfigManager(self
::$configFile);
48 $this->bookmarkService
= new BookmarkFileService($this->conf
, $this->createMock(History
::class), true);
49 $this->updater
= new Updater([], $this->bookmarkService
, $this->conf
, true);
53 * Test UpdaterUtils::read_updates_file with an empty/missing file.
55 public function testReadEmptyUpdatesFile()
57 $this->assertEquals(array(), UpdaterUtils
::read_updates_file(''));
58 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
60 $this->assertEquals(array(), UpdaterUtils
::read_updates_file($updatesFile));
65 * Test read/write updates file.
67 public function testReadWriteUpdatesFile()
69 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
70 $updatesMethods = array('m1', 'm2', 'm3');
72 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
73 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
74 $this->assertEquals($readMethods, $updatesMethods);
77 $updatesMethods[] = 'm4';
78 UpdaterUtils
::write_updates_file($updatesFile, $updatesMethods);
79 $readMethods = UpdaterUtils
::read_updates_file($updatesFile);
80 $this->assertEquals($readMethods, $updatesMethods);
85 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
87 * @expectedException Exception
88 * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/
90 public function testWriteEmptyUpdatesFile()
92 UpdaterUtils
::write_updates_file('', array('test'));
96 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
98 * @expectedException Exception
99 * @expectedExceptionMessageRegExp /Unable to write(.*)/
101 public function testWriteUpdatesFileNotWritable()
103 $updatesFile = $this->conf
->get('resource.data_dir') . '/updates.txt';
105 chmod($updatesFile, 0444);
107 @UpdaterUtils
::write_updates_file($updatesFile, array('test'));
108 } catch (Exception
$e) {
109 unlink($updatesFile);
115 * Test the update() method, with no update to run.
116 * 1. Everything already run.
117 * 2. User is logged out.
119 public function testNoUpdates()
122 'updateMethodDummy1',
123 'updateMethodDummy2',
124 'updateMethodDummy3',
125 'updateMethodException',
127 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
128 $this->assertEquals(array(), $updater->update());
130 $updater = new DummyUpdater(array(), array(), $this->conf
, false);
131 $this->assertEquals(array(), $updater->update());
135 * Test the update() method, with all updates to run (except the failing one).
137 public function testUpdatesFirstTime()
139 $updates = array('updateMethodException',);
140 $expectedUpdates = array(
141 'updateMethodDummy1',
142 'updateMethodDummy2',
143 'updateMethodDummy3',
145 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
146 $this->assertEquals($expectedUpdates, $updater->update());
150 * Test the update() method, only one update to run.
152 public function testOneUpdate()
155 'updateMethodDummy1',
156 'updateMethodDummy3',
157 'updateMethodException',
159 $expectedUpdate = array('updateMethodDummy2');
161 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
162 $this->assertEquals($expectedUpdate, $updater->update());
166 * Test Update failed.
168 * @expectedException \Exception
170 public function testUpdateFailed()
173 'updateMethodDummy1',
174 'updateMethodDummy2',
175 'updateMethodDummy3',
178 $updater = new DummyUpdater($updates, array(), $this->conf
, true);
182 public function testUpdateMethodRelativeHomeLinkRename(): void
184 $this->conf
->set('general.header_link', '?');
185 $this->updater
->updateMethodRelativeHomeLink();
187 static::assertSame();