From 50d71c0b8e2a6844c5f4496031b563fd597d7710 Mon Sep 17 00:00:00 2001 From: Imani Manyara Date: Wed, 11 Nov 2026 13:30:56 -0500 Subject: [PATCH] Refuse colliding import keys and read sources from disks --- CHANGELOG.md | 21 +++ README.md | 1 + .../migrating-from-laranail-toolkit.md | 42 ++++++ .../migrating-from-spatie-laravel-settings.md | 2 +- docs/tools/commands.md | 2 +- docs/tools/import-export.md | 3 + docs/tools/importers.md | 30 +++- src/Console/ImportCommand.php | 46 +++++- src/Contracts/DiskImporter.php | 24 ++++ src/Importers/AkauntingSettingsImporter.php | 6 +- src/Importers/ImportedKeys.php | 34 +++++ src/Importers/SpatieSettingsImporter.php | 8 +- src/Importers/ToolkitJsonImporter.php | 75 ++++++++-- .../Feature/Database/ImporterSourcesTest.php | 134 ++++++++++++++++++ .../toolkit-non-colliding.export.json | 61 ++++++++ .../Importers/toolkit-non-colliding.json | 1 + 16 files changed, 462 insertions(+), 28 deletions(-) create mode 100644 docs/recipes/migrating-from-laranail-toolkit.md create mode 100644 src/Contracts/DiskImporter.php create mode 100644 tests/Feature/Database/ImporterSourcesTest.php create mode 100644 tests/Fixtures/Importers/toolkit-non-colliding.export.json create mode 100644 tests/Fixtures/Importers/toolkit-non-colliding.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 4d3afab..f4f5404 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,27 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- **Importers read from filesystem disks.** `laranail::settings.import --disk=` reads an + export file, or the `toolkit` importer's JSON file, from any configured disk, with the source as + the path on it; local paths work as before. In PHP, the new `Contracts\DiskImporter` adds + `readFromDisk(?string $disk, ?string $path, StoreName $store)`, which `ToolkitJsonImporter` + implements through the injected filesystem factory. With no source, the `toolkit` importer reads + toolkit's own file from `laranail.toolkit.settings`' disk and path when that config is loaded, + otherwise `laranail/settings.json` on `local`; toolkit itself is not needed. The command's + `source` argument is now optional. The REST import takes a document, so it has no disk option. + +### Fixed + +- **Importers no longer overwrite colliding keys in silence.** Source keys that normalise to the + same settings key (differing by case, `-`, `_` or spaces, or toolkit's nested and dotted spelling + of one path) made the later one win with no report. The `spatie`, `toolkit` and `akaunting` + importers now fail with `InvalidImport` naming every colliding group + (`[Mail-Host, mail_host] -> mail_host`) before anything is written, in a dry run and when + applied. Input without collisions imports exactly as before. `ImportedKeys::values()` applies the + same check for a custom importer. + ## [0.1.0] - 2026-11-11 The first public release. `Added` describes the package; `Changed`, `Removed`, `Fixed` and diff --git a/README.md b/README.md index a2bdef1..fe22b03 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,7 @@ The hosted docs live at mail_host. +``` + +Keep one key in each group in toolkit's file, then run the dry run again. + +## 3. Apply + +```bash +php artisan laranail::settings.import --from=toolkit --apply +``` + +Each value goes through the write pipeline, in one transaction. Then replace `Toolkit::settings()->get('mail.host')` with `settings()->get('mail.host')`. + +See [Importers](../tools/importers.md). + +--- + +[← Docs index](../../README.md#documentation) diff --git a/docs/recipes/migrating-from-spatie-laravel-settings.md b/docs/recipes/migrating-from-spatie-laravel-settings.md index 5e94319..b933e96 100644 --- a/docs/recipes/migrating-from-spatie-laravel-settings.md +++ b/docs/recipes/migrating-from-spatie-laravel-settings.md @@ -23,7 +23,7 @@ php artisan laranail::settings.import :settings --from=spatie # a dry php artisan laranail::settings.import :settings --from=spatie --apply --trusted ``` -`:settings` is spatie's table on the default connection (`:`). Each row becomes the key `.`; `--trusted` keeps spatie's locks. Values spatie stored encrypted arrive as spatie's ciphertext: define those keys `encrypted()` here and set them again. +`:settings` is spatie's table on the default connection (`:
`). Each row becomes the key `.`; `--trusted` keeps spatie's locks. Keys are lower-cased and `-` becomes `_`, so two rows such as `general.site_name` and `General.Site-Name` would land on one key: the import then fails, names both, and writes nothing. Rename one in spatie's table and import again. Values spatie stored encrypted arrive as spatie's ciphertext: define those keys `encrypted()` here and set them again. ## 3. Keep the typed classes diff --git a/docs/tools/commands.md b/docs/tools/commands.md index 75b99ac..b8f7a66 100644 --- a/docs/tools/commands.md +++ b/docs/tools/commands.md @@ -20,7 +20,7 @@ Nineteen Artisan commands, all named `laranail::settings.`. | `laranail::settings.clear-cache [--store=*]` | resets the cached settings of every store, or the named ones, for every tenant | | `laranail::settings.prune-changes` | marks open change requests past `approvals.expire_after` as expired and deletes decided ones older than `approvals.keep_days` | | `laranail::settings.export [path] [--store=] [--with-secrets] [--decrypt] [--confirm=]` | writes a store as a JSON document; secrets redacted unless asked for | -| `laranail::settings.import {source} [--store=] [--from=] [--apply] [--trusted]` | imports an export file, or another package's settings through an importer; a dry run without `--apply` | +| `laranail::settings.import {source?} [--store=] [--from=] [--disk=] [--apply] [--trusted]` | imports an export file, or another package's settings through an importer, from a local path or a filesystem disk; a dry run without `--apply` | | `laranail::settings.diff {path} [--store=]` | compares a store with an export file, by identity only; exits 1 when they differ | | `laranail::settings.reencrypt` | encrypts every stored secret again under the current key (values, history, change requests) | | `laranail::settings.rescope {from} {to}` | renames an owner alias in stored values, history and change requests | diff --git a/docs/tools/import-export.md b/docs/tools/import-export.md index 17e4cb9..dae46d7 100644 --- a/docs/tools/import-export.md +++ b/docs/tools/import-export.md @@ -47,8 +47,11 @@ php artisan laranail::settings.diff storage/settings.json # exits 1 whe php artisan laranail::settings.import storage/settings.json # a dry run php artisan laranail::settings.import storage/settings.json --apply php artisan laranail::settings.import storage/settings.json --apply --trusted +php artisan laranail::settings.import exports/settings.json --disk=s3 --apply # read the file from a disk ``` +`--disk=` reads the file from any configured filesystem disk, with the source as the path on that disk; without it the source is a local path, as before. The REST import takes the document in the request body, so it has no disk option. + The console command and `POST /stores/{store}/export` stream the document, reading 500 rows at a time, so a large store is never held in memory; the bytes are the ones `export()->toJson()` gives. In PHP, the `StreamExport` action (`handle($request, $stream, $actor)`) writes to any stream; `export()` still returns the whole `ExportDocument`. An import creates missing categories first, by slug, parents before children, then writes each setting through the normal write pipeline as the actor: validated and cast under the destination's definition, encrypted, with history and one audit entry per key. A setting that needs approval opens a change request instead (the report lists them). Everything runs in one transaction, so one failed write leaves the store as it was. Redacted values, owners the store does not accept and global values for a dedicated store are skipped, each with its reason in the report. diff --git a/docs/tools/importers.md b/docs/tools/importers.md index 8f8a736..56b7d2c 100644 --- a/docs/tools/importers.md +++ b/docs/tools/importers.md @@ -7,17 +7,43 @@ Three importers read another package's settings into an export document, which t | Name | Reads | Source | |---|---|---| | `spatie` | spatie/laravel-settings' table (`group`, `name`, `payload`, `locked`): each row becomes the global key `.`, its JSON payload decoded, its lock kept for a trusted import | `` or `:
` (default table `settings`) | -| `toolkit` | laranail/toolkit's JSON `SettingsStore` file, flattened (`{"mail": {"host": …}}` is `mail.host`); a list stays one value | the file's absolute path | +| `toolkit` | laranail/toolkit's JSON `SettingsStore` file, flattened (`{"mail": {"host": …}}` is `mail.host`); a list stays one value | the file's absolute path; with `--disk`, the path on that disk; with no source, toolkit's own file | | `akaunting` | akaunting/laravel-setting's database table (`key`, `value`); rows its extra columns scope to a model or company are skipped | `` or `:
` | Keys are lower-cased and `-` and spaces become `_`; a key that still is not valid fails the import with `InvalidImport` naming it. +Two or more source keys that land on the same key (`Mail-Host` and `mail_host`, or toolkit's nested `{"mail": {"host": …}}` beside a literal `"mail.host"`) fail the whole import before anything is written, in a dry run and with `--apply` alike. The `InvalidImport` names every colliding group: + +```text +toolkit: 2 groups of source keys collide after normalising; nothing was imported: [A, a] -> a; [b-c, b_c] -> b_c. +``` + +Rename or remove all but one key in each group at the source, then import again. + ```bash php artisan laranail::settings.import legacy:settings --from=spatie # a dry run php artisan laranail::settings.import legacy:settings --from=spatie --apply --trusted php artisan laranail::settings.import /var/www/storage/app/laranail/settings.json --from=toolkit --apply +php artisan laranail::settings.import backups/settings.json --from=toolkit --disk=s3 --apply +php artisan laranail::settings.import --from=toolkit --apply # toolkit's own disk and path +``` + +## Reading from a disk + +The `toolkit` importer reads its file through Laravel's filesystem as well as from a local path. `--disk=` reads the source as a path on that disk. With no source at all it reads toolkit's own file: the disk and path in `laranail.toolkit.settings` (`LARANAIL_SETTINGS_DISK`, `LARANAIL_SETTINGS_PATH`) when that config is loaded, otherwise toolkit's defaults, `laranail/settings.json` on `local`. Toolkit itself is not needed. + +In PHP, an importer that reads files implements `Simtabi\Laranail\Settings\Contracts\DiskImporter`: + +```php +$importer = app(Importers::class)->get('toolkit'); // a DiskImporter +$document = $importer->readFromDisk('s3', 'backups/settings.json', new StoreName('settings')); +$document = $importer->readFromDisk(null, null, new StoreName('settings')); // toolkit's own disk and path + +settings()->import($document); ``` +`spatie` and `akaunting` read a database, so they refuse `--disk`. A disk that is not configured, or a path that is not on it, fails with `InvalidImport`. + spatie/laravel-settings and this package both default to a table named `settings`. To read spatie's from the same database, point this package at other names (`tables.prefix` or `stores.settings.table`) before installing, or read spatie's table from another connection. Values spatie stored encrypted arrive as spatie's ciphertext: define those keys `encrypted()` here and set them again. @@ -48,7 +74,7 @@ final readonly class LegacyIniImporter implements Importer ], ``` -An importer only reads. It is built from the container, so it can take its dependencies in its constructor. +An importer only reads. It is built from the container, so it can take its dependencies in its constructor. To accept `--disk`, implement `DiskImporter` and its `readFromDisk(?string $disk, ?string $path, StoreName $store)`, reading through an injected `Illuminate\Contracts\Filesystem\Factory`. `ImportedKeys::values()` normalises keys and refuses collisions the way the shipped importers do. --- diff --git a/src/Console/ImportCommand.php b/src/Console/ImportCommand.php index c68f241..4e100a3 100644 --- a/src/Console/ImportCommand.php +++ b/src/Console/ImportCommand.php @@ -4,10 +4,13 @@ namespace Simtabi\Laranail\Settings\Console; +use InvalidArgumentException; use Illuminate\Filesystem\Filesystem; use Simtabi\Laranail\Settings\Data\Actor; use Simtabi\Laranail\Settings\Data\StoreName; +use Simtabi\Laranail\Settings\Contracts\Importer; use Simtabi\Laranail\Console\Tools\Commands\Command; +use Simtabi\Laranail\Settings\Contracts\DiskImporter; use Simtabi\Laranail\Settings\Exceptions\InvalidImport; use Simtabi\Laranail\Settings\Data\Transfer\ImportReport; use Simtabi\Laranail\Settings\Data\Transfer\ImportRequest; @@ -16,37 +19,44 @@ use Simtabi\Laranail\Settings\Data\Transfer\ExportDocument; use Simtabi\Laranail\Settings\Exceptions\SettingsException; use Simtabi\Laranail\Settings\Services\Stores\StoreRegistry; +use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; use Simtabi\Laranail\Console\Tools\Commands\Concerns\SupportsNamespacedNames; /** * Import an export file, or another package's settings with `--from=`. A dry run unless - * `--apply`; `--trusted` lets the file set locks and system categories. + * `--apply`; `--trusted` lets the file set locks and system categories. `--disk` reads the file + * from a filesystem disk, for an export file or an importer that reads files. */ final class ImportCommand extends Command { use SupportsNamespacedNames; protected $signature = 'laranail::settings.import - {source : An export file, or what the importer reads (a connection, connection:table or file)} + {source? : An export file, or what the importer reads (a connection, connection:table or file); a path on the disk with --disk} {--store= : The store (default: the default store)} {--from= : Read with an importer instead of an export file} + {--disk= : Read the file from this filesystem disk instead of a local path} {--apply : Write the changes (without it, a dry run)} {--trusted : Let the file set locks and system categories}'; protected $description = 'Import settings from an export file or another package (dry run by default)'; - public function handle(ImportStore $import, StoreRegistry $stores, Importers $importers, Filesystem $files): int + public function handle(ImportStore $import, StoreRegistry $stores, Importers $importers, Filesystem $files, FilesystemFactory $disks): int { $source = $this->argument('source'); $source = is_string($source) ? $source : ''; $from = $this->option('from'); + $disk = $this->option('disk'); + $disk = is_string($disk) && $disk !== '' ? $disk : null; $storeOption = $this->option('store'); try { $store = is_string($storeOption) && $storeOption !== '' ? $stores->get(new StoreName($storeOption)) : $stores->default(); if (is_string($from) && $from !== '') { - $document = $importers->get($from)->read($source, $store->name); + $document = $this->readWith($importers->get($from), $from, $source, $disk, $store->name); + } elseif ($disk !== null) { + $document = ExportDocument::fromJson($this->readDisk($disks, $disk, $source)); } else { if (! $files->isFile($source)) { throw new InvalidImport(sprintf('[%s] is not a file.', $source)); @@ -73,6 +83,34 @@ public function handle(ImportStore $import, StoreRegistry $stores, Importers $im return self::SUCCESS; } + /** @throws InvalidImport when `--disk` is given for an importer that does not read files */ + private function readWith(Importer $importer, string $name, string $source, ?string $disk, StoreName $store): ExportDocument + { + if ($disk === null) { + return $importer->read($source, $store); + } + + if (! $importer instanceof DiskImporter) { + throw new InvalidImport(sprintf('%s: this importer does not read from a disk; drop --disk.', $name)); + } + + return $importer->readFromDisk($disk, $source === '' ? null : $source, $store); + } + + /** @throws InvalidImport when the disk or the file on it cannot be read */ + private function readDisk(FilesystemFactory $disks, string $disk, string $path): string + { + try { + $filesystem = $disks->disk($disk); + } catch (InvalidArgumentException $exception) { + throw new InvalidImport(sprintf('Disk [%s] cannot be used (%s).', $disk, $exception->getMessage()), 0, $exception); + } + + $json = $path !== '' && $filesystem->exists($path) ? $filesystem->get($path) : null; + + return $json ?? throw new InvalidImport(sprintf('[%s] does not exist on disk [%s].', $path, $disk)); + } + private function report(ImportReport $report): void { $this->info(trans_choice('laranail/settings::settings.console.imported', count($report->categories), [ diff --git a/src/Contracts/DiskImporter.php b/src/Contracts/DiskImporter.php new file mode 100644 index 0000000..d334bff --- /dev/null +++ b/src/Contracts/DiskImporter.php @@ -0,0 +1,24 @@ + --disk=` calls it; an importer without it + * refuses `--disk`. + */ +interface DiskImporter extends Importer +{ + /** + * `$disk` and `$path` may be null for the importer's own default, if it has one. + * + * @throws InvalidImport when the disk or file cannot be read or holds something unusable + */ + public function readFromDisk(?string $disk, ?string $path, StoreName $store): ExportDocument; +} diff --git a/src/Importers/AkauntingSettingsImporter.php b/src/Importers/AkauntingSettingsImporter.php index f1e16a1..d1ea473 100644 --- a/src/Importers/AkauntingSettingsImporter.php +++ b/src/Importers/AkauntingSettingsImporter.php @@ -33,7 +33,7 @@ public function read(string $source, StoreName $store): ExportDocument throw new InvalidImport(sprintf('akaunting: table [%s] on connection [%s] cannot be read (%s).', $table, $connection ?: 'default', $exception->getMessage()), 0, $exception); } - $values = []; + $entries = []; foreach ($rows as $row) { $row = (array) $row; @@ -47,9 +47,9 @@ public function read(string $source, StoreName $store): ExportDocument continue; } - $values[ImportedKeys::key(is_string($row['key']) ? $row['key'] : '', 'akaunting')->value] = [$row['value'], false]; + $entries[] = [is_string($row['key']) ? $row['key'] : '', $row['value'], false]; } - return ImportedKeys::document($store, $values, 'akaunting'); + return ImportedKeys::document($store, ImportedKeys::values($entries, 'akaunting'), 'akaunting'); } } diff --git a/src/Importers/ImportedKeys.php b/src/Importers/ImportedKeys.php index 8d96435..c1fa830 100644 --- a/src/Importers/ImportedKeys.php +++ b/src/Importers/ImportedKeys.php @@ -29,6 +29,40 @@ public static function key(string $key, string $source): Key } } + /** + * Normalise every source key and refuse the whole source when two or more of them land on + * the same settings key, naming each colliding group, so nothing is overwritten in silence. + * The check runs before any document exists, so a dry run and an applied import fail alike. + * + * @param list $entries [source key, value, locked], in source order + * + * @return array key => [value, locked] + * + * @throws InvalidImport when a key cannot be made valid, or keys collide + */ + public static function values(array $entries, string $source): array + { + $values = []; + $origins = []; + + foreach ($entries as [$sourceKey, $value, $locked]) { + $key = self::key($sourceKey, $source)->value; + $values[$key] = [$value, $locked]; + $origins[$key][] = $sourceKey; + } + + $collisions = array_filter($origins, static fn (array $keys): bool => count($keys) > 1); + + if ($collisions !== []) { + ksort($collisions); + $groups = array_map(static fn (string $key, array $keys): string => sprintf('[%s] -> %s', implode(', ', $keys), $key), array_keys($collisions), $collisions); + + throw new InvalidImport(sprintf('%s: %d %s of source keys collide after normalising; nothing was imported: %s.', $source, count($collisions), count($collisions) === 1 ? 'group' : 'groups', implode('; ', $groups))); + } + + return $values; + } + /** * Global values as an export document. * diff --git a/src/Importers/SpatieSettingsImporter.php b/src/Importers/SpatieSettingsImporter.php index 5c684ba..04f9777 100644 --- a/src/Importers/SpatieSettingsImporter.php +++ b/src/Importers/SpatieSettingsImporter.php @@ -33,18 +33,16 @@ public function read(string $source, StoreName $store): ExportDocument throw new InvalidImport(sprintf('spatie: table [%s] on connection [%s] is not a spatie/laravel-settings table (%s).', $table, $connection ?: 'default', $exception->getMessage()), 0, $exception); } - $values = []; + $entries = []; foreach ($rows as $row) { $row = (array) $row; $group = is_string($row['group'] ?? null) ? $row['group'] : ''; $name = is_string($row['name'] ?? null) ? $row['name'] : ''; $payload = is_string($row['payload'] ?? null) ? json_decode($row['payload'], true) : null; - $key = ImportedKeys::key($group . '.' . $name, 'spatie'); - - $values[$key->value] = [$payload, (bool) ($row['locked'] ?? false)]; + $entries[] = [$group . '.' . $name, $payload, (bool) ($row['locked'] ?? false)]; } - return ImportedKeys::document($store, $values, 'spatie'); + return ImportedKeys::document($store, ImportedKeys::values($entries, 'spatie'), 'spatie'); } } diff --git a/src/Importers/ToolkitJsonImporter.php b/src/Importers/ToolkitJsonImporter.php index 49eaae8..5ecdfcc 100644 --- a/src/Importers/ToolkitJsonImporter.php +++ b/src/Importers/ToolkitJsonImporter.php @@ -4,47 +4,98 @@ namespace Simtabi\Laranail\Settings\Importers; +use InvalidArgumentException; use Illuminate\Filesystem\Filesystem; +use Illuminate\Contracts\Config\Repository; use Simtabi\Laranail\Settings\Data\StoreName; -use Simtabi\Laranail\Settings\Contracts\Importer; +use Simtabi\Laranail\Settings\Contracts\DiskImporter; use Simtabi\Laranail\Settings\Exceptions\InvalidImport; use Simtabi\Laranail\Settings\Data\Transfer\ExportDocument; +use Illuminate\Contracts\Filesystem\Factory as FilesystemFactory; /** * Imports laranail/toolkit's JSON settings file, flattening nested objects to dot keys. A list is - * one value. `$source` is the file's absolute path. + * one value. `$source` is the file's absolute path; an empty source reads toolkit's own file from + * its `laranail.toolkit.settings` disk and path. {@see readFromDisk()} reads any disk. */ -final readonly class ToolkitJsonImporter implements Importer +final readonly class ToolkitJsonImporter implements DiskImporter { + /** Toolkit's own defaults, used when its config is not loaded (the package need not be installed). */ + public const string DEFAULT_DISK = 'local'; + + public const string DEFAULT_PATH = 'laranail/settings.json'; + public function __construct( private Filesystem $files, + private FilesystemFactory $disks, + private Repository $config, ) {} public function read(string $source, StoreName $store): ExportDocument { + if ($source === '') { + return $this->readFromDisk(null, null, $store); + } + if (! $this->files->isFile($source)) { throw new InvalidImport(sprintf('toolkit: [%s] is not a file.', $source)); } - $data = json_decode($this->files->get($source), true); + return $this->document($this->files->get($source), $source, $store); + } + + public function readFromDisk(?string $disk, ?string $path, StoreName $store): ExportDocument + { + $disk ??= $this->configured('disk', self::DEFAULT_DISK); + $path ??= $this->configured('path', self::DEFAULT_PATH); + + try { + $filesystem = $this->disks->disk($disk); + } catch (InvalidArgumentException $exception) { + throw new InvalidImport(sprintf('toolkit: disk [%s] cannot be used (%s).', $disk, $exception->getMessage()), 0, $exception); + } + + $json = $filesystem->exists($path) ? $filesystem->get($path) : null; + + if ($json === null) { + throw new InvalidImport(sprintf('toolkit: [%s] does not exist on disk [%s].', $path, $disk)); + } + + return $this->document($json, $disk . ':' . $path, $store); + } + + private function document(string $json, string $label, StoreName $store): ExportDocument + { + $data = json_decode($json, true); if (! is_array($data)) { - throw new InvalidImport(sprintf('toolkit: [%s] is not a JSON object.', $source)); + throw new InvalidImport(sprintf('toolkit: [%s] is not a JSON object.', $label)); } - $values = []; + $entries = []; - foreach ($this->flatten($data) as $key => $value) { - $values[ImportedKeys::key($key, 'toolkit')->value] = [$value, false]; + foreach ($this->flatten($data) as [$key, $value]) { + $entries[] = [$key, $value, false]; } - return ImportedKeys::document($store, $values, 'toolkit'); + return ImportedKeys::document($store, ImportedKeys::values($entries, 'toolkit'), 'toolkit'); + } + + /** Toolkit's `laranail.toolkit.settings.`, read only if set, so toolkit is not needed. */ + private function configured(string $name, string $default): string + { + $value = $this->config->get('laranail.toolkit.settings.' . $name); + + return is_string($value) && $value !== '' ? $value : $default; } /** + * Every leaf as [dot path, value], keeping a nested and a dotted spelling of the same path + * apart so the collision check sees both. + * * @param array $data * - * @return array + * @return list */ private function flatten(array $data, string $prefix = ''): array { @@ -54,9 +105,9 @@ private function flatten(array $data, string $prefix = ''): array $path = $prefix === '' ? (string) $key : $prefix . '.' . $key; if (is_array($value) && $value !== [] && ! array_is_list($value)) { - $flat += $this->flatten($value, $path); + array_push($flat, ...$this->flatten($value, $path)); } else { - $flat[$path] = $value; + $flat[] = [$path, $value]; } } diff --git a/tests/Feature/Database/ImporterSourcesTest.php b/tests/Feature/Database/ImporterSourcesTest.php new file mode 100644 index 0000000..8e9b16a --- /dev/null +++ b/tests/Feature/Database/ImporterSourcesTest.php @@ -0,0 +1,134 @@ + $path, '--from' => 'toolkit', '--apply' => $apply]))->toBe(1) + ->and(Artisan::output())->toContain('collide')->toContain($group); + } + + expect(fn () => app(Importers::class)->get('toolkit')->read($path, new StoreName('settings')))->toThrow(InvalidImport::class, $group) + ->and(DB::table('settings')->count())->toBe(0); +})->with([ + 'case' => [['Mail' => ['Host' => 'a'], 'mail' => ['host' => 'b'], 'site' => 'kept'], '[Mail.Host, mail.host] -> mail.host'], + 'hyphen vs underscore' => [['mail-host' => 'a', 'mail_host' => 'b', 'site' => 'kept'], '[mail-host, mail_host] -> mail_host'], + 'case and hyphen' => [['Mail-Host' => 'a', 'mail_host' => 'b', 'site' => 'kept'], '[Mail-Host, mail_host] -> mail_host'], + 'space vs underscore' => [['debug mode' => true, 'debug_mode' => false], '[debug mode, debug_mode] -> debug_mode'], + 'nested vs dotted' => [['mail' => ['host' => 'a'], 'mail.host' => 'b'], '[mail.host, mail.host] -> mail.host'], +]); + +it('names every colliding group in one failure', function (): void { + $path = toolkitFile(['A' => 1, 'a' => 2, 'b-c' => 3, 'b_c' => 4, 'B_C' => 5, 'ok' => 6]); + + expect(fn () => app(Importers::class)->get('toolkit')->read($path, new StoreName('settings'))) + ->toThrow(InvalidImport::class, 'toolkit: 2 groups of source keys collide after normalising; nothing was imported: [A, a] -> a; [b-c, b_c, B_C] -> b_c.'); +}); + +it('refuses colliding spatie and akaunting keys before writing', function (): void { + Schema::create('spatie_settings', static function (Blueprint $table): void { + $table->id(); + $table->string('group'); + $table->string('name'); + $table->boolean('locked')->default(false); + $table->json('payload'); + }); + DB::table('spatie_settings')->insert([ + ['group' => 'general', 'name' => 'site_name', 'locked' => false, 'payload' => json_encode('a')], + ['group' => 'General', 'name' => 'Site-Name', 'locked' => false, 'payload' => json_encode('b')], + ]); + Schema::create('akaunting_settings', static function (Blueprint $table): void { + $table->id(); + $table->string('key'); + $table->text('value')->nullable(); + }); + DB::table('akaunting_settings')->insert([['key' => 'invoice-prefix', 'value' => 'a'], ['key' => 'Invoice_Prefix', 'value' => 'b']]); + + expect(Artisan::call('laranail::settings.import', ['source' => ':spatie_settings', '--from' => 'spatie', '--apply' => true]))->toBe(1) + ->and(Artisan::output())->toContain('[general.site_name, General.Site-Name] -> general.site_name') + ->and(Artisan::call('laranail::settings.import', ['source' => ':akaunting_settings', '--from' => 'akaunting', '--apply' => true]))->toBe(1) + ->and(Artisan::output())->toContain('[invoice-prefix, Invoice_Prefix] -> invoice_prefix') + ->and(DB::table('settings')->count())->toBe(0); +}); + +it('imports a non-colliding toolkit file exactly as before', function (): void { + $fixtures = __DIR__ . '/../../Fixtures/Importers/'; + $document = app(Importers::class)->get('toolkit')->read($fixtures . 'toolkit-non-colliding.json', new StoreName('settings')); + + expect($document->toJson())->toBe(file_get_contents($fixtures . 'toolkit-non-colliding.export.json')); +}); + +it('reads the toolkit file from a filesystem disk', function (): void { + Storage::fake('s3'); + Storage::disk('s3')->put('backups/settings.json', json_encode(['mail' => ['host' => 'smtp.remote']])); + + $importer = app(Importers::class)->get('toolkit'); + + expect($importer)->toBeInstanceOf(DiskImporter::class) + ->and($importer instanceof DiskImporter ? $importer->readFromDisk('s3', 'backups/settings.json', new StoreName('settings'))->settings[0]->value : null)->toBe('smtp.remote') + ->and(Artisan::call('laranail::settings.import', ['source' => 'backups/settings.json', '--from' => 'toolkit', '--disk' => 's3', '--apply' => true]))->toBe(0) + ->and(settings()->get('mail.host'))->toBe('smtp.remote') + ->and(Artisan::call('laranail::settings.import', ['source' => 'missing.json', '--from' => 'toolkit', '--disk' => 's3']))->toBe(1) + ->and(Artisan::output())->toContain('toolkit: [missing.json] does not exist on disk [s3].') + ->and(Artisan::call('laranail::settings.import', ['source' => 'x.json', '--from' => 'toolkit', '--disk' => 'no-such-disk']))->toBe(1) + ->and(Artisan::output())->toContain('no-such-disk') + ->and(Artisan::call('laranail::settings.import', ['source' => ':settings', '--from' => 'spatie', '--disk' => 's3']))->toBe(1) + ->and(Artisan::output())->toContain('does not read from a disk'); +}); + +it('defaults to toolkit\'s own disk and path when no source is given', function (): void { + Storage::fake('s3'); + Storage::disk('s3')->put('custom/toolkit.json', json_encode(['site' => ['name' => 'From config']])); + config()->set('laranail.toolkit.settings', ['disk' => 's3', 'path' => 'custom/toolkit.json']); + + expect(Artisan::call('laranail::settings.import', ['--from' => 'toolkit', '--apply' => true]))->toBe(0) + ->and(settings()->get('site.name'))->toBe('From config'); +}); + +it('falls back to toolkit\'s documented default disk and path when its config is absent', function (): void { + config()->set('laranail.toolkit'); + Storage::disk('local')->put('laranail/settings.json', json_encode(['site' => ['name' => 'Default']])); + + expect(Artisan::call('laranail::settings.import', ['--from' => 'toolkit', '--apply' => true]))->toBe(0) + ->and(settings()->get('site.name'))->toBe('Default'); +}); + +it('reads an export file from a disk too', function (): void { + Storage::fake('s3'); + settings()->set('site.name', 'Acme'); + Storage::disk('s3')->put('exports/settings.json', settings()->export()->toJson()); + settings()->forget('site.name'); + + expect(Artisan::call('laranail::settings.import', ['source' => 'exports/settings.json', '--disk' => 's3', '--apply' => true]))->toBe(0) + ->and(settings()->get('site.name'))->toBe('Acme'); +}); diff --git a/tests/Fixtures/Importers/toolkit-non-colliding.export.json b/tests/Fixtures/Importers/toolkit-non-colliding.export.json new file mode 100644 index 0000000..629d6b8 --- /dev/null +++ b/tests/Fixtures/Importers/toolkit-non-colliding.export.json @@ -0,0 +1,61 @@ +{ + "format": "laranail/settings", + "version": 1, + "store": "settings", + "mode": "plaintext", + "source": "toolkit", + "categories": [], + "settings": [ + { + "scope": "global", + "key": "debug_mode", + "locale": "", + "value": false + }, + { + "scope": "global", + "key": "empty", + "locale": "", + "value": [] + }, + { + "scope": "global", + "key": "mail.cc", + "locale": "", + "value": [ + "a@x.test", + "b@x.test" + ] + }, + { + "scope": "global", + "key": "mail.host", + "locale": "", + "value": "smtp.test" + }, + { + "scope": "global", + "key": "mail.port", + "locale": "", + "value": 587 + }, + { + "scope": "global", + "key": "ratio", + "locale": "", + "value": 1.5 + }, + { + "scope": "global", + "key": "site.active_flag", + "locale": "", + "value": true + }, + { + "scope": "global", + "key": "site.name", + "locale": "", + "value": "Acme" + } + ] +} diff --git a/tests/Fixtures/Importers/toolkit-non-colliding.json b/tests/Fixtures/Importers/toolkit-non-colliding.json new file mode 100644 index 0000000..0fd559e --- /dev/null +++ b/tests/Fixtures/Importers/toolkit-non-colliding.json @@ -0,0 +1 @@ +{"site":{"name":"Acme","Active-Flag":true},"mail":{"host":"smtp.test","cc":["a@x.test","b@x.test"],"port":587},"Debug Mode":false,"empty":[],"ratio":1.5}