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