From e46003eff74248db946e0ab599ac3b56f3101c9a Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 7 Apr 2021 21:05:03 +0100 Subject: [PATCH 1/6] Strip ending slash from end of in FrontendController --- src/Http/Controllers/FrontendController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Http/Controllers/FrontendController.php b/src/Http/Controllers/FrontendController.php index d5d13d5100..b1fb196167 100644 --- a/src/Http/Controllers/FrontendController.php +++ b/src/Http/Controllers/FrontendController.php @@ -45,6 +45,10 @@ public function index(Request $request) $url = substr($url, 0, strpos($url, '?')); } + if (Str::endsWith($url, '/')) { + $url = rtrim($url, '/'); + } + if ($data = Data::findByUri($url, Site::current()->handle())) { return $data; } From 2e1ee9d0775950c3e2f3240f0084bb4babb122ee Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 7 Apr 2021 21:08:08 +0100 Subject: [PATCH 2/6] Add test to cover this use case --- tests/FrontendTest.php | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/FrontendTest.php b/tests/FrontendTest.php index 5aa2911145..39b5edcba3 100644 --- a/tests/FrontendTest.php +++ b/tests/FrontendTest.php @@ -76,6 +76,27 @@ public function page_is_displayed_with_query_string() $this->assertEquals('

The About Page

This is the about page.

', trim($response->content())); } + /** @test */ + public function page_is_displayed_with_query_string_and_ending_slash() + { + $this->withStandardBlueprints(); + $this->withFakeViews(); + $this->viewShouldReturnRaw('layout', '{{ template_content }}'); + $this->viewShouldReturnRaw('some_template', '

{{ title }}

{{ content }}

'); + + $page = $this->createPage('about', [ + 'with' => [ + 'title' => 'The About Page', + 'content' => 'This is the about page.', + 'template' => 'some_template', + ], + ]); + + $response = $this->get('/about/?some=querystring')->assertStatus(200); + + $this->assertEquals('

The About Page

This is the about page.

', trim($response->content())); + } + /** @test */ public function drafts_are_not_visible() { From 7bb5a74249fec62c25311fca82b9b73d248eea1b Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 7 Apr 2021 21:14:53 +0100 Subject: [PATCH 3/6] We don't want to be stripping off the slash when it comes to homepages --- src/Http/Controllers/FrontendController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Controllers/FrontendController.php b/src/Http/Controllers/FrontendController.php index b1fb196167..f2a51010d7 100644 --- a/src/Http/Controllers/FrontendController.php +++ b/src/Http/Controllers/FrontendController.php @@ -45,7 +45,7 @@ public function index(Request $request) $url = substr($url, 0, strpos($url, '?')); } - if (Str::endsWith($url, '/')) { + if (Str::endsWith($url, '/') && Str::length($url) > 1) { $url = rtrim($url, '/'); } From df5575a61016d5b8cf8f339bc672275db21cf7b7 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 7 Apr 2021 21:20:50 +0100 Subject: [PATCH 4/6] Cleanup --- src/Http/Controllers/FrontendController.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Http/Controllers/FrontendController.php b/src/Http/Controllers/FrontendController.php index f2a51010d7..1fef342159 100644 --- a/src/Http/Controllers/FrontendController.php +++ b/src/Http/Controllers/FrontendController.php @@ -29,9 +29,7 @@ public function __construct() */ public function index(Request $request) { - $url = Site::current()->relativePath( - str_finish($request->getUri(), '/') - ); + $url = Site::current()->relativePath($request->getUri()); if ($url === '') { $url = '/'; @@ -45,10 +43,6 @@ public function index(Request $request) $url = substr($url, 0, strpos($url, '?')); } - if (Str::endsWith($url, '/') && Str::length($url) > 1) { - $url = rtrim($url, '/'); - } - if ($data = Data::findByUri($url, Site::current()->handle())) { return $data; } From e5dc7b6273739a9c6ae4b04eca4949bd53d45af5 Mon Sep 17 00:00:00 2001 From: Duncan McClean Date: Wed, 7 Apr 2021 21:24:51 +0100 Subject: [PATCH 5/6] Revert that actually --- src/Http/Controllers/FrontendController.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Http/Controllers/FrontendController.php b/src/Http/Controllers/FrontendController.php index 1fef342159..f2a51010d7 100644 --- a/src/Http/Controllers/FrontendController.php +++ b/src/Http/Controllers/FrontendController.php @@ -29,7 +29,9 @@ public function __construct() */ public function index(Request $request) { - $url = Site::current()->relativePath($request->getUri()); + $url = Site::current()->relativePath( + str_finish($request->getUri(), '/') + ); if ($url === '') { $url = '/'; @@ -43,6 +45,10 @@ public function index(Request $request) $url = substr($url, 0, strpos($url, '?')); } + if (Str::endsWith($url, '/') && Str::length($url) > 1) { + $url = rtrim($url, '/'); + } + if ($data = Data::findByUri($url, Site::current()->handle())) { return $data; } From a20ecb9d6dbd5b967421237c07866fbe70b551e2 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Thu, 8 Apr 2021 12:07:11 -0400 Subject: [PATCH 6/6] Add more tests and simplify --- src/Http/Controllers/FrontendController.php | 8 +- tests/FrontendTest.php | 96 +++++++++++++++++++++ 2 files changed, 97 insertions(+), 7 deletions(-) diff --git a/src/Http/Controllers/FrontendController.php b/src/Http/Controllers/FrontendController.php index f2a51010d7..56c2e4d543 100644 --- a/src/Http/Controllers/FrontendController.php +++ b/src/Http/Controllers/FrontendController.php @@ -29,13 +29,7 @@ public function __construct() */ public function index(Request $request) { - $url = Site::current()->relativePath( - str_finish($request->getUri(), '/') - ); - - if ($url === '') { - $url = '/'; - } + $url = Site::current()->relativePath($request->getUri()); if (Statamic::isAmpRequest()) { $url = str_after($url, '/'.config('statamic.amp.route')); diff --git a/tests/FrontendTest.php b/tests/FrontendTest.php index 39b5edcba3..87eb914e9b 100644 --- a/tests/FrontendTest.php +++ b/tests/FrontendTest.php @@ -76,6 +76,27 @@ public function page_is_displayed_with_query_string() $this->assertEquals('

The About Page

This is the about page.

', trim($response->content())); } + /** @test */ + public function page_is_displayed_with_ending_slash() + { + $this->withStandardBlueprints(); + $this->withFakeViews(); + $this->viewShouldReturnRaw('layout', '{{ template_content }}'); + $this->viewShouldReturnRaw('some_template', '

{{ title }}

{{ content }}

'); + + $page = $this->createPage('about', [ + 'with' => [ + 'title' => 'The About Page', + 'content' => 'This is the about page.', + 'template' => 'some_template', + ], + ]); + + $response = $this->get('/about/')->assertStatus(200); + + $this->assertEquals('

The About Page

This is the about page.

', trim($response->content())); + } + /** @test */ public function page_is_displayed_with_query_string_and_ending_slash() { @@ -97,6 +118,81 @@ public function page_is_displayed_with_query_string_and_ending_slash() $this->assertEquals('

The About Page

This is the about page.

', trim($response->content())); } + /** @test */ + public function home_page_on_second_subdirectory_based_site_is_displayed() + { + Site::setConfig(['sites' => [ + 'english' => ['url' => 'http://localhost/', 'locale' => 'en'], + 'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], + ]]); + + $this->createHomePagesForTwoSites(); + + $response = $this->get('/fr')->assertStatus(200); + + $this->assertEquals('French Home', trim($response->content())); + } + + /** @test */ + public function home_page_on_second_subdirectory_based_site_is_displayed_with_ending_slash() + { + Site::setConfig(['sites' => [ + 'english' => ['url' => 'http://localhost/', 'locale' => 'en'], + 'french' => ['url' => 'http://localhost/fr/', 'locale' => 'fr'], + ]]); + + $this->createHomePagesForTwoSites(); + + $response = $this->get('/fr/')->assertStatus(200); + + $this->assertEquals('French Home', trim($response->content())); + } + + /** @test */ + public function home_page_on_second_domain_site_is_displayed() + { + Site::setConfig(['sites' => [ + 'english' => ['url' => 'http://localhost/', 'locale' => 'en'], + 'french' => ['url' => 'http://anotherhost.com/', 'locale' => 'fr'], + ]]); + + $this->createHomePagesForTwoSites(); + + $response = $this->get('http://anotherhost.com')->assertStatus(200); + + $this->assertEquals('French Home', trim($response->content())); + } + + /** @test */ + public function home_page_on_second_domain_site_is_displayed_with_ending_slash() + { + Site::setConfig(['sites' => [ + 'english' => ['url' => 'http://localhost/', 'locale' => 'en'], + 'french' => ['url' => 'http://anotherhost.com/', 'locale' => 'fr'], + ]]); + + $this->createHomePagesForTwoSites(); + + $response = $this->get('http://anotherhost.com/')->assertStatus(200); + + $this->assertEquals('French Home', trim($response->content())); + } + + private function createHomePagesForTwoSites() + { + $this->withStandardBlueprints(); + $this->withoutExceptionHandling(); + $this->withStandardFakeViews(); + + $c = tap(Collection::make('pages')->sites(['english', 'french'])->routes('{slug}')->structureContents(['root' => true]))->save(); + + EntryFactory::id('1')->locale('english')->slug('home')->collection('pages')->data(['content' => 'Home'])->create(); + EntryFactory::id('2')->locale('french')->slug('french-home')->collection('pages')->data(['content' => 'French Home'])->create(); + + $c->structure()->in('english')->tree([['entry' => '1']])->save(); + $c->structure()->in('french')->tree([['entry' => '2']])->save(); + } + /** @test */ public function drafts_are_not_visible() {