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