]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/updater/UpdaterTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / updater / UpdaterTest.php
CommitLineData
510377d2 1<?php
bcf056c9 2namespace Shaarli\Updater;
f24896b2 3
bcf056c9 4use Exception;
1a8ac737
A
5use Shaarli\Bookmark\BookmarkFileService;
6use Shaarli\Bookmark\BookmarkServiceInterface;
3c66e564 7use Shaarli\Config\ConfigManager;
1a8ac737 8use Shaarli\History;
a5a9cf23 9use Shaarli\TestCase;
510377d2 10
510377d2
A
11
12/**
13 * Class UpdaterTest.
bcf056c9 14 * Runs unit tests against the updater class.
510377d2 15 */
f7f08cee 16class UpdaterTest extends TestCase
510377d2 17{
21979ff1
A
18 /**
19 * @var string Path to test datastore.
20 */
21 protected static $testDatastore = 'sandbox/datastore.php';
22
684e662a 23 /**
b74b96bf 24 * @var string Config file path (without extension).
684e662a 25 */
28f26524 26 protected static $configFile = 'sandbox/config';
684e662a
A
27
28 /**
29 * @var ConfigManager
30 */
31 protected $conf;
32
1a8ac737
A
33 /** @var BookmarkServiceInterface */
34 protected $bookmarkService;
35
c4ad3d4f
A
36 /** @var \ReferenceLinkDB */
37 protected $refDB;
38
1a8ac737
A
39 /** @var Updater */
40 protected $updater;
41
510377d2
A
42 /**
43 * Executed before each test.
44 */
8f60e120 45 protected function setUp(): void
510377d2 46 {
c4ad3d4f
A
47 $this->refDB = new \ReferenceLinkDB();
48 $this->refDB->write(self::$testDatastore);
49
28f26524 50 copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php');
278d9ee2 51 $this->conf = new ConfigManager(self::$configFile);
1a8ac737
A
52 $this->bookmarkService = new BookmarkFileService($this->conf, $this->createMock(History::class), true);
53 $this->updater = new Updater([], $this->bookmarkService, $this->conf, true);
510377d2
A
54 }
55
56 /**
e26e2060 57 * Test UpdaterUtils::read_updates_file with an empty/missing file.
510377d2
A
58 */
59 public function testReadEmptyUpdatesFile()
60 {
e26e2060 61 $this->assertEquals(array(), UpdaterUtils::read_updates_file(''));
894a3c4b 62 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
510377d2 63 touch($updatesFile);
e26e2060 64 $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile));
da10377b 65 unlink($updatesFile);
510377d2
A
66 }
67
68 /**
69 * Test read/write updates file.
70 */
71 public function testReadWriteUpdatesFile()
72 {
894a3c4b 73 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
510377d2
A
74 $updatesMethods = array('m1', 'm2', 'm3');
75
e26e2060
A
76 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
77 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
510377d2
A
78 $this->assertEquals($readMethods, $updatesMethods);
79
80 // Update
81 $updatesMethods[] = 'm4';
e26e2060
A
82 UpdaterUtils::write_updates_file($updatesFile, $updatesMethods);
83 $readMethods = UpdaterUtils::read_updates_file($updatesFile);
510377d2 84 $this->assertEquals($readMethods, $updatesMethods);
da10377b 85 unlink($updatesFile);
510377d2
A
86 }
87
88 /**
e26e2060 89 * Test errors in UpdaterUtils::write_updates_file(): empty updates file.
510377d2
A
90 *
91 * @expectedException Exception
510377d2
A
92 */
93 public function testWriteEmptyUpdatesFile()
94 {
b1baca99
A
95 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
96
e26e2060 97 UpdaterUtils::write_updates_file('', array('test'));
510377d2
A
98 }
99
100 /**
e26e2060 101 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
510377d2
A
102 *
103 * @expectedException Exception
510377d2
A
104 */
105 public function testWriteUpdatesFileNotWritable()
106 {
b1baca99
A
107 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
108
894a3c4b 109 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
510377d2
A
110 touch($updatesFile);
111 chmod($updatesFile, 0444);
da10377b 112 try {
e26e2060 113 @UpdaterUtils::write_updates_file($updatesFile, array('test'));
da10377b
A
114 } catch (Exception $e) {
115 unlink($updatesFile);
116 throw $e;
117 }
510377d2
A
118 }
119
120 /**
121 * Test the update() method, with no update to run.
122 * 1. Everything already run.
123 * 2. User is logged out.
124 */
125 public function testNoUpdates()
126 {
127 $updates = array(
128 'updateMethodDummy1',
129 'updateMethodDummy2',
130 'updateMethodDummy3',
131 'updateMethodException',
132 );
278d9ee2 133 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
134 $this->assertEquals(array(), $updater->update());
135
278d9ee2 136 $updater = new DummyUpdater(array(), array(), $this->conf, false);
510377d2
A
137 $this->assertEquals(array(), $updater->update());
138 }
139
140 /**
141 * Test the update() method, with all updates to run (except the failing one).
142 */
143 public function testUpdatesFirstTime()
144 {
145 $updates = array('updateMethodException',);
146 $expectedUpdates = array(
147 'updateMethodDummy1',
148 'updateMethodDummy2',
149 'updateMethodDummy3',
150 );
278d9ee2 151 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
152 $this->assertEquals($expectedUpdates, $updater->update());
153 }
154
155 /**
156 * Test the update() method, only one update to run.
157 */
158 public function testOneUpdate()
159 {
160 $updates = array(
161 'updateMethodDummy1',
162 'updateMethodDummy3',
163 'updateMethodException',
164 );
165 $expectedUpdate = array('updateMethodDummy2');
166
278d9ee2 167 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
168 $this->assertEquals($expectedUpdate, $updater->update());
169 }
170
171 /**
172 * Test Update failed.
510377d2
A
173 */
174 public function testUpdateFailed()
175 {
b1baca99
A
176 $this->expectException(\Exception::class);
177
510377d2
A
178 $updates = array(
179 'updateMethodDummy1',
180 'updateMethodDummy2',
181 'updateMethodDummy3',
182 );
183
278d9ee2 184 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
185 $updater->update();
186 }
1a8ac737
A
187
188 public function testUpdateMethodRelativeHomeLinkRename(): void
189 {
c4ad3d4f 190 $this->updater->setBasePath('/subfolder');
1a8ac737 191 $this->conf->set('general.header_link', '?');
c4ad3d4f
A
192
193 $this->updater->updateMethodRelativeHomeLink();
194
195 static::assertSame('/subfolder/', $this->conf->get('general.header_link'));
196 }
197
198 public function testUpdateMethodRelativeHomeLinkDoNotRename(): void
199 {
c4ad3d4f
A
200 $this->conf->set('general.header_link', '~/my-blog');
201
1a8ac737
A
202 $this->updater->updateMethodRelativeHomeLink();
203
c4ad3d4f
A
204 static::assertSame('~/my-blog', $this->conf->get('general.header_link'));
205 }
206
207 public function testUpdateMethodMigrateExistingNotesUrl(): void
208 {
c4ad3d4f
A
209 $this->updater->updateMethodMigrateExistingNotesUrl();
210
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());
f7f08cee
A
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());
1a8ac737 222 }
510377d2 223}