Files
laravel-manager/tests/Feature/StatisticsTest.php

132 lines
5.8 KiB
PHP

<?php
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Route;
use Illuminate\Testing\TestResponse;
use IriksIT\Manager\Models\PageView;
use IriksIT\Manager\Models\Post;
use IriksIT\Manager\Models\Visit;
use IriksIT\Manager\Models\VisitorSalt;
use IriksIT\Manager\Modules\Statistics\CountedPages;
use IriksIT\Manager\Tests\StatisticsOnTestCase;
uses(StatisticsOnTestCase::class);
const BROWSER = 'Mozilla/5.0 (X11; Linux x86_64; rv:130.0) Gecko/20100101 Firefox/130.0';
function pulse(array $payload = [], array $headers = []): TestResponse
{
return test()->withHeaders(['User-Agent' => BROWSER, ...$headers])->postJson('/pulse/view', ['path' => '/', ...$payload]);
}
describe('counting a page view', function () {
it('counts a page of the list, with how the visitor arrived, and keeps no address', function () {
$token = pulse(['referrer' => 'https://www.google.com/search?q=x'], ['CF-IPCountry' => 'NL'])->assertOk()->json('token');
$visit = Visit::sole();
expect($token)->toBeString()
->and($visit->entry_path)->toBe('/')
->and($visit->channel)->toBe('search')
->and($visit->country)->toBe('NL')
->and($visit->browser)->toBe('Firefox')
->and($visit->page_views)->toBe(1)
->and(PageView::sole()->token)->toBe($token);
expect(json_encode(DB::table('manager_visits')->get()))->not->toContain('127.0.0.1')->not->toContain('Firefox/130');
});
it('puts the next page in the same visit', function () {
pulse();
pulse(['path' => '/about']);
expect(Visit::count())->toBe(1)->and(Visit::sole()->page_views)->toBe(2)->and(Visit::sole()->exit_path)->toBe('/about');
});
it('ignores an address that is not a page, a program, and a browser that asks not to be followed', function () {
expect(pulse(['path' => '/made-up'])->json('token'))->toBeNull();
expect(pulse([], ['User-Agent' => 'Googlebot/2.1'])->json('token'))->toBeNull();
expect(pulse([], ['DNT' => '1'])->json('token'))->toBeNull();
expect(pulse([], ['Sec-GPC' => '1'])->json('token'))->toBeNull();
expect(Visit::count())->toBe(0);
});
it('counts a published post under the posts path, and not a draft', function () {
Post::create(['title' => 'Live', 'slug' => 'live', 'body' => 'x', 'published_at' => now()->subDay()]);
Post::create(['title' => 'Draft', 'slug' => 'draft', 'body' => 'x']);
expect(pulse(['path' => '/blog/live'])->json('token'))->toBeString();
expect(pulse(['path' => '/blog/draft'])->json('token'))->toBeNull();
expect(PageView::sole()->post_id)->toBe(Post::where('slug', 'live')->value('id'));
});
it('asks the resolvers of the site for the rest', function () {
app(CountedPages::class)->register(fn (string $path) => $path === '/shop/1' ? ['path' => $path, 'post_id' => null] : null);
expect(pulse(['path' => '/shop/1'])->json('token'))->toBeString();
expect(pulse(['path' => '/shop/2'])->json('token'))->toBeNull();
});
it('records the time on a page, never longer than has passed', function () {
$token = pulse()->json('token');
$this->travel(20)->seconds();
$this->postJson('/pulse/leave', ['token' => $token, 'seconds' => 9999])->assertNoContent();
expect(PageView::sole()->seconds)->toBeLessThanOrEqual(30)->and(Visit::sole()->seconds)->toBe(PageView::sole()->seconds);
});
it('makes a new visitor code every day and forgets the old keys', function () {
pulse();
$this->travel(1)->days();
pulse();
expect(Visit::distinct()->count('visitor_hash'))->toBe(2)->and(VisitorSalt::count())->toBe(1);
$this->artisan('manager:forget-visitor-keys')->assertSuccessful();
});
});
describe('programs', function () {
it('counts a page that was served to a program, per day, and nothing about the program', function () {
Route::middleware('web')->get('/robots-test', fn () => 'ok')->name('robots-test');
$this->withHeaders(['User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'])->get('/robots-test')->assertOk();
$this->withHeaders(['User-Agent' => 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'])->get('/robots-test')->assertOk();
$this->withHeaders(['User-Agent' => BROWSER])->get('/robots-test')->assertOk();
$row = DB::table('manager_bot_hits')->sole();
expect($row->hits)->toBe(2)->and($row->kind)->toBe('search')->and($row->path)->toBe('/robots-test');
});
});
describe('the report', function () {
it('needs statistics.view', function () {
$this->actingAs(makeUser());
$this->getJson('/manager/statistics')->assertForbidden();
$this->get('/manager/statistics/export?sheet=days')->assertForbidden();
});
it('shows the totals, pages and channels of the period', function () {
pulse(['referrer' => 'https://www.google.com/']);
pulse(['path' => '/about']);
$this->actingAs(makeUser(['statistics.view']));
$this->getJson('/manager/statistics?range=today')->assertOk()
->assertJsonPath('totals.visits', 1)
->assertJsonPath('totals.page_views', 2)
->assertJsonCount(2, 'pages')
->assertJsonPath('channels.0.name', 'search');
});
it('downloads a sheet as CSV', function () {
app(CountedPages::class)->register(fn (string $path) => $path === '/=1+1' ? ['path' => $path, 'post_id' => null] : null);
pulse(['path' => '/=1+1']);
$this->actingAs(makeUser(['statistics.view']));
$response = $this->get('/manager/statistics/export?sheet=pages&range=today')->assertOk();
expect($response->headers->get('Content-Type'))->toContain('text/csv')
->and($response->streamedContent())->toContain('/=1+1,,1,');
});
});