Skip to content

Commit

Permalink
Merge pull request #156 from creative-commoners/pulls/5/redirector-pa…
Browse files Browse the repository at this point in the history
…ge-cache

FIX Cache RedirectorPage as redirect instead of as target
  • Loading branch information
dhensby authored Dec 15, 2022
2 parents 200fbb9 + dd09522 commit 5ccf2b0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Extension/Publishable/PublishableSiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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];
}
}
25 changes: 25 additions & 0 deletions tests/php/PublishableSiteTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand All @@ -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([
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 5ccf2b0

Please sign in to comment.