presseportale/tests/Feature/CustomerPressReleaseCreatePhase7Test.php
Kevin Adametz d2ba22c0cf
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (push) Has been cancelled
create PM v0.5
2026-05-20 19:14:39 +02:00

179 lines
7 KiB
PHP

<?php
use App\Enums\PressReleaseStatus;
use App\Models\Category;
use App\Models\Company;
use App\Models\Contact;
use App\Models\PressRelease;
use App\Models\User;
use Database\Seeders\RolesAndPermissionsSeeder;
use Livewire\Volt\Volt as LivewireVolt;
use Tests\TestCase;
beforeEach(function (): void {
/** @var TestCase $this */
$this->seed(RolesAndPermissionsSeeder::class);
});
test('mount picks the alphabetically first contact of the default company', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create(['name' => 'Alpha GmbH']);
$customer->companies()->attach($company->id, ['role' => 'owner']);
$contactZ = Contact::factory()->for($company)->create([
'first_name' => 'Zacharias',
'last_name' => 'Zimmer',
'portal' => $company->portal->value,
]);
$contactA = Contact::factory()->for($company)->create([
'first_name' => 'Alfred',
'last_name' => 'Acker',
'portal' => $company->portal->value,
]);
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.create')
->assertSet('companyId', $company->id)
->assertSet('contactId', $contactA->id);
});
test('changing the company resets the contactId to the new company default', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$alphaCo = Company::factory()->presseecho()->create(['name' => 'Alpha']);
$betaCo = Company::factory()->presseecho()->create(['name' => 'Beta']);
$customer->companies()->attach($alphaCo->id, ['role' => 'owner']);
$customer->companies()->attach($betaCo->id, ['role' => 'owner']);
$alphaContact = Contact::factory()->for($alphaCo)->create(['portal' => $alphaCo->portal->value]);
$betaContact = Contact::factory()->for($betaCo)->create(['portal' => $betaCo->portal->value]);
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.create')
->set('companyId', $betaCo->id)
->assertSet('contactId', $betaContact->id)
->set('companyId', $alphaCo->id)
->assertSet('contactId', $alphaContact->id);
});
test('save with all required fields persists the press release and syncs contact', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create(['boilerplate' => 'Über uns…']);
$customer->companies()->attach($company->id, ['role' => 'owner']);
$contact = Contact::factory()->for($company)->create(['portal' => $company->portal->value]);
$category = Category::factory()->create();
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.create')
->set('title', 'Eine neue Eröffnung der Brauerei')
->set('subtitle', 'Subline mit ein wenig Kontext.')
->set('text', str_repeat('Inhaltlich relevanter Fließtext. ', 5))
->set('categoryId', $category->id)
->set('contactId', $contact->id)
->set('keywords', 'Brauerei, Eröffnung')
->set('useBoilerplateOverride', true)
->set('boilerplateOverride', 'Spezielle Boilerplate für diese PM.')
->call('save', 'draft')
->assertHasNoErrors();
$pr = PressRelease::query()->where('user_id', $customer->id)->latest('id')->firstOrFail();
expect($pr->title)->toBe('Eine neue Eröffnung der Brauerei');
expect($pr->subtitle)->toBe('Subline mit ein wenig Kontext.');
expect($pr->boilerplate_override)->toBe('Spezielle Boilerplate für diese PM.');
expect($pr->company_id)->toBe($company->id);
expect($pr->category_id)->toBe($category->id);
expect($pr->status)->toBe(PressReleaseStatus::Draft);
expect($pr->contacts)->toHaveCount(1);
expect($pr->contacts->first()->id)->toBe($contact->id);
});
test('save without a contact id fails validation', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create();
$customer->companies()->attach($company->id, ['role' => 'owner']);
$category = Category::factory()->create();
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.create')
->set('contactId', null)
->set('title', 'Titel mit genug Zeichen')
->set('text', str_repeat('x', 60))
->set('categoryId', $category->id)
->call('save', 'draft')
->assertHasErrors(['contactId']);
});
test('boilerplate override is null when toggle is off even if text is filled', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create();
$customer->companies()->attach($company->id, ['role' => 'owner']);
$contact = Contact::factory()->for($company)->create(['portal' => $company->portal->value]);
$category = Category::factory()->create();
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.create')
->set('title', 'Genug Zeichen Titel')
->set('text', str_repeat('x', 60))
->set('categoryId', $category->id)
->set('contactId', $contact->id)
->set('useBoilerplateOverride', false)
->set('boilerplateOverride', 'Wird nicht gespeichert.')
->call('save', 'draft')
->assertHasNoErrors();
$pr = PressRelease::query()->where('user_id', $customer->id)->latest('id')->firstOrFail();
expect($pr->boilerplate_override)->toBeNull();
});
test('addTag appends to keywords and removeTag drops it', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create();
$customer->companies()->attach($company->id, ['role' => 'owner']);
$this->actingAs($customer);
LivewireVolt::test('customer.press-releases.create')
->call('addTag', 'Brauerei')
->call('addTag', 'Tegernsee')
->call('addTag', 'Brauerei')
->assertSet('keywords', 'Brauerei, Tegernsee')
->call('removeTag', 'Brauerei')
->assertSet('keywords', 'Tegernsee');
});
test('addTag stops adding once the 5-tag limit is reached', function () {
/** @var TestCase $this */
$customer = User::factory()->create(['is_active' => true]);
$customer->assignRole('customer');
$company = Company::factory()->presseecho()->create();
$customer->companies()->attach($company->id, ['role' => 'owner']);
$this->actingAs($customer);
$component = LivewireVolt::test('customer.press-releases.create');
foreach (['A', 'B', 'C', 'D', 'E'] as $tag) {
$component->call('addTag', $tag);
}
$component->call('addTag', 'F'); // Soft-cap, kein Fehler
$component->assertSet('keywords', 'A, B, C, D, E');
});