Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
aerni committed Nov 7, 2024
1 parent 0b0d9a2 commit 367e1d4
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/StaticCaching/FullMeasureStaticCachingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\File;
use Statamic\Facades\StaticCache;
use Statamic\StaticCaching\Cacher;
use Statamic\StaticCaching\NoCache\Session;
use Tests\FakesContent;
use Tests\FakesViews;
Expand Down Expand Up @@ -157,4 +158,36 @@ public function it_should_add_the_javascript_if_there_is_a_csrf_token()
'<script type="text/javascript">js here</script>',
]), file_get_contents($this->dir.'/about_.html'));
}

#[Test]
public function it_decouples_csrf_and_nocache_scripts_if_option_is_enabled()
{
$this->app['config']->set('statamic.static_caching.decouple_nocache_scripts', true);

$this->withFakeViews();
$this->viewShouldReturnRaw('layout', '<html><head></head><body>{{ template_content }}</body></html>');
$this->viewShouldReturnRaw('default', '{{ csrf_token }}');

$this->createPage('about');

StaticCache::nocacheJs('js here');

$csrfTokenScript = '<script type="text/javascript">'.app(Cacher::class)->getCsrfTokenJs().'</script>';
$nocacheScript = '<script type="text/javascript">'.app(Cacher::class)->getNocacheJs().'</script>';

$this->assertFalse(file_exists($this->dir.'/about_.html'));

$response = $this
->get('/about')
->assertOk();

// Initial response should be dynamic and not contain javascript.
$this->assertEquals('<html><head></head><body>'.csrf_token().'</body></html>', $response->getContent());

// The cached response should have the token placeholder, and the javascript.
$this->assertTrue(file_exists($this->dir.'/about_.html'));
$this->assertEquals(vsprintf("<html><head>{$csrfTokenScript}</head><body>STATAMIC_CSRF_TOKEN%s</body></html>", [
$nocacheScript,
]), file_get_contents($this->dir.'/about_.html'));
}
}

0 comments on commit 367e1d4

Please sign in to comment.