Skip to content

Commit

Permalink
Bugfix - Null Campaign Content (#34)
Browse files Browse the repository at this point in the history
* wip

* wip
  • Loading branch information
joshuafranks authored Jul 10, 2020
1 parent b254805 commit 1ad6054
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/Http/Requests/CampaignStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Sendportal\Base\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class CampaignStoreRequest extends FormRequest
{
Expand Down Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/Services/Content/MergeContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
37 changes: 34 additions & 3 deletions tests/Feature/Campaigns/CampaignControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,19 +204,50 @@ 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,
'from_email' => $this->faker->safeEmail,
'email_service_id' => $emailService->id,
'template_id' => $template->id,
'content' => $this->faker->paragraph
];
], $overrides);
}
}
18 changes: 16 additions & 2 deletions tests/Feature/Content/MergeContentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '<p>Hello this is some {{content}}</p>');

$mergedContent = $this->mergeContent($message);

$expectedHtml = '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>Hello this is some </p></body></html>';

$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' => '<p>{{content}}</p>',
'content' => $templateContent ?? '<p>{{content}}</p>',
'workspace_id' => $workspace->id
]);

Expand Down

0 comments on commit 1ad6054

Please # to comment.