]>
Commit | Line | Data |
---|---|---|
510377d2 | 1 | <?php |
bcf056c9 | 2 | namespace Shaarli\Updater; |
f24896b2 | 3 | |
bcf056c9 | 4 | use Exception; |
1a8ac737 A |
5 | use Shaarli\Bookmark\BookmarkFileService; |
6 | use Shaarli\Bookmark\BookmarkServiceInterface; | |
3c66e564 | 7 | use Shaarli\Config\ConfigManager; |
1a8ac737 | 8 | use Shaarli\History; |
510377d2 | 9 | |
bcf056c9 V |
10 | require_once 'tests/updater/DummyUpdater.php'; |
11 | require_once 'tests/utils/ReferenceLinkDB.php'; | |
a0df0651 | 12 | require_once 'inc/rain.tpl.class.php'; |
510377d2 A |
13 | |
14 | /** | |
15 | * Class UpdaterTest. | |
bcf056c9 | 16 | * Runs unit tests against the updater class. |
510377d2 | 17 | */ |
bcf056c9 | 18 | class UpdaterTest extends \PHPUnit\Framework\TestCase |
510377d2 | 19 | { |
21979ff1 A |
20 | /** |
21 | * @var string Path to test datastore. | |
22 | */ | |
23 | protected static $testDatastore = 'sandbox/datastore.php'; | |
24 | ||
684e662a | 25 | /** |
b74b96bf | 26 | * @var string Config file path (without extension). |
684e662a | 27 | */ |
28f26524 | 28 | protected static $configFile = 'sandbox/config'; |
684e662a A |
29 | |
30 | /** | |
31 | * @var ConfigManager | |
32 | */ | |
33 | protected $conf; | |
34 | ||
1a8ac737 A |
35 | /** @var BookmarkServiceInterface */ |
36 | protected $bookmarkService; | |
37 | ||
38 | /** @var Updater */ | |
39 | protected $updater; | |
40 | ||
510377d2 A |
41 | /** |
42 | * Executed before each test. | |
43 | */ | |
44 | public function setUp() | |
45 | { | |
28f26524 | 46 | copy('tests/utils/config/configJson.json.php', self::$configFile .'.json.php'); |
278d9ee2 | 47 | $this->conf = new ConfigManager(self::$configFile); |
1a8ac737 A |
48 | $this->bookmarkService = new BookmarkFileService($this->conf, $this->createMock(History::class), true); |
49 | $this->updater = new Updater([], $this->bookmarkService, $this->conf, true); | |
510377d2 A |
50 | } |
51 | ||
52 | /** | |
e26e2060 | 53 | * Test UpdaterUtils::read_updates_file with an empty/missing file. |
510377d2 A |
54 | */ |
55 | public function testReadEmptyUpdatesFile() | |
56 | { | |
e26e2060 | 57 | $this->assertEquals(array(), UpdaterUtils::read_updates_file('')); |
894a3c4b | 58 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 | 59 | touch($updatesFile); |
e26e2060 | 60 | $this->assertEquals(array(), UpdaterUtils::read_updates_file($updatesFile)); |
da10377b | 61 | unlink($updatesFile); |
510377d2 A |
62 | } |
63 | ||
64 | /** | |
65 | * Test read/write updates file. | |
66 | */ | |
67 | public function testReadWriteUpdatesFile() | |
68 | { | |
894a3c4b | 69 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
70 | $updatesMethods = array('m1', 'm2', 'm3'); |
71 | ||
e26e2060 A |
72 | UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); |
73 | $readMethods = UpdaterUtils::read_updates_file($updatesFile); | |
510377d2 A |
74 | $this->assertEquals($readMethods, $updatesMethods); |
75 | ||
76 | // Update | |
77 | $updatesMethods[] = 'm4'; | |
e26e2060 A |
78 | UpdaterUtils::write_updates_file($updatesFile, $updatesMethods); |
79 | $readMethods = UpdaterUtils::read_updates_file($updatesFile); | |
510377d2 | 80 | $this->assertEquals($readMethods, $updatesMethods); |
da10377b | 81 | unlink($updatesFile); |
510377d2 A |
82 | } |
83 | ||
84 | /** | |
e26e2060 | 85 | * Test errors in UpdaterUtils::write_updates_file(): empty updates file. |
510377d2 A |
86 | * |
87 | * @expectedException Exception | |
88 | * @expectedExceptionMessageRegExp /Updates file path is not set(.*)/ | |
89 | */ | |
90 | public function testWriteEmptyUpdatesFile() | |
91 | { | |
e26e2060 | 92 | UpdaterUtils::write_updates_file('', array('test')); |
510377d2 A |
93 | } |
94 | ||
95 | /** | |
e26e2060 | 96 | * Test errors in UpdaterUtils::write_updates_file(): not writable updates file. |
510377d2 A |
97 | * |
98 | * @expectedException Exception | |
99 | * @expectedExceptionMessageRegExp /Unable to write(.*)/ | |
100 | */ | |
101 | public function testWriteUpdatesFileNotWritable() | |
102 | { | |
894a3c4b | 103 | $updatesFile = $this->conf->get('resource.data_dir') . '/updates.txt'; |
510377d2 A |
104 | touch($updatesFile); |
105 | chmod($updatesFile, 0444); | |
da10377b | 106 | try { |
e26e2060 | 107 | @UpdaterUtils::write_updates_file($updatesFile, array('test')); |
da10377b A |
108 | } catch (Exception $e) { |
109 | unlink($updatesFile); | |
110 | throw $e; | |
111 | } | |
510377d2 A |
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 | ); | |
278d9ee2 | 127 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
128 | $this->assertEquals(array(), $updater->update()); |
129 | ||
278d9ee2 | 130 | $updater = new DummyUpdater(array(), array(), $this->conf, false); |
510377d2 A |
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 | ); | |
278d9ee2 | 145 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
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 | ||
278d9ee2 | 161 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
162 | $this->assertEquals($expectedUpdate, $updater->update()); |
163 | } | |
164 | ||
165 | /** | |
166 | * Test Update failed. | |
167 | * | |
bcf056c9 | 168 | * @expectedException \Exception |
510377d2 A |
169 | */ |
170 | public function testUpdateFailed() | |
171 | { | |
172 | $updates = array( | |
173 | 'updateMethodDummy1', | |
174 | 'updateMethodDummy2', | |
175 | 'updateMethodDummy3', | |
176 | ); | |
177 | ||
278d9ee2 | 178 | $updater = new DummyUpdater($updates, array(), $this->conf, true); |
510377d2 A |
179 | $updater->update(); |
180 | } | |
1a8ac737 A |
181 | |
182 | public function testUpdateMethodRelativeHomeLinkRename(): void | |
183 | { | |
184 | $this->conf->set('general.header_link', '?'); | |
185 | $this->updater->updateMethodRelativeHomeLink(); | |
186 | ||
187 | static::assertSame(); | |
188 | } | |
510377d2 | 189 | } |