From 0437511bf105b729c48d204ef80b298bf8b38557 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 23 Apr 2024 23:48:09 +0200 Subject: [PATCH] Add filter for attachments --- app/Form/Editor/FormConditionResolver.php | 5 +--- app/Form/Mails/ConfirmRegistrationMail.php | 10 +++++-- app/Lib/Editor/ConditionResolver.php | 17 +++++++++-- database/factories/Traits/FakesMedia.php | 5 ++-- tests/Feature/Form/FormRegisterMailTest.php | 32 +++++++++++++++++++++ 5 files changed, 58 insertions(+), 11 deletions(-) diff --git a/app/Form/Editor/FormConditionResolver.php b/app/Form/Editor/FormConditionResolver.php index 76ce94eb..b87dc110 100644 --- a/app/Form/Editor/FormConditionResolver.php +++ b/app/Form/Editor/FormConditionResolver.php @@ -20,11 +20,8 @@ class FormConditionResolver extends ConditionResolver /** * @inheritdoc */ - public function filterBlock(array $block): bool + public function filterCondition($mode, $ifs): bool { - $mode = data_get($block, 'tunes.condition.mode', 'any'); - $ifs = data_get($block, 'tunes.condition.ifs', []); - if (count($ifs) === 0) { return true; } diff --git a/app/Form/Mails/ConfirmRegistrationMail.php b/app/Form/Mails/ConfirmRegistrationMail.php index 208d3169..49f89799 100644 --- a/app/Form/Mails/ConfirmRegistrationMail.php +++ b/app/Form/Mails/ConfirmRegistrationMail.php @@ -33,8 +33,8 @@ class ConfirmRegistrationMail extends Mailable $conditionResolver = app(FormConditionResolver::class)->forParticipant($participant); $this->fullname = $participant->getFields()->getFullname(); $this->config = $participant->getConfig(); - $this->topText = $conditionResolver->make($participant->form->mail_top); - $this->bottomText = $conditionResolver->make($participant->form->mail_bottom); + $this->topText = $conditionResolver->makeBlocks($participant->form->mail_top); + $this->bottomText = $conditionResolver->makeBlocks($participant->form->mail_bottom); } /** @@ -68,7 +68,13 @@ class ConfirmRegistrationMail extends Mailable */ public function attachments() { + $conditionResolver = app(FormConditionResolver::class)->forParticipant($this->participant); + return $this->participant->form->getMedia('mailattachments') + ->filter(fn ($media) => $conditionResolver->filterCondition( + data_get($media->getCustomProperty('conditions'), 'mode', 'all'), + data_get($media->getCustomProperty('conditions'), 'ifs', []), + )) ->map(fn ($media) => Attachment::fromStorageDisk($media->disk, $media->getPathRelativeToRoot())) ->all(); } diff --git a/app/Lib/Editor/ConditionResolver.php b/app/Lib/Editor/ConditionResolver.php index 171a6a7f..b80bd981 100644 --- a/app/Lib/Editor/ConditionResolver.php +++ b/app/Lib/Editor/ConditionResolver.php @@ -6,16 +6,27 @@ abstract class ConditionResolver { /** - * @param array $block + * @param array $ifs */ - abstract public function filterBlock(array $block): bool; + abstract public function filterCondition(string $mode, array $ifs): bool; /** * @param array $content * @return array */ - public function make(array $content): array + public function makeBlocks(array $content): array { return array_filter(data_get($content, 'blocks', []), fn ($block) => $this->filterBlock($block)); } + + /** + * @inheritdoc + */ + public function filterBlock(array $block): bool + { + $mode = data_get($block, 'tunes.condition.mode', 'any'); + $ifs = data_get($block, 'tunes.condition.ifs', []); + + return $this->filterCondition($mode, $ifs); + } } diff --git a/database/factories/Traits/FakesMedia.php b/database/factories/Traits/FakesMedia.php index 3027302b..50a1e5f2 100644 --- a/database/factories/Traits/FakesMedia.php +++ b/database/factories/Traits/FakesMedia.php @@ -22,9 +22,9 @@ trait FakesMedia }); } - public function withDocument(string $collection, string $filename, string $content = ''): self + public function withDocument(string $collection, string $filename, string $content = '', array $properties = []): self { - return $this->afterCreating(function (HasMedia $model) use ($filename, $collection, $content) { + return $this->afterCreating(function (HasMedia $model) use ($filename, $collection, $content, $properties) { $pathinfo = pathinfo($filename); UploadedFile::fake()->create($filename, $content, 'application/pdf')->storeAs('media-library', $filename, 'temp'); @@ -32,6 +32,7 @@ trait FakesMedia $model->addMediaFromDisk('media-library/' . $filename, 'temp') ->usingName($pathinfo['filename']) ->usingFileName($pathinfo['basename']) + ->withCustomProperties($properties) ->toMediaCollection($collection); }); } diff --git a/tests/Feature/Form/FormRegisterMailTest.php b/tests/Feature/Form/FormRegisterMailTest.php index 9d7b45e3..c655112b 100644 --- a/tests/Feature/Form/FormRegisterMailTest.php +++ b/tests/Feature/Form/FormRegisterMailTest.php @@ -214,6 +214,8 @@ class FormRegisterMailTest extends FormTestCase /** * @dataProvider blockDataProvider + * @param array $conditions + * @param array $participantValues */ public function testItFiltersForBlockConditions(array $conditions, FormtemplateFieldRequest $field, array $participantValues, bool $result): void { @@ -256,4 +258,34 @@ class FormRegisterMailTest extends FormTestCase $mail = new ConfirmRegistrationMail($participant); $mail->assertSeeInText('Max'); } + + /** + * @dataProvider blockDataProvider + * @param array $conditions + * @param array $participantValues + */ + public function testItFiltersForAttachments(array $conditions, FormtemplateFieldRequest $field, array $participantValues, bool $result): void + { + $this->login()->loginNami()->withoutExceptionHandling(); + + $participant = Participant::factory()->for( + Form::factory() + ->fields([ + $field, + $this->textField('firstname')->specialType(SpecialType::FIRSTNAME), + $this->textField('lastname')->specialType(SpecialType::LASTNAME), + ]) + ->withDocument('mailattachments', 'beispiel.pdf', 'content', ['conditions' => $conditions]) + ) + ->data(['firstname' => 'Max', 'lastname' => 'Muster', ...$participantValues]) + ->create(); + + $mail = new ConfirmRegistrationMail($participant); + $mail->assertSeeInHtml('Daten'); + if ($result) { + $this->assertTrue($mail->hasAttachedData('content', 'beispiel.pdf', ['mime' => 'application/pdf'])); + } else { + $this->assertFalse($mail->hasAttachedData('content', 'beispiel.pdf', ['mime' => 'application/pdf'])); + } + } }