Merge pull request #2685 from GuySartorelli/patch-1

FIX: Treat absolute links in a consistent manner in get_by_link.
This commit is contained in:
Steve Boyd 2021-11-15 15:10:58 +13:00 committed by GitHub
commit c754486693
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 3 deletions

View File

@ -415,9 +415,8 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
$urlSegmentExpr = sprintf('"%s"."URLSegment"', $tableName); $urlSegmentExpr = sprintf('"%s"."URLSegment"', $tableName);
$parentIDExpr = sprintf('"%s"."ParentID"', $tableName); $parentIDExpr = sprintf('"%s"."ParentID"', $tableName);
if (trim($link, '/')) {
$link = trim(Director::makeRelative($link), '/'); $link = trim(Director::makeRelative($link), '/');
} else { if (!$link) {
$link = RootURLController::get_homepage_link(); $link = RootURLController::get_homepage_link();
} }

View File

@ -467,6 +467,21 @@ class SiteTreeTest extends SapphireTest
); );
} }
public function testGetByLinkAbsolute()
{
$home = $this->objFromFixture('Page', 'home');
$about = $this->objFromFixture('Page', 'about');
$staff = $this->objFromFixture('Page', 'staff');
$product = $this->objFromFixture('Page', 'product1');
$base = 'https://example.test/';
$this->assertEquals($home->ID, SiteTree::get_by_link(Controller::join_links($base, '/'), false)->ID);
$this->assertEquals($home->ID, SiteTree::get_by_link(Controller::join_links($base, '/home/'), false)->ID);
$this->assertEquals($about->ID, SiteTree::get_by_link(Controller::join_links($base, $about->Link()), false)->ID);
$this->assertEquals($staff->ID, SiteTree::get_by_link(Controller::join_links($base, $staff->Link()), false)->ID);
$this->assertEquals($product->ID, SiteTree::get_by_link(Controller::join_links($base, $product->Link()), false)->ID);
}
public function testRelativeLink() public function testRelativeLink()
{ {
$about = $this->objFromFixture('Page', 'about'); $about = $this->objFromFixture('Page', 'about');