API Allow SiteTree::Link to be extended

This commit is contained in:
Damian Mooyman 2017-09-04 19:48:21 +12:00
parent 71852fc536
commit a9c479f26a
No known key found for this signature in database
GPG Key ID: 78B823A10DE27D1A
4 changed files with 42 additions and 4 deletions

View File

@ -517,7 +517,10 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
*/ */
public function Link($action = null) public function Link($action = null)
{ {
return Controller::join_links(Director::baseURL(), $this->RelativeLink($action)); $relativeLink = $this->RelativeLink($action);
$link = Controller::join_links(Director::baseURL(), $relativeLink);
$this->extend('updateLink', $link, $action, $relativeLink);
return $link;
} }
/** /**

View File

@ -890,7 +890,7 @@ class SiteTreeTest extends SapphireTest
} }
/** /**
* @covers SilverStripe\CMS\Model\SiteTree::validURLSegment * @covers \SilverStripe\CMS\Model\SiteTree::validURLSegment
*/ */
public function testValidURLSegmentURLSegmentConflicts() public function testValidURLSegmentURLSegmentConflicts()
{ {
@ -922,7 +922,7 @@ class SiteTreeTest extends SapphireTest
} }
/** /**
* @covers SilverStripe\CMS\Model\SiteTree::validURLSegment * @covers \SilverStripe\CMS\Model\SiteTree::validURLSegment
*/ */
public function testValidURLSegmentClassNameConflicts() public function testValidURLSegmentClassNameConflicts()
{ {
@ -933,7 +933,7 @@ class SiteTreeTest extends SapphireTest
} }
/** /**
* @covers SilverStripe\CMS\Model\SiteTree::validURLSegment * @covers \SilverStripe\CMS\Model\SiteTree::validURLSegment
*/ */
public function testValidURLSegmentControllerConflicts() public function testValidURLSegmentControllerConflicts()
{ {
@ -1401,6 +1401,29 @@ class SiteTreeTest extends SapphireTest
$this->assertFalse($page->canPublish()); $this->assertFalse($page->canPublish());
} }
/**
* Test url rewriting extensions
*/
public function testLinkExtension()
{
Director::config()->set('alternate_base_url', 'http://www.baseurl.com');
$page = new SiteTreeTest_ClassD();
$page->URLSegment = 'classd';
$page->write();
$this->assertEquals(
'http://www.updatedhost.com/classd/myaction?extra=1',
$page->Link('myaction')
);
$this->assertEquals(
'http://www.updatedhost.com/classd/myaction?extra=1',
$page->AbsoluteLink('myaction')
);
$this->assertEquals(
'classd/myaction',
$page->RelativeLink('myaction')
);
}
/** /**
* Test that the controller name for a SiteTree instance can be gathered by appending "Controller" to the SiteTree * Test that the controller name for a SiteTree instance can be gathered by appending "Controller" to the SiteTree
* class name in a PSR-2 compliant manner. * class name in a PSR-2 compliant manner.

View File

@ -3,6 +3,7 @@
namespace SilverStripe\CMS\Tests\Model; namespace SilverStripe\CMS\Tests\Model;
use SilverStripe\CMS\Model\SiteTreeExtension; use SilverStripe\CMS\Model\SiteTreeExtension;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\TestOnly; use SilverStripe\Dev\TestOnly;
class SiteTreeTest_ExtensionA extends SiteTreeExtension implements TestOnly class SiteTreeTest_ExtensionA extends SiteTreeExtension implements TestOnly
@ -13,4 +14,9 @@ class SiteTreeTest_ExtensionA extends SiteTreeExtension implements TestOnly
{ {
return static::$can_publish; return static::$can_publish;
} }
public function updateLink(&$link, $action = null)
{
$link = Controller::join_links($link, '?extra=1');
}
} }

View File

@ -3,6 +3,7 @@
namespace SilverStripe\CMS\Tests\Model; namespace SilverStripe\CMS\Tests\Model;
use SilverStripe\CMS\Model\SiteTreeExtension; use SilverStripe\CMS\Model\SiteTreeExtension;
use SilverStripe\Control\Controller;
use SilverStripe\Dev\TestOnly; use SilverStripe\Dev\TestOnly;
class SiteTreeTest_ExtensionB extends SiteTreeExtension implements TestOnly class SiteTreeTest_ExtensionB extends SiteTreeExtension implements TestOnly
@ -13,4 +14,9 @@ class SiteTreeTest_ExtensionB extends SiteTreeExtension implements TestOnly
{ {
return static::$can_publish; return static::$can_publish;
} }
public function updateLink(&$link, $action = null)
{
$link = Controller::join_links('http://www.updatedhost.com', $link);
}
} }