Skip to content

Commit

Permalink
[4.x] Strip out .html from parent_uri (#9364)
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean authored Jan 19, 2024
1 parent bb93e62 commit 50263e8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Structures/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Statamic\Facades\Site;
use Statamic\Facades\URL;
use Statamic\GraphQL\ResolvesValues;
use Statamic\Support\Str;

class Page implements Arrayable, ArrayAccess, Augmentable, Entry, JsonSerializable, Protectable, ResolvesValuesContract, Responsable
{
Expand Down Expand Up @@ -193,10 +194,13 @@ public function uri()
return $uris[$this->reference] = $this->entry()->uri();
}

$parentUri = $this->parent && ! $this->parent->isRoot() ? $this->parent->uri() : '';
$parentUri = Str::replace(['.html', '.htm'], '', $parentUri);

return $uris[$this->reference] = app(UrlBuilder::class)
->content($this)
->merge([
'parent_uri' => $this->parent && ! $this->parent->isRoot() ? $this->parent->uri() : '',
'parent_uri' => $parentUri,
'slug' => $this->isRoot() ? '' : $this->slug(),
'depth' => $this->depth,
'is_root' => $this->isRoot(),
Expand Down
49 changes: 49 additions & 0 deletions tests/Data/Structures/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,55 @@ public function slug($slug = null)
$this->assertFalse($page->hasCustomUrl());
}

/**
* @test
*
* @dataProvider stripExtensionFromParentUriProvider
*/
public function it_builds_a_uri_and_strips_out_file_extensions_from_parent_uri($ext)
{
$entry = new class extends Entry
{
public function id($id = null)
{
return 'a';
}

public function slug($slug = null)
{
return 'entry-slug';
}
};
$collection = tap(\Statamic\Facades\Collection::make('test'))->save();
$entry->collection('test');

$parent = Mockery::mock(Page::class);
$parent->shouldReceive('id')->andReturn('the-parent-entry');
$parent->shouldReceive('uri')->andReturn('/the-parent-entry.'.$ext);
$parent->shouldReceive('isRoot')->andReturnFalse();

$tree = $this->newTree()->setStructure(
$this->mock(CollectionStructure::class)->shouldReceive('collection')->andReturn($collection)->getMock()
);

$page = (new Page)
->setTree($tree)
->setRoute('/{parent_uri}/{slug}.'.$ext)
->setParent($parent)
->setEntry($entry);

$this->assertEquals('/the-parent-entry/entry-slug.'.$ext, $page->uri());
$this->assertFalse($page->hasCustomUrl());
}

public function stripExtensionFromParentUriProvider()
{
return [
'html' => ['html'],
'htm' => ['htm'],
];
}

/** @test */
public function it_gets_the_entrys_uri_when_the_structure_does_not_have_a_collection()
{
Expand Down

0 comments on commit 50263e8

Please sign in to comment.