Skip to content

Commit

Permalink
[4.x] Fix first child redirects when using array syntax (#9965)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga authored Apr 26, 2024
1 parent 94666c2 commit 7fb6a4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/Routing/ResolveRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function item($redirect, $parent = null, $localize = false)
return null;
}

if ($redirect === '@child') {
return $this->firstChild($parent);
}

if (is_array($redirect)) {
$redirect = $redirect['url'];
}

if ($redirect === '@child') {
return $this->firstChild($parent);
}

if ($redirect instanceof Values) {
// Assume it's a `group` fieldtype with a `url` subfield.
return $redirect->url->value();
Expand Down
20 changes: 16 additions & 4 deletions tests/Data/Entries/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -630,8 +630,12 @@ public function a_localized_entry_in_a_structured_collection_without_a_route_for
$this->assertNull($entry->url());
}

/** @test */
public function it_gets_urls_for_first_child_redirects()
/**
* @test
*
* @dataProvider firstChildRedirectProvider
*/
public function it_gets_urls_for_first_child_redirects($value)
{
\Event::fake(); // Don't invalidate static cache etc when saving entries.

Expand All @@ -641,9 +645,9 @@ public function it_gets_urls_for_first_child_redirects()

$collection = tap((new Collection)->handle('pages')->routes('{parent_uri}/{slug}'))->save();

$parent = tap((new Entry)->id('1')->locale('en')->collection($collection)->slug('parent')->set('redirect', '@child'))->save();
$parent = tap((new Entry)->id('1')->locale('en')->collection($collection)->slug('parent')->set('redirect', $value))->save();
$child = tap((new Entry)->id('2')->locale('en')->collection($collection)->slug('child'))->save();
$noChildren = tap((new Entry)->id('3')->locale('en')->collection($collection)->slug('nochildren')->set('redirect', '@child'))->save();
$noChildren = tap((new Entry)->id('3')->locale('en')->collection($collection)->slug('nochildren')->set('redirect', $value))->save();

$collection->structureContents([
'expects_root' => false, // irrelevant. just can't pass an empty array at the moment.
Expand Down Expand Up @@ -684,6 +688,14 @@ public function it_gets_urls_for_first_child_redirects()
$this->assertEquals(404, $noChildren->redirectUrl());
}

public static function firstChildRedirectProvider()
{
return [
'string' => ['@child'],
'array' => [['url' => '@child']],
];
}

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

0 comments on commit 7fb6a4f

Please sign in to comment.