Loheide-eu-mitarbeiterplatt.../tests/Feature/News/NewsWorkflowTest.php
Friederich Loheide bdd21a38cb
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 3m42s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m3s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 4m32s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m9s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 5m41s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m5s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 4m57s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 4m59s
CI / Build-Artefakte aktuell (pull_request) Failing after 1m35s
Beiträge mit dem Baukasten der Seiten, Galerie-Großansicht mit Blättern
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-26 19:43:05 +00:00

207 lines
10 KiB
PHP

<?php
namespace Tests\Feature\News;
use App\Models\Role;
use App\Models\User;
use App\Notifications\PortalNotification;
use App\Pagebuilder\Content;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Forms\Models\Form;
use Inhaus\Plugins\News\Models\NewsCategory;
use Inhaus\Plugins\News\Models\NewsPost;
use Tests\TestCase;
/**
* Redaktion im Portal: Die MAV schreibt in „MAV-Info“, die Redaktion gibt frei.
*/
class NewsWorkflowTest extends TestCase
{
use RefreshDatabase;
private NewsCategory $mavInfo;
private NewsCategory $company;
private User $author;
private User $editor;
protected function setUp(): void
{
parent::setUp();
Storage::fake('uploads');
$this->mavInfo = NewsCategory::factory()->create(['name' => 'MAV-Info', 'slug' => 'mav-info']);
$this->company = NewsCategory::factory()->create(['name' => 'Unternehmen', 'slug' => 'unternehmen']);
$writers = Role::query()->create(['name' => 'MAV']);
$writers->grants()->create(['ability' => 'news.post.create', 'resource_type' => 'news_category', 'resource_id' => $this->mavInfo->id]);
$this->author = User::factory()->withRoles([$writers])->create(['first_name' => 'Maria', 'last_name' => 'Autorin']);
$publishers = Role::query()->create(['name' => 'Redaktion']);
$publishers->grants()->create(['ability' => 'news.post.create', 'resource_type' => 'news_category', 'resource_id' => null]);
$publishers->grants()->create(['ability' => 'news.post.publish', 'resource_type' => 'news_category', 'resource_id' => $this->mavInfo->id]);
$this->editor = User::factory()->withRoles([$publishers])->create(['first_name' => 'Rita', 'last_name' => 'Redaktion']);
}
private function payload(array $overrides = []): array
{
return array_merge([
'title' => 'Mitarbeiterversammlung',
'teaser' => 'Einladung für alle.',
'body' => '<p>Am <strong>Freitag</strong> <script>alert(1)</script></p>',
'category_id' => $this->mavInfo->id,
'action' => 'draft',
], $overrides);
}
public function test_author_writes_submits_and_editor_approves(): void
{
Notification::fake();
$employee = User::factory()->create();
$this->actingAs($this->author)->get('/neuigkeiten')->assertInertia(fn (Assert $p) => $p->where('canWrite', true));
$this->get('/redaktion/neuigkeiten/neu')->assertOk()->assertInertia(fn (Assert $p) => $p
->component('news::Editor')
->where('categories', [['id' => $this->mavInfo->id, 'name' => 'MAV-Info', 'can_publish' => false]]));
$this->post('/redaktion/neuigkeiten', $this->payload(['cover' => UploadedFile::fake()->image('foto.jpg', 1600, 900)]))->assertRedirect();
$post = NewsPost::query()->sole();
$this->assertSame(NewsPost::DRAFT, $post->status);
$this->assertSame($this->author->id, $post->author_id);
$this->assertStringNotContainsString('script', $post->body_html);
$this->assertNotNull($post->cover_path);
// Veröffentlichen darf die MAV nicht selbst.
$this->put('/redaktion/neuigkeiten/'.$post->id, $this->payload(['action' => 'publish']))->assertSessionHasErrors('action');
$this->assertSame(NewsPost::DRAFT, $post->fresh()->status);
$this->put('/redaktion/neuigkeiten/'.$post->id, $this->payload(['action' => 'submit']))->assertSessionHas('status', fn ($s) => str_contains($s, 'Zur Freigabe eingereicht'));
$this->assertSame(NewsPost::PENDING, $post->fresh()->status);
Notification::assertSentTo($this->editor, PortalNotification::class, fn ($n) => str_contains($n->title, 'Mitarbeiterversammlung'));
Notification::assertNotSentTo($this->author, PortalNotification::class);
$this->actingAs($employee)->get('/neuigkeiten/'.$post->slug)->assertNotFound();
$this->actingAs($this->editor)->get('/redaktion')->assertInertia(fn (Assert $p) => $p
->where('sections.0.key', 'news')
->where('sections.0.props.review.0.title', 'Mitarbeiterversammlung'));
$this->post('/redaktion/neuigkeiten/'.$post->id.'/freigeben')->assertRedirect('/redaktion');
$post->refresh();
$this->assertTrue($post->isPublished());
$this->assertSame($this->editor->id, $post->reviewed_by);
Notification::assertSentTo($this->author, PortalNotification::class, fn ($n) => str_starts_with($n->title, 'Beitrag veröffentlicht'));
$this->actingAs($employee)->get('/neuigkeiten/'.$post->slug)->assertOk();
// Veröffentlichte Beiträge ändert die MAV nicht mehr ohne Freigabe.
$this->actingAs($this->author)->put('/redaktion/neuigkeiten/'.$post->id, $this->payload(['title' => 'Heimlich geändert']))->assertForbidden();
$this->get('/neuigkeiten/'.$post->slug)->assertInertia(fn (Assert $p) => $p->where('editUrl', route('news.editor.edit', $post)));
}
public function test_editor_returns_a_post_with_a_note(): void
{
Notification::fake();
$post = NewsPost::factory()->draft()->create(['category_id' => $this->mavInfo->id, 'author_id' => $this->author->id]);
$post->forceFill(['status' => NewsPost::PENDING, 'submitted_at' => now()])->save();
$this->actingAs($this->editor)->post('/redaktion/neuigkeiten/'.$post->id.'/zurueckgeben', ['note' => ''])->assertSessionHasErrors('note');
$this->post('/redaktion/neuigkeiten/'.$post->id.'/zurueckgeben', ['note' => 'Bitte Ort ergänzen.'])->assertRedirect('/redaktion');
$post->refresh();
$this->assertSame(NewsPost::DRAFT, $post->status);
$this->assertSame('Bitte Ort ergänzen.', $post->review_note);
Notification::assertSentTo($this->author, PortalNotification::class, fn ($n) => str_contains($n->body, 'Bitte Ort ergänzen.'));
$this->actingAs($this->author)->get('/redaktion')->assertInertia(fn (Assert $p) => $p->where('sections.0.props.mine.0.review_note', 'Bitte Ort ergänzen.'));
$this->post('/redaktion/neuigkeiten/'.$post->id.'/freigeben')->assertForbidden();
}
public function test_publishers_can_schedule_and_feature(): void
{
$this->actingAs($this->editor)->post('/redaktion/neuigkeiten', $this->payload([
'action' => 'publish',
'published_at' => now()->addDays(2)->toIso8601String(),
'is_featured' => '1',
]))->assertSessionHas('status', 'Beitrag geplant.');
$post = NewsPost::query()->sole();
$this->assertTrue($post->isScheduled());
$this->assertTrue($post->is_featured);
$this->assertFalse($post->isPublished());
$this->get('/redaktion')->assertInertia(fn (Assert $p) => $p->where('sections.0.props.mine.0.status_label', 'Geplant'));
$this->travel(3)->days();
$this->assertTrue($post->fresh()->isPublished());
}
public function test_authors_cannot_write_in_foreign_categories(): void
{
$this->actingAs($this->author)->post('/redaktion/neuigkeiten', $this->payload(['category_id' => $this->company->id]))->assertSessionHasErrors('category_id');
$this->post('/redaktion/neuigkeiten', $this->payload(['category_id' => null]))->assertSessionHasErrors('category_id');
$foreign = NewsPost::factory()->draft()->create(['category_id' => $this->company->id]);
$this->get('/redaktion/neuigkeiten/'.$foreign->id)->assertForbidden();
$this->actingAs(User::factory()->create())->get('/redaktion/neuigkeiten/neu')->assertForbidden();
$this->get('/redaktion')->assertForbidden();
}
public function test_inline_images_are_uploaded_for_the_body(): void
{
$url = $this->actingAs($this->author)->postJson('/redaktion/neuigkeiten/bilder', ['image' => UploadedFile::fake()->image('bild.png', 800, 600)])->assertOk()->json('url');
$this->assertStringContainsString('pagebuilder/news/', $url, 'Pfad, den Bild-Blöcke annehmen');
$this->actingAs(User::factory()->create())->postJson('/redaktion/neuigkeiten/bilder', ['image' => UploadedFile::fake()->image('x.png')])->assertForbidden();
}
public function test_news_block_lists_visible_posts_in_pages(): void
{
NewsPost::factory()->create(['category_id' => $this->mavInfo->id, 'title' => 'Aus der MAV']);
NewsPost::factory()->create(['category_id' => $this->company->id, 'title' => 'Quartalszahlen']);
NewsPost::factory()->draft()->create(['category_id' => $this->mavInfo->id, 'title' => 'Entwurf']);
$content = app(Content::class);
$rendered = $content->render($content->sanitize(['blocks' => [['type' => 'news.latest', 'data' => ['category' => 'mav-info', 'count' => 5]]]]), User::factory()->create());
$this->assertSame(['Aus der MAV'], array_column($rendered['blocks'][0]['props']['posts'], 'title'));
$this->assertSame('/neuigkeiten?kategorie=mav-info', $rendered['blocks'][0]['props']['more_url']);
}
public function test_admin_form_keeps_status_in_sync(): void
{
$post = NewsPost::factory()->draft()->create();
$this->assertSame(NewsPost::DRAFT, $post->status);
$post->update(['published_at' => now()]);
$this->assertSame(NewsPost::PUBLISHED, $post->fresh()->status);
$post->update(['published_at' => null]);
$this->assertSame(NewsPost::DRAFT, $post->fresh()->status);
}
public function test_a_form_can_be_shown_below_a_post(): void
{
$form = Form::query()->create(['title' => 'Anmeldung Sommerfest', 'fields' => [['type' => 'text', 'data' => ['label' => 'Name']]]]);
$this->actingAs($this->editor)->get('/redaktion/neuigkeiten/neu')
->assertInertia(fn (Assert $p) => $p->where('forms', [['value' => $form->id, 'label' => 'Anmeldung Sommerfest']]));
$this->post('/redaktion/neuigkeiten', $this->payload(['action' => 'publish', 'form_id' => $form->id]))->assertSessionHasNoErrors();
$post = NewsPost::query()->sole();
$this->assertSame($form->id, $post->form_id);
$this->actingAs(User::factory()->create())->get('/neuigkeiten/'.$post->slug)->assertInertia(fn (Assert $p) => $p
->where('form.blocks.0.type', 'forms.form')
->where('form.blocks.0.props.title', 'Anmeldung Sommerfest'));
$this->actingAs($this->editor)->put('/redaktion/neuigkeiten/'.$post->id, $this->payload(['action' => 'publish', 'form_id' => 99999]))->assertSessionHasErrors('form_id');
}
}