diff --git a/code/extensions/SiteTreeSubsites.php b/code/extensions/SiteTreeSubsites.php index abfbd2e..7df6d24 100644 --- a/code/extensions/SiteTreeSubsites.php +++ b/code/extensions/SiteTreeSubsites.php @@ -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. // 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) { $url = preg_replace('/\/\/[^\/]+\//', '//' . $this->owner->Subsite()->domain() . '/', $url); } diff --git a/tests/php/SiteTreeSubsitesTest.php b/tests/php/SiteTreeSubsitesTest.php index b72ceb0..0366f8c 100644 --- a/tests/php/SiteTreeSubsitesTest.php +++ b/tests/php/SiteTreeSubsitesTest.php @@ -3,7 +3,6 @@ namespace SilverStripe\Subsites\Tests; use Page; -use SilverStripe\Assets\FileNameFilter; use SilverStripe\CMS\Controllers\CMSMain; use SilverStripe\CMS\Controllers\ModelAsController; use SilverStripe\CMS\Model\SiteTree; @@ -399,4 +398,33 @@ class SiteTreeSubsitesTest extends BaseSubsiteTest '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); + } }