Skip to content

Commit dc4bc4c

Browse files
committedNov 25, 2024·
bug #6567 Fix an edge-case when using pretty URLs (javiereguiluz)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Fix an edge-case when using pretty URLs This is an alternative to #6553. Fixes #6550, #6535, #6498. `@alexislefebvre` thanks a lot for contributing that PR and also, thanks a lot for the failing test reproducer in #6559, which helped me a lot. Commits ------- eaa6a5b Fix an edge-case when using pretty URLs
2 parents 8603687 + eaa6a5b commit dc4bc4c

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed
 

‎src/Router/AdminUrlGenerator.php

+6
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,12 @@ public function generateUrl(): string
269269
);
270270
ksort($routeParameters, \SORT_STRING);
271271

272+
// if no route parameters are passed, the route doesn't point to any CRUD controller
273+
// action or to any custom action/route; consider it a link to the current dashboard
274+
if ([] === $routeParameters) {
275+
return $this->urlGenerator->generate($this->dashboardRoute);
276+
}
277+
272278
$context = $this->adminContextProvider->getContext();
273279
$usePrettyUrls = null !== $context && $context->usePrettyUrls();
274280
$urlType = null !== $context && false === $context->getAbsoluteUrls() ? UrlGeneratorInterface::ABSOLUTE_PATH : UrlGeneratorInterface::ABSOLUTE_URL;

‎tests/Controller/PrettyUrls/PrettyUrlsControllerTest.php

+22-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function testDefaultMainMenuUsesPrettyUrls()
129129
$crawler = $client->request('GET', '/admin/pretty/urls/blog_post');
130130

131131
$this->assertSame('/admin/pretty/urls', $crawler->filter('#header-logo a.logo')->attr('href'), 'The main Dashboard logo link points to the dashboard entry URL');
132-
$this->assertSame('http://localhost/admin/pretty/urls/blog_post', $crawler->filter('li.menu-item a:contains("Dashboard")')->attr('href'), 'The Dashboard link inside the menu points to the first entity of the menu');
132+
$this->assertSame('/admin/pretty/urls', $crawler->filter('li.menu-item a:contains("Dashboard")')->attr('href'), 'The Dashboard link inside the menu points to the dashboard entry URL (even if it later redirects to some other entity)');
133133
$this->assertSame('http://localhost/admin/pretty/urls/blog_post', $crawler->filter('li.menu-item a:contains("Blog Posts")')->attr('href'));
134134
$this->assertSame('http://localhost/admin/pretty/urls/category', $crawler->filter('li.menu-item a:contains("Categories")')->attr('href'));
135135
}
@@ -142,7 +142,7 @@ public function testCustomMainMenuUsesPrettyUrls()
142142
$crawler = $client->request('GET', '/second/dashboard/user-editor/custom/path-for-index');
143143

144144
$this->assertSame('/second/dashboard', $crawler->filter('#header-logo a.logo')->attr('href'), 'The main Dashboard logo link points to the dashboard entry URL');
145-
$this->assertSame('http://localhost/second/dashboard/user-editor/custom/path-for-index', $crawler->filter('li.menu-item a:contains("Dashboard")')->attr('href'), 'The Dashboard link inside the menu points to the first entity of the menu');
145+
$this->assertSame('/second/dashboard', $crawler->filter('li.menu-item a:contains("Dashboard")')->attr('href'), 'The Dashboard link inside the menu points to the dashboard entry URL (even if it later redirects to some other entity)');
146146
$this->assertSame('http://localhost/second/dashboard/user-editor/custom/path-for-index', $crawler->filter('li.menu-item a:contains("Users")')->attr('href'));
147147
}
148148

@@ -160,6 +160,19 @@ public function testDefaultActionsUsePrettyUrls()
160160
$this->assertMatchesRegularExpression('#http://localhost/admin/pretty/urls/blog_post/1/edit\?csrfToken=.*&fieldName=content#', $crawler->filter('td.field-boolean input[type="checkbox"]')->attr('data-toggle-url'));
161161
}
162162

163+
/**
164+
* @dataProvider provideDefaultPageUrls
165+
*/
166+
public function testDefaultPagesWithPrettyUrls(string $uri)
167+
{
168+
$client = static::createClient();
169+
$client->followRedirects();
170+
171+
$client->request('GET', $uri);
172+
173+
$this->assertResponseIsSuccessful();
174+
}
175+
163176
public function testCustomActionsUsePrettyUrls()
164177
{
165178
$client = static::createClient();
@@ -198,4 +211,11 @@ public function testCustomSortLinksUsePrettyUrls()
198211
$this->assertSame('http://localhost/second/dashboard/user-editor/custom/path-for-index?page=1&sort%5Bname%5D=DESC', $crawler->filter('th.searchable a')->eq(1)->attr('href'));
199212
$this->assertSame('http://localhost/second/dashboard/user-editor/custom/path-for-index?page=1&sort%5Bemail%5D=DESC', $crawler->filter('th.searchable a')->eq(2)->attr('href'));
200213
}
214+
215+
public static function provideDefaultPageUrls(): iterable
216+
{
217+
yield 'Create' => ['/admin/pretty/urls/category/new'];
218+
yield 'Read' => ['/admin/pretty/urls/category/1'];
219+
yield 'Update' => ['/admin/pretty/urls/category/1/edit'];
220+
}
201221
}

‎tests/Router/AdminUrlGeneratorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testUnsetAll()
6969

7070
$adminUrlGenerator->set('foo1', 'bar1');
7171
$adminUrlGenerator->unsetAll();
72-
$this->assertSame('http://localhost/admin', $adminUrlGenerator->generateUrl());
72+
$this->assertSame('/admin', $adminUrlGenerator->generateUrl());
7373
}
7474

7575
public function testUnsetAllExcept()

0 commit comments

Comments
 (0)
Please sign in to comment.