Skip to content

Commit

Permalink
RedirectorPageController: Added configurable response_code
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Hermo committed Jun 16, 2021
1 parent 4c8823f commit 47220a2
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
13 changes: 13 additions & 0 deletions code/Model/RedirectorPageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use SilverStripe\Control\HTTPRequest;
use PageController;
use SilverStripe\Control\HTTPResponse_Exception;

/**
* Controller for the {@link RedirectorPage}.
Expand All @@ -11,11 +12,18 @@ class RedirectorPageController extends PageController
{
private static $allowed_actions = ['index'];

/**
* For backwards compatibility
* @var int http response code
*/
private static $response_code = 200;

/**
* Check we don't already have a redirect code set
*
* @param HTTPRequest $request
* @return \SilverStripe\Control\HTTPResponse
* @throws HTTPResponse_Exception
*/
public function index(HTTPRequest $request)
{
Expand All @@ -24,6 +32,11 @@ public function index(HTTPRequest $request)
if (!$this->getResponse()->isFinished() && $link = $page->redirectionLink()) {
$this->redirect($link, 301);
}

if ($page->LinkToID && $this->config()->get('response_code') === 404) {
return $this->httpError(404, 'Page not found');
}

return parent::handleAction($request, 'handleIndex');
}

Expand Down
51 changes: 50 additions & 1 deletion tests/php/Model/RedirectorPageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use SilverStripe\CMS\Model\RedirectorPage;
use SilverStripe\CMS\Model\RedirectorPageController;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\FunctionalTest;

class RedirectorPageTest extends FunctionalTest
Expand All @@ -18,7 +21,7 @@ public function setUp()
{
parent::setUp();
Director::config()->update('alternate_base_url', 'http://www.mysite.com/');

Config::forClass(RedirectorPageController::class)->set('response_code', 200);
// Ensure all pages are published
/** @var Page $page */
foreach (Page::get() as $page) {
Expand All @@ -39,6 +42,8 @@ public function testEmptyRedirectors()
/* If a redirector page is misconfigured, then its link method will just return the usual URLSegment-generated value */
$page1 = $this->objFromFixture(RedirectorPage::class, 'badexternal');
$this->assertEquals('/bad-external/', $page1->Link());
$response = $this->get($page1->Link());
$this->assertEquals(200, $response->getStatusCode());

/* An error message will be shown if you visit it */
$content = $this->get(Director::makeRelative($page1->Link()))->getBody();
Expand All @@ -49,10 +54,14 @@ public function testEmptyRedirectors()
$this->assertEquals('/bad-internal/', $page2->Link());
$content = $this->get(Director::makeRelative($page2->Link()))->getBody();
$this->assertContains('message-setupWithoutRedirect', $content);
$response = $this->get($page2->Link());
$this->assertEquals(200, $response->getStatusCode());
}

public function testReflexiveAndTransitiveInternalRedirectors()
{
// Make sure default response code is 200
//Config::forClass(RedirectorPageController::class)->set('response_code', 200);
/* Reflexive redirectors are those that point to themselves. They should behave the same as an empty redirector */
$page = $this->objFromFixture(RedirectorPage::class, 'reflexive');
$this->assertEquals('/reflexive/', $page->Link());
Expand Down Expand Up @@ -117,4 +126,44 @@ public function testNoJSLinksAllowed()
$page->write();
$this->assertEmpty($page->ExternalURL);
}

public function testOnUnpublishedTargetPage()
{
// Make sure default response is 200
Config::forClass(RedirectorPageController::class)->set('response_code', 200);

$page = $this->objFromFixture(RedirectorPage::class, 'goodinternal');
$this->assertEquals('/redirection-dest/', $page->Link());
$response = $this->get($page->Link());
$this->assertEquals(200, $response->getStatusCode());

// Override default response code to 400
Config::forClass(RedirectorPageController::class)->set('response_code', 404);

// Unpublish the target page of this redirector page.
$targetPage = $this->objFromFixture(Page::class, 'dest');
$targetPage->doUnpublish();

$response = $this->get($page->Link());
$this->assertEquals(404, $response->getStatusCode());
$this->assertContains('Page not found', $response->getBody());
}

/**
* @expectedException SilverStripe\Control\HTTPResponse_Exception
*/
public function testExceptionUnpublishedTargetPage()
{
// Override default response code to 400
Config::forClass(RedirectorPageController::class)->set('response_code', 404);

// Unpublish the target page of this redirector page.
$targetPage = $this->objFromFixture(Page::class, 'dest');
$targetPage->doUnpublish();

$page = $this->objFromFixture(RedirectorPage::class, 'goodinternal');
$controller = new RedirectorPageController($page);
$request = new HTTPRequest('GET', $page->Link());
$controller->index($request);
}
}

0 comments on commit 47220a2

Please sign in to comment.