Skip to content

Commit

Permalink
Merge branch '4.13' into 4
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Nov 25, 2023
2 parents fe3ddfe + 579986a commit 089151b
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
8 changes: 5 additions & 3 deletions code/Controllers/ModelAsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ public function handleRequest(HTTPRequest $request)
public function getNestedController()
{
$request = $this->getRequest();
$urlSegment = $request->param('URLSegment');

if (!$URLSegment = $request->param('URLSegment')) {
if ($urlSegment === false || $urlSegment === null || $urlSegment === '') {
throw new Exception('ModelAsController->getNestedController(): was not passed a URLSegment value.');
}

Expand All @@ -130,13 +131,14 @@ public function getNestedController()

// url encode unless it's multibyte (already pre-encoded in the database)
$filter = URLSegmentFilter::create();

if (!$filter->getAllowMultibyte()) {
$URLSegment = rawurlencode($URLSegment ?? '');
$urlSegment = rawurlencode($urlSegment ?? '');
}

// Select child page
$tableName = DataObject::singleton(SiteTree::class)->baseTable();
$conditions = [sprintf('"%s"."URLSegment"', $tableName) => $URLSegment];
$conditions = [sprintf('"%s"."URLSegment"', $tableName) => $urlSegment];
if (SiteTree::config()->get('nested_urls')) {
$conditions[] = [sprintf('"%s"."ParentID"', $tableName) => 0];
}
Expand Down
11 changes: 8 additions & 3 deletions code/Model/SiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ public static function get_by_link($link, $cache = true)
$parentIDExpr = sprintf('"%s"."ParentID"', $tableName);

$link = trim(Director::makeRelative($link) ?? '', '/');
if (!$link) {
if ($link === false || $link === null || $link === '') {
$link = RootURLController::get_homepage_link();
}

Expand Down Expand Up @@ -1633,6 +1633,11 @@ public function requireDefaultRecords()
}
}

private function hasURLSegment(): bool
{
return $this->URLSegment !== false && $this->URLSegment !== null && $this->URLSegment !== '';
}

protected function onBeforeWrite()
{
parent::onBeforeWrite();
Expand All @@ -1653,15 +1658,15 @@ protected function onBeforeWrite()
'New {pagetype}',
['pagetype' => $this->i18n_singular_name()]
));
if ((!$this->URLSegment || $this->URLSegment == $defaultSegment) && $this->Title) {
if ((!$this->hasURLSegment() || $this->URLSegment == $defaultSegment) && $this->Title) {
$this->URLSegment = $this->generateURLSegment($this->Title);
} elseif ($this->isChanged('URLSegment', 2)) {
// Do a strict check on change level, to avoid double encoding caused by
// bogus changes through forceChange()
$filter = URLSegmentFilter::create();
$this->URLSegment = $filter->filter($this->URLSegment);
// If after sanitising there is no URLSegment, give it a reasonable default
if (!$this->URLSegment) {
if (!$this->hasURLSegment()) {
$this->URLSegment = "page-$this->ID";
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/php/Model/SiteTreeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ public function testURLGeneration()
'object' => 'object',
'controller' => 'controller',
'numericonly' => '1930',
'numeric0' => '0',
];

foreach ($expectedURLs as $fixture => $urlSegment) {
Expand Down Expand Up @@ -459,6 +460,7 @@ public function testGetByLink()
$about = $this->objFromFixture('Page', 'about');
$staff = $this->objFromFixture('Page', 'staff');
$product = $this->objFromFixture('Page', 'product1');
$numeric0 = $this->objFromFixture('Page', 'numeric0');

SiteTree::config()->nested_urls = false;

Expand All @@ -467,6 +469,7 @@ public function testGetByLink()
$this->assertEquals($about->ID, SiteTree::get_by_link($about->Link(), false)->ID);
$this->assertEquals($staff->ID, SiteTree::get_by_link($staff->Link(), false)->ID);
$this->assertEquals($product->ID, SiteTree::get_by_link($product->Link(), false)->ID);
$this->assertEquals($numeric0->ID, SiteTree::get_by_link($numeric0->Link(), false)->ID);

Config::modify()->set(SiteTree::class, 'nested_urls', true);

Expand All @@ -475,6 +478,7 @@ public function testGetByLink()
$this->assertEquals($about->ID, SiteTree::get_by_link($about->Link(), false)->ID);
$this->assertEquals($staff->ID, SiteTree::get_by_link($staff->Link(), false)->ID);
$this->assertEquals($product->ID, SiteTree::get_by_link($product->Link(), false)->ID);
$this->assertEquals($numeric0->ID, SiteTree::get_by_link($numeric0->Link(), false)->ID);

$this->assertEquals(
$staff->ID,
Expand All @@ -489,26 +493,30 @@ public function testGetByLinkAbsolute()
$about = $this->objFromFixture('Page', 'about');
$staff = $this->objFromFixture('Page', 'staff');
$product = $this->objFromFixture('Page', 'product1');
$numeric0 = $this->objFromFixture('Page', 'numeric0');

$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);
$this->assertEquals($numeric0->ID, SiteTree::get_by_link(Controller::join_links($base, $numeric0->Link()), false)->ID);
}

public function testRelativeLink()
{
$about = $this->objFromFixture('Page', 'about');
$staff = $this->objFromFixture('Page', 'staff');
$numeric0 = $this->objFromFixture('Page', 'numeric0');

Config::modify()->set(SiteTree::class, 'nested_urls', true);

$this->assertEquals('about-us/', $about->RelativeLink(), 'Matches URLSegment on top level without parameters');
$this->assertEquals('about-us/my-staff/', $staff->RelativeLink(), 'Matches URLSegment plus parent on second level without parameters');
$this->assertEquals('about-us/edit', $about->RelativeLink('edit'), 'Matches URLSegment plus parameter on top level');
$this->assertEquals('about-us/tom&jerry', $about->RelativeLink('tom&jerry'), 'Doesnt url encode parameter');
$this->assertEquals('0/', $numeric0->RelativeLink(), 'Matches URLSegment for segment = 0');
}

public function testPageLevel()
Expand Down
3 changes: 3 additions & 0 deletions tests/php/Model/SiteTreeTest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ Page:
breadcrumbs5:
Title: 'Breadcrumbs 5'
Parent: =>Page.breadcrumbs4
numeric0:
Title: 'urlsegment is 0'
URLSegment: '0'

SilverStripe\CMS\Tests\Model\SiteTreeTest_Conflicted:
parent:
Expand Down

0 comments on commit 089151b

Please sign in to comment.