4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
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.
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.
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/>.
22 namespace Galette\Core\test\units
;
24 use PHPUnit\Framework\TestCase
;
29 * @author Johan Cwiklinski <johan@x-tnd.be>
31 class Install
extends TestCase
33 private \Galette\Core\Install
$install;
40 public function setUp(): void
42 setlocale(LC_ALL
, 'en_US');
43 $this->install
= new \Galette\Core\
Install();
51 public function tearDown(): void
53 if (TYPE_DB
=== 'mysql') {
54 $zdb = new \Galette\Core\
Db();
55 $this->assertSame([], $zdb->getWarnings());
64 public function testConstructor()
66 $install = new \Galette\Core\
Install();
68 $step = $install->isCheckStep();
69 $this->assertTrue($step);
71 $mode = $install->getMode();
72 $this->assertNull($mode);
74 $is_install = $install->isInstall();
75 $this->assertFalse($is_install);
77 $is_upgrade = $install->isUpgrade();
78 $this->assertFalse($is_upgrade);
80 $connected = $install->isDbConnected();
81 $this->assertFalse($connected);
83 $title = $install->getStepTitle();
84 $this->assertSame('Checks', $title);
88 * Tests update scripts list
92 public function testGetUpgradeScripts()
94 $update_scripts = \Galette\Core\Install
::getUpdateScripts(
95 GALETTE_BASE_PATH
. '/install',
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',
122 $this->assertSame($knowns, $update_scripts);
124 $update_scripts = \Galette\Core\Install
::getUpdateScripts(
125 GALETTE_BASE_PATH
. '/install',
130 //if we're from 0.7.0, there are 4 less update scripts
131 $this->assertCount(count($knowns) - 4, $update_scripts);
133 $update_scripts = \Galette\Core\Install
::getUpdateScripts(
134 GALETTE_BASE_PATH
. '/install'
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));
141 $this->install
->setMode(\Galette\Core\Install
::UPDATE
);
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'
149 $this->assertSame($knowns, $update_scripts);
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'
157 $this->assertSame(['current' => \Galette\Core\Db
::PGSQL
. '.sql'], $update_scripts);
165 public function testTypeStep()
167 $this->install
->atTypeStep();
169 $step = $this->install
->isTypeStep();
170 $this->assertTrue($step);
172 $title = $this->install
->getStepTitle();
173 $this->assertSame('Installation mode', $title);
177 * Test DB installation step
181 public function testInstallDbStep()
183 $this->install
->setMode(\Galette\Core\Install
::INSTALL
);
184 $this->install
->atDbStep();
186 $is_install = $this->install
->isInstall();
187 $is_upgrade = $this->install
->isUpgrade();
189 $this->assertTrue($is_install);
190 $this->assertFalse($is_upgrade);
192 $title = $this->install
->getStepTitle();
193 $this->assertSame('Database', $title);
195 $this->install
->atPreviousStep();
196 $step = $this->install
->isTypeStep();
197 $this->assertTrue($step);
201 * Test DB upgrade step
205 public function testUpgradeDbStep()
207 $this->install
->setMode(\Galette\Core\Install
::UPDATE
);
208 $this->install
->atDbStep();
210 $is_install = $this->install
->isInstall();
211 $is_upgrade = $this->install
->isUpgrade();
213 $this->assertFalse($is_install);
214 $this->assertTrue($is_upgrade);
216 $title = $this->install
->getStepTitle();
217 $this->assertSame('Database', $title);
219 $this->install
->atPreviousStep();
220 $step = $this->install
->isTypeStep();
222 $this->assertTrue($step);
230 public function testUnknownMode()
232 $this->expectException(\UnexpectedValueException
::class);
233 $this->expectExceptionMessage('Unknown mode "nonsense"');
234 $this->install
->setMode('nonsense');
242 public function testSetDbType()
245 \Galette\Core\Db
::MYSQL
,
246 \Galette\Core\Db
::PGSQL
249 foreach ($types as $t) {
252 $this->install
->setDbType(\Galette\Core\Db
::MYSQL
, $errors);
253 $type = $this->install
->getDbType();
255 $this->assertSame(\Galette\Core\Db
::MYSQL
, $type);
256 $this->assertCount(0, $errors);
260 $this->install
->setDbType('nonsense', $errors);
262 $this->assertSame(['Database type unknown'], $errors);
264 $post_check = $this->install
->postCheckDb();
265 $this->assertFalse($post_check);
269 * Test Db chack step (same for install and upgrade)
273 public function testDbCheckStep()
276 $this->install
->setDbType(TYPE_DB
, $errors);
277 $this->install
->setDsn(
284 $this->install
->setTablesPrefix(
287 $this->install
->atDbCheckStep();
289 $step = $this->install
->isDbCheckStep();
290 $this->assertTrue($step);
292 $title = $this->install
->getStepTitle();
293 $this->assertSame('Database access and permissions', $title);
295 $connected = $this->install
->testDbConnexion();
296 $this->assertTrue($connected);
298 $host = $this->install
->getDbHost();
299 $this->assertSame(HOST_DB
, $host);
301 $port = $this->install
->getDbPort();
302 $this->assertSame(PORT_DB
, $port);
304 $name = $this->install
->getDbName();
305 $this->assertSame(NAME_DB
, $name);
307 $user = $this->install
->getDbUser();
308 $this->assertSame(USER_DB
, $user);
310 $prefix = $this->install
->getTablesPrefix();
311 $this->assertSame(PREFIX_DB
, $prefix);
313 $pass = $this->install
->getDbPass();
314 $this->assertSame(PWD_DB
, $pass);
316 $post_check = $this->install
->postCheckDb();
317 $this->assertTrue($post_check);
319 $this->install
->atPreviousStep();
320 $step = $this->install
->isDbStep();
321 $this->assertTrue($step);
325 * Test db install step
329 public function testDbInstallStep()
332 $this->install
->setDbType(TYPE_DB
, $errors);
333 $this->install
->setDsn(
340 $this->install
->setTablesPrefix(
344 $this->install
->atDbInstallStep();
346 $step = $this->install
->isDbinstallStep();
347 $this->assertTrue($step);
349 $title = $this->install
->getStepTitle();
350 $this->assertSame('Tables Creation', $title);
352 $post_check = $this->install
->postCheckDb();
353 $this->assertTrue($post_check);
355 $this->install
->atPreviousStep();
356 $step = $this->install
->isDbCheckStep();
357 $this->assertTrue($step);
365 public function testAdminStep()
367 $this->install
->atAdminStep();
369 $step = $this->install
->isAdminStep();
370 $this->assertTrue($step);
372 $title = $this->install
->getStepTitle();
373 $this->assertSame('Admin parameters', $title);
375 $post_check = $this->install
->postCheckDb();
376 $this->assertTrue($post_check);
378 $this->install
->atPreviousStep();
379 //db install cannot be run twice, step is still Admin
380 $step = $this->install
->isAdminStep();
381 $this->assertTrue($step);
385 * Test galette initialization
389 public function testInitStep()
391 $this->install
->atGaletteInitStep();
393 $step = $this->install
->isGaletteInitStep();
394 $this->assertTrue($step);
396 $title = $this->install
->getStepTitle();
397 $this->assertSame('Galette initialization', $title);
399 $post_check = $this->install
->postCheckDb();
400 $this->assertTrue($post_check);