Skip to content

Commit

Permalink
Merge pull request #87 from mettle/bugfix/invalid-message
Browse files Browse the repository at this point in the history
[BUGFIX] Invalid message issue
  • Loading branch information
JonoB authored Oct 15, 2020
2 parents 78c15a0 + 7c85351 commit f800697
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .php_cs.cache

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions src/Services/Messages/DispatchMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Sendportal\Base\Services\Messages;

use Exception;
use Illuminate\Database\Query\JoinClause;
use Illuminate\Support\Facades\Log;
use Sendportal\Base\Models\Campaign;
use Sendportal\Base\Models\CampaignStatus;
Expand Down Expand Up @@ -105,19 +104,20 @@ protected function markSent(Message $message, string $messageId): Message

protected function isValidMessage(Message $message): bool
{
$data = $message->newQuery()
->toBase()
->select(['messages.sent_at', 'campaigns.status_id'])
->leftJoin('campaigns', static function (JoinClause $join) {
$join->on('messages.source_id', '=', 'campaigns.id')
->where('messages.source_type', Campaign::class);
})
->first();

if (! $data) {
if ($message->sent_at) {
return false;
}

return !(bool)$data->sent_at && $data->status_id !== CampaignStatus::STATUS_CANCELLED;
if (! $message->isCampaign()) {
return true;
}

$campaign = Campaign::find($message->source_id);

if (! $campaign) {
return false;
}

return $campaign->status_id !== CampaignStatus::STATUS_CANCELLED;
}
}
34 changes: 34 additions & 0 deletions tests/Feature/Messages/MessagesControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,38 @@ public function a_sent_message_cannot_be_deleted()

$this->assertDatabaseHas('messages', ['id' => $message->id]);
}

/**
* @test
* https://github.com/mettle/sendportal/issues/90
*/
public function a_message_can_be_sent_when_other_messages_have_been_sent()
{
[$workspace, $user] = $this->createUserAndWorkspace();

$campaign = factory(Campaign::class)->state('withContent')->create(['workspace_id' => $workspace->id]);

// Message already sent
factory(Message::class)->create([
'workspace_id' => $workspace->id,
'source_id' => $campaign->id,
'sent_at' => now()
]);

/** @var Message $message */
$draftMessage = factory(Message::class)->create([
'workspace_id' => $workspace->id,
'source_id' => $campaign->id,
'queued_at' => now(),
]);

$this->actingAs($user)
->post(route('sendportal.messages.send'), ['id' => $draftMessage->id])
->assertRedirect(route('sendportal.messages.draft'))
->assertSessionHas('success');

$draftMessage->refresh();

$this->assertNotNull($draftMessage->sent_at);
}
}
17 changes: 17 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Tests;

use Collective\Html\FormFacade;
use Illuminate\Support\Str;
use Laravel\Ui\UiServiceProvider;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;
use Sendportal\Base\Models\User;
use Sendportal\Base\SendportalBaseServiceProvider;
use Sendportal\Base\Services\Messages\RelayMessage;

abstract class TestCase extends BaseTestCase
{
Expand Down Expand Up @@ -36,6 +38,7 @@ protected function setUp(): void
$this->withoutMix();
$this->withExceptionHandling();
$this->withFactories(__DIR__ . '/../database/factories');
$this->mockRelayMessageService();

$this->artisan('migrate')->run();
}
Expand All @@ -61,4 +64,18 @@ protected function getPackageAliases($app)
'Form' => FormFacade::class
];
}

/**
* @return void
*/
protected function mockRelayMessageService()
{
$service = $this->getMockBuilder(RelayMessage::class)
->disableOriginalConstructor()
->getMock();

$service->method('handle')->willReturn(Str::random());

app()->instance(RelayMessage::class, $service);
}
}

0 comments on commit f800697

Please sign in to comment.