silverstripe-cms/tests/model/RedirectorPageTest.php

114 lines
5.0 KiB
PHP
Raw Normal View History

<?php
2016-07-22 01:32:32 +02:00
use SilverStripe\CMS\Model\RedirectorPage;
use SilverStripe\CMS\Model\RedirectorPageController;
use SilverStripe\Control\Director;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Core\Extension;
use SilverStripe\Dev\TestOnly;
2017-01-25 21:59:25 +01:00
class RedirectorPageTest extends FunctionalTest
{
protected static $fixture_file = 'RedirectorPageTest.yml';
protected static $use_draft_site = true;
protected $autoFollowRedirection = false;
public function setUp()
{
parent::setUp();
Director::config()->update('alternate_base_url', 'http://www.mysite.com/');
}
public function testGoodRedirectors()
{
/* For good redirectors, the final destination URL will be returned */
$this->assertEquals("http://www.google.com", $this->objFromFixture(RedirectorPage::class, 'goodexternal')->Link());
2017-04-20 00:55:02 +02:00
$this->assertEquals("/redirection-dest/", $this->objFromFixture(RedirectorPage::class, 'goodinternal')->redirectionLink());
$this->assertEquals("/redirection-dest/", $this->objFromFixture(RedirectorPage::class, 'goodinternal')->Link());
2017-01-25 21:59:25 +01:00
}
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');
2017-04-20 00:55:02 +02:00
$this->assertEquals('/bad-external/', $page1->Link());
2017-01-25 21:59:25 +01:00
/* An error message will be shown if you visit it */
$content = $this->get(Director::makeRelative($page1->Link()))->getBody();
$this->assertContains('message-setupWithoutRedirect', $content);
/* This also applies for internal links */
$page2 = $this->objFromFixture(RedirectorPage::class, 'badinternal');
2017-04-20 00:55:02 +02:00
$this->assertEquals('/bad-internal/', $page2->Link());
2017-01-25 21:59:25 +01:00
$content = $this->get(Director::makeRelative($page2->Link()))->getBody();
$this->assertContains('message-setupWithoutRedirect', $content);
}
public function testReflexiveAndTransitiveInternalRedirectors()
{
/* Reflexive redirectors are those that point to themselves. They should behave the same as an empty redirector */
$page = $this->objFromFixture(RedirectorPage::class, 'reflexive');
2017-04-20 00:55:02 +02:00
$this->assertEquals('/reflexive/', $page->Link());
2017-01-25 21:59:25 +01:00
$content = $this->get(Director::makeRelative($page->Link()))->getBody();
$this->assertContains('message-setupWithoutRedirect', $content);
/* Transitive redirectors are those that point to another redirector page. They should send people to the URLSegment
2017-06-28 15:35:21 +02:00
* of the destination page - the middle-stop, so to speak. That should redirect to the final destination */
$page = $this->objFromFixture(RedirectorPage::class, 'transitive');
2017-04-20 00:55:02 +02:00
$this->assertEquals('/good-internal/', $page->Link());
2017-01-25 21:59:25 +01:00
$this->autoFollowRedirection = false;
$response = $this->get(Director::makeRelative($page->Link()));
2017-04-20 00:55:02 +02:00
$this->assertEquals(Director::absoluteURL('/redirection-dest/'), $response->getHeader("Location"));
2017-01-25 21:59:25 +01:00
}
public function testExternalURLGetsPrefixIfNotSet()
{
$page = $this->objFromFixture(RedirectorPage::class, 'externalnoprefix');
2017-01-25 21:59:25 +01:00
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite has prefixed with http');
$page->write();
$this->assertEquals($page->ExternalURL, 'http://google.com', 'onBeforeWrite will not double prefix if written again!');
}
public function testAllowsProtocolRelative()
{
$noProtocol = new RedirectorPage(array('ExternalURL' => 'mydomain.com'));
$noProtocol->write();
$this->assertEquals('http://mydomain.com', $noProtocol->ExternalURL);
$protocolAbsolute = new RedirectorPage(array('ExternalURL' => 'http://mydomain.com'));
$protocolAbsolute->write();
$this->assertEquals('http://mydomain.com', $protocolAbsolute->ExternalURL);
$protocolRelative = new RedirectorPage(array('ExternalURL' => '//mydomain.com'));
$protocolRelative->write();
$this->assertEquals('//mydomain.com', $protocolRelative->ExternalURL);
}
/**
* Test that we can trigger a redirection before RedirectorPageController::init() is called
*/
public function testRedirectRespectsFinishedResponse()
{
$page = $this->objFromFixture(RedirectorPage::class, 'goodinternal');
2017-01-25 21:59:25 +01:00
RedirectorPageController::add_extension('RedirectorPageTest_RedirectExtension');
$response = $this->get($page->regularLink());
$this->assertEquals(302, $response->getStatusCode());
$this->assertEquals('http://www.mysite.com/foo', $response->getHeader('Location'));
2017-01-25 21:59:25 +01:00
RedirectorPageController::remove_extension('RedirectorPageTest_RedirectExtension');
}
2017-06-28 15:35:21 +02:00
public function testNoJSLinksAllowed()
{
$page = new RedirectorPage();
$js = 'javascript:alert("hello world")';
$page->ExternalURL = $js;
$this->assertEquals($js, $page->ExternalURL);
$page->write();
$this->assertEmpty($page->ExternalURL);
}
}