Skip to content

Commit

Permalink
Don't track 404s, improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Sep 5, 2024
1 parent cfd4796 commit 39d9c16
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/Http/Middleware/CacheTracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ public function handle($request, Closure $next)

$response = $next($request);

if (method_exists($response, 'status') && $response->status() !== 200) {
return $response;
}

if ($response->wasStaticallyCached()) {
return $response;
}

if ($this->content) {
Tracker::add($url, array_unique($this->content));
}
Expand All @@ -82,7 +90,7 @@ private function isEnabled($request)
}

// Only GET requests. This disables the cache during live preview.
return $request->method() === 'GET' && ! Str::startsWith($request->path(), config('statamic.routes.action', '!').'/');
return $request->method() === 'GET' && ! Str::startsWith($request->path(), [config('statamic.routes.action', '!').'/', config('statamic.assets.image_manipulation.route')]);
}

private function setupAdditionalTracking()
Expand Down
2 changes: 2 additions & 0 deletions src/Tracker/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ private function invalidateUrls($urls)

public function remove(string $url)
{
$this->invalidateUrls([$url]);

$this->cacheStore()->forget(md5($url));
}
}
23 changes: 22 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Thoughtco\StatamicCacheTracker\Tests;

use Illuminate\Encryption\Encrypter;
use Statamic\Facades;
use Statamic\Testing\AddonTestCase;
use Statamic\Testing\Concerns\PreventsSavingStacheItemsToDisk;
use Thoughtco\StatamicCacheTracker\ServiceProvider;
Expand All @@ -19,12 +20,32 @@ protected function resolveApplicationConfiguration($app)
{
parent::resolveApplicationConfiguration($app);

$app['config']->set('app.key', 'base64:'.base64_encode(Encrypter::generateKey($app['config']['app.cipher'])));
$app['config']->set('app.key', 'base64:' . base64_encode(Encrypter::generateKey($app['config']['app.cipher'])));

// Assume the pro edition within tests
$app['config']->set('statamic.editions.pro', true);

// enable caching
$app['config']->set('statamic.static_caching.strategy', 'half');

// views for front end routing tests
$app['config']->set('view.paths', [
__DIR__ . '/__fixtures__/resources/views',
]);
}

protected function setUp(): void
{
parent::setUp();

Facades\Collection::make('pages')
->title('Pages')
->routes('{parent_uri}/{slug}')
->save();

Facades\Entry::make()
->id('home')
->collection('pages')
->save();
}
}
4 changes: 2 additions & 2 deletions tests/Unit/AdditionalTrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function tracks_additional_closures()

$this->get('/');

$this->assertSame(['test::tag'], collect(Tracker::all())->firstWhere('url', 'http://localhost/')['tags']);
$this->assertSame(['test::tag', 'pages:home'], collect(Tracker::all())->firstWhere('url', 'http://localhost/')['tags']);
}

#[Test]
Expand All @@ -27,7 +27,7 @@ public function tracks_additional_classes()

$this->get('/');

$this->assertSame(['additional::tag'], collect(Tracker::all())->firstWhere('url', 'http://localhost/')['tags']);
$this->assertSame(['additional::tag', 'pages:home'], collect(Tracker::all())->firstWhere('url', 'http://localhost/')['tags']);
}
}

Expand Down
14 changes: 11 additions & 3 deletions tests/Unit/TrackerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function it_tracks_uncached_pages()

$this->get('/');

$this->assertSame(['test::tag'], collect(Tracker::all())->first()['tags']);
$this->assertSame(['test::tag', 'pages:home'], collect(Tracker::all())->first()['tags']);

Event::assertDispatched(ContentTracked::class, 1);
}
Expand All @@ -37,11 +37,11 @@ public function it_doesnt_track_already_cached_pages()

$this->get('/');

$this->assertSame(['test::tag'], collect(Tracker::all())->first()['tags']);
$this->assertSame(['test::tag', 'pages:home'], collect(Tracker::all())->first()['tags']);

$this->get('/');

$this->assertSame(['test::tag'], collect(Tracker::all())->first()['tags']);
$this->assertSame(['test::tag', 'pages:home'], collect(Tracker::all())->first()['tags']);

Event::assertDispatched(ContentTracked::class, 1);
}
Expand All @@ -61,4 +61,12 @@ public function it_doesnt_track_pages_already_in_the_manifest()

$this->assertSame(['some:thing'], collect(Tracker::all())->first()['tags']);
}

#[Test]
public function it_doesnt_track_404_pages()
{
$this->get('/i-dont-exist');

$this->assertCount(0, Tracker::all());
}
}
3 changes: 3 additions & 0 deletions tests/__fixtures__/resources/views/default.antlers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- TEMPLATE: default -->

<h1>{{ title }}</h1>
3 changes: 3 additions & 0 deletions tests/__fixtures__/resources/views/layout.antlers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<!-- LAYOUT: layout -->

{{ template_content }}

0 comments on commit 39d9c16

Please sign in to comment.