Skip to content

Commit 3b12761

Browse files
committed
Add optionnal root_user configuration for database
1 parent 3a6515d commit 3b12761

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

docs/fr_FR/unit-tests/phpunit.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ $CONFIG = [
3636
'dbname' => 'jeedom', // Le bootstrap ajoutera '_test'
3737
'username' => 'root',
3838
'password' => 'root',
39+
'root_user' => 'my_root_user', // À ajouter si besoin, par défaut 'root'
40+
'root_password' => 'my_root_password', // À ajouter si besoin, par défaut pas de mot de passe
3941
],
4042
];
4143
```
@@ -231,6 +233,7 @@ Jeedom utilise un système de cache FileCache qui stocke les données dans `/tmp
231233
**Erreur de connexion à la base de données :**
232234
- Vérifiez que MySQL/MariaDB est démarré
233235
- Vérifiez les paramètres dans `core/config/common.config.php`
236+
- Vérifiez que l’utilisateur du SGBD a les droits de création/suppression de base
234237

235238
**Erreur de cache :**
236239
- Créez le répertoire de cache avec les bonnes permissions (voir section 4)

tests/bootstrap.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,22 @@
33
require_once dirname(__DIR__) . '/core/config/common.config.php';
44
global $CONFIG;
55

6-
$CONFIG['db']['dbname'] .= '_test';
6+
$rootUser = $CONFIG['db']['root_user'] ?? 'root';
7+
$rootPassword = $CONFIG['db']['root_password'] ?? null;
78

8-
$connection = new PDO(sprintf('mysql:host=%s;port=%u;charset=utf8', $CONFIG['db']['host'], $CONFIG['db']['port']), $CONFIG['db']['username'], $CONFIG['db']['password']);
9-
$connection->query('DROP DATABASE IF EXISTS ' . $CONFIG['db']['dbname']);
10-
$connection->query('CREATE DATABASE '.$CONFIG['db']['dbname']);
11-
$connection->query('GRANT ALL PRIVILEGES ON '.$CONFIG['db']['dbname'].'.* TO "'.$CONFIG['db']['username'].'"@"%" IDENTIFIED BY "'.$CONFIG['db']['password'].'"');
12-
$connection->query('FLUSH PRIVILEGES');
9+
$CONFIG['db']['dbname'] .= '_test';
10+
$CONFIG['db']['username'] .= '_test';
11+
$CONFIG['db']['password'] .= md5(random_bytes(10));
12+
13+
try {
14+
$connection = new PDO(sprintf('mysql:host=%s;port=%u;charset=utf8', $CONFIG['db']['host'], $CONFIG['db']['port']), $rootUser, $rootPassword);
15+
$connection->query('DROP DATABASE IF EXISTS ' . $CONFIG['db']['dbname']);
16+
$connection->query('CREATE DATABASE ' . $CONFIG['db']['dbname']);
17+
$connection->query('GRANT ALL PRIVILEGES ON ' . $CONFIG['db']['dbname'] . '.* TO "' . $CONFIG['db']['username'] . '"@"%" IDENTIFIED BY "' . $CONFIG['db']['password'] . '"');
18+
$connection->query('FLUSH PRIVILEGES');
19+
} catch (\Throwable $exception) {
20+
throw new RuntimeException(sprintf("Cannot create database: %s\nPlease check your MySQL server configuration\n%s", $exception->getMessage(), json_encode($CONFIG['db'])), 0, $exception);
21+
}
1322

1423
ob_start();
1524
require_once dirname(__DIR__).'/install/database.php';

0 commit comments

Comments
 (0)