From dd0952209a9e0f90f079175fa23aa469c2a19997 Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Mon, 12 Dec 2022 11:56:51 +1300 Subject: [PATCH] FIX Cache RedirectorPage as redirect instead of as target --- .../Publishable/PublishableSiteTree.php | 11 +++++++- tests/php/PublishableSiteTreeTest.php | 25 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/Extension/Publishable/PublishableSiteTree.php b/src/Extension/Publishable/PublishableSiteTree.php index 4570ac98..94e084fd 100644 --- a/src/Extension/Publishable/PublishableSiteTree.php +++ b/src/Extension/Publishable/PublishableSiteTree.php @@ -7,6 +7,7 @@ use SilverStripe\ORM\DataExtension; use SilverStripe\StaticPublishQueue\Contract\StaticallyPublishable; use SilverStripe\StaticPublishQueue\Contract\StaticPublishingTrigger; +use SilverStripe\CMS\Model\RedirectorPage; /** * Bare-bones implementation of a publishable page. @@ -90,6 +91,14 @@ public function objectsToDelete($context) */ public function urlsToCache() { - return [Director::absoluteURL($this->getOwner()->Link()) => 0]; + $page = $this->getOwner(); + if ($page instanceof RedirectorPage) { + // use RedirectorPage::regularLink() so that it returns the url of the page, + // rather than the url of the target of the RedirectorPage + $link = $page->regularLink(); + } else { + $link = $page->Link(); + } + return [Director::absoluteURL($link) => 0]; } } diff --git a/tests/php/PublishableSiteTreeTest.php b/tests/php/PublishableSiteTreeTest.php index 2ec50711..5ad5350e 100644 --- a/tests/php/PublishableSiteTreeTest.php +++ b/tests/php/PublishableSiteTreeTest.php @@ -2,6 +2,7 @@ namespace SilverStripe\StaticPublishQueue\Test; +use Page; use PHPUnit\Framework\MockObject\Stub\ReturnCallback; use SilverStripe\CMS\Model\SiteTree; use SilverStripe\Core\Injector\Injector; @@ -10,6 +11,9 @@ use SilverStripe\StaticPublishQueue\Extension\Engine\SiteTreePublishingEngine; use SilverStripe\StaticPublishQueue\Extension\Publishable\PublishableSiteTree; use SilverStripe\StaticPublishQueue\Test\PublishableSiteTreeTest\Model\PublishablePage; +use SilverStripe\CMS\Model\RedirectorPage; +use SilverStripe\Core\Config\Config; +use SilverStripe\Control\Director; class PublishableSiteTreeTest extends SapphireTest { @@ -25,6 +29,12 @@ class PublishableSiteTreeTest extends SapphireTest PublishablePage::class, ]; + protected function setUp(): void + { + parent::setUp(); + Config::modify()->set(Director::class, 'alternate_base_url', 'http://example.com/'); + } + public function testObjectsToUpdateOnURLSegmentChange(): void { $this->setExpectedFlushChangesOutput([ @@ -291,4 +301,19 @@ function () use ($toDelete, $toUpdate, $mockExtension, $getURL, $count) { } return $callbacks; } + + public function testUrlsToCache() + { + // Page class is required because RedirectorPage extends Page + if (!class_exists(Page::class)) { + $this->markTestSkipped('This unit test requires the Page class'); + } + $page = new Page(['Title' => 'MyPage']); + $id = $page->write(); + $this->assertSame(['http://example.com/mypage/' => 0], $page->urlsToCache()); + $redirectorPage = new RedirectorPage(['Title' => 'MyRedirectorPage']); + $redirectorPage->LinkToID = $id; + $redirectorPage->write(); + $this->assertSame(['http://example.com/myredirectorpage/' => 0], $redirectorPage->urlsToCache()); + } }