Add filter for attachments
continuous-integration/drone/push Build is failing Details

This commit is contained in:
philipp lang 2024-04-23 23:48:09 +02:00
parent 1328f359b2
commit 0437511bf1
5 changed files with 58 additions and 11 deletions

View File

@ -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;
}

View File

@ -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();
}

View File

@ -6,16 +6,27 @@ abstract class ConditionResolver
{
/**
* @param array<string, mixed> $block
* @param array<string, mixed> $ifs
*/
abstract public function filterBlock(array $block): bool;
abstract public function filterCondition(string $mode, array $ifs): bool;
/**
* @param array<string, mixed> $content
* @return array<string, mixed>
*/
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);
}
}

View File

@ -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);
});
}

View File

@ -214,6 +214,8 @@ class FormRegisterMailTest extends FormTestCase
/**
* @dataProvider blockDataProvider
* @param array<string, mixed> $conditions
* @param array<string, mixed> $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<string, mixed> $conditions
* @param array<string, mixed> $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']));
}
}
}