presseportale/tests/Feature/MigrateLegacyAgencyNamesTest.php
Kevin Adametz 402fa4a38b legacy:migrate-agency-names: Agentur-Namen → users.agency_name
Übernimmt je Owner den Namen seiner Legacy-Agentur-Firma(en) ins neue
Profilfeld (A2 des Umbaus Agentur-Attribution → User-Profil). Regeln:
zuletzt aktualisierter Datensatz gewinnt bei Mehrfach-Namen (alle
Varianten im Report), bereits gesetzte Werte werden nie überschrieben,
Owner-lose Agentur-Firmen werden nur reportet. --dry-run + JSON-Report
nach storage/app/private/migration/; im Deploy-Runbook (Erst-Einrichtung)
nach dem Firmen-/User-Import eingereiht.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-08 09:25:04 +00:00

120 lines
4.1 KiB
PHP

<?php
use App\Enums\CompanyType;
use App\Enums\Portal;
use App\Models\Company;
use App\Models\User;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
Storage::fake('local');
});
test('copies the agency name of a single agency company to the owner profile', function () {
/** @var TestCase $this */
$user = User::factory()->create();
Company::factory()->create([
'portal' => Portal::Presseecho,
'type' => CompanyType::Agency,
'name' => 'connektar.de Presseverteiler',
'owner_user_id' => $user->id,
]);
$this->artisan('legacy:migrate-agency-names')->assertSuccessful();
expect($user->fresh()->agency_name)->toBe('connektar.de Presseverteiler');
});
test('with multiple differently named agency companies the most recently updated wins', function () {
/** @var TestCase $this */
$user = User::factory()->create();
Company::factory()->create([
'portal' => Portal::Presseecho,
'type' => CompanyType::Agency,
'name' => 'Alter Verteilername',
'owner_user_id' => $user->id,
'updated_at' => now()->subYears(2),
]);
Company::factory()->create([
'portal' => Portal::Businessportal24,
'type' => CompanyType::Agency,
'name' => 'Neuer Verteilername',
'owner_user_id' => $user->id,
'updated_at' => now()->subDay(),
]);
$this->artisan('legacy:migrate-agency-names')->assertSuccessful();
expect($user->fresh()->agency_name)->toBe('Neuer Verteilername');
$reportFile = collect(Storage::disk('local')->allFiles('migration'))->first();
$report = json_decode(Storage::disk('local')->get($reportFile), true);
expect($report['multiple_names'])->toHaveCount(1)
->and($report['multiple_names'][0]['variants'])
->toContain('Alter Verteilername', 'Neuer Verteilername');
});
test('never overwrites an already set agency name', function () {
/** @var TestCase $this */
$user = User::factory()->withAgency('Manuell gepflegter Name')->create();
Company::factory()->create([
'portal' => Portal::Presseecho,
'type' => CompanyType::Agency,
'name' => 'Legacy-Name',
'owner_user_id' => $user->id,
]);
$this->artisan('legacy:migrate-agency-names')->assertSuccessful();
expect($user->fresh()->agency_name)->toBe('Manuell gepflegter Name');
});
test('only reports agency companies without an owner', function () {
/** @var TestCase $this */
$orphan = Company::factory()->create([
'portal' => Portal::Presseecho,
'type' => CompanyType::Agency,
'name' => 'Verwaiste Agentur',
'owner_user_id' => null,
]);
$this->artisan('legacy:migrate-agency-names')->assertSuccessful();
$reportFile = collect(Storage::disk('local')->allFiles('migration'))->first();
$report = json_decode(Storage::disk('local')->get($reportFile), true);
expect($report['ownerless'])->toHaveCount(1)
->and($report['ownerless'][0]['company_id'])->toBe($orphan->id);
});
test('dry-run writes nothing but still produces a report', function () {
/** @var TestCase $this */
$user = User::factory()->create();
Company::factory()->create([
'portal' => Portal::Presseecho,
'type' => CompanyType::Agency,
'name' => 'connektar.de Presseverteiler',
'owner_user_id' => $user->id,
]);
$this->artisan('legacy:migrate-agency-names', ['--dry-run' => true])->assertSuccessful();
expect($user->fresh()->agency_name)->toBeNull()
->and(Storage::disk('local')->allFiles('migration'))->toHaveCount(1);
});
test('ignores companies that are not agencies', function () {
/** @var TestCase $this */
$user = User::factory()->create();
Company::factory()->create([
'portal' => Portal::Presseecho,
'type' => CompanyType::Company,
'name' => 'Echte Firma GmbH',
'owner_user_id' => $user->id,
]);
$this->artisan('legacy:migrate-agency-names')->assertSuccessful();
expect($user->fresh()->agency_name)->toBeNull();
});