Lint PreventionTest
continuous-integration/drone/push Build is passing Details

This commit is contained in:
philipp lang 2025-05-24 16:53:11 +02:00
parent fc2b3d6885
commit 7c656afce8
1 changed files with 256 additions and 280 deletions

View File

@ -22,173 +22,174 @@ use Tests\Lib\CreatesFormFields;
use Tests\RequestFactories\EditorRequestFactory; use Tests\RequestFactories\EditorRequestFactory;
use Tests\TestCase; use Tests\TestCase;
class PreventionTest extends TestCase uses(DatabaseTransactions::class);
{ uses(CreatesFormFields::class);
use DatabaseTransactions; function createForm(): Form
use CreatesFormFields;
public function testItRemembersWhenNotRememberedYet(): void
{ {
return Form::factory()->fields([
test()->textField('vorname')->namiType(NamiType::FIRSTNAME)->specialType(SpecialType::FIRSTNAME),
test()->textField('nachname')->namiType(NamiType::FIRSTNAME)->specialType(SpecialType::LASTNAME),
test()->textField('email')->namiType(NamiType::FIRSTNAME)->specialType(SpecialType::EMAIL),
])->create(['needs_prevention' => true]);
}
function createParticipant(Form $form): Participant
{
return Participant::factory()->for($form)->data([
'vorname' => 'Max',
'nachname' => 'Muster',
'email' => 'mail@a.de',
])->for(Member::factory()->defaults()->has(Membership::factory()->inLocal('€ LeiterIn', 'Wölfling')))->create();
}
dataset('attributes', fn() => [
[
['has_vk' => true, 'efz' => null, 'ps_at' => now()],
[Prevention::EFZ]
],
[
['has_vk' => true, 'efz' => now(), 'ps_at' => null],
[Prevention::PS]
],
[
['has_vk' => true, 'efz' => now()->subDay(), 'ps_at' => now()],
[]
],
[
['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subDay()],
[]
],
[
['has_vk' => true, 'efz' => now()->subYears(5)->subDay(), 'ps_at' => now()],
[Prevention::EFZ]
],
[
['has_vk' => true, 'efz' => now(), 'ps_at' => null],
[Prevention::PS]
],
[
['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(5)->subDay()],
[Prevention::MOREPS]
],
[
['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(5)->subDay(), 'more_ps_at' => now()],
[]
],
[
['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(15), 'more_ps_at' => now()->subYears(5)->subDay()],
[Prevention::MOREPS],
],
[
['has_vk' => false, 'efz' => now(), 'ps_at' => now()],
[Prevention::VK],
],
[
['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(7)],
[Prevention::MOREPS],
]
]);
it('testItRemembersWhenNotRememberedYet', function () {
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$participant = $this->createParticipant($form); $participant = createParticipant($form);
PreventionRememberAction::run(); PreventionRememberAction::run();
$this->assertEquals(now()->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d')); $this->assertEquals(now()->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d'));
} });
public function testItDoesntRememberPastEvents(): void it('testItDoesntRememberPastEvents', function () {
{
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$participant = $this->createParticipant($form); $participant = createParticipant($form);
$form->update(['from' => now()->subDay()]); $form->update(['from' => now()->subDay()]);
PreventionRememberAction::run(); PreventionRememberAction::run();
$this->assertNull($participant->fresh()->last_remembered_at); $this->assertNull($participant->fresh()->last_remembered_at);
} });
public function testItDoesntRememberWhenConditionDoesntMatch(): void it('testItDoesntRememberWhenConditionDoesntMatch', function () {
{
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$form->update(['prevention_conditions' => Condition::from(['mode' => 'all', 'ifs' => [['field' => 'vorname', 'comparator' => 'isEqual', 'value' => 'Max']]])]); $form->update(['prevention_conditions' => Condition::from(['mode' => 'all', 'ifs' => [['field' => 'vorname', 'comparator' => 'isEqual', 'value' => 'Max']]])]);
$participant = $this->createParticipant($form); $participant = createParticipant($form);
$participant->update(['data' => [...$participant->data, 'vorname' => 'Jane']]); $participant->update(['data' => [...$participant->data, 'vorname' => 'Jane']]);
PreventionRememberAction::run(); PreventionRememberAction::run();
$this->assertNull($participant->fresh()->last_remembered_at); $this->assertNull($participant->fresh()->last_remembered_at);
} });
public function testItRemembersWhenRememberIsDue(): void it('testItRemembersWhenRememberIsDue', function () {
{
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$participant = tap($this->createParticipant($form), fn ($p) => $p->update(['last_remembered_at' => now()->subWeeks(3)])); $participant = tap(createParticipant($form), fn($p) => $p->update(['last_remembered_at' => now()->subWeeks(3)]));
PreventionRememberAction::run(); PreventionRememberAction::run();
$this->assertEquals(now()->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d')); $this->assertEquals(now()->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d'));
} });
public function testItDoesntRememberWhenRememberingIsNotDue(): void it('testItDoesntRememberWhenRememberingIsNotDue', function () {
{
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$participant = tap($this->createParticipant($form), fn ($p) => $p->update(['last_remembered_at' => now()->subWeeks(1)])); $participant = tap(createParticipant($form), fn($p) => $p->update(['last_remembered_at' => now()->subWeeks(1)]));
PreventionRememberAction::run(); PreventionRememberAction::run();
$this->assertEquals(now()->subWeeks(1)->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d')); $this->assertEquals(now()->subWeeks(1)->format('Y-m-d'), $participant->fresh()->last_remembered_at->format('Y-m-d'));
} });
public function testItDoesntRememberWhenFormDoesntNeedPrevention(): void it('testItDoesntRememberWhenFormDoesntNeedPrevention', function () {
{
Mail::fake(); Mail::fake();
$form = tap($this->createForm(), fn ($form) => $form->update(['needs_prevention' => false])); $form = tap(createForm(), fn($form) => $form->update(['needs_prevention' => false]));
$participant = $this->createParticipant($form); $participant = createParticipant($form);
PreventionRememberAction::run(); PreventionRememberAction::run();
$this->assertNull($participant->fresh()->last_remembered_at); $this->assertNull($participant->fresh()->last_remembered_at);
} });
public function testItDoesntRememberWhenParticipantDoesntHaveMember(): void it('testItDoesntRememberWhenParticipantDoesntHaveMember', function () {
{
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$participant = $this->createParticipant($form); $participant = createParticipant($form);
$participant->member->delete(); $participant->member->delete();
PreventionRememberAction::run(); PreventionRememberAction::run();
$this->assertNull($participant->fresh()->last_remembered_at); $this->assertNull($participant->fresh()->last_remembered_at);
} });
public function testItRemembersNonLeaders(): void it('testItRemembersNonLeaders', function () {
{
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$participant = $this->createParticipant($form); $participant = createParticipant($form);
$participant->member->memberships->each->delete(); $participant->member->memberships->each->delete();
PreventionRememberAction::run(); PreventionRememberAction::run();
$this->assertNotNull($participant->fresh()->last_remembered_at); $this->assertNotNull($participant->fresh()->last_remembered_at);
} });
public static function attributes(): Generator
{
yield [
'attrs' => ['has_vk' => true, 'efz' => null, 'ps_at' => now()],
'preventions' => [Prevention::EFZ]
];
yield [ it('testItRemembersMember', function ($attrs, $preventions) {
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => null],
'preventions' => [Prevention::PS]
];
yield [
'attrs' => ['has_vk' => true, 'efz' => now()->subDay(), 'ps_at' => now()],
'preventions' => []
];
yield [
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subDay()],
'preventions' => []
];
yield [
'attrs' => ['has_vk' => true, 'efz' => now()->subYears(5)->subDay(), 'ps_at' => now()],
'preventions' => [Prevention::EFZ]
];
yield [
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => null],
'preventions' => [Prevention::PS]
];
yield [
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(5)->subDay()],
'preventions' => [Prevention::MOREPS]
];
yield [
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(5)->subDay(), 'more_ps_at' => now()],
'preventions' => []
];
yield [
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(15), 'more_ps_at' => now()->subYears(5)->subDay()],
'preventions' => [Prevention::MOREPS],
];
yield [
'attrs' => ['has_vk' => false, 'efz' => now(), 'ps_at' => now()],
'preventions' => [Prevention::VK],
];
yield [
'attrs' => ['has_vk' => true, 'efz' => now(), 'ps_at' => now()->subYears(7)],
'preventions' => [Prevention::MOREPS],
];
}
/**
* @param array<int, Prevention> $preventions
* @param array<string, mixed> $memberAttributes
*/
#[DataProvider('attributes')]
public function testItRemembersMember(array $attrs, array $preventions): void
{
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$participant = $this->createParticipant($form); $participant = createParticipant($form);
$participant->member->update($attrs); $participant->member->update($attrs);
PreventionRememberAction::run(); PreventionRememberAction::run();
@ -200,39 +201,36 @@ class PreventionTest extends TestCase
Mail::assertNotSent(PreventionRememberMail::class); Mail::assertNotSent(PreventionRememberMail::class);
$this->assertNull($participant->fresh()->last_remembered_at); $this->assertNull($participant->fresh()->last_remembered_at);
} }
} })->with('attributes');
public function testItDoesntRememberParticipantThatHasNoMail(): void it('testItDoesntRememberParticipantThatHasNoMail', function () {
{
Mail::fake(); Mail::fake();
$form = $this->createForm(); $form = createForm();
$participant = $this->createParticipant($form); $participant = createParticipant($form);
$participant->update(['data' => [...$participant->data, 'email' => '']]); $participant->update(['data' => [...$participant->data, 'email' => '']]);
PreventionRememberAction::run(); PreventionRememberAction::run();
Mail::assertNotSent(PreventionRememberMail::class); Mail::assertNotSent(PreventionRememberMail::class);
} });
public function testItRendersMail(): void it('testItRendersMail', function () {
{
InvoiceSettings::fake(['from_long' => 'Stamm Beispiel']); InvoiceSettings::fake(['from_long' => 'Stamm Beispiel']);
$form = $this->createForm(); $form = createForm();
$participant = $this->createParticipant($form); $participant = createParticipant($form);
(new PreventionRememberMail($participant, app(PreventionSettings::class)->formmail)) (new PreventionRememberMail($participant, app(PreventionSettings::class)->formmail))
->assertSeeInText($participant->member->firstname) ->assertSeeInText($participant->member->firstname)
->assertSeeInText($participant->member->lastname) ->assertSeeInText($participant->member->lastname)
->assertSeeInText('Stamm Beispiel'); ->assertSeeInText('Stamm Beispiel');
} });
public function testItRendersSetttingMail(): void it('testItRendersSetttingMail', function () {
{
Mail::fake(); Mail::fake();
app(PreventionSettings::class)->fill([ app(PreventionSettings::class)->fill([
'formmail' => EditorRequestFactory::new()->paragraphs(["lorem lala {formname} g", "{wanted}", "bbb"])->toData() 'formmail' => EditorRequestFactory::new()->paragraphs(["lorem lala {formname} g", "{wanted}", "bbb"])->toData()
])->save(); ])->save();
$form = $this->createForm(); $form = createForm();
$this->createParticipant($form); createParticipant($form);
PreventionRememberAction::run(); PreventionRememberAction::run();
@ -240,67 +238,45 @@ class PreventionTest extends TestCase
'lorem lala ' . $form->name, 'lorem lala ' . $form->name,
'erweitertes' 'erweitertes'
])); ]));
} });
public function testItAppendsTextOfForm(): void it('testItAppendsTextOfForm', function () {
{
Mail::fake(); Mail::fake();
app(PreventionSettings::class)->fill([ app(PreventionSettings::class)->fill([
'formmail' => EditorRequestFactory::new()->paragraphs(["::first::"])->toData() 'formmail' => EditorRequestFactory::new()->paragraphs(["::first::"])->toData()
])->save(); ])->save();
$form = $this->createForm(); $form = createForm();
$form->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['event'])->toData()]); $form->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['event'])->toData()]);
$this->createParticipant($form); createParticipant($form);
PreventionRememberAction::run(); PreventionRememberAction::run();
Mail::assertSent(PreventionRememberMail::class, fn($mail) => $mail->bodyText->hasAll([ Mail::assertSent(PreventionRememberMail::class, fn($mail) => $mail->bodyText->hasAll([
'event' 'event'
])); ]));
} });
public function testItDoesntAppendTextTwice(): void it('testItDoesntAppendTextTwice', function () {
{
Mail::fake(); Mail::fake();
app(PreventionSettings::class)->fill(['frommail' => EditorRequestFactory::new()->paragraphs(["::first::"])->toData()])->save(); app(PreventionSettings::class)->fill(['frommail' => EditorRequestFactory::new()->paragraphs(["::first::"])->toData()])->save();
tap($this->createForm(), function ($f) { tap(createForm(), function ($f) {
$f->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['oberhausen'])->toData()]); $f->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['oberhausen'])->toData()]);
$this->createParticipant($f); createParticipant($f);
}); });
tap($this->createForm(), function ($f) { tap(createForm(), function ($f) {
$f->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['siegburg'])->toData()]); $f->update(['prevention_text' => EditorRequestFactory::new()->paragraphs(['siegburg'])->toData()]);
$this->createParticipant($f); createParticipant($f);
}); });
PreventionRememberAction::run(); PreventionRememberAction::run();
Mail::assertSent(PreventionRememberMail::class, fn($mail) => $mail->bodyText->hasAll(['oberhausen']) && !$mail->bodyText->hasAll(['siegburg'])); Mail::assertSent(PreventionRememberMail::class, fn($mail) => $mail->bodyText->hasAll(['oberhausen']) && !$mail->bodyText->hasAll(['siegburg']));
} });
public function testItDisplaysBodyTextInMail(): void it('testItDisplaysBodyTextInMail', function () {
{ $form = createForm();
$form = $this->createForm(); $participant = createParticipant($form);
$participant = $this->createParticipant($form);
$mail = new PreventionRememberMail($participant, EditorRequestFactory::new()->paragraphs(['ggtt'])->toData()); $mail = new PreventionRememberMail($participant, EditorRequestFactory::new()->paragraphs(['ggtt'])->toData());
$mail->assertSeeInText('ggtt'); $mail->assertSeeInText('ggtt');
} });
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),
])->create(['needs_prevention' => true]);
}
protected function createParticipant(Form $form): Participant
{
return Participant::factory()->for($form)->data([
'vorname' => 'Max',
'nachname' => 'Muster',
'email' => 'mail@a.de',
])->for(Member::factory()->defaults()->has(Membership::factory()->inLocal('€ LeiterIn', 'Wölfling')))->create();
}
}