first commit from hostslim
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AuthenticationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_login_screen_can_be_rendered(): void
|
||||
{
|
||||
$response = $this->get('/login');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function test_users_can_authenticate_using_the_login_screen(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->post('/login', [
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
$this->assertAuthenticated();
|
||||
$response->assertRedirect(route('dashboard', absolute: false));
|
||||
}
|
||||
|
||||
public function test_users_can_not_authenticate_with_invalid_password(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post('/login', [
|
||||
'email' => $user->email,
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
|
||||
$this->assertGuest();
|
||||
}
|
||||
|
||||
public function test_users_can_logout(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->post('/logout');
|
||||
|
||||
$this->assertGuest();
|
||||
$response->assertRedirect('/');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Events\Verified;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\Facades\URL;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EmailVerificationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_email_verification_screen_can_be_rendered(): void
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get('/verify-email');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function test_email_can_be_verified(): void
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
Event::fake();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1($user->email)]
|
||||
);
|
||||
|
||||
$response = $this->actingAs($user)->get($verificationUrl);
|
||||
|
||||
Event::assertDispatched(Verified::class);
|
||||
$this->assertTrue($user->fresh()->hasVerifiedEmail());
|
||||
$response->assertRedirect(route('dashboard', absolute: false).'?verified=1');
|
||||
}
|
||||
|
||||
public function test_email_is_not_verified_with_invalid_hash(): void
|
||||
{
|
||||
$user = User::factory()->unverified()->create();
|
||||
|
||||
$verificationUrl = URL::temporarySignedRoute(
|
||||
'verification.verify',
|
||||
now()->addMinutes(60),
|
||||
['id' => $user->id, 'hash' => sha1('wrong-email')]
|
||||
);
|
||||
|
||||
$this->actingAs($user)->get($verificationUrl);
|
||||
|
||||
$this->assertFalse($user->fresh()->hasVerifiedEmail());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PasswordConfirmationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_confirm_password_screen_can_be_rendered(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->get('/confirm-password');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function test_password_can_be_confirmed(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->post('/confirm-password', [
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
$response->assertRedirect();
|
||||
$response->assertSessionHasNoErrors();
|
||||
}
|
||||
|
||||
public function test_password_is_not_confirmed_with_invalid_password(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this->actingAs($user)->post('/confirm-password', [
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Auth\Notifications\ResetPassword;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Notification;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PasswordResetTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_reset_password_link_screen_can_be_rendered(): void
|
||||
{
|
||||
$response = $this->get('/forgot-password');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function test_reset_password_link_can_be_requested(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post('/forgot-password', ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class);
|
||||
}
|
||||
|
||||
public function test_reset_password_screen_can_be_rendered(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post('/forgot-password', ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class, function ($notification) {
|
||||
$response = $this->get('/reset-password/'.$notification->token);
|
||||
|
||||
$response->assertStatus(200);
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
public function test_password_can_be_reset_with_valid_token(): void
|
||||
{
|
||||
Notification::fake();
|
||||
|
||||
$user = User::factory()->create();
|
||||
|
||||
$this->post('/forgot-password', ['email' => $user->email]);
|
||||
|
||||
Notification::assertSentTo($user, ResetPassword::class, function ($notification) use ($user) {
|
||||
$response = $this->post('/reset-password', [
|
||||
'token' => $notification->token,
|
||||
'email' => $user->email,
|
||||
'password' => 'password',
|
||||
'password_confirmation' => 'password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect(route('login'));
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PasswordUpdateTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_password_can_be_updated(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->from('/profile')
|
||||
->put('/password', [
|
||||
'current_password' => 'password',
|
||||
'password' => 'new-password',
|
||||
'password_confirmation' => 'new-password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect('/profile');
|
||||
|
||||
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
|
||||
}
|
||||
|
||||
public function test_correct_password_must_be_provided_to_update_password(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->from('/profile')
|
||||
->put('/password', [
|
||||
'current_password' => 'wrong-password',
|
||||
'password' => 'new-password',
|
||||
'password_confirmation' => 'new-password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasErrorsIn('updatePassword', 'current_password')
|
||||
->assertRedirect('/profile');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Auth;
|
||||
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class RegistrationTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_registration_screen_can_be_rendered(): void
|
||||
{
|
||||
$response = $this->get('/register');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
|
||||
public function test_new_users_can_register(): void
|
||||
{
|
||||
$response = $this->post('/register', [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
'password' => 'password',
|
||||
'password_confirmation' => 'password',
|
||||
]);
|
||||
|
||||
$this->assertAuthenticated();
|
||||
$response->assertRedirect(route('dashboard', absolute: false));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,182 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Jobs\ProcessTrackingPayload;
|
||||
use App\Models\Website;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Bus;
|
||||
use Tests\TestCase;
|
||||
|
||||
class CollectControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Website $website;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->website = Website::factory()->create(['domain' => 'example.com']);
|
||||
}
|
||||
|
||||
private function validPayload(array $overrides = []): array
|
||||
{
|
||||
return array_merge([
|
||||
'tid' => $this->website->tracking_id,
|
||||
'vid' => 'visitor-abc-123',
|
||||
'sid' => 'session-abc-123',
|
||||
'type' => 'pageview',
|
||||
'url' => 'https://example.com/hello',
|
||||
'path' => '/hello',
|
||||
'title' => 'Hello Page',
|
||||
], $overrides);
|
||||
}
|
||||
|
||||
// -- Store endpoint --
|
||||
|
||||
public function test_valid_pageview_payload_dispatches_job(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->postJson('/api/collect', $this->validPayload());
|
||||
|
||||
$response->assertOk()->assertJson(['ok' => true]);
|
||||
Bus::assertDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
public function test_valid_event_payload_dispatches_job(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->postJson('/api/collect', $this->validPayload([
|
||||
'type' => 'event',
|
||||
'event_name' => 'signup',
|
||||
'event_category' => 'conversion',
|
||||
]));
|
||||
|
||||
$response->assertOk()->assertJson(['ok' => true]);
|
||||
Bus::assertDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
public function test_missing_required_fields_returns_validation_error(): void
|
||||
{
|
||||
$response = $this->postJson('/api/collect', []);
|
||||
|
||||
$response->assertUnprocessable();
|
||||
$response->assertJsonValidationErrors(['tid', 'vid', 'sid', 'type']);
|
||||
}
|
||||
|
||||
public function test_invalid_type_returns_validation_error(): void
|
||||
{
|
||||
$response = $this->postJson('/api/collect', $this->validPayload([
|
||||
'type' => 'invalid_type',
|
||||
]));
|
||||
|
||||
$response->assertUnprocessable();
|
||||
$response->assertJsonValidationErrors(['type']);
|
||||
}
|
||||
|
||||
public function test_domain_mismatch_returns_error(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->postJson('/api/collect', $this->validPayload([
|
||||
'url' => 'https://evil-site.com/phishing',
|
||||
]));
|
||||
|
||||
$response->assertStatus(400);
|
||||
$response->assertJson(['ok' => false, 'reason' => 'domain_mismatch']);
|
||||
Bus::assertNotDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
public function test_subdomain_is_allowed(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->postJson('/api/collect', $this->validPayload([
|
||||
'url' => 'https://www.example.com/page',
|
||||
]));
|
||||
|
||||
$response->assertOk()->assertJson(['ok' => true]);
|
||||
Bus::assertDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
public function test_unknown_tracking_id_still_succeeds(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->postJson('/api/collect', $this->validPayload([
|
||||
'tid' => 'TA-NONEXISTENT',
|
||||
'url' => null,
|
||||
]));
|
||||
|
||||
$response->assertOk()->assertJson(['ok' => true]);
|
||||
Bus::assertDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
public function test_bot_requests_are_rejected(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->postJson('/api/collect', $this->validPayload(), [
|
||||
'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
|
||||
]);
|
||||
|
||||
$response->assertOk()->assertJson(['ok' => false, 'reason' => 'bot']);
|
||||
Bus::assertNotDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
public function test_payload_with_utm_parameters(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->postJson('/api/collect', $this->validPayload([
|
||||
'utm_source' => 'google',
|
||||
'utm_medium' => 'cpc',
|
||||
'utm_campaign' => 'spring_sale',
|
||||
'utm_term' => 'shoes',
|
||||
'utm_content' => 'ad_v1',
|
||||
]));
|
||||
|
||||
$response->assertOk()->assertJson(['ok' => true]);
|
||||
Bus::assertDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
// -- Pixel endpoint --
|
||||
|
||||
public function test_pixel_returns_gif(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->get('/api/pixel?tid='.$this->website->tracking_id);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertHeader('Content-Type', 'image/gif');
|
||||
Bus::assertDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
public function test_pixel_without_tid_returns_gif_without_dispatching(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->get('/api/pixel');
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertHeader('Content-Type', 'image/gif');
|
||||
Bus::assertNotDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
|
||||
public function test_pixel_rejects_bots(): void
|
||||
{
|
||||
Bus::fake();
|
||||
|
||||
$response = $this->get('/api/pixel?tid='.$this->website->tracking_id, [
|
||||
'User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
|
||||
]);
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertHeader('Content-Type', 'image/gif');
|
||||
Bus::assertNotDispatched(ProcessTrackingPayload::class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
// use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*/
|
||||
public function test_the_application_returns_a_successful_response(): void
|
||||
{
|
||||
$response = $this->get('/');
|
||||
|
||||
$response->assertStatus(200);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProfileTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_profile_page_is_displayed(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->get('/profile');
|
||||
|
||||
$response->assertOk();
|
||||
}
|
||||
|
||||
public function test_profile_information_can_be_updated(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->patch('/profile', [
|
||||
'name' => 'Test User',
|
||||
'email' => 'test@example.com',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect('/profile');
|
||||
|
||||
$user->refresh();
|
||||
|
||||
$this->assertSame('Test User', $user->name);
|
||||
$this->assertSame('test@example.com', $user->email);
|
||||
$this->assertNull($user->email_verified_at);
|
||||
}
|
||||
|
||||
public function test_email_verification_status_is_unchanged_when_the_email_address_is_unchanged(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->patch('/profile', [
|
||||
'name' => 'Test User',
|
||||
'email' => $user->email,
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect('/profile');
|
||||
|
||||
$this->assertNotNull($user->refresh()->email_verified_at);
|
||||
}
|
||||
|
||||
public function test_user_can_delete_their_account(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->delete('/profile', [
|
||||
'password' => 'password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasNoErrors()
|
||||
->assertRedirect('/');
|
||||
|
||||
$this->assertGuest();
|
||||
$this->assertNull($user->fresh());
|
||||
}
|
||||
|
||||
public function test_correct_password_must_be_provided_to_delete_account(): void
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
$response = $this
|
||||
->actingAs($user)
|
||||
->from('/profile')
|
||||
->delete('/profile', [
|
||||
'password' => 'wrong-password',
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertSessionHasErrorsIn('userDeletion', 'password')
|
||||
->assertRedirect('/profile');
|
||||
|
||||
$this->assertNotNull($user->fresh());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Models\Website;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class WebsiteControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private User $user;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->user = User::factory()->create();
|
||||
}
|
||||
|
||||
// -- Index --
|
||||
|
||||
public function test_guests_cannot_access_website_list(): void
|
||||
{
|
||||
$this->get(route('websites.index'))->assertRedirect(route('login'));
|
||||
}
|
||||
|
||||
public function test_authenticated_user_can_see_their_websites(): void
|
||||
{
|
||||
$website = Website::factory()->for($this->user)->create();
|
||||
Website::factory()->create(); // another user's website
|
||||
|
||||
$response = $this->actingAs($this->user)->get(route('websites.index'));
|
||||
|
||||
$response->assertOk();
|
||||
$response->assertSeeText($website->name);
|
||||
}
|
||||
|
||||
// -- Create / Store --
|
||||
|
||||
public function test_user_can_view_create_form(): void
|
||||
{
|
||||
$this->actingAs($this->user)
|
||||
->get(route('websites.create'))
|
||||
->assertOk();
|
||||
}
|
||||
|
||||
public function test_user_can_create_website(): void
|
||||
{
|
||||
$response = $this->actingAs($this->user)->post(route('websites.store'), [
|
||||
'name' => 'My Site',
|
||||
'domain' => 'mysite.com',
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('websites.index'));
|
||||
$this->assertDatabaseHas('websites', [
|
||||
'user_id' => $this->user->id,
|
||||
'name' => 'My Site',
|
||||
'domain' => 'mysite.com',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_create_website_generates_tracking_id(): void
|
||||
{
|
||||
$this->actingAs($this->user)->post(route('websites.store'), [
|
||||
'name' => 'My Site',
|
||||
'domain' => 'mysite.com',
|
||||
]);
|
||||
|
||||
$website = Website::where('domain', 'mysite.com')->first();
|
||||
$this->assertNotNull($website->tracking_id);
|
||||
$this->assertStringStartsWith('TA-', $website->tracking_id);
|
||||
}
|
||||
|
||||
public function test_create_website_validates_domain_format(): void
|
||||
{
|
||||
$response = $this->actingAs($this->user)->post(route('websites.store'), [
|
||||
'name' => 'My Site',
|
||||
'domain' => 'not a domain!!',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('domain');
|
||||
}
|
||||
|
||||
public function test_create_website_requires_name(): void
|
||||
{
|
||||
$response = $this->actingAs($this->user)->post(route('websites.store'), [
|
||||
'name' => '',
|
||||
'domain' => 'mysite.com',
|
||||
]);
|
||||
|
||||
$response->assertSessionHasErrors('name');
|
||||
}
|
||||
|
||||
// -- Edit / Update --
|
||||
|
||||
public function test_owner_can_edit_website(): void
|
||||
{
|
||||
$website = Website::factory()->for($this->user)->create();
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->get(route('websites.edit', $website))
|
||||
->assertOk();
|
||||
}
|
||||
|
||||
public function test_non_owner_cannot_edit_website(): void
|
||||
{
|
||||
$otherUser = User::factory()->create();
|
||||
$website = Website::factory()->for($otherUser)->create();
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->get(route('websites.edit', $website))
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
public function test_owner_can_update_website(): void
|
||||
{
|
||||
$website = Website::factory()->for($this->user)->create();
|
||||
|
||||
$response = $this->actingAs($this->user)->put(route('websites.update', $website), [
|
||||
'name' => 'Updated Name',
|
||||
'domain' => 'updated.com',
|
||||
]);
|
||||
|
||||
$response->assertRedirect(route('dashboard.show', $website));
|
||||
$this->assertDatabaseHas('websites', [
|
||||
'id' => $website->id,
|
||||
'name' => 'Updated Name',
|
||||
'domain' => 'updated.com',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_non_owner_cannot_update_website(): void
|
||||
{
|
||||
$otherUser = User::factory()->create();
|
||||
$website = Website::factory()->for($otherUser)->create();
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->put(route('websites.update', $website), [
|
||||
'name' => 'Hacked',
|
||||
'domain' => 'hacked.com',
|
||||
])
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
// -- Delete --
|
||||
|
||||
public function test_owner_can_delete_website(): void
|
||||
{
|
||||
$website = Website::factory()->for($this->user)->create();
|
||||
|
||||
$response = $this->actingAs($this->user)
|
||||
->delete(route('websites.destroy', $website));
|
||||
|
||||
$response->assertRedirect(route('websites.index'));
|
||||
$this->assertDatabaseMissing('websites', ['id' => $website->id]);
|
||||
}
|
||||
|
||||
public function test_non_owner_cannot_delete_website(): void
|
||||
{
|
||||
$otherUser = User::factory()->create();
|
||||
$website = Website::factory()->for($otherUser)->create();
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->delete(route('websites.destroy', $website))
|
||||
->assertForbidden();
|
||||
|
||||
$this->assertDatabaseHas('websites', ['id' => $website->id]);
|
||||
}
|
||||
|
||||
// -- Show redirects to dashboard --
|
||||
|
||||
public function test_show_redirects_to_dashboard(): void
|
||||
{
|
||||
$website = Website::factory()->for($this->user)->create();
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->get(route('websites.show', $website))
|
||||
->assertRedirect(route('dashboard.show', $website));
|
||||
}
|
||||
|
||||
// -- Snippet --
|
||||
|
||||
public function test_owner_can_view_snippet(): void
|
||||
{
|
||||
$website = Website::factory()->for($this->user)->create();
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->get(route('websites.snippet', $website))
|
||||
->assertOk()
|
||||
->assertSeeText($website->tracking_id);
|
||||
}
|
||||
|
||||
public function test_non_owner_cannot_view_snippet(): void
|
||||
{
|
||||
$otherUser = User::factory()->create();
|
||||
$website = Website::factory()->for($otherUser)->create();
|
||||
|
||||
$this->actingAs($this->user)
|
||||
->get(route('websites.snippet', $website))
|
||||
->assertForbidden();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Tests;
|
||||
|
||||
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
|
||||
|
||||
abstract class TestCase extends BaseTestCase
|
||||
{
|
||||
//
|
||||
}
|
||||
@@ -0,0 +1,285 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\AnalyticsSession;
|
||||
use App\Models\DailyAggregate;
|
||||
use App\Models\PageView;
|
||||
use App\Models\Visitor;
|
||||
use App\Models\Website;
|
||||
use App\Services\AnalyticsQueryService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCase;
|
||||
|
||||
class AnalyticsQueryServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private Website $website;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->website = Website::factory()->create();
|
||||
}
|
||||
|
||||
private function makeService(?Carbon $from = null, ?Carbon $to = null): AnalyticsQueryService
|
||||
{
|
||||
return AnalyticsQueryService::for(
|
||||
$this->website,
|
||||
$from ?? Carbon::today()->subDays(6),
|
||||
$to ?? Carbon::today(),
|
||||
);
|
||||
}
|
||||
|
||||
private function createVisitorWithPageViews(int $pvCount, ?Carbon $viewedAt = null): void
|
||||
{
|
||||
$visitor = Visitor::factory()->for($this->website)->create();
|
||||
$session = AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor->id,
|
||||
'started_at' => $viewedAt ?? now(),
|
||||
'ended_at' => $viewedAt ?? now(),
|
||||
'duration' => 60,
|
||||
'pages_count' => $pvCount,
|
||||
'is_bounce' => $pvCount <= 1,
|
||||
]);
|
||||
|
||||
for ($i = 0; $i < $pvCount; $i++) {
|
||||
PageView::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor->id,
|
||||
'analytics_session_id' => $session->id,
|
||||
'viewed_at' => $viewedAt ?? now(),
|
||||
'path' => '/page-'.$i,
|
||||
'time_on_page' => 30,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// -- Summary metrics from raw data --
|
||||
|
||||
public function test_total_page_views_counts_correctly(): void
|
||||
{
|
||||
$this->createVisitorWithPageViews(3);
|
||||
$this->createVisitorWithPageViews(2);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$this->assertEquals(5, $service->totalPageViews());
|
||||
}
|
||||
|
||||
public function test_unique_visitors_counts_distinct(): void
|
||||
{
|
||||
$this->createVisitorWithPageViews(2);
|
||||
$this->createVisitorWithPageViews(1);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$this->assertEquals(2, $service->uniqueVisitors());
|
||||
}
|
||||
|
||||
public function test_total_sessions(): void
|
||||
{
|
||||
$this->createVisitorWithPageViews(2);
|
||||
$this->createVisitorWithPageViews(1);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$this->assertEquals(2, $service->totalSessions());
|
||||
}
|
||||
|
||||
public function test_bounce_rate_calculation(): void
|
||||
{
|
||||
// Non-bounce session
|
||||
$this->createVisitorWithPageViews(3);
|
||||
// Bounce session
|
||||
$visitor = Visitor::factory()->for($this->website)->create();
|
||||
AnalyticsSession::factory()->for($this->website)->bounce()->create([
|
||||
'visitor_id' => $visitor->id,
|
||||
'started_at' => now(),
|
||||
]);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$this->assertEquals(50.0, $service->bounceRate());
|
||||
}
|
||||
|
||||
public function test_bounce_rate_zero_when_no_sessions(): void
|
||||
{
|
||||
$service = $this->makeService();
|
||||
$this->assertEquals(0, $service->bounceRate());
|
||||
}
|
||||
|
||||
public function test_avg_session_duration(): void
|
||||
{
|
||||
$visitor1 = Visitor::factory()->for($this->website)->create();
|
||||
AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor1->id,
|
||||
'duration' => 100,
|
||||
'started_at' => now(),
|
||||
]);
|
||||
$visitor2 = Visitor::factory()->for($this->website)->create();
|
||||
AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor2->id,
|
||||
'duration' => 200,
|
||||
'started_at' => now(),
|
||||
]);
|
||||
|
||||
$service = $this->makeService();
|
||||
|
||||
$this->assertEquals(150, $service->avgSessionDuration());
|
||||
}
|
||||
|
||||
public function test_summary_returns_all_keys(): void
|
||||
{
|
||||
$service = $this->makeService();
|
||||
$summary = $service->summary();
|
||||
|
||||
$this->assertArrayHasKey('page_views', $summary);
|
||||
$this->assertArrayHasKey('visitors', $summary);
|
||||
$this->assertArrayHasKey('sessions', $summary);
|
||||
$this->assertArrayHasKey('bounce_rate', $summary);
|
||||
$this->assertArrayHasKey('avg_duration', $summary);
|
||||
$this->assertArrayHasKey('avg_pages', $summary);
|
||||
}
|
||||
|
||||
// -- Date range filtering --
|
||||
|
||||
public function test_only_counts_data_within_date_range(): void
|
||||
{
|
||||
$this->createVisitorWithPageViews(2, Carbon::today());
|
||||
$this->createVisitorWithPageViews(1, Carbon::today()->subDays(30));
|
||||
|
||||
$service = $this->makeService(Carbon::today(), Carbon::today());
|
||||
|
||||
$this->assertEquals(2, $service->totalPageViews());
|
||||
$this->assertEquals(1, $service->uniqueVisitors());
|
||||
}
|
||||
|
||||
// -- Aggregate data --
|
||||
|
||||
public function test_reads_from_aggregate_tables_for_past_days(): void
|
||||
{
|
||||
DailyAggregate::create([
|
||||
'website_id' => $this->website->id,
|
||||
'date' => Carbon::yesterday()->toDateString(),
|
||||
'visitors' => 50,
|
||||
'sessions' => 80,
|
||||
'page_views' => 200,
|
||||
'bounce_count' => 20,
|
||||
'total_duration' => 4000,
|
||||
]);
|
||||
|
||||
$service = $this->makeService(
|
||||
Carbon::yesterday(),
|
||||
Carbon::yesterday(),
|
||||
);
|
||||
|
||||
$this->assertEquals(200, $service->totalPageViews());
|
||||
$this->assertEquals(50, $service->uniqueVisitors());
|
||||
$this->assertEquals(80, $service->totalSessions());
|
||||
}
|
||||
|
||||
// -- Top pages --
|
||||
|
||||
public function test_top_pages_returns_ordered_results(): void
|
||||
{
|
||||
$visitor = Visitor::factory()->for($this->website)->create();
|
||||
$session = AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor->id,
|
||||
'started_at' => now(),
|
||||
]);
|
||||
|
||||
PageView::factory()->for($this->website)->count(3)->create([
|
||||
'visitor_id' => $visitor->id,
|
||||
'analytics_session_id' => $session->id,
|
||||
'path' => '/popular',
|
||||
'viewed_at' => now(),
|
||||
]);
|
||||
PageView::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor->id,
|
||||
'analytics_session_id' => $session->id,
|
||||
'path' => '/less-popular',
|
||||
'viewed_at' => now(),
|
||||
]);
|
||||
|
||||
$service = $this->makeService();
|
||||
$pages = $service->topPages(10);
|
||||
|
||||
$this->assertGreaterThanOrEqual(2, $pages->count());
|
||||
$this->assertEquals('/popular', $pages->first()->path);
|
||||
}
|
||||
|
||||
// -- Visitors over time --
|
||||
|
||||
public function test_visitors_over_time_returns_collection(): void
|
||||
{
|
||||
$this->createVisitorWithPageViews(1);
|
||||
|
||||
$service = $this->makeService();
|
||||
$timeseries = $service->visitorsOverTime();
|
||||
|
||||
$this->assertNotEmpty($timeseries);
|
||||
$first = $timeseries->first();
|
||||
$this->assertArrayHasKey('period', $first);
|
||||
$this->assertArrayHasKey('visitors', $first);
|
||||
$this->assertArrayHasKey('page_views', $first);
|
||||
$this->assertArrayHasKey('sessions', $first);
|
||||
}
|
||||
|
||||
// -- Traffic sources --
|
||||
|
||||
public function test_traffic_sources_groups_by_referrer(): void
|
||||
{
|
||||
$visitor = Visitor::factory()->for($this->website)->create();
|
||||
AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor->id,
|
||||
'referrer_domain' => 'google.com',
|
||||
'started_at' => now(),
|
||||
]);
|
||||
AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor->id,
|
||||
'referrer_domain' => '',
|
||||
'started_at' => now(),
|
||||
]);
|
||||
|
||||
$service = $this->makeService();
|
||||
$sources = $service->trafficSources();
|
||||
|
||||
$this->assertGreaterThanOrEqual(1, $sources->count());
|
||||
}
|
||||
|
||||
// -- Segment filtering --
|
||||
|
||||
public function test_segment_filters_data_by_visitor_ids(): void
|
||||
{
|
||||
$visitor1 = Visitor::factory()->for($this->website)->create(['country' => 'DE']);
|
||||
$visitor2 = Visitor::factory()->for($this->website)->create(['country' => 'US']);
|
||||
$session1 = AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor1->id,
|
||||
'started_at' => now(),
|
||||
]);
|
||||
$session2 = AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $visitor2->id,
|
||||
'started_at' => now(),
|
||||
]);
|
||||
|
||||
PageView::factory()->for($this->website)->count(3)->create([
|
||||
'visitor_id' => $visitor1->id,
|
||||
'analytics_session_id' => $session1->id,
|
||||
'viewed_at' => now(),
|
||||
]);
|
||||
PageView::factory()->for($this->website)->count(2)->create([
|
||||
'visitor_id' => $visitor2->id,
|
||||
'analytics_session_id' => $session2->id,
|
||||
'viewed_at' => now(),
|
||||
]);
|
||||
|
||||
$service = $this->makeService();
|
||||
$service->withSegment(collect([$visitor1->id]));
|
||||
|
||||
$this->assertEquals(3, $service->totalPageViews());
|
||||
$this->assertEquals(1, $service->uniqueVisitors());
|
||||
$this->assertEquals(1, $service->totalSessions());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ExampleTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* A basic test example.
|
||||
*/
|
||||
public function test_that_true_is_true(): void
|
||||
{
|
||||
$this->assertTrue(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Models\AnalyticsSession;
|
||||
use App\Models\Goal;
|
||||
use App\Models\GoalConversion;
|
||||
use App\Models\Visitor;
|
||||
use App\Models\Website;
|
||||
use App\Services\GoalEvaluationService;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCase;
|
||||
|
||||
class GoalEvaluationServiceTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
private GoalEvaluationService $service;
|
||||
|
||||
private Website $website;
|
||||
|
||||
private Visitor $visitor;
|
||||
|
||||
private AnalyticsSession $session;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
$this->service = new GoalEvaluationService;
|
||||
$this->website = Website::factory()->create();
|
||||
$this->visitor = Visitor::factory()->for($this->website)->create();
|
||||
$this->session = AnalyticsSession::factory()->for($this->website)->create([
|
||||
'visitor_id' => $this->visitor->id,
|
||||
'duration' => 120,
|
||||
'pages_count' => 5,
|
||||
]);
|
||||
}
|
||||
|
||||
// -- URL Destination --
|
||||
|
||||
public function test_url_destination_exact_match(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/thank-you',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseHas('goal_conversions', [
|
||||
'goal_id' => $goal->id,
|
||||
'visitor_id' => $this->visitor->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_url_destination_exact_mismatch(): void
|
||||
{
|
||||
Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/other-page',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseCount('goal_conversions', 0);
|
||||
}
|
||||
|
||||
public function test_url_destination_starts_with(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->urlDestination('/checkout', 'starts_with')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/checkout/step-2',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
|
||||
}
|
||||
|
||||
public function test_url_destination_contains(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->urlDestination('success', 'contains')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/order/success/123',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
|
||||
}
|
||||
|
||||
public function test_url_destination_regex(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->urlDestination('/^\\/order\\/\\d+$/', 'regex')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/order/42',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
|
||||
}
|
||||
|
||||
// -- Event Match --
|
||||
|
||||
public function test_event_match_by_name(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->eventMatch('purchase')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'event',
|
||||
'event_name' => 'purchase',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
|
||||
}
|
||||
|
||||
public function test_event_match_wrong_name_does_not_convert(): void
|
||||
{
|
||||
Goal::factory()->for($this->website)->eventMatch('purchase')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'event',
|
||||
'event_name' => 'signup',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseCount('goal_conversions', 0);
|
||||
}
|
||||
|
||||
public function test_event_match_with_category(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->eventMatch('click', 'cta')->create();
|
||||
|
||||
// wrong category
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'event',
|
||||
'event_name' => 'click',
|
||||
'event_category' => 'navigation',
|
||||
], Carbon::now());
|
||||
$this->assertDatabaseCount('goal_conversions', 0);
|
||||
|
||||
// right category
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'event',
|
||||
'event_name' => 'click',
|
||||
'event_category' => 'cta',
|
||||
], Carbon::now());
|
||||
$this->assertDatabaseCount('goal_conversions', 1);
|
||||
}
|
||||
|
||||
public function test_event_match_requires_event_type(): void
|
||||
{
|
||||
Goal::factory()->for($this->website)->eventMatch('purchase')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'event_name' => 'purchase',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseCount('goal_conversions', 0);
|
||||
}
|
||||
|
||||
// -- Duration --
|
||||
|
||||
public function test_duration_goal_converts_when_threshold_met(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->duration(60)->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
|
||||
}
|
||||
|
||||
public function test_duration_goal_does_not_convert_below_threshold(): void
|
||||
{
|
||||
Goal::factory()->for($this->website)->duration(300)->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseCount('goal_conversions', 0);
|
||||
}
|
||||
|
||||
// -- Pages per session --
|
||||
|
||||
public function test_pages_per_session_goal_converts(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->pagesPerSession(3)->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal->id]);
|
||||
}
|
||||
|
||||
public function test_pages_per_session_goal_does_not_convert_below_threshold(): void
|
||||
{
|
||||
Goal::factory()->for($this->website)->pagesPerSession(10)->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseCount('goal_conversions', 0);
|
||||
}
|
||||
|
||||
// -- De-duplication --
|
||||
|
||||
public function test_does_not_duplicate_conversion_within_same_session(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/thank-you',
|
||||
], Carbon::now());
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/thank-you',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseCount('goal_conversions', 1);
|
||||
}
|
||||
|
||||
// -- Inactive goals --
|
||||
|
||||
public function test_inactive_goals_are_skipped(): void
|
||||
{
|
||||
Goal::factory()->for($this->website)->urlDestination('/thank-you')->inactive()->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/thank-you',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseCount('goal_conversions', 0);
|
||||
}
|
||||
|
||||
// -- Conversion value --
|
||||
|
||||
public function test_conversion_stores_goal_default_value(): void
|
||||
{
|
||||
$goal = Goal::factory()->for($this->website)->urlDestination('/buy')->create([
|
||||
'default_value' => 49.99,
|
||||
]);
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/buy',
|
||||
], Carbon::now());
|
||||
|
||||
$conversion = GoalConversion::where('goal_id', $goal->id)->first();
|
||||
$this->assertNotNull($conversion);
|
||||
$this->assertEquals(49.99, (float) $conversion->value);
|
||||
}
|
||||
|
||||
// -- Multiple goals --
|
||||
|
||||
public function test_evaluates_multiple_goals_independently(): void
|
||||
{
|
||||
$goal1 = Goal::factory()->for($this->website)->urlDestination('/thank-you')->create();
|
||||
$goal2 = Goal::factory()->for($this->website)->duration(60)->create();
|
||||
Goal::factory()->for($this->website)->urlDestination('/other-page')->create();
|
||||
|
||||
$this->service->evaluate($this->website, $this->visitor, $this->session, [
|
||||
'type' => 'pageview',
|
||||
'path' => '/thank-you',
|
||||
], Carbon::now());
|
||||
|
||||
$this->assertDatabaseCount('goal_conversions', 2);
|
||||
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal1->id]);
|
||||
$this->assertDatabaseHas('goal_conversions', ['goal_id' => $goal2->id]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user