From e5dceb2bc5d6c240ae0c100472a505367f6740cf Mon Sep 17 00:00:00 2001 From: James Barnard Date: Wed, 2 Oct 2019 13:56:51 +0100 Subject: [PATCH] Check for tld on end of certificate file name to support multi segment tld's --- cli/Valet/Site.php | 14 ++++++++++++-- tests/SiteTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/cli/Valet/Site.php b/cli/Valet/Site.php index 88c82ab..0d9cdf6 100644 --- a/cli/Valet/Site.php +++ b/cli/Valet/Site.php @@ -120,11 +120,21 @@ function links() */ function getCertificates($path) { + $config = $this->config->read(); + return collect($this->files->scandir($path))->filter(function ($value, $key) { return ends_with($value, '.crt'); - })->map(function ($cert) { + })->map(function ($cert) use ($config) { $certWithoutSuffix = substr($cert, 0, -4); - return substr($certWithoutSuffix, 0, strrpos($certWithoutSuffix, '.')); + $trimToString = '.'; + + // If we have the cert ending in our tld stripe that tld specifically + // if not then just strip the last segment for backwards compatibility. + if (ends_with($certWithoutSuffix, $config['tld'])) { + $trimToString .= $config['tld']; + } + + return substr($certWithoutSuffix, 0, strrpos($certWithoutSuffix, $trimToString)); })->flip(); } diff --git a/tests/SiteTest.php b/tests/SiteTest.php index 71362ec..08cf139 100644 --- a/tests/SiteTest.php +++ b/tests/SiteTest.php @@ -28,6 +28,28 @@ public function tearDown() } + public function test_get_certificates_will_return_without_multi_segment_tld() + { + $files = Mockery::mock(Filesystem::class); + $files->shouldReceive('scandir') + ->once() + ->with($certPath = '/Users/testuser/.config/valet/Certificates') + ->andReturn(['helloworld.multi.segment.tld.com.crt']); + $config = Mockery::mock(Configuration::class); + $config->shouldReceive('read') + ->once() + ->andReturn(['tld' => 'multi.segment.tld.com']); + + swap(Filesystem::class, $files); + swap(Configuration::class, $config); + + /** @var Site $site */ + $site = resolve(Site::class); + $certs = $site->getCertificates($certPath); + $this->assertSame(['helloworld' => 0], $certs->all()); + } + + public function test_symlink_creates_symlink_to_given_path() { $files = Mockery::mock(Filesystem::class); @@ -77,6 +99,12 @@ public function test_certificates_trim_tld_for_custom_tlds() 'fiveletters.local.crt', ]); + $config = Mockery::mock(Configuration::class); + $config->shouldReceive('read') + ->once() + ->andReturn(['tld' => 'other']); + + swap(Configuration::class, $config); swap(Filesystem::class, $files); $site = resolve(Site::class);