['mailbox_password'], 'channels' => ['previous_export_token', 'export_snapshot', 'ical_analysis'], 'blocked_periods' => ['ical_properties', 'ical_raw', 'ical_interpretation', 'linked_booking_id', 'guest_name'], ]; /** Encrypted columns that are carried as plain values and encrypted again in the run. */ public const ENCRYPTED = ['tenants' => ['bank_account_holder', 'bank_iban', 'bank_bic', 'bank_name', 'tax_note']]; /** * @param array $draft model, variant, steps, settings of the tested channel and intake kind * @return array{tenant_id: int, channel_id: int, intake: string, tables: array>>, encrypted: array>} */ public static function build(Channel $channel, IntakeKind $intake, array $draft): array { $tenant = Tenant::query()->findOrFail($channel->tenant_id); $propertyIds = DB::table('properties')->where('tenant_id', $tenant->id)->pluck('id')->all(); $tables = []; foreach (self::TABLES as $table) { if (! Schema::hasTable($table)) { continue; } $query = DB::table($table); $query = match (true) { $table === 'tenants' => $query->where('id', $tenant->id), Schema::hasColumn($table, 'tenant_id') => $query->where('tenant_id', $tenant->id), Schema::hasColumn($table, 'property_id') => $query->whereIn('property_id', $propertyIds), default => null, }; if ($query === null) { continue; } $tables[$table] = $query->orderBy('id')->get()->map(fn (object $row): array => self::clean($table, (array) $row))->all(); } // The tested channel carries the draft (also unsaved) โ€“ and creates cases from portal mails in the run. foreach ($tables['channels'] ?? [] as $index => $row) { if ((int) $row['id'] === $channel->id) { $overrides = (array) json_decode((string) ($row['process_overrides'] ?? '[]'), true); $overrides[$intake->value] = [...array_filter((array) ($draft['settings'] ?? []), fn (mixed $value): bool => $value !== null && $value !== ''), 'model' => $draft['model'] ?? null, 'variant' => $draft['variant'] ?? 'standard', 'steps' => (array) ($draft['steps'] ?? []), 'templates' => (array) ($draft['templates'] ?? [])]; $tables['channels'][$index]['process_overrides'] = json_encode($overrides); $tables['channels'][$index]['case_intake'] = 'create'; // In the run the tested portal is active, so its (synthetic) booking mails are recognised. $tables['channels'][$index]['is_active'] = 1; $tables['channels'][$index]['status'] = 'active'; $settings = (array) json_decode((string) ($row['settings'] ?? '[]'), true); $settings['email_senders'] = filled($settings['email_senders'] ?? null) ? $settings['email_senders'] : 'portal.invalid'; $tables['channels'][$index]['settings'] = json_encode($settings); } } return ['tenant_id' => $tenant->id, 'channel_id' => $channel->id, 'intake' => $intake->value, 'tables' => $tables, 'encrypted' => self::ENCRYPTED]; } /** * @param array $row * @return array */ private static function clean(string $table, array $row): array { foreach (self::DROPPED[$table] ?? [] as $column) { if (array_key_exists($column, $row)) { $row[$column] = null; } } foreach (self::ENCRYPTED[$table] ?? [] as $column) { if (filled($row[$column] ?? null)) { try { $row[$column] = Crypt::decryptString((string) $row[$column]); } catch (Throwable) { $row[$column] = null; } } } if ($table === 'channels' && array_key_exists('export_token', $row)) { // The real export link is never part of a run: a random one of no use outside it. $row['export_token'] = bin2hex(random_bytes(20)); } if ($table === 'tenants') { // The run is always in test operation and writes only to its own test address. $row['operating_mode'] = 'test'; $row['test_recipient'] = 'tester@sandbox.invalid'; } return $row; } /** * Writes the snapshot into the run's own database (inside the tester process only). * * @param array{tables: array>>, encrypted: array>} $snapshot */ public static function seed(array $snapshot): void { if (config('database.default') !== 'sandbox') { throw SandboxViolation::record('database', 'Einspielen nur in die Testdatenbank.'); } Schema::disableForeignKeyConstraints(); foreach ($snapshot['tables'] as $table => $rows) { if (! Schema::hasTable($table)) { continue; } $columns = array_flip(Schema::getColumnListing($table)); foreach ($rows as $row) { foreach ($snapshot['encrypted'][$table] ?? [] as $column) { if (filled($row[$column] ?? null)) { $row[$column] = Crypt::encryptString((string) $row[$column]); } } DB::table($table)->insert(array_intersect_key($row, $columns)); } } Schema::enableForeignKeyConstraints(); } }