Loheide-eu-mitarbeiterplatt.../tests/Feature/Stellen/StellenTest.php
Friederich Loheide f1202bea08
Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Sicherheitsupdate nach Audit (0.29.0)
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-09-27 11:34:54 +00:00

202 lines
11 KiB
PHP

<?php
namespace Tests\Feature\Stellen;
use App\Models\Role;
use App\Models\User;
use App\Notifications\PortalNotification;
use App\Plugins\PluginAccess;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Inertia\Testing\AssertableInertia as Assert;
use Inhaus\Plugins\Stellen\Announcer;
use Inhaus\Plugins\Stellen\Mail\ApplicationReceived;
use Inhaus\Plugins\Stellen\Models\JobApplication;
use Inhaus\Plugins\Stellen\Models\JobPosting;
use Tests\TestCase;
class StellenTest extends TestCase
{
use RefreshDatabase;
private User $hr;
protected function setUp(): void
{
parent::setUp();
Storage::fake('local');
$role = Role::query()->create(['name' => 'Personal']);
$role->grants()->create(['ability' => 'jobs.manage']);
$this->hr = User::factory()->withRoles([$role])->create();
}
private function posting(array $data = []): JobPosting
{
return JobPosting::query()->create([
'title' => 'Pflegefachkraft (m/w/d)', 'department' => 'Pflege', 'location' => 'Haus am See',
'employment' => 'part', 'hours' => '30 Std./Woche', 'contract' => 'permanent', 'apply_mode' => 'online',
'published_at' => now()->subHour(), 'deadline' => today()->addWeeks(2), 'created_by' => $this->hr->id, ...$data,
]);
}
public function test_hr_publishes_a_posting_and_everyone_is_notified_once(): void
{
Notification::fake();
$employees = User::factory()->count(3)->create();
$this->actingAs($this->hr)->get('/redaktion/stellen/neu')->assertOk();
$this->assertFalse($this->hr->canAccessAdmin(), 'Stellen pflegen braucht kein Backend');
$this->post('/redaktion/stellen', [
'title' => 'Leitung Hauswirtschaft', 'department' => 'Hauswirtschaft', 'location' => 'Zentrale',
'employment' => 'full', 'contract' => 'fixed', 'contract_note' => 'bis 31.12.2027',
'description' => '<h2>Ihre Aufgaben</h2><p>Organisation<script>x</script></p>', 'apply_mode' => 'online',
'publish' => 'now', 'deadline' => today()->addMonth()->toDateString(),
'pdf' => UploadedFile::fake()->create('ausschreibung.pdf', 50, 'application/pdf'),
])->assertRedirect()->assertSessionHasNoErrors();
$posting = JobPosting::query()->sole();
$this->assertTrue($posting->isOpen());
$this->assertNotNull($posting->announced_at);
$this->assertStringNotContainsString('script', $posting->description);
Storage::disk('local')->assertExists($posting->pdf_path);
Notification::assertSentTo($employees, PortalNotification::class, fn ($n) => str_contains($n->title, 'Leitung Hauswirtschaft'));
Notification::assertNotSentTo($this->hr, PortalNotification::class);
// Erneutes Speichern kündigt nicht noch einmal an
Notification::fake();
$this->post("/redaktion/stellen/{$posting->id}", ['title' => 'Leitung Hauswirtschaft (m/w/d)', 'employment' => 'full', 'contract' => 'fixed', 'apply_mode' => 'online', 'publish' => 'now'])->assertSessionHasNoErrors();
Notification::assertNothingSent();
$this->assertTrue($posting->fresh()->published_at->equalTo($posting->published_at));
}
public function test_only_open_postings_are_listed_and_scheduled_ones_announced_later(): void
{
Notification::fake();
$this->posting(['title' => 'Offen']);
$this->posting(['title' => 'Abgelaufen', 'deadline' => today()->subDay()]);
$this->posting(['title' => 'Entwurf', 'published_at' => null]);
$scheduled = $this->posting(['title' => 'Geplant', 'published_at' => now()->addHour()]);
$this->actingAs(User::factory()->create())->get('/stellen')->assertInertia(fn (Assert $page) => $page
->where('postings', fn ($postings) => collect($postings)->pluck('title')->all() === ['Offen']));
$this->get("/stellen/{$scheduled->id}")->assertNotFound();
$this->travel(2)->hours();
$this->assertSame(2, app(Announcer::class)->announceDue()); // „Offen“ und „Geplant“ (per Modell angelegt)
$this->assertSame(0, app(Announcer::class)->announceDue());
}
public function test_online_application_is_confidential(): void
{
Notification::fake();
Mail::fake();
$contact = User::factory()->create();
$posting = $this->posting(['contact_id' => $contact->id, 'notify_email' => 'personal@firma.de']);
$anna = User::factory()->create();
$colleague = User::factory()->create();
$this->actingAs($anna)->post("/stellen/{$posting->id}/bewerben", [])->assertSessionHasErrors('message');
$this->post("/stellen/{$posting->id}/bewerben", ['message' => 'Ich habe Interesse.', 'file' => UploadedFile::fake()->create('lebenslauf.pdf', 100, 'application/pdf')])->assertSessionHasNoErrors();
$this->post("/stellen/{$posting->id}/bewerben", ['message' => 'Nochmal'])->assertStatus(422);
$application = JobApplication::query()->sole();
Notification::assertSentTo($contact, PortalNotification::class);
Mail::assertQueued(ApplicationReceived::class, fn ($m) => $m->hasTo('personal@firma.de'));
$this->get("/stellen/{$posting->id}")->assertInertia(fn (Assert $page) => $page->where('application.status', 'received')->where('canApply', false)->where('applicationsUrl', null));
$this->get('/stellen/meine-bewerbungen')->assertInertia(fn (Assert $page) => $page->where('applications.0.id', $application->id));
$this->get("/stellen/bewerbungen/{$application->id}/unterlagen")->assertOk();
// Kolleg:innen sehen nichts, die Ansprechperson schon
$this->actingAs($colleague)->get("/redaktion/stellen/{$posting->id}/bewerbungen")->assertForbidden();
$this->actingAs($colleague)->get("/stellen/bewerbungen/{$application->id}/unterlagen")->assertNotFound();
$this->actingAs($contact)->get("/redaktion/stellen/{$posting->id}/bewerbungen")->assertOk()->assertInertia(fn (Assert $page) => $page->where('applications.0.person.id', $anna->id));
// Stand ändern → Bewerberin wird benachrichtigt, Notiz bleibt intern
$this->put("/redaktion/stellen/bewerbungen/{$application->id}", ['status' => 'interview', 'note' => 'Termin 12.10.'])->assertRedirect();
Notification::assertSentTo($anna, PortalNotification::class, fn ($n) => str_contains($n->body, 'Gespräch vereinbart'));
$this->actingAs($anna)->get('/stellen/meine-bewerbungen')->assertInertia(fn (Assert $page) => $page->where('applications.0.note', null)->where('applications.0.status', 'interview'));
}
public function test_applicants_can_withdraw_and_other_apply_modes(): void
{
$posting = $this->posting();
$anna = User::factory()->create();
$this->actingAs($anna)->post("/stellen/{$posting->id}/bewerben", ['message' => 'Hallo', 'file' => UploadedFile::fake()->create('cv.docx', 20)])->assertSessionHasNoErrors();
$application = JobApplication::query()->sole();
$path = $application->file_path;
$this->actingAs(User::factory()->create())->delete("/stellen/bewerbungen/{$application->id}")->assertForbidden();
$this->actingAs($anna)->delete("/stellen/bewerbungen/{$application->id}")->assertRedirect();
$this->assertSame(0, JobApplication::query()->count());
Storage::disk('local')->assertMissing($path);
$link = $this->posting(['apply_mode' => 'link', 'apply_url' => 'https://karriere.example.org/123']);
$this->get("/stellen/{$link->id}")->assertInertia(fn (Assert $page) => $page->where('canApply', false)->where('posting.apply_url', 'https://karriere.example.org/123'));
$this->post("/stellen/{$link->id}/bewerben", ['message' => 'x'])->assertNotFound();
}
public function test_others_cannot_manage_postings(): void
{
$posting = $this->posting();
$user = User::factory()->create();
$this->actingAs($user)->get('/redaktion/stellen/neu')->assertForbidden();
$this->actingAs($user)->post('/redaktion/stellen', ['title' => 'x'])->assertForbidden();
$this->actingAs($user)->delete("/redaktion/stellen/{$posting->id}")->assertForbidden();
}
public function test_images_can_be_used_in_the_description(): void
{
Storage::fake('uploads');
$url = $this->actingAs($this->hr)->postJson('/redaktion/stellen/bilder', ['image' => UploadedFile::fake()->image('team.jpg', 1200, 800)])->assertOk()->json('url');
$this->assertStringContainsString('stellen/', $url);
$this->post('/redaktion/stellen', [
'title' => 'Mit Bild', 'employment' => 'full', 'contract' => 'permanent', 'apply_mode' => 'online', 'publish' => 'draft',
'description' => '<p>Unser Team</p><p><img src="'.$url.'" alt="Team"></p>',
])->assertSessionHasNoErrors();
$this->assertStringContainsString('<img src="'.strtok($url, '?'), JobPosting::query()->where('title', 'Mit Bild')->sole()->description);
$this->actingAs(User::factory()->create())->postJson('/redaktion/stellen/bilder', ['image' => UploadedFile::fake()->image('x.jpg')])->assertForbidden();
}
public function test_application_by_email_address(): void
{
$this->actingAs($this->hr)->post('/redaktion/stellen', ['title' => 'Per Mail', 'employment' => 'full', 'contract' => 'permanent', 'apply_mode' => 'email', 'publish' => 'now'])
->assertSessionHasErrors(['apply_email' => 'Bitte die E-Mail-Adresse für Bewerbungen angeben.']);
$this->post('/redaktion/stellen', ['title' => 'Per Mail', 'employment' => 'full', 'contract' => 'permanent', 'apply_mode' => 'email', 'apply_email' => 'Bewerbung@Firma.de', 'publish' => 'now'])->assertSessionHasNoErrors();
$posting = JobPosting::query()->where('title', 'Per Mail')->sole();
$this->actingAs(User::factory()->create())->get("/stellen/{$posting->id}")->assertInertia(fn (Assert $page) => $page
->where('posting.apply_mode', 'email')->where('posting.apply_email', 'bewerbung@firma.de')->where('canApply', false));
$this->post("/stellen/{$posting->id}/bewerben", ['message' => 'x'])->assertNotFound();
}
public function test_posting_editors_cannot_make_themselves_contact_to_read_applications(): void
{
$this->actingAs($this->hr)->post('/redaktion/stellen', [
'title' => 'Selbst', 'employment' => 'full', 'contract' => 'permanent', 'apply_mode' => 'online', 'publish' => 'draft', 'contact_id' => $this->hr->id,
])->assertSessionHasErrors('contact_id');
}
public function test_excluded_roles_are_not_notified_about_new_postings(): void
{
Notification::fake();
$external = Role::query()->create(['name' => 'Extern']);
app(PluginAccess::class)->exclude('stellen', [$external->id]);
$guest = User::factory()->withRoles([$external])->create();
$staff = User::factory()->create();
app(Announcer::class)->announceIfDue($this->posting());
Notification::assertSentTo($staff, PortalNotification::class);
Notification::assertNotSentTo($guest, PortalNotification::class);
}
}