alternateAbsoluteLink() respects action parameter

fixes #275
This commit is contained in:
Werner M. Krauß 2017-09-07 15:56:42 +02:00 committed by Daniel Hensby
parent 40b8e102d0
commit 774dee91d6
No known key found for this signature in database
GPG Key ID: 5DE415D786BBB2FD
2 changed files with 35 additions and 3 deletions

View File

@ -347,11 +347,15 @@ class SiteTreeSubsites extends DataExtension
} }
} }
public function alternateAbsoluteLink() /**
* @param null $action
* @return string
*/
public function alternateAbsoluteLink($action = null)
{ {
// Generate the existing absolute URL and replace the domain with the subsite domain. // Generate the existing absolute URL and replace the domain with the subsite domain.
// This helps deal with Link() returning an absolute URL. // This helps deal with Link() returning an absolute URL.
$url = Director::absoluteURL($this->owner->Link()); $url = Director::absoluteURL($this->owner->Link($action));
if ($this->owner->SubsiteID) { if ($this->owner->SubsiteID) {
$url = preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url); $url = preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url);
} }

View File

@ -3,7 +3,6 @@
namespace SilverStripe\Subsites\Tests; namespace SilverStripe\Subsites\Tests;
use Page; use Page;
use SilverStripe\Assets\FileNameFilter;
use SilverStripe\CMS\Controllers\CMSMain; use SilverStripe\CMS\Controllers\CMSMain;
use SilverStripe\CMS\Controllers\ModelAsController; use SilverStripe\CMS\Controllers\ModelAsController;
use SilverStripe\CMS\Model\SiteTree; use SilverStripe\CMS\Model\SiteTree;
@ -399,4 +398,33 @@ class SiteTreeSubsitesTest extends BaseSubsiteTest
'Themes should be modified when Subsite has theme defined' 'Themes should be modified when Subsite has theme defined'
); );
} }
protected function provideAlternateAbsoluteLink()
{
return [
['home', null, 'http://localhost/'],
['home', 'myaction', 'http://localhost/home/myaction'],
['contact', null, 'http://localhost/contact-us/'],
['contact', 'myaction', 'http://localhost/contact-us/myaction'],
['subsite1_home', null, 'http://subsite1.localhost/'],
['subsite1_home', 'myaction', 'http://subsite1.localhost/home/myaction'],
['subsite1_contactus', null, 'http://subsite1.localhost/contact-us/'],
['subsite1_contactus', 'myaction', 'http://subsite1.localhost/contact-us/myaction']
];
}
/**
* @dataProvider provideAlternateAbsoluteLink
* @param $fixture
* @param $expected
*/
public function testAlternateAbsoluteLink($pageFixtureName, $action, $expectedAbsoluteLink)
{
/** @var Page $page */
$page = $this->objFromFixture('Page', $pageFixtureName);
$result = $page->AbsoluteLink($action);
$this->assertEquals($expectedAbsoluteLink, $result);
}
} }