Add Campaigns module for grouping short links.
Deploy Ladill Link / deploy (push) Successful in 1m26s
Deploy Ladill Link / deploy (push) Successful in 1m26s
Let accounts organise links by promo or launch, attach or detach links, and view combined click totals from the sidebar.
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Http\Middleware\EnsurePlatformSession;
|
||||
use App\Models\Campaign;
|
||||
use App\Models\ShortLink;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CampaignTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->withoutMiddleware(EnsurePlatformSession::class);
|
||||
if (method_exists($this, 'withoutVite')) {
|
||||
$this->withoutVite();
|
||||
}
|
||||
}
|
||||
|
||||
private function user(): User
|
||||
{
|
||||
return User::create([
|
||||
'public_id' => (string) Str::uuid(),
|
||||
'name' => 'Owner',
|
||||
'email' => 'owner+'.uniqid().'@example.com',
|
||||
]);
|
||||
}
|
||||
|
||||
private function link(User $user, array $attrs = []): ShortLink
|
||||
{
|
||||
return ShortLink::create(array_merge([
|
||||
'user_id' => $user->id,
|
||||
'slug' => strtolower(Str::random(8)),
|
||||
'source_app' => 'link',
|
||||
'source_kind' => 'redirect',
|
||||
'is_managed_here' => true,
|
||||
'destination_url' => 'https://example.com',
|
||||
'is_active' => true,
|
||||
'clicks_total' => 12,
|
||||
'unique_clicks_total' => 7,
|
||||
], $attrs));
|
||||
}
|
||||
|
||||
public function test_owner_can_create_campaign_and_attach_links(): void
|
||||
{
|
||||
$user = $this->user();
|
||||
$link = $this->link($user);
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('link.campaigns.store'), [
|
||||
'name' => 'Launch week',
|
||||
'description' => 'Homepage CTAs',
|
||||
'status' => Campaign::STATUS_ACTIVE,
|
||||
])
|
||||
->assertRedirect();
|
||||
|
||||
$campaign = Campaign::first();
|
||||
$this->assertNotNull($campaign);
|
||||
$this->assertSame('Launch week', $campaign->name);
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('link.campaigns.attach', $campaign), [
|
||||
'short_link_ids' => [$link->id],
|
||||
])
|
||||
->assertRedirect()
|
||||
->assertSessionHas('success');
|
||||
|
||||
$this->assertSame($campaign->id, $link->fresh()->campaign_id);
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('link.campaigns.show', $campaign))
|
||||
->assertOk()
|
||||
->assertSee('Launch week')
|
||||
->assertSee($link->slug);
|
||||
}
|
||||
|
||||
public function test_owner_can_detach_and_delete_campaign(): void
|
||||
{
|
||||
$user = $this->user();
|
||||
$campaign = Campaign::create([
|
||||
'user_id' => $user->id,
|
||||
'name' => 'Temp',
|
||||
'status' => Campaign::STATUS_ACTIVE,
|
||||
]);
|
||||
$link = $this->link($user, ['campaign_id' => $campaign->id]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->delete(route('link.campaigns.detach', [$campaign, $link]))
|
||||
->assertRedirect();
|
||||
|
||||
$this->assertNull($link->fresh()->campaign_id);
|
||||
|
||||
$link->update(['campaign_id' => $campaign->id]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->delete(route('link.campaigns.destroy', $campaign))
|
||||
->assertRedirect(route('link.campaigns.index'));
|
||||
|
||||
$this->assertDatabaseMissing('campaigns', ['id' => $campaign->id]);
|
||||
$this->assertNull($link->fresh()->campaign_id);
|
||||
}
|
||||
|
||||
public function test_index_lists_campaigns(): void
|
||||
{
|
||||
$user = $this->user();
|
||||
Campaign::create([
|
||||
'user_id' => $user->id,
|
||||
'name' => 'Visible campaign',
|
||||
'status' => Campaign::STATUS_ACTIVE,
|
||||
]);
|
||||
|
||||
$this->actingAs($user)
|
||||
->get(route('link.campaigns.index'))
|
||||
->assertOk()
|
||||
->assertSee('Visible campaign')
|
||||
->assertSee('New campaign');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user