adrema/tests/Feature/Member/PreventionTest.php

295 lines
10 KiB
PHP
Raw Normal View History

2024-07-02 22:55:37 +02:00
<?php
namespace Tests\Feature\Member;
use App\Prevention\Enums\Prevention;
use App\Form\Actions\PreventionRememberAction;
use App\Form\Enums\NamiType;
use App\Form\Enums\SpecialType;
2024-07-02 22:55:37 +02:00
use App\Form\Models\Form;
use App\Form\Models\Participant;
2024-07-12 18:05:11 +02:00
use App\Lib\Editor\Condition;
2024-07-02 22:55:37 +02:00
use App\Prevention\Mails\PreventionRememberMail;
use App\Member\Member;
use App\Member\Membership;
use App\Prevention\PreventionSettings;
2024-07-02 22:55:37 +02:00
use Generator;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\Mail;
2024-10-14 21:21:19 +02:00
use Modules\Invoice\InvoiceSettings;
2024-09-21 22:46:38 +02:00
use PHPUnit\Framework\Attributes\DataProvider;
2024-07-02 22:55:37 +02:00
use Tests\Lib\CreatesFormFields;
2024-07-04 23:54:37 +02:00
use Tests\RequestFactories\EditorRequestFactory;
2024-07-02 22:55:37 +02:00
use Tests\TestCase;
class PreventionTest extends TestCase
{
use DatabaseTransactions;
use CreatesFormFields;
public function testItRemembersWhenNotRememberedYet(): void
{
Mail::fake();
$form = $this->createForm();
$participant = $this->createParticipant($form);
PreventionRememberAction::run();
$this->assertEquals(now()->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d'));
}
2024-07-12 18:05:11 +02:00
public function testItDoesntRememberWhenConditionDoesntMatch(): void
{
Mail::fake();
$form = $this->createForm();
$form->update(['prevention_conditions' => Condition::from(['mode' => 'all', 'ifs' => [['field' => 'vorname', 'comparator' => 'isEqual', 'value' => 'Max']]])]);
$participant = $this->createParticipant($form);
$participant->update(['data' => [...$participant->data, 'vorname' => 'Jane']]);
PreventionRememberAction::run();
$this->assertNull($participant->fresh()->last_remembered_at);
}
2024-07-02 22:55:37 +02:00
public function testItRemembersWhenRememberIsDue(): void
{
Mail::fake();
$form = $this->createForm();
$participant = tap($this->createParticipant($form), fn ($p) => $p->update(['last_remembered_at' => now()->subWeeks(3)]));
PreventionRememberAction::run();
$this->assertEquals(now()->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d'));
}
public function testItDoesntRememberWhenRememberingIsNotDue(): void
{
Mail::fake();
$form = $this->createForm();
$participant = tap($this->createParticipant($form), fn ($p) => $p->update(['last_remembered_at' => now()->subWeeks(1)]));
PreventionRememberAction::run();
$this->assertEquals(now()->subWeeks(1)->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d'));
}
public function testItDoesntRememberWhenFormDoesntNeedPrevention(): void
{
Mail::fake();
$form = tap($this->createForm(), fn ($form) => $form->update(['needs_prevention' => false]));
$participant = $this->createParticipant($form);
PreventionRememberAction::run();
$this->assertNull($participant->fresh()->last_remembered_at);
}
public function testItDoesntRememberWhenParticipantDoesntHaveMember(): void
{
Mail::fake();
$form = $this->createForm();
$participant = $this->createParticipant($form);
$participant->member->delete();
PreventionRememberAction::run();
$this->assertNull($participant->fresh()->last_remembered_at);
}
2024-07-12 18:06:52 +02:00
public function testItRemembersNonLeaders(): void
2024-07-02 22:55:37 +02:00
{
Mail::fake();
$form = $this->createForm();
$participant = $this->createParticipant($form);
$participant->member->memberships->each->delete();
PreventionRememberAction::run();
2024-07-12 18:06:52 +02:00
$this->assertNotNull($participant->fresh()->last_remembered_at);
2024-07-02 22:55:37 +02:00
}
2024-09-22 01:41:56 +02:00
public static function attributes(): Generator
2024-07-02 22:55:37 +02:00
{
yield [
2024-07-18 13:57:52 +02:00
'attrs' => ['has_vk' => true, 'efz' => null, 'ps_at' => now()],
2024-07-02 22:55:37 +02:00
'preventions' => [Prevention::EFZ]
];
yield [
2024-07-18 13:57:52 +02:00
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => null],
2024-07-02 22:55:37 +02:00
'preventions' => [Prevention::PS]
];
yield [
2024-07-18 13:57:52 +02:00
'attrs' => ['has_vk' => true, 'efz' => now()->subDay(), 'ps_at' => now()],
2024-07-02 22:55:37 +02:00
'preventions' => []
];
yield [
2024-07-18 13:57:52 +02:00
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subDay()],
2024-07-02 22:55:37 +02:00
'preventions' => []
];
yield [
2024-07-18 13:57:52 +02:00
'attrs' => ['has_vk' => true, 'efz' => now()->subYears(5)->subDay(), 'ps_at' => now()],
2024-07-02 22:55:37 +02:00
'preventions' => [Prevention::EFZ]
];
yield [
2024-07-25 12:53:40 +02:00
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => null],
2024-07-02 22:55:37 +02:00
'preventions' => [Prevention::PS]
];
2024-07-25 12:53:40 +02:00
yield [
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(5)->subDay()],
'preventions' => [Prevention::MOREPS]
];
2024-07-02 22:55:37 +02:00
yield [
2024-07-18 13:57:52 +02:00
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(5)->subDay(), 'more_ps_at' => now()],
2024-07-02 22:55:37 +02:00
'preventions' => []
];
yield [
2024-07-18 13:57:52 +02:00
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(15), 'more_ps_at' => now()->subYears(5)->subDay()],
2024-07-02 22:55:37 +02:00
'preventions' => [Prevention::MOREPS],
];
2024-07-18 13:57:52 +02:00
yield [
'attrs' => ['has_vk' => false, 'efz' => now(), 'ps_at' => now()],
'preventions' => [Prevention::VK],
];
2024-07-25 11:13:40 +02:00
yield [
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(7)],
'preventions' => [Prevention::MOREPS],
];
2024-07-02 22:55:37 +02:00
}
/**
* @param array<int, Prevention> $preventions
2024-07-02 23:00:09 +02:00
* @param array<string, mixed> $memberAttributes
2024-07-02 22:55:37 +02:00
*/
2024-09-21 22:46:38 +02:00
#[DataProvider('attributes')]
2024-09-22 01:41:56 +02:00
public function testItRemembersMember(array $attrs, array $preventions): void
2024-07-02 22:55:37 +02:00
{
Mail::fake();
$form = $this->createForm();
$participant = $this->createParticipant($form);
2024-09-22 01:41:56 +02:00
$participant->member->update($attrs);
2024-07-02 22:55:37 +02:00
PreventionRememberAction::run();
if (count($preventions)) {
Mail::assertSent(PreventionRememberMail::class, fn ($mail) => $mail->preventable->preventions() === $preventions);
$this->assertNotNull($participant->fresh()->last_remembered_at);
} else {
Mail::assertNotSent(PreventionRememberMail::class);
$this->assertNull($participant->fresh()->last_remembered_at);
}
}
public function testItDoesntRememberParticipantThatHasNoMail(): void
{
Mail::fake();
$form = $this->createForm();
$participant = $this->createParticipant($form);
$participant->update(['data' => [...$participant->data, 'email' => '']]);
PreventionRememberAction::run();
Mail::assertNotSent(PreventionRememberMail::class);
}
2024-07-02 22:55:37 +02:00
public function testItRendersMail(): void
{
InvoiceSettings::fake(['from_long' => 'Stamm Beispiel']);
$form = $this->createForm();
$participant = $this->createParticipant($form);
2024-07-04 23:54:37 +02:00
(new PreventionRememberMail($participant, app(PreventionSettings::class)->formmail))
2024-07-02 22:55:37 +02:00
->assertSeeInText($participant->member->firstname)
2024-07-04 23:54:37 +02:00
->assertSeeInText($participant->member->lastname)
->assertSeeInText('Stamm Beispiel');
}
public function testItRendersSetttingMail(): void
{
Mail::fake();
2024-07-08 22:39:11 +02:00
app(PreventionSettings::class)->fill([
2024-07-04 23:54:37 +02:00
'formmail' => EditorRequestFactory::new()->paragraphs(["lorem lala {formname} g", "{wanted}", "bbb"])->toData()
])->save();
$form = $this->createForm();
$this->createParticipant($form);
PreventionRememberAction::run();
Mail::assertSent(PreventionRememberMail::class, fn ($mail) => $mail->bodyText->hasAll([
'lorem lala ' . $form->name,
'erweitertes'
]));
}
2024-07-06 15:08:13 +02:00
public function testItAppendsTextOfForm(): void
{
Mail::fake();
2024-07-08 22:39:11 +02:00
app(PreventionSettings::class)->fill([
2024-07-06 15:08:13 +02:00
'formmail' => EditorRequestFactory::new()->paragraphs(["::first::"])->toData()
])->save();
$form = $this->createForm();
$form->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['event'])->toData()]);
$this->createParticipant($form);
PreventionRememberAction::run();
Mail::assertSent(PreventionRememberMail::class, fn ($mail) => $mail->bodyText->hasAll([
'event'
]));
}
2024-07-08 22:39:11 +02:00
public function testItDoesntAppendTextTwice(): void
{
Mail::fake();
app(PreventionSettings::class)->fill(['frommail' => EditorRequestFactory::new()->paragraphs(["::first::"])->toData()])->save();
tap($this->createForm(), function ($f) {
$f->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['oberhausen'])->toData()]);
$this->createParticipant($f);
});
tap($this->createForm(), function ($f) {
$f->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['siegburg'])->toData()]);
$this->createParticipant($f);
});
PreventionRememberAction::run();
Mail::assertSent(PreventionRememberMail::class, fn ($mail) => $mail->bodyText->hasAll(['oberhausen']) && !$mail->bodyText->hasAll(['siegburg']));
}
2024-07-04 23:54:37 +02:00
public function testItDisplaysBodyTextInMail(): void
{
$form = $this->createForm();
$participant = $this->createParticipant($form);
$mail = new PreventionRememberMail($participant, EditorRequestFactory::new()->paragraphs(['ggtt'])->toData());
$mail->assertSeeInText('ggtt');
2024-07-02 22:55:37 +02:00
}
protected function createForm(): Form
{
return Form::factory()->fields([
$this->textField('vorname')->namiType(NamiType::FIRSTNAME)->specialType(SpecialType::FIRSTNAME),
$this->textField('nachname')->namiType(NamiType::FIRSTNAME)->specialType(SpecialType::LASTNAME),
$this->textField('email')->namiType(NamiType::FIRSTNAME)->specialType(SpecialType::EMAIL),
2024-07-02 22:55:37 +02:00
])->create(['needs_prevention' => true]);
}
protected function createParticipant(Form $form): Participant
{
return Participant::factory()->for($form)->data([
'vorname' => 'Max',
'nachname' => 'Muster',
2024-07-04 22:12:45 +02:00
'email' => 'mail@a.de',
])->for(Member::factory()->defaults()->has(Membership::factory()->inLocal('€ LeiterIn', 'Wölfling')))->create();
2024-07-02 22:55:37 +02:00
}
}