|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Feature\Entries; |
| 4 | + |
| 5 | +use Facades\Statamic\CP\LivePreview; |
| 6 | +use Facades\Tests\Factories\EntryFactory; |
| 7 | +use Illuminate\Support\Facades\Route; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use Statamic\Facades\Entry; |
| 10 | +use Statamic\View\View; |
| 11 | +use Tests\FakesViews; |
| 12 | +use Tests\PreventSavingStacheItemsToDisk; |
| 13 | +use Tests\TestCase; |
| 14 | + |
| 15 | +class AddsHeadersToLivePreviewTest extends TestCase |
| 16 | +{ |
| 17 | + use FakesViews; |
| 18 | + use PreventSavingStacheItemsToDisk; |
| 19 | + |
| 20 | + protected function setUp(): void |
| 21 | + { |
| 22 | + parent::setUp(); |
| 23 | + |
| 24 | + // The array driver would store entry instances in memory, and we could get false-positive |
| 25 | + // tests by just modifying the entry without actually performing the substitution. |
| 26 | + config(['cache.default' => 'file']); |
| 27 | + |
| 28 | + EntryFactory::collection('test')->id('1')->slug('alfa')->data(['title' => 'Alfa', 'foo' => 'Alfa foo'])->create(); |
| 29 | + |
| 30 | + $this->withFakeViews(); |
| 31 | + |
| 32 | + $this->viewShouldReturnRaw('test', ''); |
| 33 | + } |
| 34 | + |
| 35 | + protected function resolveApplicationConfiguration($app) |
| 36 | + { |
| 37 | + parent::resolveApplicationConfiguration($app); |
| 38 | + |
| 39 | + // Use our View::make() to make sure the Cascade is used. |
| 40 | + // We'd use Route::statamic() but it isn't available at this point. |
| 41 | + Route::get('/test', fn () => View::make('test'))->middleware('statamic.web'); |
| 42 | + } |
| 43 | + |
| 44 | + #[Test] |
| 45 | + public function it_doesnt_set_header_when_single_site() |
| 46 | + { |
| 47 | + $this->setSites(['en' => ['url' => 'http://localhost/', 'locale' => 'en']]); |
| 48 | + $substitute = EntryFactory::collection('test')->id('2')->slug('charlie')->data(['title' => 'Substituted title', 'foo' => 'Substituted foo'])->make(); |
| 49 | + |
| 50 | + LivePreview::tokenize('test-token', $substitute); |
| 51 | + |
| 52 | + $this->get('/test?token=test-token') |
| 53 | + ->assertHeader('X-Statamic-Live-Preview', true) |
| 54 | + ->assertHeaderMissing('Content-Security-Policy', true); |
| 55 | + } |
| 56 | + |
| 57 | + #[Test] |
| 58 | + public function it_sets_header_when_multisite() |
| 59 | + { |
| 60 | + config()->set('statamic.system.multisite', true); |
| 61 | + $this->setSites([ |
| 62 | + 'en' => ['url' => 'http://localhost/', 'locale' => 'en'], |
| 63 | + 'fr' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], |
| 64 | + 'third' => ['url' => 'http://third/', 'locale' => 'en'], |
| 65 | + ]); |
| 66 | + $substitute = EntryFactory::collection('test')->id('2')->slug('charlie')->data(['title' => 'Substituted title', 'foo' => 'Substituted foo'])->make(); |
| 67 | + |
| 68 | + LivePreview::tokenize('test-token', $substitute); |
| 69 | + |
| 70 | + $this->get('/test?token=test-token') |
| 71 | + ->assertHeader('X-Statamic-Live-Preview', true) |
| 72 | + ->assertHeader('Content-Security-Policy', 'frame-ancestors http://localhost http://third'); |
| 73 | + } |
| 74 | +} |
0 commit comments