Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/StaticCaching/NoCache/DatabaseSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function region(string $key): Region
throw new RegionNotFound($key);
}

return unserialize($region->region);
return unserialize($region->region, ['allowed_classes' => true]);
}

protected function cacheRegion(Region $region)
Expand Down
10 changes: 8 additions & 2 deletions src/StaticCaching/NoCache/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public function regions(): Collection
public function region(string $key): Region
{
if ($this->regions->contains($key) && ($region = StaticCache::cacheStore()->get('nocache::region.'.$key))) {
return $region;
if ($region instanceof Region) {
return $region;
}

if (is_string($region)) {
return unserialize($region, ['allowed_classes' => true]);
}
}

throw new RegionNotFound($key);
Expand Down Expand Up @@ -150,6 +156,6 @@ protected function resolvePageAndPathForPagination(): void

protected function cacheRegion(Region $region)
{
StaticCache::cacheStore()->forever('nocache::region.'.$region->key(), $region);
StaticCache::cacheStore()->forever('nocache::region.'.$region->key(), serialize($region));
}
}
35 changes: 35 additions & 0 deletions tests/StaticCaching/NoCacheSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
use Illuminate\Support\Facades\Cache;
use Mockery;
use PHPUnit\Framework\Attributes\Test;
use Statamic\Facades\StaticCache;
use Statamic\StaticCaching\Cacher;
use Statamic\StaticCaching\NoCache\RegionNotFound;
use Statamic\StaticCaching\NoCache\Session;
use Statamic\StaticCaching\NoCache\StringRegion;
use Tests\FakesContent;
Expand Down Expand Up @@ -148,6 +150,39 @@ public function it_restores_from_cache()
$this->assertEquals('http://localhost/cp', $cascade['cp_url']);
}

#[Test]
public function it_serializes_and_unserializes_regions_through_cache()
{
$session = new Session('http://localhost/test');

$region = $session->pushRegion('the contents', ['foo' => 'bar'], '.html');

$cached = StaticCache::cacheStore()->get('nocache::region.'.$region->key());
$this->assertIsString($cached, 'Region should be stored as a serialized string, not an object.');

$retrieved = $session->region($region->key());

$this->assertInstanceOf(StringRegion::class, $retrieved);
$this->assertEquals($region->key(), $retrieved->key());
$this->assertEquals(['foo' => 'bar'], $retrieved->context());
}

#[Test]
public function it_throws_region_not_found_when_cached_region_is_an_incomplete_class()
{
$session = new Session('http://localhost/test');

$region = $session->pushRegion('the contents', ['foo' => 'bar'], '.html');

// Simulate what happens when serializable_classes enforcement
// turns a cached Region object into __PHP_Incomplete_Class.
StaticCache::cacheStore()->forever('nocache::region.'.$region->key(), new \__PHP_Incomplete_Class);

$this->expectException(RegionNotFound::class);

$session->region($region->key());
}

#[Test]
public function a_singleton_is_bound_in_the_container()
{
Expand Down
Loading