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