1
0
mirror of https://github.com/laravel/valet.git synced 2026-02-04 16:10:08 +01:00

Update unlink command to also unsecure, if necessary

This commit is contained in:
Matt Stauffer
2023-02-10 18:46:58 -05:00
parent 21587b7f07
commit 0e9060d0a6
4 changed files with 75 additions and 4 deletions

View File

@@ -15,7 +15,7 @@ public function __construct(public Brew $brew, public Configuration $config, pub
/**
* Get the name of the site.
*/
private function getRealSiteName(?string $name): string
private function getSiteLinkName(?string $name): string
{
if (! is_null($name)) {
return $name;
@@ -25,7 +25,7 @@ private function getRealSiteName(?string $name): string
return $link;
}
return basename(getcwd());
throw new DomainException(basename(getcwd()).' is not linked.');
}
/**
@@ -42,6 +42,8 @@ private function getLinkNameByCurrentDir(): ?string
if ($count > 1) {
throw new DomainException("There are {$count} links related to the current directory, please specify the name: valet unlink <name>.");
}
return null;
}
/**
@@ -283,7 +285,7 @@ public function getSites(string $path, Collection $certs): Collection
*/
public function unlink(?string $name = null): string
{
$name = $this->getRealSiteName($name);
$name = $this->getSiteLinkName($name);
if ($this->files->exists($path = $this->sitesPath($name))) {
$this->files->unlink($path);
@@ -433,6 +435,13 @@ public function secured(): array
})->unique()->values()->all();
}
public function isSecured(string $site): bool
{
$tld = $this->config->read()['tld'];
return in_array($site.'.'.$tld, $this->secured());
}
/**
* Secure the given host with TLS.
*

View File

@@ -217,7 +217,16 @@ function (ConsoleCommandEvent $event) {
* Unlink a link from the Valet links directory.
*/
$app->command('unlink [name]', function (OutputInterface $output, $name) {
info('The ['.Site::unlink($name).'] symbolic link has been removed.');
$name = Site::unlink($name);
info('The ['.$name.'] symbolic link has been removed.');
if (Site::isSecured($name)) {
info('Unsecuring '.$name.'...');
Site::unsecure(Site::domain($name));
Nginx::restart();
}
})->descriptions('Remove the specified Valet link');
/**

View File

@@ -269,6 +269,18 @@ public function test_unlink_command()
{
[$app, $tester] = $this->appAndTester();
Site::link(getcwd(), basename(getcwd()));
$tester->run(['command' => 'unlink']);
$tester->assertCommandIsSuccessful();
$this->assertEquals(0, Site::links()->count());
}
public function test_unlink_command_with_parameter()
{
[$app, $tester] = $this->appAndTester();
Site::link(__DIR__.'/fixtures/Parked/Sites/my-best-site', 'tighten');
$tester->run(['command' => 'unlink', 'name' => 'tighten']);
@@ -277,6 +289,27 @@ public function test_unlink_command()
$this->assertEquals(0, Site::links()->count());
}
public function test_unlink_command_unsecures_as_well()
{
[$app, $tester] = $this->appAndTester();
Site::link(__DIR__.'/fixtures/Parked/Sites/my-best-site', 'tighten');
$site = Mockery::mock(RealSite::class);
$site->shouldReceive('domain')->with('tighten')->once()->andReturn('tighten.test');
$site->shouldReceive('unlink')->with('tighten')->once()->andReturn('tighten');
$site->shouldReceive('unsecure')->with('tighten.test')->once();
$site->shouldReceive('isSecured')->with('tighten')->once()->andReturn(true);
swap(RealSite::class, $site);
$nginx = Mockery::mock(Nginx::class);
$nginx->shouldReceive('restart')->once();
swap(Nginx::class, $nginx);
$tester->run(['command' => 'unlink', 'name' => 'tighten']);
$tester->assertCommandIsSuccessful();
}
public function test_secure_command()
{
[$app, $tester] = $this->appAndTester();

View File

@@ -900,6 +900,26 @@ public function test_it_returns_secured_sites()
$this->assertSame(['helloworld.tld'], $sites);
}
public function test_it_returns_true_if_a_site_is_secured()
{
$files = Mockery::mock(Filesystem::class);
$files->shouldReceive('scandir')
->once()
->andReturn(['helloworld.tld.crt', '.DS_Store']);
$config = Mockery::mock(Configuration::class);
$config->shouldReceive('read')
->once()
->andReturn(['tld' => 'tld']);
swap(Filesystem::class, $files);
swap(Configuration::class, $config);
$site = resolve(Site::class);
$this->assertTrue($site->isSecured('helloworld'));
}
public function test_it_can_read_valet_rc_files()
{
resolve(Configuration::class)->addPath(__DIR__.'/fixtures/Parked/Sites');