]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/updater/UpdaterTest.php
Merge pull request #1540 from ArthurHoaro/fix/metadata-regexes
[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 public function testWriteEmptyUpdatesFile()
92 {
f447edb7 93 $this->expectException(\Exception::class);
b1baca99
A
94 $this->expectExceptionMessageRegExp('/Updates file path is not set(.*)/');
95
e26e2060 96 UpdaterUtils::write_updates_file('', array('test'));
510377d2
A
97 }
98
99 /**
e26e2060 100 * Test errors in UpdaterUtils::write_updates_file(): not writable updates file.
510377d2
A
101 */
102 public function testWriteUpdatesFileNotWritable()
103 {
f447edb7 104 $this->expectException(\Exception::class);
b1baca99
A
105 $this->expectExceptionMessageRegExp('/Unable to write(.*)/');
106
894a3c4b 107 $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt';
510377d2
A
108 touch($updatesFile);
109 chmod($updatesFile, 0444);
da10377b 110 try {
e26e2060 111 @UpdaterUtils::write_updates_file($updatesFile, array('test'));
da10377b
A
112 } catch (Exception $e) {
113 unlink($updatesFile);
114 throw $e;
115 }
510377d2
A
116 }
117
118 /**
119 * Test the update() method, with no update to run.
120 * 1. Everything already run.
121 * 2. User is logged out.
122 */
123 public function testNoUpdates()
124 {
125 $updates = array(
126 'updateMethodDummy1',
127 'updateMethodDummy2',
128 'updateMethodDummy3',
129 'updateMethodException',
130 );
278d9ee2 131 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
132 $this->assertEquals(array(), $updater->update());
133
278d9ee2 134 $updater = new DummyUpdater(array(), array(), $this->conf, false);
510377d2
A
135 $this->assertEquals(array(), $updater->update());
136 }
137
138 /**
139 * Test the update() method, with all updates to run (except the failing one).
140 */
141 public function testUpdatesFirstTime()
142 {
143 $updates = array('updateMethodException',);
144 $expectedUpdates = array(
145 'updateMethodDummy1',
146 'updateMethodDummy2',
147 'updateMethodDummy3',
148 );
278d9ee2 149 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
150 $this->assertEquals($expectedUpdates, $updater->update());
151 }
152
153 /**
154 * Test the update() method, only one update to run.
155 */
156 public function testOneUpdate()
157 {
158 $updates = array(
159 'updateMethodDummy1',
160 'updateMethodDummy3',
161 'updateMethodException',
162 );
163 $expectedUpdate = array('updateMethodDummy2');
164
278d9ee2 165 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
166 $this->assertEquals($expectedUpdate, $updater->update());
167 }
168
169 /**
170 * Test Update failed.
510377d2
A
171 */
172 public function testUpdateFailed()
173 {
b1baca99
A
174 $this->expectException(\Exception::class);
175
510377d2
A
176 $updates = array(
177 'updateMethodDummy1',
178 'updateMethodDummy2',
179 'updateMethodDummy3',
180 );
181
278d9ee2 182 $updater = new DummyUpdater($updates, array(), $this->conf, true);
510377d2
A
183 $updater->update();
184 }
1a8ac737
A
185
186 public function testUpdateMethodRelativeHomeLinkRename(): void
187 {
c4ad3d4f 188 $this->updater->setBasePath('/subfolder');
1a8ac737 189 $this->conf->set('general.header_link', '?');
c4ad3d4f
A
190
191 $this->updater->updateMethodRelativeHomeLink();
192
193 static::assertSame('/subfolder/', $this->conf->get('general.header_link'));
194 }
195
196 public function testUpdateMethodRelativeHomeLinkDoNotRename(): void
197 {
c4ad3d4f
A
198 $this->conf->set('general.header_link', '~/my-blog');
199
1a8ac737
A
200 $this->updater->updateMethodRelativeHomeLink();
201
c4ad3d4f
A
202 static::assertSame('~/my-blog', $this->conf->get('general.header_link'));
203 }
204
205 public function testUpdateMethodMigrateExistingNotesUrl(): void
206 {
c4ad3d4f
A
207 $this->updater->updateMethodMigrateExistingNotesUrl();
208
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());
f7f08cee
A
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());
1a8ac737 220 }
510377d2 221}