diff --git a/src/Http/Requests/CampaignStoreRequest.php b/src/Http/Requests/CampaignStoreRequest.php index 5782a9d5..f88446fe 100644 --- a/src/Http/Requests/CampaignStoreRequest.php +++ b/src/Http/Requests/CampaignStoreRequest.php @@ -5,6 +5,7 @@ namespace Sendportal\Base\Http\Requests; use Illuminate\Foundation\Http\FormRequest; +use Illuminate\Validation\Rule; class CampaignStoreRequest extends FormRequest { @@ -38,14 +39,12 @@ public function rules(): array 'exists:templates,id', ], 'content' => [ - 'nullable', + Rule::requiredIf($this->template_id === null), ], - 'is_open_tracking' => [ 'boolean', 'nullable' ], - 'is_click_tracking' => [ 'boolean', 'nullable' diff --git a/src/Services/Content/MergeContent.php b/src/Services/Content/MergeContent.php index b1e842db..764afd72 100644 --- a/src/Services/Content/MergeContent.php +++ b/src/Services/Content/MergeContent.php @@ -91,7 +91,7 @@ protected function mergeAutomationContent(Message $message): string return $this->mergeContent($content, $template->content); } - protected function mergeContent(string $customContent, string $templateContent): string + protected function mergeContent(?string $customContent, string $templateContent): string { return str_ireplace(['{{content}}', '{{ content }}'], $customContent, $templateContent); } diff --git a/tests/Feature/Campaigns/CampaignControllerTest.php b/tests/Feature/Campaigns/CampaignControllerTest.php index 03300275..844fca36 100644 --- a/tests/Feature/Campaigns/CampaignControllerTest.php +++ b/tests/Feature/Campaigns/CampaignControllerTest.php @@ -204,12 +204,43 @@ function campaigns_can_be_set_to_track_clicks() ]); } - private function generateCampaignStoreData(Workspace $workspace): array + /** @test */ + function campaign_content_is_required_if_no_template_is_selected() + { + [$workspace, $user] = $this->createUserAndWorkspace(); + + $campaignStoreData = $this->generateCampaignStoreData($workspace, [ + 'template_id' => null, + 'content' => null, + ]); + + $response = $this->actingAs($user) + ->post(route('sendportal.campaigns.store'), $campaignStoreData); + + $response->assertSessionHasErrors('content'); + } + + /** @test */ + function campaign_content_is_not_required_if_a_template_is_selected() + { + [$workspace, $user] = $this->createUserAndWorkspace(); + + $campaignStoreData = $this->generateCampaignStoreData($workspace, [ + 'content' => null, + ]); + + $response = $this->actingAs($user) + ->post(route('sendportal.campaigns.store'), $campaignStoreData); + + $response->assertSessionHasNoErrors(); + } + + private function generateCampaignStoreData(Workspace $workspace, array $overrides = []): array { $emailService = factory(EmailService::class)->create(['workspace_id' => $workspace->id]); $template = factory(Template::class)->create(['workspace_id' => $workspace->id]); - return [ + return array_merge([ 'name' => $this->faker->word, 'subject' => $this->faker->sentence, 'from_name' => $this->faker->name, @@ -217,6 +248,6 @@ private function generateCampaignStoreData(Workspace $workspace): array 'email_service_id' => $emailService->id, 'template_id' => $template->id, 'content' => $this->faker->paragraph - ]; + ], $overrides); } } diff --git a/tests/Feature/Content/MergeContentTest.php b/tests/Feature/Content/MergeContentTest.php index 143fd168..01a3471e 100644 --- a/tests/Feature/Content/MergeContentTest.php +++ b/tests/Feature/Content/MergeContentTest.php @@ -36,14 +36,28 @@ function campaign_content_can_be_merged() $this->assertEquals($expectedHtml, $mergedContent); } - private function generateCampaignMessage(string $campaignContent): Message + /** @test */ + function it_can_handle_a_null_value_for_campaign_content() + { + $content = null; + $message = $this->generateCampaignMessage(null, '

Hello this is some {{content}}

'); + + $mergedContent = $this->mergeContent($message); + + $expectedHtml = ' +

Hello this is some

'; + + $this->assertEquals($expectedHtml, $mergedContent); + } + + private function generateCampaignMessage(?string $campaignContent, ?string $templateContent = null): Message { /** @var Workspace $workspace */ $workspace = factory(Workspace::class)->create(); /** @var Template $template */ $template = factory(Template::class)->create([ - 'content' => '

{{content}}

', + 'content' => $templateContent ?? '

{{content}}

', 'workspace_id' => $workspace->id ]);