]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Install.php
411afd07bc48e675b9648f5af075fd01644adf98
[galette.git] / tests / Galette / Core / tests / units / Install.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Galette\Core\test\units;
23
24 use PHPUnit\Framework\TestCase;
25
26 /**
27 * Install tests class
28 *
29 * @author Johan Cwiklinski <johan@x-tnd.be>
30 */
31 class Install extends TestCase
32 {
33 private \Galette\Core\Install $install;
34
35 /**
36 * Set up tests
37 *
38 * @return void
39 */
40 public function setUp(): void
41 {
42 setlocale(LC_ALL, 'en_US');
43 $this->install = new \Galette\Core\Install();
44 }
45
46 /**
47 * Tear down tests
48 *
49 * @return void
50 */
51 public function tearDown(): void
52 {
53 if (TYPE_DB === 'mysql') {
54 $zdb = new \Galette\Core\Db();
55 $this->assertSame([], $zdb->getWarnings());
56 }
57 }
58
59 /**
60 * Test constructor
61 *
62 * @return void
63 */
64 public function testConstructor()
65 {
66 $install = new \Galette\Core\Install();
67
68 $step = $install->isCheckStep();
69 $this->assertTrue($step);
70
71 $mode = $install->getMode();
72 $this->assertNull($mode);
73
74 $is_install = $install->isInstall();
75 $this->assertFalse($is_install);
76
77 $is_upgrade = $install->isUpgrade();
78 $this->assertFalse($is_upgrade);
79
80 $connected = $install->isDbConnected();
81 $this->assertFalse($connected);
82
83 $title = $install->getStepTitle();
84 $this->assertSame('Checks', $title);
85 }
86
87 /**
88 * Tests update scripts list
89 *
90 * @return void
91 */
92 public function testGetUpgradeScripts()
93 {
94 $update_scripts = \Galette\Core\Install::getUpdateScripts(
95 GALETTE_BASE_PATH . '/install',
96 'pgsql',
97 '0.6'
98 );
99
100 $knowns = array(
101 '0.61' => 'upgrade-to-0.61-pgsql.sql',
102 '0.62' => 'upgrade-to-0.62-pgsql.sql',
103 '0.63' => 'upgrade-to-0.63-pgsql.sql',
104 '0.70' => 'upgrade-to-0.70.php',
105 '0.71' => 'upgrade-to-0.71-pgsql.sql',
106 '0.74' => 'upgrade-to-0.74-pgsql.sql',
107 '0.75' => 'upgrade-to-0.75-pgsql.sql',
108 '0.76' => 'upgrade-to-0.76-pgsql.sql',
109 '0.8' => 'upgrade-to-0.8.php',
110 '0.81' => 'upgrade-to-0.81-pgsql.sql',
111 '0.82' => 'upgrade-to-0.82-pgsql.sql',
112 '0.91' => 'upgrade-to-0.91-pgsql.sql',
113 '0.92' => 'upgrade-to-0.92-pgsql.sql',
114 '0.93' => 'upgrade-to-0.93-pgsql.sql',
115 '0.931' => 'upgrade-to-0.931-pgsql.sql',
116 '0.94' => 'upgrade-to-0.94-pgsql.sql',
117 '0.95' => 'upgrade-to-0.95-pgsql.sql',
118 '0.96' => 'upgrade-to-0.96-pgsql.sql',
119 '1.10' => 'upgrade-to-1.10.php',
120 );
121
122 $this->assertSame($knowns, $update_scripts);
123
124 $update_scripts = \Galette\Core\Install::getUpdateScripts(
125 GALETTE_BASE_PATH . '/install',
126 'pgsql',
127 '0.7'
128 );
129
130 //if we're from 0.7.0, there are 4 less update scripts
131 $this->assertCount(count($knowns) - 4, $update_scripts);
132
133 $update_scripts = \Galette\Core\Install::getUpdateScripts(
134 GALETTE_BASE_PATH . '/install'
135 );
136
137 //without specifying database nor version, we got all update scripts
138 $all_knowns = ['0.60' => 'upgrade-to-0.60-pgsql.sql'] + $knowns;
139 $this->assertEquals(array_values($update_scripts), array_keys($all_knowns));
140
141 $this->install->setMode(\Galette\Core\Install::UPDATE);
142 $errors = array();
143 $this->install->setDbType(\Galette\Core\Db::PGSQL, $errors);
144 $this->install->setInstalledVersion('0.6');
145 $update_scripts = $this->install->getScripts(
146 GALETTE_BASE_PATH . '/install'
147 );
148
149 $this->assertSame($knowns, $update_scripts);
150
151 //for installation, only one script is present :)
152 $this->install->setMode(\Galette\Core\Install::INSTALL);
153 $update_scripts = $this->install->getScripts(
154 GALETTE_BASE_PATH . '/install'
155 );
156
157 $this->assertSame(['current' => \Galette\Core\Db::PGSQL . '.sql'], $update_scripts);
158 }
159
160 /**
161 * Test type step
162 *
163 * @return void
164 */
165 public function testTypeStep()
166 {
167 $this->install->atTypeStep();
168
169 $step = $this->install->isTypeStep();
170 $this->assertTrue($step);
171
172 $title = $this->install->getStepTitle();
173 $this->assertSame('Installation mode', $title);
174 }
175
176 /**
177 * Test DB installation step
178 *
179 * @return void
180 */
181 public function testInstallDbStep()
182 {
183 $this->install->setMode(\Galette\Core\Install::INSTALL);
184 $this->install->atDbStep();
185
186 $is_install = $this->install->isInstall();
187 $is_upgrade = $this->install->isUpgrade();
188
189 $this->assertTrue($is_install);
190 $this->assertFalse($is_upgrade);
191
192 $title = $this->install->getStepTitle();
193 $this->assertSame('Database', $title);
194
195 $this->install->atPreviousStep();
196 $step = $this->install->isTypeStep();
197 $this->assertTrue($step);
198 }
199
200 /**
201 * Test DB upgrade step
202 *
203 * @return void
204 */
205 public function testUpgradeDbStep()
206 {
207 $this->install->setMode(\Galette\Core\Install::UPDATE);
208 $this->install->atDbStep();
209
210 $is_install = $this->install->isInstall();
211 $is_upgrade = $this->install->isUpgrade();
212
213 $this->assertFalse($is_install);
214 $this->assertTrue($is_upgrade);
215
216 $title = $this->install->getStepTitle();
217 $this->assertSame('Database', $title);
218
219 $this->install->atPreviousStep();
220 $step = $this->install->isTypeStep();
221
222 $this->assertTrue($step);
223 }
224
225 /**
226 * Test unknown mode
227 *
228 * @return void
229 */
230 public function testUnknownMode()
231 {
232 $this->expectException(\UnexpectedValueException::class);
233 $this->expectExceptionMessage('Unknown mode "nonsense"');
234 $this->install->setMode('nonsense');
235 }
236
237 /**
238 * Test Db types
239 *
240 * @return void
241 */
242 public function testSetDbType()
243 {
244 $types = array(
245 \Galette\Core\Db::MYSQL,
246 \Galette\Core\Db::PGSQL
247 );
248
249 foreach ($types as $t) {
250 $errors = array();
251
252 $this->install->setDbType(\Galette\Core\Db::MYSQL, $errors);
253 $type = $this->install->getDbType();
254
255 $this->assertSame(\Galette\Core\Db::MYSQL, $type);
256 $this->assertCount(0, $errors);
257 }
258
259 $errors = array();
260 $this->install->setDbType('nonsense', $errors);
261
262 $this->assertSame(['Database type unknown'], $errors);
263
264 $post_check = $this->install->postCheckDb();
265 $this->assertFalse($post_check);
266 }
267
268 /**
269 * Test Db chack step (same for install and upgrade)
270 *
271 * @return void
272 */
273 public function testDbCheckStep()
274 {
275 $errors = array();
276 $this->install->setDbType(TYPE_DB, $errors);
277 $this->install->setDsn(
278 HOST_DB,
279 PORT_DB,
280 NAME_DB,
281 USER_DB,
282 PWD_DB
283 );
284 $this->install->setTablesPrefix(
285 PREFIX_DB
286 );
287 $this->install->atDbCheckStep();
288
289 $step = $this->install->isDbCheckStep();
290 $this->assertTrue($step);
291
292 $title = $this->install->getStepTitle();
293 $this->assertSame('Database access and permissions', $title);
294
295 $connected = $this->install->testDbConnexion();
296 $this->assertTrue($connected);
297
298 $host = $this->install->getDbHost();
299 $this->assertSame(HOST_DB, $host);
300
301 $port = $this->install->getDbPort();
302 $this->assertSame(PORT_DB, $port);
303
304 $name = $this->install->getDbName();
305 $this->assertSame(NAME_DB, $name);
306
307 $user = $this->install->getDbUser();
308 $this->assertSame(USER_DB, $user);
309
310 $prefix = $this->install->getTablesPrefix();
311 $this->assertSame(PREFIX_DB, $prefix);
312
313 $pass = $this->install->getDbPass();
314 $this->assertSame(PWD_DB, $pass);
315
316 $post_check = $this->install->postCheckDb();
317 $this->assertTrue($post_check);
318
319 $this->install->atPreviousStep();
320 $step = $this->install->isDbStep();
321 $this->assertTrue($step);
322 }
323
324 /**
325 * Test db install step
326 *
327 * @return void
328 */
329 public function testDbInstallStep()
330 {
331 $errors = array();
332 $this->install->setDbType(TYPE_DB, $errors);
333 $this->install->setDsn(
334 HOST_DB,
335 PORT_DB,
336 NAME_DB,
337 USER_DB,
338 PWD_DB
339 );
340 $this->install->setTablesPrefix(
341 PREFIX_DB
342 );
343
344 $this->install->atDbInstallStep();
345
346 $step = $this->install->isDbinstallStep();
347 $this->assertTrue($step);
348
349 $title = $this->install->getStepTitle();
350 $this->assertSame('Tables Creation', $title);
351
352 $post_check = $this->install->postCheckDb();
353 $this->assertTrue($post_check);
354
355 $this->install->atPreviousStep();
356 $step = $this->install->isDbCheckStep();
357 $this->assertTrue($step);
358 }
359
360 /**
361 * Test admin step
362 *
363 * @return void
364 */
365 public function testAdminStep()
366 {
367 $this->install->atAdminStep();
368
369 $step = $this->install->isAdminStep();
370 $this->assertTrue($step);
371
372 $title = $this->install->getStepTitle();
373 $this->assertSame('Admin parameters', $title);
374
375 $post_check = $this->install->postCheckDb();
376 $this->assertTrue($post_check);
377
378 $this->install->atPreviousStep();
379 //db install cannot be run twice, step is still Admin
380 $step = $this->install->isAdminStep();
381 $this->assertTrue($step);
382 }
383
384 /**
385 * Test galette initialization
386 *
387 * @return void
388 */
389 public function testInitStep()
390 {
391 $this->install->atGaletteInitStep();
392
393 $step = $this->install->isGaletteInitStep();
394 $this->assertTrue($step);
395
396 $title = $this->install->getStepTitle();
397 $this->assertSame('Galette initialization', $title);
398
399 $post_check = $this->install->postCheckDb();
400 $this->assertTrue($post_check);
401 }
402 }