3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2014-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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
37 namespace Galette\Core\test\units
;
39 use PHPUnit\Framework\TestCase
;
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
53 class Install
extends TestCase
55 private \Galette\Core\Install
$install;
62 public function setUp(): void
64 setlocale(LC_ALL
, 'en_US');
65 $this->install
= new \Galette\Core\
Install();
73 public function tearDown(): void
75 if (TYPE_DB
=== 'mysql') {
76 $zdb = new \Galette\Core\
Db();
77 $this->assertSame([], $zdb->getWarnings());
86 public function testConstructor()
88 $install = new \Galette\Core\
Install();
90 $step = $install->isCheckStep();
91 $this->assertTrue($step);
93 $mode = $install->getMode();
94 $this->assertNull($mode);
96 $is_install = $install->isInstall();
97 $this->assertFalse($is_install);
99 $is_upgrade = $install->isUpgrade();
100 $this->assertFalse($is_upgrade);
102 $connected = $install->isDbConnected();
103 $this->assertFalse($connected);
105 $title = $install->getStepTitle();
106 $this->assertSame('Checks', $title);
110 * Tests update scripts list
114 public function testGetUpgradeScripts()
116 $update_scripts = \Galette\Core\Install
::getUpdateScripts(
117 GALETTE_BASE_PATH
. '/install',
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'
143 $this->assertSame($knowns, $update_scripts);
145 $update_scripts = \Galette\Core\Install
::getUpdateScripts(
146 GALETTE_BASE_PATH
. '/install',
151 //if we're from 0.7.0, there are 4 less update scripts
152 $this->assertCount(count($knowns) - 4, $update_scripts);
154 $update_scripts = \Galette\Core\Install
::getUpdateScripts(
155 GALETTE_BASE_PATH
. '/install'
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));
162 $this->install
->setMode(\Galette\Core\Install
::UPDATE
);
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'
170 $this->assertSame($knowns, $update_scripts);
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'
178 $this->assertSame(['current' => \Galette\Core\Db
::PGSQL
. '.sql'], $update_scripts);
186 public function testTypeStep()
188 $this->install
->atTypeStep();
190 $step = $this->install
->isTypeStep();
191 $this->assertTrue($step);
193 $title = $this->install
->getStepTitle();
194 $this->assertSame('Installation mode', $title);
198 * Test DB installation step
202 public function testInstallDbStep()
204 $this->install
->setMode(\Galette\Core\Install
::INSTALL
);
205 $this->install
->atDbStep();
207 $is_install = $this->install
->isInstall();
208 $is_upgrade = $this->install
->isUpgrade();
210 $this->assertTrue($is_install);
211 $this->assertFalse($is_upgrade);
213 $title = $this->install
->getStepTitle();
214 $this->assertSame('Database', $title);
216 $this->install
->atPreviousStep();
217 $step = $this->install
->isTypeStep();
218 $this->assertTrue($step);
222 * Test DB upgrade step
226 public function testUpgradeDbStep()
228 $this->install
->setMode(\Galette\Core\Install
::UPDATE
);
229 $this->install
->atDbStep();
231 $is_install = $this->install
->isInstall();
232 $is_upgrade = $this->install
->isUpgrade();
234 $this->assertFalse($is_install);
235 $this->assertTrue($is_upgrade);
237 $title = $this->install
->getStepTitle();
238 $this->assertSame('Database', $title);
240 $this->install
->atPreviousStep();
241 $step = $this->install
->isTypeStep();
243 $this->assertTrue($step);
251 public function testUnknownMode()
253 $this->expectException(\UnexpectedValueException
::class);
254 $this->expectExceptionMessage('Unknown mode "nonsense"');
255 $this->install
->setMode('nonsense');
263 public function testSetDbType()
266 \Galette\Core\Db
::MYSQL
,
267 \Galette\Core\Db
::PGSQL
270 foreach ($types as $t) {
273 $this->install
->setDbType(\Galette\Core\Db
::MYSQL
, $errors);
274 $type = $this->install
->getDbType();
276 $this->assertSame(\Galette\Core\Db
::MYSQL
, $type);
277 $this->assertCount(0, $errors);
281 $this->install
->setDbType('nonsense', $errors);
283 $this->assertSame(['Database type unknown'], $errors);
285 $post_check = $this->install
->postCheckDb();
286 $this->assertFalse($post_check);
290 * Test Db chack step (same for install and upgrade)
294 public function testDbCheckStep()
297 $this->install
->setDbType(TYPE_DB
, $errors);
298 $this->install
->setDsn(
305 $this->install
->setTablesPrefix(
308 $this->install
->atDbCheckStep();
310 $step = $this->install
->isDbCheckStep();
311 $this->assertTrue($step);
313 $title = $this->install
->getStepTitle();
314 $this->assertSame('Database access and permissions', $title);
316 $connected = $this->install
->testDbConnexion();
317 $this->assertTrue($connected);
319 $host = $this->install
->getDbHost();
320 $this->assertSame(HOST_DB
, $host);
322 $port = $this->install
->getDbPort();
323 $this->assertSame(PORT_DB
, $port);
325 $name = $this->install
->getDbName();
326 $this->assertSame(NAME_DB
, $name);
328 $user = $this->install
->getDbUser();
329 $this->assertSame(USER_DB
, $user);
331 $prefix = $this->install
->getTablesPrefix();
332 $this->assertSame(PREFIX_DB
, $prefix);
334 $pass = $this->install
->getDbPass();
335 $this->assertSame(PWD_DB
, $pass);
337 $post_check = $this->install
->postCheckDb();
338 $this->assertTrue($post_check);
340 $this->install
->atPreviousStep();
341 $step = $this->install
->isDbStep();
342 $this->assertTrue($step);
346 * Test db install step
350 public function testDbInstallStep()
352 $this->install
->setDbType(TYPE_DB
, $errors);
353 $this->install
->setDsn(
360 $this->install
->setTablesPrefix(
364 $this->install
->atDbInstallStep();
366 $step = $this->install
->isDbinstallStep();
367 $this->assertTrue($step);
369 $title = $this->install
->getStepTitle();
370 $this->assertSame('Tables Creation', $title);
372 $post_check = $this->install
->postCheckDb();
373 $this->assertTrue($post_check);
375 $this->install
->atPreviousStep();
376 $step = $this->install
->isDbCheckStep();
377 $this->assertTrue($step);
385 public function testAdminStep()
387 $this->install
->atAdminStep();
389 $step = $this->install
->isAdminStep();
390 $this->assertTrue($step);
392 $title = $this->install
->getStepTitle();
393 $this->assertSame('Admin parameters', $title);
395 $post_check = $this->install
->postCheckDb();
396 $this->assertTrue($post_check);
398 $this->install
->atPreviousStep();
399 //db install cannot be run twice, step is still Admin
400 $step = $this->install
->isAdminStep();
401 $this->assertTrue($step);
405 * Test galette initialization
409 public function testInitStep()
411 $this->install
->atGaletteInitStep();
413 $step = $this->install
->isGaletteInitStep();
414 $this->assertTrue($step);
416 $title = $this->install
->getStepTitle();
417 $this->assertSame('Galette initialization', $title);
419 $post_check = $this->install
->postCheckDb();
420 $this->assertTrue($post_check);