Loheide-eu-mitarbeiterplatt.../tests/Feature/Downloads/RenameMigrationTest.php
Friederich Loheide f78cc96be7
All checks were successful
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 2m38s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 2m31s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 2m39s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 3m54s
CI / PHP 8.3 / mysql:8.0 (pull_request) Successful in 3m50s
CI / PHP 8.4 / mysql:8.0 (pull_request) Successful in 3m55s
CI / PHP 8.3 / mysql:8.4 (pull_request) Successful in 3m18s
CI / PHP 8.4 / mysql:8.4 (pull_request) Successful in 3m19s
CI / Build-Artefakte aktuell (pull_request) Successful in 55s
„Dokumente“ heißt jetzt „Downloads“
Der Name „Dokumente“ bleibt dem kommenden QM-Modul (QM-Dokumente und
-Formulare) vorbehalten.

- Plugin plugins-core/downloads (Inhaus\Plugins\Downloads), Adresse
  /downloads, Rechte downloads.*, Objekttyp download_folder,
  Block downloads.folder, Kachel downloads.favorites
- Migration für bestehende Installationen: Tabellen und Spalten,
  Rechte, Sichtbarkeiten, Menü, Startseiten-Anordnung, Blöcke in
  Seiten und Fassungen, Plugin-Zustand; mit Test
- Alte Links unter /dokumente leiten dauerhaft weiter

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 15:03:32 +00:00

101 lines
5.5 KiB
PHP

<?php
namespace Tests\Feature\Downloads;
use App\Models\Role;
use App\Models\User;
use App\Support\Navigation;
use App\Support\Settings;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Tests\TestCase;
/**
* Upgrade älterer Installationen: aus „Dokumente“ wird „Downloads“.
*/
class RenameMigrationTest extends TestCase
{
use RefreshDatabase;
public function test_old_installations_are_renamed_with_all_data(): void
{
// Stand vor der Umbenennung nachbauen.
foreach (['download_favorites', 'download_versions', 'downloads', 'download_folders'] as $table) {
Schema::drop($table);
}
Schema::create('document_folders', function (Blueprint $t) {
$t->id();
$t->foreignId('parent_id')->nullable()->constrained('document_folders');
$t->string('name');
$t->string('description', 500)->nullable();
$t->unsignedInteger('sort')->default(0);
$t->foreignId('created_by')->nullable();
$t->timestamps();
});
Schema::create('documents', function (Blueprint $t) {
$t->id();
$t->foreignId('folder_id')->constrained('document_folders');
$t->string('title');
$t->text('description')->nullable();
$t->unsignedBigInteger('current_version_id')->nullable();
$t->foreignId('created_by')->nullable();
$t->foreignId('updated_by')->nullable();
$t->unsignedInteger('downloads')->default(0);
$t->timestamps();
});
Schema::create('document_versions', function (Blueprint $t) {
$t->id();
$t->foreignId('document_id')->constrained('documents')->cascadeOnDelete();
$t->unsignedInteger('version');
$t->string('name');
$t->string('path');
$t->string('mime')->nullable();
$t->unsignedBigInteger('size')->default(0);
$t->string('note', 500)->nullable();
$t->foreignId('uploaded_by')->nullable();
$t->timestamp('created_at')->nullable();
});
Schema::create('document_favorites', function (Blueprint $t) {
$t->foreignId('user_id')->constrained()->cascadeOnDelete();
$t->foreignId('document_id')->constrained()->cascadeOnDelete();
$t->timestamp('created_at')->nullable();
$t->primary(['user_id', 'document_id']);
});
$user = User::factory()->create();
$role = Role::query()->create(['name' => 'Personal']);
$folder = DB::table('document_folders')->insertGetId(['name' => 'Personal']);
$doc = DB::table('documents')->insertGetId(['folder_id' => $folder, 'title' => 'Urlaubsantrag', 'downloads' => 42]);
DB::table('document_versions')->insert(['document_id' => $doc, 'version' => 1, 'name' => 'a.pdf', 'path' => 'documents/1/a.pdf']);
DB::table('document_favorites')->insert(['user_id' => $user->id, 'document_id' => $doc]);
DB::table('grants')->insert(['role_id' => $role->id, 'ability' => 'documents.edit', 'resource_type' => 'document_folder', 'resource_id' => $folder, 'include_children' => true]);
DB::table('audiences')->insert(['resource_type' => 'document_folder', 'resource_id' => $folder, 'rule_type' => 'department', 'rule_value' => 'Personal']);
app(Settings::class)->set(Navigation::MENU_SETTING, [['id' => 'a', 'type' => 'group', 'label' => 'Service', 'children' => [['id' => 'b', 'type' => 'module', 'module' => 'documents']]]]);
app(Settings::class)->set('dashboard.layout', ['documents.favorites' => ['sort' => 5, 'visible' => false]]);
DB::table('pages')->insert(['title' => 'Seite', 'slug' => 'seite', 'path' => 'seite', 'content' => json_encode(['blocks' => [['id' => 'x', 'type' => 'documents.folder', 'data' => ['folder' => (string) $folder]]]])]);
(require base_path('plugins-core/downloads/database/migrations/2026_09_29_000002_rename_documents_to_downloads.php'))->up();
$this->assertFalse(Schema::hasTable('documents'));
$this->assertSame(42, (int) DB::table('downloads')->where('id', $doc)->value('download_count'));
$this->assertSame($doc, (int) DB::table('download_versions')->value('download_id'));
$this->assertSame($doc, (int) DB::table('download_favorites')->value('download_id'));
$this->assertSame(['downloads.edit', 'download_folder'], array_values((array) DB::table('grants')->where('role_id', $role->id)->first(['ability', 'resource_type'])));
$this->assertSame('download_folder', DB::table('audiences')->value('resource_type'));
$this->assertSame('downloads', app(Settings::class)->get(Navigation::MENU_SETTING)[0]['children'][0]['module']);
$this->assertSame(['downloads.favorites'], array_keys(app(Settings::class)->get('dashboard.layout')));
$this->assertStringContainsString('"downloads.folder"', DB::table('pages')->value('content'));
// Zweiter Lauf ändert nichts.
(require base_path('plugins-core/downloads/database/migrations/2026_09_29_000002_rename_documents_to_downloads.php'))->up();
$this->assertSame(42, (int) DB::table('downloads')->where('id', $doc)->value('download_count'));
}
public function test_old_links_redirect(): void
{
$this->get('/dokumente/ordner/5?q=urlaub')->assertRedirect('/downloads/ordner/5?q=urlaub')->assertStatus(301);
$this->get('/dokumente')->assertRedirect('/downloads');
}
}