2011-03-18 04:23:47 +01:00
|
|
|
<?php
|
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
use SilverStripe\CMS\Model\RedirectorPage;
|
|
|
|
use SilverStripe\CMS\Model\RedirectorPage_Controller;
|
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
class RedirectorPageTest extends FunctionalTest {
|
2013-03-18 11:47:15 +01:00
|
|
|
protected static $fixture_file = 'RedirectorPageTest.yml';
|
|
|
|
protected static $use_draft_site = true;
|
2015-06-26 11:40:43 +02:00
|
|
|
protected $autoFollowRedirection = false;
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testGoodRedirectors() {
|
2011-03-18 04:23:47 +01:00
|
|
|
/* For good redirectors, the final destination URL will be returned */
|
2016-07-22 01:32:32 +02:00
|
|
|
$this->assertEquals("http://www.google.com", $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage','goodexternal')->Link());
|
|
|
|
$this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage','goodinternal')->redirectionLink());
|
|
|
|
$this->assertEquals(Director::baseURL() . "redirection-dest/", $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage','goodinternal')->Link());
|
2011-03-18 04:23:47 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testEmptyRedirectors() {
|
2011-03-18 04:23:47 +01:00
|
|
|
/* If a redirector page is misconfigured, then its link method will just return the usual URLSegment-generated value */
|
2016-07-22 01:32:32 +02:00
|
|
|
$page1 = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage','badexternal');
|
2011-03-18 04:23:47 +01:00
|
|
|
$this->assertEquals(Director::baseURL() . 'bad-external/', $page1->Link());
|
|
|
|
|
|
|
|
/* 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 */
|
2016-07-22 01:32:32 +02:00
|
|
|
$page2 = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage','badinternal');
|
2011-03-18 04:23:47 +01:00
|
|
|
$this->assertEquals(Director::baseURL() . 'bad-internal/', $page2->Link());
|
|
|
|
$content = $this->get(Director::makeRelative($page2->Link()))->getBody();
|
|
|
|
$this->assertContains('message-setupWithoutRedirect', $content);
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testReflexiveAndTransitiveInternalRedirectors() {
|
2011-03-18 04:23:47 +01:00
|
|
|
/* Reflexive redirectors are those that point to themselves. They should behave the same as an empty redirector */
|
2016-07-22 01:32:32 +02:00
|
|
|
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage','reflexive');
|
2011-03-18 04:23:47 +01:00
|
|
|
$this->assertEquals(Director::baseURL() . 'reflexive/', $page->Link());
|
|
|
|
$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
|
|
|
|
* of the destination page - the middle-stop, so to speak. That should redirect to the final destination */
|
2016-07-22 01:32:32 +02:00
|
|
|
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage','transitive');
|
2011-03-18 04:23:47 +01:00
|
|
|
$this->assertEquals(Director::baseURL() . 'good-internal/', $page->Link());
|
2016-03-08 21:50:55 +01:00
|
|
|
|
2011-03-18 04:23:47 +01:00
|
|
|
$this->autoFollowRedirection = false;
|
|
|
|
$response = $this->get(Director::makeRelative($page->Link()));
|
|
|
|
$this->assertEquals(Director::baseURL() . "redirection-dest/", $response->getHeader("Location"));
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function testExternalURLGetsPrefixIfNotSet() {
|
2016-07-22 01:32:32 +02:00
|
|
|
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'externalnoprefix');
|
2011-03-18 04:23:47 +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!');
|
|
|
|
}
|
2014-05-29 13:59:45 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2015-07-20 12:09:29 +02:00
|
|
|
|
2015-06-26 11:40:43 +02:00
|
|
|
/**
|
|
|
|
* Test that we can trigger a redirection before RedirectorPage_Controller::init() is called
|
|
|
|
*/
|
|
|
|
public function testRedirectRespectsFinishedResponse() {
|
2016-07-22 01:32:32 +02:00
|
|
|
$page = $this->objFromFixture('SilverStripe\\CMS\\Model\\RedirectorPage', 'goodinternal');
|
2015-06-26 11:40:43 +02:00
|
|
|
RedirectorPage_Controller::add_extension('RedirectorPageTest_RedirectExtension');
|
|
|
|
|
|
|
|
$response = $this->get($page->regularLink());
|
|
|
|
$this->assertEquals(302, $response->getStatusCode());
|
|
|
|
$this->assertEquals('/foo', $response->getHeader('Location'));
|
|
|
|
|
|
|
|
RedirectorPage_Controller::remove_extension('RedirectorPageTest_RedirectExtension');
|
|
|
|
}
|
2015-07-20 12:09:29 +02:00
|
|
|
|
2015-06-26 11:40:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class RedirectorPageTest_RedirectExtension extends Extension implements TestOnly {
|
|
|
|
|
|
|
|
public function onBeforeInit() {
|
|
|
|
$this->owner->redirect('/foo');
|
|
|
|
}
|
|
|
|
|
2012-04-12 09:23:20 +02:00
|
|
|
}
|