Übersicht mit Status und Standort, Anfragen im Portal (Benachrichtigung + E-Mail ans Team), Zusagen/Absagen mit automatischem Eintrag, Zurücknehmen, Recht „Verleih verwalten“, einstellbare Bezeichnungen und Übernahme aus dem WordPress-Plugin „FL-Verleihsystem“ (WXR mit Bildern). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
230 lines
11 KiB
PHP
230 lines
11 KiB
PHP
<?php
|
||
|
||
namespace Tests\Plugins\Verleih;
|
||
|
||
use App\Models\Role;
|
||
use App\Models\User;
|
||
use App\Notifications\PortalNotification;
|
||
use App\Support\SafeOutbound;
|
||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||
use Illuminate\Http\UploadedFile;
|
||
use Illuminate\Support\Facades\Http;
|
||
use Illuminate\Support\Facades\Mail;
|
||
use Illuminate\Support\Facades\Notification;
|
||
use Illuminate\Support\Facades\Storage;
|
||
use Inertia\Testing\AssertableInertia as Assert;
|
||
use Inhaus\Plugins\Verleih\Mail\Notice;
|
||
use Inhaus\Plugins\Verleih\Models\Item;
|
||
use Inhaus\Plugins\Verleih\Models\LoanRequest;
|
||
use Inhaus\Plugins\Verleih\WordpressImport;
|
||
use Tests\TestCase;
|
||
|
||
/**
|
||
* Läuft innerhalb einer Inhaus-Installation mit dem Plugin unter plugins/verleih:
|
||
* vendor-dev/bin/phpunit plugins/verleih/tests
|
||
*/
|
||
class VerleihTest extends TestCase
|
||
{
|
||
use RefreshDatabase;
|
||
|
||
private ?string $previousState = null;
|
||
|
||
private User $artist;
|
||
|
||
protected function setUp(): void
|
||
{
|
||
// Plugin aktivieren, bevor die Anwendung startet – so laufen seine Migrationen mit
|
||
$file = getcwd().'/storage/app/plugins.json';
|
||
$this->previousState = is_file($file) ? file_get_contents($file) : null;
|
||
$state = json_decode($this->previousState ?? '{}', true) ?: [];
|
||
$state['verleih'] = ['enabled' => true];
|
||
file_put_contents($file, json_encode($state));
|
||
|
||
parent::setUp();
|
||
|
||
Storage::fake('local');
|
||
$role = Role::query()->create(['name' => 'Künstlerteam']);
|
||
$role->grants()->create(['ability' => 'verleih.manage']);
|
||
$this->artist = User::factory()->withRoles([$role])->create(['first_name' => 'Kim', 'last_name' => 'Kunst']);
|
||
}
|
||
|
||
protected function tearDown(): void
|
||
{
|
||
$file = storage_path('app/plugins.json');
|
||
$this->previousState === null ? @unlink($file) : file_put_contents($file, $this->previousState);
|
||
|
||
parent::tearDown();
|
||
}
|
||
|
||
private function item(array $overrides = []): Item
|
||
{
|
||
return Item::query()->create(['title' => 'Sommerwiese', 'description' => '<p>Acryl auf Leinwand</p>', 'lent' => false, 'location' => 'Lager', ...$overrides]);
|
||
}
|
||
|
||
public function test_team_manages_pictures_everyone_sees_them_available_first(): void
|
||
{
|
||
$colleague = User::factory()->create();
|
||
|
||
$this->actingAs($colleague)->get('/verleih/neu')->assertForbidden();
|
||
$this->actingAs($colleague)->post('/verleih', ['title' => 'X', 'lent' => 0])->assertForbidden();
|
||
|
||
$this->actingAs($this->artist)->post('/verleih', [
|
||
'title' => 'Blaue Stunde',
|
||
'description' => '<p>Öl</p><script>alert(1)</script>',
|
||
'lent' => 1,
|
||
'location' => 'Verwaltung',
|
||
'image' => UploadedFile::fake()->image('bild.jpg', 3000, 2000),
|
||
])->assertRedirect();
|
||
|
||
$lent = Item::query()->where('title', 'Blaue Stunde')->firstOrFail();
|
||
$this->assertTrue($lent->lent);
|
||
$this->assertStringNotContainsString('<script', (string) $lent->description);
|
||
$this->assertSame(2000, $lent->image_width, 'Verkleinert auf 2000 px, ohne Zuschnitt');
|
||
$this->assertSame(1333, $lent->image_height);
|
||
$this->item(['title' => 'Sommerwiese']);
|
||
|
||
$this->actingAs($colleague)->get('/verleih')->assertInertia(fn (Assert $p) => $p
|
||
->component('verleih::Index')
|
||
->where('items.0.title', 'Sommerwiese')->where('items.0.lent', false)
|
||
->where('items.1.title', 'Blaue Stunde')->where('items.1.location', 'Verwaltung')
|
||
->where('canManage', false)->where('pendingRequests', null)
|
||
->where('labels.singular', 'Bild'));
|
||
|
||
$this->actingAs($colleague)->get('/verleih?status=verliehen')->assertInertia(fn (Assert $p) => $p->has('items', 1)->where('counts.available', 1));
|
||
$this->actingAs($colleague)->get("/verleih/bilder/{$lent->id}/thumb")->assertOk();
|
||
auth()->logout();
|
||
$this->get("/verleih/bilder/{$lent->id}/thumb")->assertRedirect('/login');
|
||
}
|
||
|
||
public function test_request_accept_decline_and_notifications(): void
|
||
{
|
||
Notification::fake();
|
||
Mail::fake();
|
||
$item = $this->item();
|
||
[$ada, $bob] = User::factory()->count(2)->create()->all();
|
||
|
||
$this->actingAs($ada)->post("/verleih/{$item->id}/anfragen", ['location' => 'Besprechungsraum 2. OG', 'from' => today()->addDay()->toDateString(), 'message' => 'Für die Jubiläumsfeier'])
|
||
->assertSessionHasNoErrors();
|
||
$this->actingAs($ada)->post("/verleih/{$item->id}/anfragen", ['location' => 'Nochmal'])->assertSessionHasErrors('location');
|
||
$this->actingAs($bob)->post("/verleih/{$item->id}/anfragen", ['location' => 'Empfang'])->assertSessionHasNoErrors();
|
||
|
||
Notification::assertSentTo($this->artist, PortalNotification::class, fn ($n) => str_contains($n->body, 'Besprechungsraum'));
|
||
Mail::assertQueued(Notice::class, fn (Notice $mail) => $mail->hasTo($this->artist->email) && $mail->replyPerson?->is($ada));
|
||
Notification::assertNotSentTo($bob, PortalNotification::class);
|
||
|
||
[$first, $second] = LoanRequest::query()->orderBy('id')->get()->all();
|
||
$this->actingAs($ada)->post("/verleih/anfragen/{$first->id}/zusagen")->assertForbidden();
|
||
|
||
$this->actingAs($this->artist)->get('/verleih/anfragen')->assertInertia(fn (Assert $p) => $p->has('pending', 2)->where('pending.0.user.id', $ada->id));
|
||
$this->actingAs($this->artist)->post("/verleih/anfragen/{$first->id}/zusagen", ['note' => 'Bringen wir Montag vorbei'])->assertSessionHasNoErrors();
|
||
|
||
$item->refresh();
|
||
$this->assertTrue($item->lent);
|
||
$this->assertSame('Besprechungsraum 2. OG', $item->location);
|
||
$this->assertSame($ada->id, $item->lent_to_id);
|
||
Notification::assertSentTo($ada, PortalNotification::class, fn ($n) => str_starts_with($n->title, 'Zusage') && str_contains($n->body, 'Montag'));
|
||
|
||
// Wer es hat, sieht nur das Team
|
||
$this->actingAs($bob)->get("/verleih/{$item->id}")->assertInertia(fn (Assert $p) => $p->where('item.lent_to', null)->where('myRequest.location', 'Empfang'));
|
||
$this->actingAs($this->artist)->get("/verleih/{$item->id}")->assertInertia(fn (Assert $p) => $p->where('item.lent_to.id', $ada->id)->has('requests', 2));
|
||
|
||
$this->actingAs($this->artist)->post("/verleih/anfragen/{$second->id}/absagen", ['note' => 'Ist gerade vergeben'])->assertSessionHasNoErrors();
|
||
$this->assertSame('declined', $second->fresh()->status);
|
||
Notification::assertSentTo($bob, PortalNotification::class, fn ($n) => str_starts_with($n->title, 'Absage'));
|
||
$this->actingAs($this->artist)->post("/verleih/anfragen/{$second->id}/zusagen")->assertSessionHasErrors('status');
|
||
|
||
// Zurückgenommen
|
||
$this->actingAs($this->artist)->post("/verleih/{$item->id}/zurueck", ['location' => 'Lager Künstlerteam'])->assertRedirect();
|
||
$item->refresh();
|
||
$this->assertFalse($item->lent);
|
||
$this->assertNull($item->lent_to_id);
|
||
$this->assertSame('Lager Künstlerteam', $item->location);
|
||
}
|
||
|
||
public function test_requesters_can_withdraw_only_their_own(): void
|
||
{
|
||
$item = $this->item();
|
||
[$ada, $bob] = User::factory()->count(2)->create()->all();
|
||
$this->actingAs($ada)->post("/verleih/{$item->id}/anfragen", ['location' => 'Empfang']);
|
||
$request = LoanRequest::query()->sole();
|
||
|
||
$this->actingAs($bob)->delete("/verleih/anfragen/{$request->id}")->assertForbidden();
|
||
$this->actingAs($ada)->delete("/verleih/anfragen/{$request->id}")->assertRedirect();
|
||
$this->assertSame('cancelled', $request->fresh()->status);
|
||
}
|
||
|
||
public function test_import_from_the_wordpress_plugin(): void
|
||
{
|
||
$file = tempnam(sys_get_temp_dir(), 'wxr');
|
||
file_put_contents($file, <<<'XML'
|
||
<?xml version="1.0" encoding="UTF-8" ?>
|
||
<rss version="2.0" xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:wp="http://wordpress.org/export/1.2/">
|
||
<channel>
|
||
<title>Altes Intranet</title>
|
||
<wp:wxr_version>1.2</wp:wxr_version>
|
||
<item>
|
||
<title>Sonnenblumen</title>
|
||
<content:encoded><![CDATA[<!-- wp:paragraph -->Öl auf Leinwand, 60 × 80 cm
|
||
|
||
Gemalt von Anna[gallery ids="1"]<!-- /wp:paragraph -->]]></content:encoded>
|
||
<wp:post_id>11</wp:post_id>
|
||
<wp:status>publish</wp:status>
|
||
<wp:post_type>bv_objekt</wp:post_type>
|
||
<wp:postmeta><wp:meta_key>_bv_verliehen</wp:meta_key><wp:meta_value><![CDATA[ja]]></wp:meta_value></wp:postmeta>
|
||
<wp:postmeta><wp:meta_key>_bv_verleihort</wp:meta_key><wp:meta_value><![CDATA[Haus B, Flur]]></wp:meta_value></wp:postmeta>
|
||
<wp:postmeta><wp:meta_key>_thumbnail_id</wp:meta_key><wp:meta_value><![CDATA[21]]></wp:meta_value></wp:postmeta>
|
||
</item>
|
||
<item>
|
||
<title>Entwurf</title>
|
||
<wp:post_id>12</wp:post_id>
|
||
<wp:status>draft</wp:status>
|
||
<wp:post_type>bv_objekt</wp:post_type>
|
||
</item>
|
||
<item>
|
||
<title>Hafen & Meer</title>
|
||
<content:encoded><![CDATA[<p>Aquarell</p>]]></content:encoded>
|
||
<wp:post_id>13</wp:post_id>
|
||
<wp:status>publish</wp:status>
|
||
<wp:post_type>bv_objekt</wp:post_type>
|
||
<wp:postmeta><wp:meta_key>_bv_verliehen</wp:meta_key><wp:meta_value><![CDATA[nein]]></wp:meta_value></wp:postmeta>
|
||
</item>
|
||
<item>
|
||
<title>sonnenblumen.jpg</title>
|
||
<wp:post_id>21</wp:post_id>
|
||
<wp:status>inherit</wp:status>
|
||
<wp:post_type>attachment</wp:post_type>
|
||
<wp:attachment_url><![CDATA[https://intranet.example.org/wp-content/uploads/sonnenblumen.jpg]]></wp:attachment_url>
|
||
</item>
|
||
</channel>
|
||
</rss>
|
||
XML);
|
||
|
||
$import = app(WordpressImport::class);
|
||
$result = $import->import($file, $this->artist->id);
|
||
$this->assertSame(['created' => 2, 'skipped' => 0, 'found' => 2], $result);
|
||
|
||
$sun = Item::query()->where('wp_id', 11)->sole();
|
||
$this->assertTrue($sun->lent);
|
||
$this->assertSame('Haus B, Flur', $sun->location);
|
||
$this->assertSame('<p>Öl auf Leinwand, 60 × 80 cm</p><p>Gemalt von Anna</p>', $sun->description);
|
||
$this->assertSame('https://intranet.example.org/wp-content/uploads/sonnenblumen.jpg', $sun->wp_image_url);
|
||
$this->assertSame('Hafen & Meer', Item::query()->where('wp_id', 13)->sole()->title);
|
||
$this->assertSame(1, $import->pendingImages());
|
||
|
||
// Bilder nachladen
|
||
SafeOutbound::fakeResolve(['intranet.example.org' => '93.184.216.34']);
|
||
$jpeg = UploadedFile::fake()->image('x.jpg', 800, 600)->getContent();
|
||
Http::fake(['intranet.example.org/*' => Http::response($jpeg, 200, ['Content-Type' => 'image/jpeg'])]);
|
||
$this->assertSame(['loaded' => 1, 'failed' => 0, 'remaining' => 0], $import->fetchImages());
|
||
$this->assertNotNull($sun->fresh()->image_path);
|
||
|
||
// Zweiter Lauf überschreibt nichts
|
||
$sun->update(['location' => 'Im Portal geändert']);
|
||
$this->assertSame(['created' => 0, 'skipped' => 2, 'found' => 2], $import->import($file));
|
||
$this->assertSame('Im Portal geändert', $sun->fresh()->location);
|
||
|
||
file_put_contents($file, '<?xml version="1.0"?><!DOCTYPE x [<!ENTITY a "b">]><rss></rss>');
|
||
$this->expectExceptionMessage('Entitäten');
|
||
$import->import($file);
|
||
}
|
||
}
|