Loheide-eu-mitarbeiterplatt.../tests/Feature/News/NewsWorkflowTest.php
Friederich Loheide 546c94db1c
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 4m47s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 4m40s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m0s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 4m51s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m1s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 6m57s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 5m45s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 5m36s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m21s
Profil zeigt geschriebene Beiträge, Als Person anmelden (0.27.1)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 10:03:27 +00:00

244 lines
13 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\DB;
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 Inhaus\Plugins\Pages\Models\Page;
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' => ['filter' => 'include', 'categories' => ['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');
}
public function test_old_single_category_news_blocks_are_converted(): void
{
$page = Page::query()->create(['title' => 'Alt']);
DB::table('pages')->where('id', $page->id)->update(['content' => json_encode(['blocks' => [
['id' => 'blkcols1', 'type' => 'columns', 'data' => ['layout' => '1-1'], 'items' => [
['id' => 'itm1', 'blocks' => [['id' => 'blknews1', 'type' => 'news.latest', 'data' => ['title' => 'MAV', 'category' => 'mav-info', 'count' => 3, 'layout' => 'cards']]]],
['id' => 'itm2', 'blocks' => [['id' => 'blknews2', 'type' => 'news.latest', 'data' => ['title' => 'Alle', 'category' => '', 'count' => 3, 'layout' => 'list']]]],
]],
]])]);
(require base_path('plugins-core/news/database/migrations/2026_10_11_000002_convert_news_block_category_filter.php'))->up();
$items = $page->fresh()->content['blocks'][0]['items'];
$this->assertSame(['title' => 'MAV', 'count' => 3, 'layout' => 'cards', 'filter' => 'include', 'categories' => ['mav-info']], $items[0]['blocks'][0]['data']);
$this->assertSame('all', $items[1]['blocks'][0]['data']['filter']);
}
public function test_profile_lists_the_posts_a_person_has_written(): void
{
NewsPost::factory()->create(['author_id' => $this->author->id, 'category_id' => $this->company->id, 'title' => 'Neues Leitbild', 'published_at' => now()->subDay()]);
NewsPost::factory()->draft()->create(['author_id' => $this->author->id, 'category_id' => $this->company->id, 'title' => 'Entwurf']);
$viewer = User::factory()->create();
$this->actingAs($viewer)->get("/personen/{$this->author->id}")->assertInertia(fn ($page) => $page
->where('panels', fn ($panels) => ($posts = collect($panels)->firstWhere('key', 'news.posts'))
&& $posts['props']['total'] === 1 && $posts['props']['posts'][0]['title'] === 'Neues Leitbild'));
$this->get("/neuigkeiten?autor={$this->author->id}")->assertInertia(fn ($page) => $page
->where('filter', 'Von: '.$this->author->name)
->where('posts.data', fn ($posts) => collect($posts)->pluck('title')->all() === ['Neues Leitbild']));
// Ohne Beiträge kein Bereich
$this->get("/personen/{$viewer->id}")->assertInertia(fn ($page) => $page->where('panels', fn ($panels) => ! collect($panels)->contains('key', 'news.posts')));
}
}