From 3bd47ef2f4fe14bb2375ed81235fcbcc5f6c3d54 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 09:39:20 +1100 Subject: [PATCH 01/25] [#3325] Added a Drush listener that blocks 'sql:sync', 'sql:drop', 'site:install' and 'core:rsync' against production site aliases. --- drush/Listeners/ProductionGuardListener.php | 168 ++++++++++++++ drush/drush.yml | 9 + phpcs.xml | 1 + phpstan.neon | 1 + phpunit.xml | 1 + rector.php | 1 + .../Drush/ProductionGuardListenerTest.php | 212 ++++++++++++++++++ 7 files changed, 393 insertions(+) create mode 100644 drush/Listeners/ProductionGuardListener.php create mode 100644 tests/phpunit/Drush/ProductionGuardListenerTest.php diff --git a/drush/Listeners/ProductionGuardListener.php b/drush/Listeners/ProductionGuardListener.php new file mode 100644 index 0000000000..c77ce96a04 --- /dev/null +++ b/drush/Listeners/ProductionGuardListener.php @@ -0,0 +1,168 @@ +getCommand()?->getName(); + + if ($command === NULL) { + return; + } + + $alias = $this->resolveTargetAlias($command, $event->getInput()); + + if ($alias === NULL || !$this->isProductionAlias($alias)) { + return; + } + + $context = ['command' => $command, 'alias' => $alias, 'variable' => self::SKIP_VARIABLE]; + + if (getenv(self::SKIP_VARIABLE) === '1') { + $this->logger?->warning('Running {command} against the production site alias {alias} because {variable} is set to 1.', $context); + + return; + } + + $this->logger?->error('Refusing to run {command} against the production site alias {alias}. Set {variable}=1 to bypass this guard.', $context); + $event->disableCommand(); + } + + /** + * Resolves the alias that a guarded command writes to. + * + * @param string $command + * The command name. + * @param \Symfony\Component\Console\Input\InputInterface $input + * The command input. + * + * @return string|null + * The alias name, or NULL if the command is not guarded or its target is + * not an alias. + */ + protected function resolveTargetAlias(string $command, InputInterface $input): ?string { + return match ($command) { + 'sql:sync' => $this->resolveAlias((string) $input->getArgument('target')), + // The target is '@alias:path' or a plain path. + 'core:rsync' => $this->resolveAlias(explode(':', (string) $input->getArgument('target'), 2)[0]), + 'sql:drop', 'site:install' => $this->siteAliasManager()->getSelf()->name(), + default => NULL, + }; + } + + /** + * Resolves an alias argument to the name of its alias record. + * + * @param string $name + * The argument value. + * + * @return string|null + * The alias name, or NULL if the value is not an alias. + */ + protected function resolveAlias(string $name): ?string { + if (!str_starts_with($name, '@')) { + return NULL; + } + + // '@self' resolves to the alias selected on the command line. + $record = $this->siteAliasManager()->get($name); + + return $record instanceof SiteAlias ? $record->name() : $name; + } + + /** + * Checks whether an alias matches a configured production alias. + * + * @param string $alias + * The alias name. + * + * @return bool + * TRUE if the alias is a production alias, FALSE otherwise. + */ + protected function isProductionAlias(string $alias): bool { + $name = ltrim($alias, '@'); + + foreach ($this->productionAliases() as $production_alias) { + if ($name === $production_alias || str_ends_with($name, '.' . $production_alias)) { + return TRUE; + } + } + + return FALSE; + } + + /** + * Gets the configured production aliases. + * + * @return string[] + * The production alias names without the leading '@'. + */ + protected function productionAliases(): array { + $value = $this->getConfig()->get(self::CONFIG_KEY, []); + $values = is_array($value) ? $value : [$value]; + + $aliases = []; + + foreach ($values as $item) { + $alias = is_string($item) ? ltrim(trim($item), '@') : ''; + + if ($alias !== '') { + $aliases[] = $alias; + } + } + + return $aliases; + } + +} diff --git a/drush/drush.yml b/drush/drush.yml index 747dafe67d..b38082b240 100644 --- a/drush/drush.yml +++ b/drush/drush.yml @@ -1,3 +1,12 @@ +vortex: + # Site aliases that 'sql:sync', 'sql:drop', 'site:install' and 'core:rsync' + # refuse to target. An entry matches the alias with that name and any alias + # whose environment has that name: 'prod' matches '@prod' and '@site.prod'. + # Set VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 to bypass the guard. + # @see drush/Listeners/ProductionGuardListener.php + production_aliases: + - prod + command: sql: cli: diff --git a/phpcs.xml b/phpcs.xml index e70489e7d5..68b59d7f3a 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -16,6 +16,7 @@ web/themes/custom web/sites/default/settings.php web/sites/default/includes + drush/Listeners tests scripts diff --git a/phpstan.neon b/phpstan.neon index f94f1842d7..287df38f80 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -22,6 +22,7 @@ parameters: - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes + - drush/Listeners - tests - scripts diff --git a/phpunit.xml b/phpunit.xml index 8def1c9da2..a01e8c1118 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -105,6 +105,7 @@ web/themes/custom web/sites/default/includes web/sites/default/settings.php + drush/Listeners web/modules/custom diff --git a/rector.php b/rector.php index 490711e89d..f805a7190e 100644 --- a/rector.php +++ b/rector.php @@ -39,6 +39,7 @@ __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', + __DIR__ . '/drush/Listeners', __DIR__ . '/tests', ]) ->withSkip([ diff --git a/tests/phpunit/Drush/ProductionGuardListenerTest.php b/tests/phpunit/Drush/ProductionGuardListenerTest.php new file mode 100644 index 0000000000..31e83b9918 --- /dev/null +++ b/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -0,0 +1,212 @@ + $arguments + * The command arguments. + * @param string $self + * The name of the '@self' alias. + * @param mixed $production_aliases + * The 'vortex.production_aliases' configuration value, or NULL to leave + * it unset. + * @param string|null $skip + * The value of the skip variable, or NULL to leave it unset. + * @param bool $expected_run + * Whether the command is expected to run. + * @param string[] $expected_messages + * The expected log messages. + */ + #[DataProvider('dataProviderInvoke')] + public function testInvoke(string $command, array $arguments, string $self, mixed $production_aliases, ?string $skip, bool $expected_run, array $expected_messages = []): void { + if ($skip !== NULL) { + putenv(ProductionGuardListener::SKIP_VARIABLE . '=' . $skip); + } + + $config = $production_aliases === NULL ? [] : ['vortex' => ['production_aliases' => $production_aliases]]; + $messages = []; + $logger = static::createLogger($messages); + + $alias_manager = new SiteAliasManager(); + $alias_manager->setSelf(new SiteAlias([], $self)); + + $listener = new ProductionGuardListener(); + $listener->setConfig(new Config($config)); + $listener->setLogger($logger); + $listener->setSiteAliasManager($alias_manager); + + $command_object = new Command($command); + $command_object->addArgument('source', InputArgument::OPTIONAL); + $command_object->addArgument('target', InputArgument::OPTIONAL); + + $event = new ConsoleCommandEvent($command_object, new ArrayInput($arguments, $command_object->getDefinition()), new NullOutput()); + + $listener($event); + + $this->assertSame($expected_run, $event->commandShouldRun()); + $this->assertSame($expected_messages, $messages); + } + + /** + * Data provider for testInvoke(). + */ + public static function dataProviderInvoke(): \Iterator { + $prod = ['prod']; + + yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; + yield 'sql:sync, production target with a site' => ['sql:sync', ['source' => '@self', 'target' => '@your_site.prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@your_site.prod')]]; + yield 'sql:sync, self target resolved to production' => ['sql:sync', ['source' => '@stage', 'target' => '@self'], '@prod', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; + yield 'sql:sync, production source' => ['sql:sync', ['source' => '@prod', 'target' => '@self'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, non-production target' => ['sql:sync', ['source' => '@prod', 'target' => '@stage'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@your_site.preprod'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, environment starting with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@production'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, site named as the production environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod.stage'], '@self', $prod, NULL, TRUE]; + + yield 'core:rsync, production target' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; + yield 'core:rsync, production target without a path' => ['core:rsync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; + yield 'core:rsync, production source' => ['core:rsync', ['source' => '@prod:%files', 'target' => '@self:%files'], '@self', $prod, NULL, TRUE]; + yield 'core:rsync, path target' => ['core:rsync', ['source' => '@self:%files', 'target' => '/var/backups/files'], '@prod', $prod, NULL, TRUE]; + yield 'core:rsync, remote path target' => ['core:rsync', ['source' => '@self:%files', 'target' => 'user@prod:/var/www'], '@self', $prod, NULL, TRUE]; + + yield 'sql:drop, production self' => ['sql:drop', [], '@prod', $prod, NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'sql:drop, non-production self' => ['sql:drop', [], '@self', $prod, NULL, TRUE]; + yield 'site:install, production self with a site' => ['site:install', [], '@lagoon.prod', $prod, NULL, FALSE, [static::blocked('site:install', '@lagoon.prod')]]; + yield 'site:install, non-production self' => ['site:install', [], '@lagoon.develop', $prod, NULL, TRUE]; + + yield 'unguarded command, production self' => ['cache:rebuild', [], '@prod', $prod, NULL, TRUE]; + yield 'unguarded command, production target' => ['sql:query', ['source' => '@self', 'target' => '@prod'], '@prod', $prod, NULL, TRUE]; + + yield 'skip variable set to 1' => ['sql:drop', [], '@prod', $prod, '1', TRUE, [static::bypassed('sql:drop', '@prod')]]; + yield 'skip variable set to 0' => ['sql:drop', [], '@prod', $prod, '0', FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'skip variable empty' => ['sql:drop', [], '@prod', $prod, '', FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'skip variable on a non-production target' => ['sql:drop', [], '@stage', $prod, '1', TRUE]; + + yield 'config, single string' => ['sql:drop', [], '@prod', 'prod', NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'config, entry with a leading @' => ['sql:drop', [], '@prod', ['@prod'], NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'config, entry with a site' => ['sql:drop', [], '@lagoon.main', ['lagoon.main'], NULL, FALSE, [static::blocked('sql:drop', '@lagoon.main')]]; + yield 'config, entry with a site, other site' => ['sql:drop', [], '@other.main', ['lagoon.main'], NULL, TRUE]; + yield 'config, multiple entries' => ['sql:drop', [], '@lagoon.main', ['prod', 'main'], NULL, FALSE, [static::blocked('sql:drop', '@lagoon.main')]]; + yield 'config, invalid entries' => ['sql:drop', [], '@prod', [' ', '@', 123, NULL], NULL, TRUE]; + yield 'config, empty' => ['sql:drop', [], '@prod', [], NULL, TRUE]; + yield 'config, unset' => ['sql:drop', [], '@prod', NULL, NULL, TRUE]; + } + + /** + * Tests that an event without a command is ignored. + */ + public function testInvokeWithoutCommand(): void { + $messages = []; + $logger = static::createLogger($messages); + + $listener = new ProductionGuardListener(); + $listener->setConfig(new Config(['vortex' => ['production_aliases' => ['prod']]])); + $listener->setLogger($logger); + $listener->setSiteAliasManager(new SiteAliasManager()); + + $event = new ConsoleCommandEvent(NULL, new ArrayInput([]), new NullOutput()); + + $listener($event); + + $this->assertTrue($event->commandShouldRun()); + $this->assertSame([], $messages); + } + + /** + * Builds the message logged for a blocked command. + */ + protected static function blocked(string $command, string $alias): string { + return sprintf('error: Refusing to run %s against the production site alias %s. Set VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 to bypass this guard.', $command, $alias); + } + + /** + * Builds the message logged for a bypassed command. + */ + protected static function bypassed(string $command, string $alias): string { + return sprintf('warning: Running %s against the production site alias %s because VORTEX_DRUSH_PRODUCTION_GUARD_SKIP is set to 1.', $command, $alias); + } + + /** + * Creates a logger that records interpolated messages. + * + * @param string[] $messages + * The array that receives the messages, prefixed with their level. + */ + protected static function createLogger(array &$messages): LoggerInterface { + return new class($messages) extends AbstractLogger { + + /** + * Recorded messages, prefixed with their level. + * + * @var string[] + */ + protected array $messages; + + /** + * Constructs the logger. + * + * @param string[] $messages + * The array that receives the messages. + */ + public function __construct(array &$messages) { + $this->messages = &$messages; + } + + /** + * {@inheritdoc} + */ + public function log($level, string|\Stringable $message, array $context = []): void { + $replacements = []; + + foreach ($context as $key => $value) { + $replacements['{' . $key . '}'] = (string) $value; + } + + $this->messages[] = $level . ': ' . strtr((string) $message, $replacements); + } + + }; + } + +} From 8917e6c74f666f3e500767f50ec6fab21abc9ccc Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 09:39:22 +1100 Subject: [PATCH 02/25] [#3325] Documented the Drush production guard and the 'VORTEX_DRUSH_PRODUCTION_GUARD_SKIP' bypass. --- .../content/development/environment/drush.mdx | 86 ++++++++++++++++++- 1 file changed, 85 insertions(+), 1 deletion(-) diff --git a/.vortex/docs/content/development/environment/drush.mdx b/.vortex/docs/content/development/environment/drush.mdx index f8f9b5a3ea..ef1f244a19 100644 --- a/.vortex/docs/content/development/environment/drush.mdx +++ b/.vortex/docs/content/development/environment/drush.mdx @@ -62,7 +62,10 @@ The shipped Drush configuration lives in the `drush/` directory: passes `--disable-ssl` to the `sql:*` commands (`sql:cli`, `sql:query`, `sql:dump`, `sql:drop`, `sql:create`) and to `site:install`. The database commands then connect to the stack's database service without TLS. The - file also adds `--no-tablespaces` to `sql:dump`. + file also adds `--no-tablespaces` to `sql:dump` and lists the production + site aliases. See [Production guard](#production-guard). +- [`drush/Listeners/ProductionGuardListener.php`](https://github.com/drevops/vortex/blob/main/drush/Listeners/ProductionGuardListener.php) + blocks destructive commands that target a production site alias. - [`drush/sites/lagoon.site.yml`](https://github.com/drevops/vortex/blob/main/drush/sites/lagoon.site.yml) defines the site aliases for Lagoon-hosted environments. See [Aliases](#aliases). @@ -250,3 +253,84 @@ machine: ``` + +## Production guard + +**Vortex** blocks 4 destructive Drush commands when they target a +production site alias. A mistyped `drush sql:sync @self @prod` then fails +before it overwrites the production database. The following table lists +the guarded commands and the alias the guard checks for each one: + +| Command | Alias the guard checks | +|----------------|----------------------------------------------------------| +| `sql:sync` | The target, such as `@prod` in `drush sql:sync @self @prod` | +| `core:rsync` | The target, such as `@prod` in `drush core:rsync @self:%files @prod:%files` | +| `sql:drop` | The selected site, such as `@prod` in `drush @prod sql:drop` | +| `site:install` | The selected site, such as `@prod` in `drush @prod site:install` | + +A blocked command stops before Drush connects to the site, and Drush prints +this error: + +```text + [error] Refusing to run sql:drop against the production site alias @prod. Set VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 to bypass this guard. +``` + +The guard checks the alias where you type the command. A command you run on +the production server itself, such as after `drush @prod ssh`, targets no +alias, so the guard lets it run. + +### Production aliases + +`drush/drush.yml` lists the production aliases under +`vortex.production_aliases`: + +```yaml +vortex: + production_aliases: + - prod +``` + +An entry matches the alias with that name and every alias whose environment +has that name. The `prod` entry matches `@prod`, `@self.prod` and +`@lagoon.prod`. It doesn't match `@production` or `@your_site.preprod`. An +entry with a site name, such as `lagoon.main`, matches only that alias. + +On Lagoon, the alias of the production environment is +`@lagoon.`, where `` is the value of +`VORTEX_LAGOON_PRODUCTION_BRANCH`. With the default `main` branch, add +`lagoon.main` to the list: + +```yaml +vortex: + production_aliases: + - prod + - lagoon.main +``` + +### Bypass the guard + +To run a guarded command against production on purpose, set +`VORTEX_DRUSH_PRODUCTION_GUARD_SKIP` to `1` for that command. Drush prints a +warning that names the variable, then runs the command: + + + + ```shell + VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 drush @prod sql:drop + ``` + + + ```shell + VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 ahoy drush @prod sql:drop + ``` + + + ```shell + docker compose exec -e VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 cli \ + drush @prod sql:drop + ``` + + + +Set the variable on the command line only. An `export` in your shell turns +off the guard for every command that follows. From 3aa9f7783650b76f49f2931bdfcc82052df102d3 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 09:42:47 +1100 Subject: [PATCH 03/25] Updated snapshots. --- .../Listeners/ProductionGuardListener.php | 168 ++++++++++++++ .../handler_process/_baseline/drush/drush.yml | 9 + .../handler_process/_baseline/phpcs.xml | 1 + .../handler_process/_baseline/phpstan.neon | 1 + .../handler_process/_baseline/phpunit.xml | 1 + .../handler_process/_baseline/rector.php | 1 + .../Drush/ProductionGuardListenerTest.php | 212 ++++++++++++++++++ .../handler_process/hosting_acquia/phpcs.xml | 3 +- .../hosting_acquia/phpstan.neon | 6 +- .../hosting_acquia/phpunit.xml | 5 +- .../handler_process/hosting_acquia/rector.php | 4 +- .../hosting_project_name___acquia/phpcs.xml | 3 +- .../phpstan.neon | 6 +- .../hosting_project_name___acquia/phpunit.xml | 5 +- .../hosting_project_name___acquia/rector.php | 4 +- .../Drush/ProductionGuardListenerTest.php | 14 ++ .../handler_process/theme_claro/phpcs.xml | 4 +- .../handler_process/theme_claro/phpstan.neon | 6 +- .../handler_process/theme_claro/phpunit.xml | 4 +- .../handler_process/theme_claro/rector.php | 4 +- .../handler_process/theme_olivero/phpcs.xml | 4 +- .../theme_olivero/phpstan.neon | 6 +- .../handler_process/theme_olivero/phpunit.xml | 4 +- .../handler_process/theme_olivero/rector.php | 4 +- .../handler_process/theme_stark/phpcs.xml | 4 +- .../handler_process/theme_stark/phpstan.neon | 6 +- .../handler_process/theme_stark/phpunit.xml | 4 +- .../handler_process/theme_stark/rector.php | 4 +- .../Drush/ProductionGuardListenerTest.php | 8 + .../Drush/ProductionGuardListenerTest.php | 8 + .../tools_groups_no_be_tests/phpcs.xml | 2 +- .../tools_groups_no_be_tests/phpstan.neon | 2 +- .../tools_groups_no_be_tests/rector.php | 4 +- .../Drush/-ProductionGuardListenerTest.php | 0 .../phpcs.xml | 2 +- .../phpstan.neon | 2 +- .../rector.php | 4 +- .../Drush/-ProductionGuardListenerTest.php | 0 .../phpcs.xml | 4 +- .../phpstan.neon | 6 +- .../phpunit.xml | 4 +- .../rector.php | 4 +- .../phpcs.xml | 4 +- .../phpstan.neon | 6 +- .../phpunit.xml | 4 +- .../rector.php | 4 +- .../handler_process/tools_no_behat/phpcs.xml | 2 +- .../handler_process/tools_no_behat/rector.php | 2 +- .../tools_no_behat_circleci/phpcs.xml | 2 +- .../tools_no_behat_circleci/rector.php | 2 +- .../tools_no_eslint_no_theme/phpcs.xml | 4 +- .../tools_no_eslint_no_theme/phpstan.neon | 6 +- .../tools_no_eslint_no_theme/phpunit.xml | 4 +- .../tools_no_eslint_no_theme/rector.php | 4 +- .../Drush/ProductionGuardListenerTest.php | 8 + .../Drush/ProductionGuardListenerTest.php | 8 + .../tools_no_phpunit/phpstan.neon | 2 +- .../tools_no_phpunit/rector.php | 2 +- .../Drush/-ProductionGuardListenerTest.php | 0 .../tools_no_phpunit_circleci/phpstan.neon | 2 +- .../tools_no_phpunit_circleci/rector.php | 2 +- .../Drush/-ProductionGuardListenerTest.php | 0 .../tools_no_stylelint_no_theme/phpcs.xml | 4 +- .../tools_no_stylelint_no_theme/phpstan.neon | 6 +- .../tools_no_stylelint_no_theme/phpunit.xml | 4 +- .../tools_no_stylelint_no_theme/rector.php | 4 +- .../Drush/-ProductionGuardListenerTest.php | 0 67 files changed, 538 insertions(+), 95 deletions(-) create mode 100644 .vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint/tests/phpunit/Drush/ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/tests/phpunit/Drush/-ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs/tests/phpunit/Drush/ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/tests/phpunit/Drush/-ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php create mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_none/tests/phpunit/Drush/-ProductionGuardListenerTest.php diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php new file mode 100644 index 0000000000..c77ce96a04 --- /dev/null +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php @@ -0,0 +1,168 @@ +getCommand()?->getName(); + + if ($command === NULL) { + return; + } + + $alias = $this->resolveTargetAlias($command, $event->getInput()); + + if ($alias === NULL || !$this->isProductionAlias($alias)) { + return; + } + + $context = ['command' => $command, 'alias' => $alias, 'variable' => self::SKIP_VARIABLE]; + + if (getenv(self::SKIP_VARIABLE) === '1') { + $this->logger?->warning('Running {command} against the production site alias {alias} because {variable} is set to 1.', $context); + + return; + } + + $this->logger?->error('Refusing to run {command} against the production site alias {alias}. Set {variable}=1 to bypass this guard.', $context); + $event->disableCommand(); + } + + /** + * Resolves the alias that a guarded command writes to. + * + * @param string $command + * The command name. + * @param \Symfony\Component\Console\Input\InputInterface $input + * The command input. + * + * @return string|null + * The alias name, or NULL if the command is not guarded or its target is + * not an alias. + */ + protected function resolveTargetAlias(string $command, InputInterface $input): ?string { + return match ($command) { + 'sql:sync' => $this->resolveAlias((string) $input->getArgument('target')), + // The target is '@alias:path' or a plain path. + 'core:rsync' => $this->resolveAlias(explode(':', (string) $input->getArgument('target'), 2)[0]), + 'sql:drop', 'site:install' => $this->siteAliasManager()->getSelf()->name(), + default => NULL, + }; + } + + /** + * Resolves an alias argument to the name of its alias record. + * + * @param string $name + * The argument value. + * + * @return string|null + * The alias name, or NULL if the value is not an alias. + */ + protected function resolveAlias(string $name): ?string { + if (!str_starts_with($name, '@')) { + return NULL; + } + + // '@self' resolves to the alias selected on the command line. + $record = $this->siteAliasManager()->get($name); + + return $record instanceof SiteAlias ? $record->name() : $name; + } + + /** + * Checks whether an alias matches a configured production alias. + * + * @param string $alias + * The alias name. + * + * @return bool + * TRUE if the alias is a production alias, FALSE otherwise. + */ + protected function isProductionAlias(string $alias): bool { + $name = ltrim($alias, '@'); + + foreach ($this->productionAliases() as $production_alias) { + if ($name === $production_alias || str_ends_with($name, '.' . $production_alias)) { + return TRUE; + } + } + + return FALSE; + } + + /** + * Gets the configured production aliases. + * + * @return string[] + * The production alias names without the leading '@'. + */ + protected function productionAliases(): array { + $value = $this->getConfig()->get(self::CONFIG_KEY, []); + $values = is_array($value) ? $value : [$value]; + + $aliases = []; + + foreach ($values as $item) { + $alias = is_string($item) ? ltrim(trim($item), '@') : ''; + + if ($alias !== '') { + $aliases[] = $alias; + } + } + + return $aliases; + } + +} diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/drush.yml b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/drush.yml index 747dafe67d..b38082b240 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/drush.yml +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/drush.yml @@ -1,3 +1,12 @@ +vortex: + # Site aliases that 'sql:sync', 'sql:drop', 'site:install' and 'core:rsync' + # refuse to target. An entry matches the alias with that name and any alias + # whose environment has that name: 'prod' matches '@prod' and '@site.prod'. + # Set VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 to bypass the guard. + # @see drush/Listeners/ProductionGuardListener.php + production_aliases: + - prod + command: sql: cli: diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpcs.xml index d5be956a0c..a8352d1e0c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpcs.xml @@ -16,6 +16,7 @@ web/themes/custom web/sites/default/settings.php web/sites/default/includes + drush/Listeners tests scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpstan.neon index 8519d1b882..0a571042ca 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpstan.neon @@ -22,6 +22,7 @@ parameters: - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes + - drush/Listeners - tests - scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpunit.xml index 8def1c9da2..a01e8c1118 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/phpunit.xml @@ -105,6 +105,7 @@ web/themes/custom web/sites/default/includes web/sites/default/settings.php + drush/Listeners web/modules/custom diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/rector.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/rector.php index 14094c829e..782bf14171 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/rector.php @@ -39,6 +39,7 @@ __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', + __DIR__ . '/drush/Listeners', __DIR__ . '/tests', ]) ->withSkip([ diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php new file mode 100644 index 0000000000..272eb8019a --- /dev/null +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -0,0 +1,212 @@ + $arguments + * The command arguments. + * @param string $self + * The name of the '@self' alias. + * @param mixed $production_aliases + * The 'vortex.production_aliases' configuration value, or NULL to leave + * it unset. + * @param string|null $skip + * The value of the skip variable, or NULL to leave it unset. + * @param bool $expected_run + * Whether the command is expected to run. + * @param string[] $expected_messages + * The expected log messages. + */ + #[DataProvider('dataProviderInvoke')] + public function testInvoke(string $command, array $arguments, string $self, mixed $production_aliases, ?string $skip, bool $expected_run, array $expected_messages = []): void { + if ($skip !== NULL) { + putenv(ProductionGuardListener::SKIP_VARIABLE . '=' . $skip); + } + + $config = $production_aliases === NULL ? [] : ['vortex' => ['production_aliases' => $production_aliases]]; + $messages = []; + $logger = static::createLogger($messages); + + $alias_manager = new SiteAliasManager(); + $alias_manager->setSelf(new SiteAlias([], $self)); + + $listener = new ProductionGuardListener(); + $listener->setConfig(new Config($config)); + $listener->setLogger($logger); + $listener->setSiteAliasManager($alias_manager); + + $command_object = new Command($command); + $command_object->addArgument('source', InputArgument::OPTIONAL); + $command_object->addArgument('target', InputArgument::OPTIONAL); + + $event = new ConsoleCommandEvent($command_object, new ArrayInput($arguments, $command_object->getDefinition()), new NullOutput()); + + $listener($event); + + $this->assertSame($expected_run, $event->commandShouldRun()); + $this->assertSame($expected_messages, $messages); + } + + /** + * Data provider for testInvoke(). + */ + public static function dataProviderInvoke(): \Iterator { + $prod = ['prod']; + + yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; + yield 'sql:sync, production target with a site' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@star_wars.prod')]]; + yield 'sql:sync, self target resolved to production' => ['sql:sync', ['source' => '@stage', 'target' => '@self'], '@prod', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; + yield 'sql:sync, production source' => ['sql:sync', ['source' => '@prod', 'target' => '@self'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, non-production target' => ['sql:sync', ['source' => '@prod', 'target' => '@stage'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.preprod'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, environment starting with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@production'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, site named as the production environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod.stage'], '@self', $prod, NULL, TRUE]; + + yield 'core:rsync, production target' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; + yield 'core:rsync, production target without a path' => ['core:rsync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; + yield 'core:rsync, production source' => ['core:rsync', ['source' => '@prod:%files', 'target' => '@self:%files'], '@self', $prod, NULL, TRUE]; + yield 'core:rsync, path target' => ['core:rsync', ['source' => '@self:%files', 'target' => '/var/backups/files'], '@prod', $prod, NULL, TRUE]; + yield 'core:rsync, remote path target' => ['core:rsync', ['source' => '@self:%files', 'target' => 'user@prod:/var/www'], '@self', $prod, NULL, TRUE]; + + yield 'sql:drop, production self' => ['sql:drop', [], '@prod', $prod, NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'sql:drop, non-production self' => ['sql:drop', [], '@self', $prod, NULL, TRUE]; + yield 'site:install, production self with a site' => ['site:install', [], '@lagoon.prod', $prod, NULL, FALSE, [static::blocked('site:install', '@lagoon.prod')]]; + yield 'site:install, non-production self' => ['site:install', [], '@lagoon.develop', $prod, NULL, TRUE]; + + yield 'unguarded command, production self' => ['cache:rebuild', [], '@prod', $prod, NULL, TRUE]; + yield 'unguarded command, production target' => ['sql:query', ['source' => '@self', 'target' => '@prod'], '@prod', $prod, NULL, TRUE]; + + yield 'skip variable set to 1' => ['sql:drop', [], '@prod', $prod, '1', TRUE, [static::bypassed('sql:drop', '@prod')]]; + yield 'skip variable set to 0' => ['sql:drop', [], '@prod', $prod, '0', FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'skip variable empty' => ['sql:drop', [], '@prod', $prod, '', FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'skip variable on a non-production target' => ['sql:drop', [], '@stage', $prod, '1', TRUE]; + + yield 'config, single string' => ['sql:drop', [], '@prod', 'prod', NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'config, entry with a leading @' => ['sql:drop', [], '@prod', ['@prod'], NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; + yield 'config, entry with a site' => ['sql:drop', [], '@lagoon.main', ['lagoon.main'], NULL, FALSE, [static::blocked('sql:drop', '@lagoon.main')]]; + yield 'config, entry with a site, other site' => ['sql:drop', [], '@other.main', ['lagoon.main'], NULL, TRUE]; + yield 'config, multiple entries' => ['sql:drop', [], '@lagoon.main', ['prod', 'main'], NULL, FALSE, [static::blocked('sql:drop', '@lagoon.main')]]; + yield 'config, invalid entries' => ['sql:drop', [], '@prod', [' ', '@', 123, NULL], NULL, TRUE]; + yield 'config, empty' => ['sql:drop', [], '@prod', [], NULL, TRUE]; + yield 'config, unset' => ['sql:drop', [], '@prod', NULL, NULL, TRUE]; + } + + /** + * Tests that an event without a command is ignored. + */ + public function testInvokeWithoutCommand(): void { + $messages = []; + $logger = static::createLogger($messages); + + $listener = new ProductionGuardListener(); + $listener->setConfig(new Config(['vortex' => ['production_aliases' => ['prod']]])); + $listener->setLogger($logger); + $listener->setSiteAliasManager(new SiteAliasManager()); + + $event = new ConsoleCommandEvent(NULL, new ArrayInput([]), new NullOutput()); + + $listener($event); + + $this->assertTrue($event->commandShouldRun()); + $this->assertSame([], $messages); + } + + /** + * Builds the message logged for a blocked command. + */ + protected static function blocked(string $command, string $alias): string { + return sprintf('error: Refusing to run %s against the production site alias %s. Set VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 to bypass this guard.', $command, $alias); + } + + /** + * Builds the message logged for a bypassed command. + */ + protected static function bypassed(string $command, string $alias): string { + return sprintf('warning: Running %s against the production site alias %s because VORTEX_DRUSH_PRODUCTION_GUARD_SKIP is set to 1.', $command, $alias); + } + + /** + * Creates a logger that records interpolated messages. + * + * @param string[] $messages + * The array that receives the messages, prefixed with their level. + */ + protected static function createLogger(array &$messages): LoggerInterface { + return new class($messages) extends AbstractLogger { + + /** + * Recorded messages, prefixed with their level. + * + * @var string[] + */ + protected array $messages; + + /** + * Constructs the logger. + * + * @param string[] $messages + * The array that receives the messages. + */ + public function __construct(array &$messages) { + $this->messages = &$messages; + } + + /** + * {@inheritdoc} + */ + public function log($level, string|\Stringable $message, array $context = []): void { + $replacements = []; + + foreach ($context as $key => $value) { + $replacements['{' . $key . '}'] = (string) $value; + } + + $this->messages[] = $level . ': ' . strtr((string) $message, $replacements); + } + + }; + } + +} diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpcs.xml index eccff9ac08..285a0ae3f8 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpcs.xml @@ -1,4 +1,4 @@ -@@ -12,18 +12,18 @@ +@@ -12,19 +12,19 @@ @@ -10,6 +10,7 @@ + docroot/themes/custom + docroot/sites/default/settings.php + docroot/sites/default/includes + drush/Listeners tests scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpstan.neon index d9fac99680..44dec7f6a8 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpstan.neon @@ -10,10 +10,10 @@ + - docroot/themes/custom + - docroot/sites/default/settings.php + - docroot/sites/default/includes + - drush/Listeners - tests - scripts - -@@ -39,12 +39,12 @@ +@@ -40,12 +40,12 @@ # parameters or return values in iterable type arrays. message: '#.*no value type specified in iterable type array#' paths: @@ -29,7 +29,7 @@ reportUnmatched: false - # Since tests and data providers do not have to have parameter docblocks, # it is not possible to specify the type of the parameter, so we ignore -@@ -51,7 +51,7 @@ +@@ -52,7 +52,7 @@ # this error. message: '#.*no value type specified in iterable type array.#' paths: diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpunit.xml index cb4d78f4f3..5b745e7901 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/phpunit.xml @@ -28,7 +28,7 @@ -@@ -101,18 +101,18 @@ +@@ -101,19 +101,19 @@ @@ -40,6 +40,7 @@ + docroot/themes/custom + docroot/sites/default/includes + docroot/sites/default/settings.php + drush/Listeners - web/modules/custom @@ -57,7 +58,7 @@ tests -@@ -128,7 +128,7 @@ +@@ -129,7 +129,7 @@ to DRUPAL_ROOT/sites/simpletest/browser_output. @see https://www.drupal.org/project/drupal/issues/2992069 --> diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/rector.php b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/rector.php index f5510ea193..bffb15a242 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_acquia/rector.php @@ -10,10 +10,10 @@ + __DIR__ . '/docroot/themes/custom', + __DIR__ . '/docroot/sites/default/settings.php', + __DIR__ . '/docroot/sites/default/includes', + __DIR__ . '/drush/Listeners', __DIR__ . '/tests', ]) - ->withSkip([ -@@ -55,12 +55,12 @@ +@@ -56,12 +56,12 @@ PrivatizeLocalGetterToPropertyRector::class, RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpcs.xml index eccff9ac08..285a0ae3f8 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpcs.xml @@ -1,4 +1,4 @@ -@@ -12,18 +12,18 @@ +@@ -12,19 +12,19 @@ @@ -10,6 +10,7 @@ + docroot/themes/custom + docroot/sites/default/settings.php + docroot/sites/default/includes + drush/Listeners tests scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpstan.neon index d9fac99680..44dec7f6a8 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpstan.neon @@ -10,10 +10,10 @@ + - docroot/themes/custom + - docroot/sites/default/settings.php + - docroot/sites/default/includes + - drush/Listeners - tests - scripts - -@@ -39,12 +39,12 @@ +@@ -40,12 +40,12 @@ # parameters or return values in iterable type arrays. message: '#.*no value type specified in iterable type array#' paths: @@ -29,7 +29,7 @@ reportUnmatched: false - # Since tests and data providers do not have to have parameter docblocks, # it is not possible to specify the type of the parameter, so we ignore -@@ -51,7 +51,7 @@ +@@ -52,7 +52,7 @@ # this error. message: '#.*no value type specified in iterable type array.#' paths: diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpunit.xml index cb4d78f4f3..5b745e7901 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/phpunit.xml @@ -28,7 +28,7 @@ -@@ -101,18 +101,18 @@ +@@ -101,19 +101,19 @@ @@ -40,6 +40,7 @@ + docroot/themes/custom + docroot/sites/default/includes + docroot/sites/default/settings.php + drush/Listeners - web/modules/custom @@ -57,7 +58,7 @@ tests -@@ -128,7 +128,7 @@ +@@ -129,7 +129,7 @@ to DRUPAL_ROOT/sites/simpletest/browser_output. @see https://www.drupal.org/project/drupal/issues/2992069 --> diff --git a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/rector.php b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/rector.php index f5510ea193..bffb15a242 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/hosting_project_name___acquia/rector.php @@ -10,10 +10,10 @@ + __DIR__ . '/docroot/themes/custom', + __DIR__ . '/docroot/sites/default/settings.php', + __DIR__ . '/docroot/sites/default/includes', + __DIR__ . '/drush/Listeners', __DIR__ . '/tests', ]) - ->withSkip([ -@@ -55,12 +55,12 @@ +@@ -56,12 +56,12 @@ PrivatizeLocalGetterToPropertyRector::class, RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ diff --git a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php new file mode 100644 index 0000000000..56b903e52b --- /dev/null +++ b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -0,0 +1,14 @@ +@@ -96,11 +96,11 @@ + $prod = ['prod']; + + yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; +- yield 'sql:sync, production target with a site' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@star_wars.prod')]]; ++ yield 'sql:sync, production target with a site' => ['sql:sync', ['source' => '@self', 'target' => '@the_new_hope.prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@the_new_hope.prod')]]; + yield 'sql:sync, self target resolved to production' => ['sql:sync', ['source' => '@stage', 'target' => '@self'], '@prod', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; + yield 'sql:sync, production source' => ['sql:sync', ['source' => '@prod', 'target' => '@self'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, non-production target' => ['sql:sync', ['source' => '@prod', 'target' => '@stage'], '@self', $prod, NULL, TRUE]; +- yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.preprod'], '@self', $prod, NULL, TRUE]; ++ yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@the_new_hope.preprod'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, environment starting with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@production'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, site named as the production environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod.stage'], '@self', $prod, NULL, TRUE]; + diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpcs.xml index f51a74c464..4ef2ab7850 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpcs.xml @@ -5,8 +5,8 @@ - web/themes/custom web/sites/default/settings.php web/sites/default/includes - tests -@@ -20,10 +19,6 @@ + drush/Listeners +@@ -21,10 +20,6 @@ scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpstan.neon index bd30151506..8284c6986c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpstan.neon @@ -5,8 +5,8 @@ - - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes - - tests -@@ -40,7 +39,6 @@ + - drush/Listeners +@@ -41,7 +40,6 @@ message: '#.*no value type specified in iterable type array#' paths: - web/modules/custom/* @@ -14,7 +14,7 @@ - # Included settings files are not aware about global variables. message: '#Variable .* might not be defined.#' paths: -@@ -52,6 +50,5 @@ +@@ -53,6 +51,5 @@ message: '#.*no value type specified in iterable type array.#' paths: - web/modules/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpunit.xml index c0da686c87..060e8f9422 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_claro/phpunit.xml @@ -26,8 +26,8 @@ - web/themes/custom web/sites/default/includes web/sites/default/settings.php - -@@ -111,8 +106,6 @@ + drush/Listeners +@@ -112,8 +107,6 @@ web/modules/custom web/modules/custom web/modules/custom/**/node_modules diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_claro/rector.php b/.vortex/installer/tests/Fixtures/handler_process/theme_claro/rector.php index 98964a3f4d..cfce8352a9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_claro/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_claro/rector.php @@ -5,8 +5,8 @@ - __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', - __DIR__ . '/tests', -@@ -56,7 +55,6 @@ + __DIR__ . '/drush/Listeners', +@@ -57,7 +56,6 @@ RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ __DIR__ . '/web/modules/custom/*/src/Hook/*', diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpcs.xml index f51a74c464..4ef2ab7850 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpcs.xml @@ -5,8 +5,8 @@ - web/themes/custom web/sites/default/settings.php web/sites/default/includes - tests -@@ -20,10 +19,6 @@ + drush/Listeners +@@ -21,10 +20,6 @@ scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpstan.neon index bd30151506..8284c6986c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpstan.neon @@ -5,8 +5,8 @@ - - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes - - tests -@@ -40,7 +39,6 @@ + - drush/Listeners +@@ -41,7 +40,6 @@ message: '#.*no value type specified in iterable type array#' paths: - web/modules/custom/* @@ -14,7 +14,7 @@ - # Included settings files are not aware about global variables. message: '#Variable .* might not be defined.#' paths: -@@ -52,6 +50,5 @@ +@@ -53,6 +51,5 @@ message: '#.*no value type specified in iterable type array.#' paths: - web/modules/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpunit.xml index c0da686c87..060e8f9422 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/phpunit.xml @@ -26,8 +26,8 @@ - web/themes/custom web/sites/default/includes web/sites/default/settings.php - -@@ -111,8 +106,6 @@ + drush/Listeners +@@ -112,8 +107,6 @@ web/modules/custom web/modules/custom web/modules/custom/**/node_modules diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/rector.php b/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/rector.php index 98964a3f4d..cfce8352a9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_olivero/rector.php @@ -5,8 +5,8 @@ - __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', - __DIR__ . '/tests', -@@ -56,7 +55,6 @@ + __DIR__ . '/drush/Listeners', +@@ -57,7 +56,6 @@ RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ __DIR__ . '/web/modules/custom/*/src/Hook/*', diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpcs.xml index f51a74c464..4ef2ab7850 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpcs.xml @@ -5,8 +5,8 @@ - web/themes/custom web/sites/default/settings.php web/sites/default/includes - tests -@@ -20,10 +19,6 @@ + drush/Listeners +@@ -21,10 +20,6 @@ scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpstan.neon index bd30151506..8284c6986c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpstan.neon @@ -5,8 +5,8 @@ - - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes - - tests -@@ -40,7 +39,6 @@ + - drush/Listeners +@@ -41,7 +40,6 @@ message: '#.*no value type specified in iterable type array#' paths: - web/modules/custom/* @@ -14,7 +14,7 @@ - # Included settings files are not aware about global variables. message: '#Variable .* might not be defined.#' paths: -@@ -52,6 +50,5 @@ +@@ -53,6 +51,5 @@ message: '#.*no value type specified in iterable type array.#' paths: - web/modules/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpunit.xml index c0da686c87..060e8f9422 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_stark/phpunit.xml @@ -26,8 +26,8 @@ - web/themes/custom web/sites/default/includes web/sites/default/settings.php - -@@ -111,8 +106,6 @@ + drush/Listeners +@@ -112,8 +107,6 @@ web/modules/custom web/modules/custom web/modules/custom/**/node_modules diff --git a/.vortex/installer/tests/Fixtures/handler_process/theme_stark/rector.php b/.vortex/installer/tests/Fixtures/handler_process/theme_stark/rector.php index 98964a3f4d..cfce8352a9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/theme_stark/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/theme_stark/rector.php @@ -5,8 +5,8 @@ - __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', - __DIR__ . '/tests', -@@ -56,7 +55,6 @@ + __DIR__ . '/drush/Listeners', +@@ -57,7 +56,6 @@ RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ __DIR__ . '/web/modules/custom/*/src/Hook/*', diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint/tests/phpunit/Drush/ProductionGuardListenerTest.php new file mode 100644 index 0000000000..ee39b0d85b --- /dev/null +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -0,0 +1,8 @@ +@@ -26,7 +26,6 @@ + * + * Tests for the Drush production guard listener. + * +- * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName + */ + #[Group('drush')] + class ProductionGuardListenerTest extends TestCase { diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php new file mode 100644 index 0000000000..ee39b0d85b --- /dev/null +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -0,0 +1,8 @@ +@@ -26,7 +26,6 @@ + * + * Tests for the Drush production guard listener. + * +- * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName + */ + #[Group('drush')] + class ProductionGuardListenerTest extends TestCase { diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/phpcs.xml index 189b662b69..2d42163dd1 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/phpcs.xml @@ -1,4 +1,4 @@ -@@ -59,11 +59,6 @@ +@@ -60,11 +60,6 @@ *.test diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/phpstan.neon index 00c59b0d78..86718736fe 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/phpstan.neon @@ -1,4 +1,4 @@ -@@ -53,5 +53,4 @@ +@@ -54,5 +54,4 @@ paths: - web/modules/custom/*/tests/* - web/themes/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/rector.php index d033956ac6..a3b2b64492 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/rector.php @@ -6,7 +6,7 @@ use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector; use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector; use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector; -@@ -69,8 +68,6 @@ +@@ -70,8 +69,6 @@ // PHP version upgrade sets. Called without an argument, the target version // comes from `composer.json`, so the sets follow the project's PHP version. ->withPhpSets() @@ -15,7 +15,7 @@ // Code quality improvement sets. ->withPreparedSets( codeQuality: TRUE, -@@ -83,9 +80,5 @@ +@@ -84,9 +81,5 @@ // carry the autoload paths and the file extensions Drupal executes PHP // from, so this file declares neither. ->withComposerBased(drupal: TRUE) diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/tests/phpunit/Drush/-ProductionGuardListenerTest.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/phpcs.xml index 189b662b69..2d42163dd1 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/phpcs.xml @@ -1,4 +1,4 @@ -@@ -59,11 +59,6 @@ +@@ -60,11 +60,6 @@ *.test diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/phpstan.neon index 00c59b0d78..86718736fe 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/phpstan.neon @@ -1,4 +1,4 @@ -@@ -53,5 +53,4 @@ +@@ -54,5 +54,4 @@ paths: - web/modules/custom/*/tests/* - web/themes/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/rector.php index d033956ac6..a3b2b64492 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/rector.php @@ -6,7 +6,7 @@ use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector; use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector; use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector; -@@ -69,8 +68,6 @@ +@@ -70,8 +69,6 @@ // PHP version upgrade sets. Called without an argument, the target version // comes from `composer.json`, so the sets follow the project's PHP version. ->withPhpSets() @@ -15,7 +15,7 @@ // Code quality improvement sets. ->withPreparedSets( codeQuality: TRUE, -@@ -83,9 +80,5 @@ +@@ -84,9 +81,5 @@ // carry the autoload paths and the file extensions Drupal executes PHP // from, so this file declares neither. ->withComposerBased(drupal: TRUE) diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpcs.xml index f51a74c464..4ef2ab7850 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpcs.xml @@ -5,8 +5,8 @@ - web/themes/custom web/sites/default/settings.php web/sites/default/includes - tests -@@ -20,10 +19,6 @@ + drush/Listeners +@@ -21,10 +20,6 @@ scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpstan.neon index bd30151506..8284c6986c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpstan.neon @@ -5,8 +5,8 @@ - - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes - - tests -@@ -40,7 +39,6 @@ + - drush/Listeners +@@ -41,7 +40,6 @@ message: '#.*no value type specified in iterable type array#' paths: - web/modules/custom/* @@ -14,7 +14,7 @@ - # Included settings files are not aware about global variables. message: '#Variable .* might not be defined.#' paths: -@@ -52,6 +50,5 @@ +@@ -53,6 +51,5 @@ message: '#.*no value type specified in iterable type array.#' paths: - web/modules/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpunit.xml index c0da686c87..060e8f9422 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/phpunit.xml @@ -26,8 +26,8 @@ - web/themes/custom web/sites/default/includes web/sites/default/settings.php - -@@ -111,8 +106,6 @@ + drush/Listeners +@@ -112,8 +107,6 @@ web/modules/custom web/modules/custom web/modules/custom/**/node_modules diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/rector.php index 98964a3f4d..cfce8352a9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme/rector.php @@ -5,8 +5,8 @@ - __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', - __DIR__ . '/tests', -@@ -56,7 +55,6 @@ + __DIR__ . '/drush/Listeners', +@@ -57,7 +56,6 @@ RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ __DIR__ . '/web/modules/custom/*/src/Hook/*', diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpcs.xml index f51a74c464..4ef2ab7850 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpcs.xml @@ -5,8 +5,8 @@ - web/themes/custom web/sites/default/settings.php web/sites/default/includes - tests -@@ -20,10 +19,6 @@ + drush/Listeners +@@ -21,10 +20,6 @@ scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpstan.neon index bd30151506..8284c6986c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpstan.neon @@ -5,8 +5,8 @@ - - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes - - tests -@@ -40,7 +39,6 @@ + - drush/Listeners +@@ -41,7 +40,6 @@ message: '#.*no value type specified in iterable type array#' paths: - web/modules/custom/* @@ -14,7 +14,7 @@ - # Included settings files are not aware about global variables. message: '#Variable .* might not be defined.#' paths: -@@ -52,6 +50,5 @@ +@@ -53,6 +51,5 @@ message: '#.*no value type specified in iterable type array.#' paths: - web/modules/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpunit.xml index c0da686c87..060e8f9422 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/phpunit.xml @@ -26,8 +26,8 @@ - web/themes/custom web/sites/default/includes web/sites/default/settings.php - -@@ -111,8 +106,6 @@ + drush/Listeners +@@ -112,8 +107,6 @@ web/modules/custom web/modules/custom web/modules/custom/**/node_modules diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/rector.php index 98964a3f4d..cfce8352a9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_fe_lint_no_theme_circleci/rector.php @@ -5,8 +5,8 @@ - __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', - __DIR__ . '/tests', -@@ -56,7 +55,6 @@ + __DIR__ . '/drush/Listeners', +@@ -57,7 +56,6 @@ RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ __DIR__ . '/web/modules/custom/*/src/Hook/*', diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat/phpcs.xml index 189b662b69..2d42163dd1 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat/phpcs.xml @@ -1,4 +1,4 @@ -@@ -59,11 +59,6 @@ +@@ -60,11 +60,6 @@ *.test diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat/rector.php index 5c2268eb0e..2832f7715f 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat/rector.php @@ -1,4 +1,4 @@ -@@ -69,8 +69,6 @@ +@@ -70,8 +70,6 @@ // PHP version upgrade sets. Called without an argument, the target version // comes from `composer.json`, so the sets follow the project's PHP version. ->withPhpSets() diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat_circleci/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat_circleci/phpcs.xml index 189b662b69..2d42163dd1 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat_circleci/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat_circleci/phpcs.xml @@ -1,4 +1,4 @@ -@@ -59,11 +59,6 @@ +@@ -60,11 +60,6 @@ *.test diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat_circleci/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat_circleci/rector.php index 5c2268eb0e..2832f7715f 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat_circleci/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_behat_circleci/rector.php @@ -1,4 +1,4 @@ -@@ -69,8 +69,6 @@ +@@ -70,8 +70,6 @@ // PHP version upgrade sets. Called without an argument, the target version // comes from `composer.json`, so the sets follow the project's PHP version. ->withPhpSets() diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpcs.xml index f51a74c464..4ef2ab7850 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpcs.xml @@ -5,8 +5,8 @@ - web/themes/custom web/sites/default/settings.php web/sites/default/includes - tests -@@ -20,10 +19,6 @@ + drush/Listeners +@@ -21,10 +20,6 @@ scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpstan.neon index bd30151506..8284c6986c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpstan.neon @@ -5,8 +5,8 @@ - - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes - - tests -@@ -40,7 +39,6 @@ + - drush/Listeners +@@ -41,7 +40,6 @@ message: '#.*no value type specified in iterable type array#' paths: - web/modules/custom/* @@ -14,7 +14,7 @@ - # Included settings files are not aware about global variables. message: '#Variable .* might not be defined.#' paths: -@@ -52,6 +50,5 @@ +@@ -53,6 +51,5 @@ message: '#.*no value type specified in iterable type array.#' paths: - web/modules/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpunit.xml index c0da686c87..060e8f9422 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/phpunit.xml @@ -26,8 +26,8 @@ - web/themes/custom web/sites/default/includes web/sites/default/settings.php - -@@ -111,8 +106,6 @@ + drush/Listeners +@@ -112,8 +107,6 @@ web/modules/custom web/modules/custom web/modules/custom/**/node_modules diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/rector.php index 98964a3f4d..cfce8352a9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_eslint_no_theme/rector.php @@ -5,8 +5,8 @@ - __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', - __DIR__ . '/tests', -@@ -56,7 +55,6 @@ + __DIR__ . '/drush/Listeners', +@@ -57,7 +56,6 @@ RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ __DIR__ . '/web/modules/custom/*/src/Hook/*', diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs/tests/phpunit/Drush/ProductionGuardListenerTest.php new file mode 100644 index 0000000000..ee39b0d85b --- /dev/null +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -0,0 +1,8 @@ +@@ -26,7 +26,6 @@ + * + * Tests for the Drush production guard listener. + * +- * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName + */ + #[Group('drush')] + class ProductionGuardListenerTest extends TestCase { diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php new file mode 100644 index 0000000000..ee39b0d85b --- /dev/null +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -0,0 +1,8 @@ +@@ -26,7 +26,6 @@ + * + * Tests for the Drush production guard listener. + * +- * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName + */ + #[Group('drush')] + class ProductionGuardListenerTest extends TestCase { diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/phpstan.neon index 00c59b0d78..86718736fe 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/phpstan.neon @@ -1,4 +1,4 @@ -@@ -53,5 +53,4 @@ +@@ -54,5 +54,4 @@ paths: - web/modules/custom/*/tests/* - web/themes/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/rector.php index 69ec3a7589..74ecd449e9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/rector.php @@ -6,7 +6,7 @@ use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector; use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector; use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector; -@@ -83,9 +82,5 @@ +@@ -84,9 +83,5 @@ // carry the autoload paths and the file extensions Drupal executes PHP // from, so this file declares neither. ->withComposerBased(drupal: TRUE) diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/tests/phpunit/Drush/-ProductionGuardListenerTest.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/phpstan.neon index 00c59b0d78..86718736fe 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/phpstan.neon @@ -1,4 +1,4 @@ -@@ -53,5 +53,4 @@ +@@ -54,5 +54,4 @@ paths: - web/modules/custom/*/tests/* - web/themes/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/rector.php index 69ec3a7589..74ecd449e9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/rector.php @@ -6,7 +6,7 @@ use Rector\Privatization\Rector\ClassMethod\PrivatizeFinalClassMethodRector; use Rector\Privatization\Rector\MethodCall\PrivatizeLocalGetterToPropertyRector; use Rector\Privatization\Rector\Property\PrivatizeFinalClassPropertyRector; -@@ -83,9 +82,5 @@ +@@ -84,9 +83,5 @@ // carry the autoload paths and the file extensions Drupal executes PHP // from, so this file declares neither. ->withComposerBased(drupal: TRUE) diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php new file mode 100644 index 0000000000..e69de29bb2 diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpcs.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpcs.xml index f51a74c464..4ef2ab7850 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpcs.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpcs.xml @@ -5,8 +5,8 @@ - web/themes/custom web/sites/default/settings.php web/sites/default/includes - tests -@@ -20,10 +19,6 @@ + drush/Listeners +@@ -21,10 +20,6 @@ scripts diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpstan.neon b/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpstan.neon index bd30151506..8284c6986c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpstan.neon +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpstan.neon @@ -5,8 +5,8 @@ - - web/themes/custom - web/sites/default/settings.php - web/sites/default/includes - - tests -@@ -40,7 +39,6 @@ + - drush/Listeners +@@ -41,7 +40,6 @@ message: '#.*no value type specified in iterable type array#' paths: - web/modules/custom/* @@ -14,7 +14,7 @@ - # Included settings files are not aware about global variables. message: '#Variable .* might not be defined.#' paths: -@@ -52,6 +50,5 @@ +@@ -53,6 +51,5 @@ message: '#.*no value type specified in iterable type array.#' paths: - web/modules/custom/*/tests/* diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpunit.xml b/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpunit.xml index c0da686c87..060e8f9422 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpunit.xml +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/phpunit.xml @@ -26,8 +26,8 @@ - web/themes/custom web/sites/default/includes web/sites/default/settings.php - -@@ -111,8 +106,6 @@ + drush/Listeners +@@ -112,8 +107,6 @@ web/modules/custom web/modules/custom web/modules/custom/**/node_modules diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/rector.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/rector.php index 98964a3f4d..cfce8352a9 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/rector.php +++ b/.vortex/installer/tests/Fixtures/handler_process/tools_no_stylelint_no_theme/rector.php @@ -5,8 +5,8 @@ - __DIR__ . '/web/themes/custom', __DIR__ . '/web/sites/default/settings.php', __DIR__ . '/web/sites/default/includes', - __DIR__ . '/tests', -@@ -56,7 +55,6 @@ + __DIR__ . '/drush/Listeners', +@@ -57,7 +56,6 @@ RemoveAlwaysTrueIfConditionRector::class, RemoveUnusedPublicMethodParameterRector::class => [ __DIR__ . '/web/modules/custom/*/src/Hook/*', diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_none/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_none/tests/phpunit/Drush/-ProductionGuardListenerTest.php new file mode 100644 index 0000000000..e69de29bb2 From b7e6b32467d8c57e27ffaed9d82fe27df18af879 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 09:45:42 +1100 Subject: [PATCH 04/25] [#3325] Stopped event propagation for a blocked command so 'core:rsync' does not connect to the production target. --- drush/Listeners/ProductionGuardListener.php | 1 + tests/phpunit/Drush/ProductionGuardListenerTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/drush/Listeners/ProductionGuardListener.php b/drush/Listeners/ProductionGuardListener.php index c77ce96a04..9cb8790fc6 100644 --- a/drush/Listeners/ProductionGuardListener.php +++ b/drush/Listeners/ProductionGuardListener.php @@ -77,6 +77,7 @@ public function __invoke(ConsoleCommandEvent $event): void { $this->logger?->error('Refusing to run {command} against the production site alias {alias}. Set {variable}=1 to bypass this guard.', $context); $event->disableCommand(); + $event->stopPropagation(); } /** diff --git a/tests/phpunit/Drush/ProductionGuardListenerTest.php b/tests/phpunit/Drush/ProductionGuardListenerTest.php index 31e83b9918..4b5cbce996 100644 --- a/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -86,6 +86,7 @@ public function testInvoke(string $command, array $arguments, string $self, mixe $listener($event); $this->assertSame($expected_run, $event->commandShouldRun()); + $this->assertSame(!$expected_run, $event->isPropagationStopped()); $this->assertSame($expected_messages, $messages); } From 3e3de46a90423110b7126f9263fb1ecfa3dd406e Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 09:45:52 +1100 Subject: [PATCH 05/25] Updated snapshots. --- .../_baseline/drush/Listeners/ProductionGuardListener.php | 1 + .../tests/phpunit/Drush/ProductionGuardListenerTest.php | 1 + .../names/tests/phpunit/Drush/ProductionGuardListenerTest.php | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php index c77ce96a04..9cb8790fc6 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php @@ -77,6 +77,7 @@ public function __invoke(ConsoleCommandEvent $event): void { $this->logger?->error('Refusing to run {command} against the production site alias {alias}. Set {variable}=1 to bypass this guard.', $context); $event->disableCommand(); + $event->stopPropagation(); } /** diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php index 272eb8019a..c9bfcddcab 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -86,6 +86,7 @@ public function testInvoke(string $command, array $arguments, string $self, mixe $listener($event); $this->assertSame($expected_run, $event->commandShouldRun()); + $this->assertSame(!$expected_run, $event->isPropagationStopped()); $this->assertSame($expected_messages, $messages); } diff --git a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php index 56b903e52b..3e4e1f3b2d 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -1,4 +1,4 @@ -@@ -96,11 +96,11 @@ +@@ -97,11 +97,11 @@ $prod = ['prod']; yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; From a4313777f93d86943cd5d1f245aebe53086b4a83 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 09:52:44 +1100 Subject: [PATCH 06/25] [#3325] Removed the unneeded 'LowerCamelName' PHPCS suppression from 'ProductionGuardListenerTest'. --- tests/phpunit/Drush/ProductionGuardListenerTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/phpunit/Drush/ProductionGuardListenerTest.php b/tests/phpunit/Drush/ProductionGuardListenerTest.php index 4b5cbce996..d0139543f2 100644 --- a/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -25,8 +25,6 @@ * Class ProductionGuardListenerTest. * * Tests for the Drush production guard listener. - * - * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName */ #[Group('drush')] class ProductionGuardListenerTest extends TestCase { From 61341e7fafe587ea3501dba6d0388a76f3a92f52 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 09:53:19 +1100 Subject: [PATCH 07/25] Updated snapshots. --- .../tests/phpunit/Drush/ProductionGuardListenerTest.php | 2 -- .../tests/phpunit/Drush/ProductionGuardListenerTest.php | 2 +- .../tests/phpunit/Drush/ProductionGuardListenerTest.php | 8 -------- .../tests/phpunit/Drush/ProductionGuardListenerTest.php | 8 -------- .../tests/phpunit/Drush/ProductionGuardListenerTest.php | 8 -------- .../tests/phpunit/Drush/ProductionGuardListenerTest.php | 8 -------- 6 files changed, 1 insertion(+), 35 deletions(-) delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint/tests/phpunit/Drush/ProductionGuardListenerTest.php delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs/tests/phpunit/Drush/ProductionGuardListenerTest.php delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php index c9bfcddcab..4093aee2b0 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -25,8 +25,6 @@ * Class ProductionGuardListenerTest. * * Tests for the Drush production guard listener. - * - * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName */ #[Group('drush')] class ProductionGuardListenerTest extends TestCase { diff --git a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php index 3e4e1f3b2d..bc8bdf045c 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -1,4 +1,4 @@ -@@ -97,11 +97,11 @@ +@@ -95,11 +95,11 @@ $prod = ['prod']; yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint/tests/phpunit/Drush/ProductionGuardListenerTest.php deleted file mode 100644 index ee39b0d85b..0000000000 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ /dev/null @@ -1,8 +0,0 @@ -@@ -26,7 +26,6 @@ - * - * Tests for the Drush production guard listener. - * -- * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName - */ - #[Group('drush')] - class ProductionGuardListenerTest extends TestCase { diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php deleted file mode 100644 index ee39b0d85b..0000000000 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_lint_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ /dev/null @@ -1,8 +0,0 @@ -@@ -26,7 +26,6 @@ - * - * Tests for the Drush production guard listener. - * -- * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName - */ - #[Group('drush')] - class ProductionGuardListenerTest extends TestCase { diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs/tests/phpunit/Drush/ProductionGuardListenerTest.php deleted file mode 100644 index ee39b0d85b..0000000000 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ /dev/null @@ -1,8 +0,0 @@ -@@ -26,7 +26,6 @@ - * - * Tests for the Drush production guard listener. - * -- * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName - */ - #[Group('drush')] - class ProductionGuardListenerTest extends TestCase { diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php deleted file mode 100644 index ee39b0d85b..0000000000 --- a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpcs_circleci/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ /dev/null @@ -1,8 +0,0 @@ -@@ -26,7 +26,6 @@ - * - * Tests for the Drush production guard listener. - * -- * phpcs:disable Drupal.NamingConventions.ValidVariableName.LowerCamelName - */ - #[Group('drush')] - class ProductionGuardListenerTest extends TestCase { From b0f995c0e11ddaa85c1fa6b3868a42a88b5ae25d Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 10:08:51 +1100 Subject: [PATCH 08/25] [#3325] Fixed the production aliases config read passing an array default to 'ConfigInterface::get()'. --- drush/Listeners/ProductionGuardListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drush/Listeners/ProductionGuardListener.php b/drush/Listeners/ProductionGuardListener.php index 9cb8790fc6..1fa06b6059 100644 --- a/drush/Listeners/ProductionGuardListener.php +++ b/drush/Listeners/ProductionGuardListener.php @@ -150,7 +150,7 @@ protected function isProductionAlias(string $alias): bool { * The production alias names without the leading '@'. */ protected function productionAliases(): array { - $value = $this->getConfig()->get(self::CONFIG_KEY, []); + $value = $this->getConfig()->get(self::CONFIG_KEY) ?? []; $values = is_array($value) ? $value : [$value]; $aliases = []; From 69525461017a41b289d299bd52c1ac86403151d9 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 10:08:58 +1100 Subject: [PATCH 09/25] Updated baseline. --- .../_baseline/drush/Listeners/ProductionGuardListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php index 9cb8790fc6..1fa06b6059 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php @@ -150,7 +150,7 @@ protected function isProductionAlias(string $alias): bool { * The production alias names without the leading '@'. */ protected function productionAliases(): array { - $value = $this->getConfig()->get(self::CONFIG_KEY, []); + $value = $this->getConfig()->get(self::CONFIG_KEY) ?? []; $values = is_array($value) ? $value : [$value]; $aliases = []; From 76e417d8a268b62929bfdb00d46c5269f23b59b8 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 10:12:10 +1100 Subject: [PATCH 10/25] [#3325] Replaced the production alias 'foreach' loop with 'array_any()'. --- drush/Listeners/ProductionGuardListener.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/drush/Listeners/ProductionGuardListener.php b/drush/Listeners/ProductionGuardListener.php index 1fa06b6059..3082df035a 100644 --- a/drush/Listeners/ProductionGuardListener.php +++ b/drush/Listeners/ProductionGuardListener.php @@ -134,13 +134,7 @@ protected function resolveAlias(string $name): ?string { protected function isProductionAlias(string $alias): bool { $name = ltrim($alias, '@'); - foreach ($this->productionAliases() as $production_alias) { - if ($name === $production_alias || str_ends_with($name, '.' . $production_alias)) { - return TRUE; - } - } - - return FALSE; + return array_any($this->productionAliases(), fn(string $production_alias): bool => $name === $production_alias || str_ends_with($name, '.' . $production_alias)); } /** From 6d98e3b84c6e99caa67aebfa93f67011572fd98d Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 10:12:16 +1100 Subject: [PATCH 11/25] Updated baseline. --- .../_baseline/drush/Listeners/ProductionGuardListener.php | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php index 1fa06b6059..3082df035a 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php @@ -134,13 +134,7 @@ protected function resolveAlias(string $name): ?string { protected function isProductionAlias(string $alias): bool { $name = ltrim($alias, '@'); - foreach ($this->productionAliases() as $production_alias) { - if ($name === $production_alias || str_ends_with($name, '.' . $production_alias)) { - return TRUE; - } - } - - return FALSE; + return array_any($this->productionAliases(), fn(string $production_alias): bool => $name === $production_alias || str_ends_with($name, '.' . $production_alias)); } /** From dbac1b8d7f5957fe11c461d02d78630cedc88e87 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 10:18:29 +1100 Subject: [PATCH 12/25] [#3325] Removed the 'preprod' alias example that the docs spellcheck rejects. --- .vortex/docs/content/development/environment/drush.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.vortex/docs/content/development/environment/drush.mdx b/.vortex/docs/content/development/environment/drush.mdx index ef1f244a19..9c2f560210 100644 --- a/.vortex/docs/content/development/environment/drush.mdx +++ b/.vortex/docs/content/development/environment/drush.mdx @@ -292,8 +292,8 @@ vortex: An entry matches the alias with that name and every alias whose environment has that name. The `prod` entry matches `@prod`, `@self.prod` and -`@lagoon.prod`. It doesn't match `@production` or `@your_site.preprod`. An -entry with a site name, such as `lagoon.main`, matches only that alias. +`@lagoon.prod`. It doesn't match `@production`. An entry with a site name, +such as `lagoon.main`, matches only that alias. On Lagoon, the alias of the production environment is `@lagoon.`, where `` is the value of From 6e0dbe0279decc5c88606d3633b853d35220472f Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 10:37:59 +1100 Subject: [PATCH 13/25] Re-triggered CI to clear a flaky check. From 7138daa37e2e56105f9e57816278011e60d28e2a Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 11:00:28 +1100 Subject: [PATCH 14/25] Addressed code review: matched production aliases against the requested and the resolved alias name, and restored the inherited skip variable in tests. --- drush/Listeners/ProductionGuardListener.php | 39 ++++++++++--------- .../Drush/ProductionGuardListenerTest.php | 38 ++++++++++++++++-- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/drush/Listeners/ProductionGuardListener.php b/drush/Listeners/ProductionGuardListener.php index 3082df035a..7ae9922b99 100644 --- a/drush/Listeners/ProductionGuardListener.php +++ b/drush/Listeners/ProductionGuardListener.php @@ -61,9 +61,9 @@ public function __invoke(ConsoleCommandEvent $event): void { return; } - $alias = $this->resolveTargetAlias($command, $event->getInput()); + $alias = array_find($this->resolveTargetAliases($command, $event->getInput()), $this->isProductionAlias(...)); - if ($alias === NULL || !$this->isProductionAlias($alias)) { + if ($alias === NULL) { return; } @@ -81,45 +81,48 @@ public function __invoke(ConsoleCommandEvent $event): void { } /** - * Resolves the alias that a guarded command writes to. + * Resolves the alias names that a guarded command writes to. * * @param string $command * The command name. * @param \Symfony\Component\Console\Input\InputInterface $input * The command input. * - * @return string|null - * The alias name, or NULL if the command is not guarded or its target is - * not an alias. + * @return string[] + * The alias names, or an empty array if the command is not guarded or its + * target is not an alias. */ - protected function resolveTargetAlias(string $command, InputInterface $input): ?string { + protected function resolveTargetAliases(string $command, InputInterface $input): array { return match ($command) { - 'sql:sync' => $this->resolveAlias((string) $input->getArgument('target')), + 'sql:sync' => $this->resolveAliases((string) $input->getArgument('target')), // The target is '@alias:path' or a plain path. - 'core:rsync' => $this->resolveAlias(explode(':', (string) $input->getArgument('target'), 2)[0]), - 'sql:drop', 'site:install' => $this->siteAliasManager()->getSelf()->name(), - default => NULL, + 'core:rsync' => $this->resolveAliases(explode(':', (string) $input->getArgument('target'), 2)[0]), + 'sql:drop', 'site:install' => [$this->siteAliasManager()->getSelf()->name()], + default => [], }; } /** - * Resolves an alias argument to the name of its alias record. + * Resolves an alias argument to the requested and the resolved alias names. + * + * '@self' resolves to the alias selected on the command line, and a site + * name resolves to its default environment, such as '@prod' to '@prod.live'. * * @param string $name * The argument value. * - * @return string|null - * The alias name, or NULL if the value is not an alias. + * @return string[] + * The requested name, followed by the resolved name if it differs, or an + * empty array if the value is not an alias. */ - protected function resolveAlias(string $name): ?string { + protected function resolveAliases(string $name): array { if (!str_starts_with($name, '@')) { - return NULL; + return []; } - // '@self' resolves to the alias selected on the command line. $record = $this->siteAliasManager()->get($name); - return $record instanceof SiteAlias ? $record->name() : $name; + return $record instanceof SiteAlias && $record->name() !== $name ? [$name, $record->name()] : [$name]; } /** diff --git a/tests/phpunit/Drush/ProductionGuardListenerTest.php b/tests/phpunit/Drush/ProductionGuardListenerTest.php index d0139543f2..948c933313 100644 --- a/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -7,6 +7,7 @@ use Consolidation\Config\Config; use Consolidation\SiteAlias\SiteAlias; use Consolidation\SiteAlias\SiteAliasManager; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -29,11 +30,27 @@ #[Group('drush')] class ProductionGuardListenerTest extends TestCase { + /** + * The skip variable value before the test, or FALSE if it was unset. + */ + protected string|false $skipVariableOriginal; + /** * {@inheritdoc} */ - protected function tearDown(): void { + protected function setUp(): void { + parent::setUp(); + + // An inherited value would bypass the guard in every case. + $this->skipVariableOriginal = getenv(ProductionGuardListener::SKIP_VARIABLE); putenv(ProductionGuardListener::SKIP_VARIABLE); + } + + /** + * {@inheritdoc} + */ + protected function tearDown(): void { + putenv($this->skipVariableOriginal === FALSE ? ProductionGuardListener::SKIP_VARIABLE : ProductionGuardListener::SKIP_VARIABLE . '=' . $this->skipVariableOriginal); parent::tearDown(); } @@ -56,9 +73,12 @@ protected function tearDown(): void { * Whether the command is expected to run. * @param string[] $expected_messages * The expected log messages. + * @param array $resolved + * Alias records the alias manager returns, keyed by the requested name. + * Any other name besides '@self' has no record. */ #[DataProvider('dataProviderInvoke')] - public function testInvoke(string $command, array $arguments, string $self, mixed $production_aliases, ?string $skip, bool $expected_run, array $expected_messages = []): void { + public function testInvoke(string $command, array $arguments, string $self, mixed $production_aliases, ?string $skip, bool $expected_run, array $expected_messages = [], array $resolved = []): void { if ($skip !== NULL) { putenv(ProductionGuardListener::SKIP_VARIABLE . '=' . $skip); } @@ -67,8 +87,14 @@ public function testInvoke(string $command, array $arguments, string $self, mixe $messages = []; $logger = static::createLogger($messages); - $alias_manager = new SiteAliasManager(); - $alias_manager->setSelf(new SiteAlias([], $self)); + $self_alias = new SiteAlias([], $self); + $alias_manager = $this->createStub(SiteAliasManagerInterface::class); + $alias_manager->method('getSelf')->willReturn($self_alias); + $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => match (TRUE) { + $name === '@self' => $self_alias, + isset($resolved[$name]) => new SiteAlias([], $resolved[$name]), + default => FALSE, + }); $listener = new ProductionGuardListener(); $listener->setConfig(new Config($config)); @@ -102,9 +128,13 @@ public static function dataProviderInvoke(): \Iterator { yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@your_site.preprod'], '@self', $prod, NULL, TRUE]; yield 'sql:sync, environment starting with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@production'], '@self', $prod, NULL, TRUE]; yield 'sql:sync, site named as the production environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod.stage'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')], ['@prod' => '@prod.live']]; + yield 'sql:sync, target resolved to a production environment' => ['sql:sync', ['source' => '@self', 'target' => '@live'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@your_site.prod')], ['@live' => '@your_site.prod']]; + yield 'sql:sync, non-production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@stage'], '@self', $prod, NULL, TRUE, [], ['@stage' => '@stage.live']]; yield 'core:rsync, production target' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; yield 'core:rsync, production target without a path' => ['core:rsync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; + yield 'core:rsync, production target resolved to a default environment' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')], ['@prod' => '@prod.live']]; yield 'core:rsync, production source' => ['core:rsync', ['source' => '@prod:%files', 'target' => '@self:%files'], '@self', $prod, NULL, TRUE]; yield 'core:rsync, path target' => ['core:rsync', ['source' => '@self:%files', 'target' => '/var/backups/files'], '@prod', $prod, NULL, TRUE]; yield 'core:rsync, remote path target' => ['core:rsync', ['source' => '@self:%files', 'target' => 'user@prod:/var/www'], '@self', $prod, NULL, TRUE]; From 28975c3a556054f9b2f7a1e9a73c2b002447af17 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 11:00:34 +1100 Subject: [PATCH 15/25] Updated snapshots. --- .../Listeners/ProductionGuardListener.php | 39 ++++++++++--------- .../Drush/ProductionGuardListenerTest.php | 38 ++++++++++++++++-- .../Drush/ProductionGuardListenerTest.php | 7 +++- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php index 3082df035a..7ae9922b99 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php @@ -61,9 +61,9 @@ public function __invoke(ConsoleCommandEvent $event): void { return; } - $alias = $this->resolveTargetAlias($command, $event->getInput()); + $alias = array_find($this->resolveTargetAliases($command, $event->getInput()), $this->isProductionAlias(...)); - if ($alias === NULL || !$this->isProductionAlias($alias)) { + if ($alias === NULL) { return; } @@ -81,45 +81,48 @@ public function __invoke(ConsoleCommandEvent $event): void { } /** - * Resolves the alias that a guarded command writes to. + * Resolves the alias names that a guarded command writes to. * * @param string $command * The command name. * @param \Symfony\Component\Console\Input\InputInterface $input * The command input. * - * @return string|null - * The alias name, or NULL if the command is not guarded or its target is - * not an alias. + * @return string[] + * The alias names, or an empty array if the command is not guarded or its + * target is not an alias. */ - protected function resolveTargetAlias(string $command, InputInterface $input): ?string { + protected function resolveTargetAliases(string $command, InputInterface $input): array { return match ($command) { - 'sql:sync' => $this->resolveAlias((string) $input->getArgument('target')), + 'sql:sync' => $this->resolveAliases((string) $input->getArgument('target')), // The target is '@alias:path' or a plain path. - 'core:rsync' => $this->resolveAlias(explode(':', (string) $input->getArgument('target'), 2)[0]), - 'sql:drop', 'site:install' => $this->siteAliasManager()->getSelf()->name(), - default => NULL, + 'core:rsync' => $this->resolveAliases(explode(':', (string) $input->getArgument('target'), 2)[0]), + 'sql:drop', 'site:install' => [$this->siteAliasManager()->getSelf()->name()], + default => [], }; } /** - * Resolves an alias argument to the name of its alias record. + * Resolves an alias argument to the requested and the resolved alias names. + * + * '@self' resolves to the alias selected on the command line, and a site + * name resolves to its default environment, such as '@prod' to '@prod.live'. * * @param string $name * The argument value. * - * @return string|null - * The alias name, or NULL if the value is not an alias. + * @return string[] + * The requested name, followed by the resolved name if it differs, or an + * empty array if the value is not an alias. */ - protected function resolveAlias(string $name): ?string { + protected function resolveAliases(string $name): array { if (!str_starts_with($name, '@')) { - return NULL; + return []; } - // '@self' resolves to the alias selected on the command line. $record = $this->siteAliasManager()->get($name); - return $record instanceof SiteAlias ? $record->name() : $name; + return $record instanceof SiteAlias && $record->name() !== $name ? [$name, $record->name()] : [$name]; } /** diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php index 4093aee2b0..4f51cf4991 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -7,6 +7,7 @@ use Consolidation\Config\Config; use Consolidation\SiteAlias\SiteAlias; use Consolidation\SiteAlias\SiteAliasManager; +use Consolidation\SiteAlias\SiteAliasManagerInterface; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -29,11 +30,27 @@ #[Group('drush')] class ProductionGuardListenerTest extends TestCase { + /** + * The skip variable value before the test, or FALSE if it was unset. + */ + protected string|false $skipVariableOriginal; + /** * {@inheritdoc} */ - protected function tearDown(): void { + protected function setUp(): void { + parent::setUp(); + + // An inherited value would bypass the guard in every case. + $this->skipVariableOriginal = getenv(ProductionGuardListener::SKIP_VARIABLE); putenv(ProductionGuardListener::SKIP_VARIABLE); + } + + /** + * {@inheritdoc} + */ + protected function tearDown(): void { + putenv($this->skipVariableOriginal === FALSE ? ProductionGuardListener::SKIP_VARIABLE : ProductionGuardListener::SKIP_VARIABLE . '=' . $this->skipVariableOriginal); parent::tearDown(); } @@ -56,9 +73,12 @@ protected function tearDown(): void { * Whether the command is expected to run. * @param string[] $expected_messages * The expected log messages. + * @param array $resolved + * Alias records the alias manager returns, keyed by the requested name. + * Any other name besides '@self' has no record. */ #[DataProvider('dataProviderInvoke')] - public function testInvoke(string $command, array $arguments, string $self, mixed $production_aliases, ?string $skip, bool $expected_run, array $expected_messages = []): void { + public function testInvoke(string $command, array $arguments, string $self, mixed $production_aliases, ?string $skip, bool $expected_run, array $expected_messages = [], array $resolved = []): void { if ($skip !== NULL) { putenv(ProductionGuardListener::SKIP_VARIABLE . '=' . $skip); } @@ -67,8 +87,14 @@ public function testInvoke(string $command, array $arguments, string $self, mixe $messages = []; $logger = static::createLogger($messages); - $alias_manager = new SiteAliasManager(); - $alias_manager->setSelf(new SiteAlias([], $self)); + $self_alias = new SiteAlias([], $self); + $alias_manager = $this->createStub(SiteAliasManagerInterface::class); + $alias_manager->method('getSelf')->willReturn($self_alias); + $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => match (TRUE) { + $name === '@self' => $self_alias, + isset($resolved[$name]) => new SiteAlias([], $resolved[$name]), + default => FALSE, + }); $listener = new ProductionGuardListener(); $listener->setConfig(new Config($config)); @@ -102,9 +128,13 @@ public static function dataProviderInvoke(): \Iterator { yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.preprod'], '@self', $prod, NULL, TRUE]; yield 'sql:sync, environment starting with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@production'], '@self', $prod, NULL, TRUE]; yield 'sql:sync, site named as the production environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod.stage'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')], ['@prod' => '@prod.live']]; + yield 'sql:sync, target resolved to a production environment' => ['sql:sync', ['source' => '@self', 'target' => '@live'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@star_wars.prod')], ['@live' => '@star_wars.prod']]; + yield 'sql:sync, non-production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@stage'], '@self', $prod, NULL, TRUE, [], ['@stage' => '@stage.live']]; yield 'core:rsync, production target' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; yield 'core:rsync, production target without a path' => ['core:rsync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; + yield 'core:rsync, production target resolved to a default environment' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')], ['@prod' => '@prod.live']]; yield 'core:rsync, production source' => ['core:rsync', ['source' => '@prod:%files', 'target' => '@self:%files'], '@self', $prod, NULL, TRUE]; yield 'core:rsync, path target' => ['core:rsync', ['source' => '@self:%files', 'target' => '/var/backups/files'], '@prod', $prod, NULL, TRUE]; yield 'core:rsync, remote path target' => ['core:rsync', ['source' => '@self:%files', 'target' => 'user@prod:/var/www'], '@self', $prod, NULL, TRUE]; diff --git a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php index bc8bdf045c..9270f04549 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -1,4 +1,4 @@ -@@ -95,11 +95,11 @@ +@@ -121,15 +121,15 @@ $prod = ['prod']; yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; @@ -11,4 +11,9 @@ + yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@the_new_hope.preprod'], '@self', $prod, NULL, TRUE]; yield 'sql:sync, environment starting with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@production'], '@self', $prod, NULL, TRUE]; yield 'sql:sync, site named as the production environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod.stage'], '@self', $prod, NULL, TRUE]; + yield 'sql:sync, production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')], ['@prod' => '@prod.live']]; +- yield 'sql:sync, target resolved to a production environment' => ['sql:sync', ['source' => '@self', 'target' => '@live'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@star_wars.prod')], ['@live' => '@star_wars.prod']]; ++ yield 'sql:sync, target resolved to a production environment' => ['sql:sync', ['source' => '@self', 'target' => '@live'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@the_new_hope.prod')], ['@live' => '@the_new_hope.prod']]; + yield 'sql:sync, non-production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@stage'], '@self', $prod, NULL, TRUE, [], ['@stage' => '@stage.live']]; + yield 'core:rsync, production target' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; From 70c1a6e8a9dd7d3ed0a7312b95976a9142fa32a7 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 11:03:30 +1100 Subject: [PATCH 16/25] [#3325] Replaced the 'match' alias stub with a lookup map and declared the skip variable property with a '@var' docblock in 'ProductionGuardListenerTest'. --- tests/phpunit/Drush/ProductionGuardListenerTest.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/phpunit/Drush/ProductionGuardListenerTest.php b/tests/phpunit/Drush/ProductionGuardListenerTest.php index 948c933313..a9c36d607f 100644 --- a/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -32,8 +32,10 @@ class ProductionGuardListenerTest extends TestCase { /** * The skip variable value before the test, or FALSE if it was unset. + * + * @var string|false */ - protected string|false $skipVariableOriginal; + protected $skipVariableOriginal; /** * {@inheritdoc} @@ -90,11 +92,8 @@ public function testInvoke(string $command, array $arguments, string $self, mixe $self_alias = new SiteAlias([], $self); $alias_manager = $this->createStub(SiteAliasManagerInterface::class); $alias_manager->method('getSelf')->willReturn($self_alias); - $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => match (TRUE) { - $name === '@self' => $self_alias, - isset($resolved[$name]) => new SiteAlias([], $resolved[$name]), - default => FALSE, - }); + $records = ['@self' => $self_alias] + array_map(fn(string $resolved_name): SiteAlias => new SiteAlias([], $resolved_name), $resolved); + $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => $records[$name] ?? FALSE); $listener = new ProductionGuardListener(); $listener->setConfig(new Config($config)); From c17f8372e40f5011c5824180b04ffe9bf12b9991 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 11:03:37 +1100 Subject: [PATCH 17/25] Updated snapshots. --- .../phpunit/Drush/ProductionGuardListenerTest.php | 11 +++++------ .../phpunit/Drush/ProductionGuardListenerTest.php | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php index 4f51cf4991..d056398e00 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -32,8 +32,10 @@ class ProductionGuardListenerTest extends TestCase { /** * The skip variable value before the test, or FALSE if it was unset. + * + * @var string|false */ - protected string|false $skipVariableOriginal; + protected $skipVariableOriginal; /** * {@inheritdoc} @@ -90,11 +92,8 @@ public function testInvoke(string $command, array $arguments, string $self, mixe $self_alias = new SiteAlias([], $self); $alias_manager = $this->createStub(SiteAliasManagerInterface::class); $alias_manager->method('getSelf')->willReturn($self_alias); - $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => match (TRUE) { - $name === '@self' => $self_alias, - isset($resolved[$name]) => new SiteAlias([], $resolved[$name]), - default => FALSE, - }); + $records = ['@self' => $self_alias] + array_map(fn(string $resolved_name): SiteAlias => new SiteAlias([], $resolved_name), $resolved); + $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => $records[$name] ?? FALSE); $listener = new ProductionGuardListener(); $listener->setConfig(new Config($config)); diff --git a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php index 9270f04549..d1ebed1223 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -1,4 +1,4 @@ -@@ -121,15 +121,15 @@ +@@ -120,15 +120,15 @@ $prod = ['prod']; yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; From a7af985147fdef77a732c7652d463d49ff57416b Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 11:06:41 +1100 Subject: [PATCH 18/25] [#3325] Stubbed the concrete 'SiteAliasManager' in 'ProductionGuardListenerTest' to match the 'setSiteAliasManager()' contract. --- tests/phpunit/Drush/ProductionGuardListenerTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/phpunit/Drush/ProductionGuardListenerTest.php b/tests/phpunit/Drush/ProductionGuardListenerTest.php index a9c36d607f..959a5ba248 100644 --- a/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -7,7 +7,6 @@ use Consolidation\Config\Config; use Consolidation\SiteAlias\SiteAlias; use Consolidation\SiteAlias\SiteAliasManager; -use Consolidation\SiteAlias\SiteAliasManagerInterface; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -90,7 +89,7 @@ public function testInvoke(string $command, array $arguments, string $self, mixe $logger = static::createLogger($messages); $self_alias = new SiteAlias([], $self); - $alias_manager = $this->createStub(SiteAliasManagerInterface::class); + $alias_manager = $this->createStub(SiteAliasManager::class); $alias_manager->method('getSelf')->willReturn($self_alias); $records = ['@self' => $self_alias] + array_map(fn(string $resolved_name): SiteAlias => new SiteAlias([], $resolved_name), $resolved); $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => $records[$name] ?? FALSE); From 4c528a50c4462426114b49fee887676f3fa4d16c Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 11:06:48 +1100 Subject: [PATCH 19/25] Updated snapshots. --- .../tests/phpunit/Drush/ProductionGuardListenerTest.php | 3 +-- .../names/tests/phpunit/Drush/ProductionGuardListenerTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php index d056398e00..a47c5fb2fc 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -7,7 +7,6 @@ use Consolidation\Config\Config; use Consolidation\SiteAlias\SiteAlias; use Consolidation\SiteAlias\SiteAliasManager; -use Consolidation\SiteAlias\SiteAliasManagerInterface; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -90,7 +89,7 @@ public function testInvoke(string $command, array $arguments, string $self, mixe $logger = static::createLogger($messages); $self_alias = new SiteAlias([], $self); - $alias_manager = $this->createStub(SiteAliasManagerInterface::class); + $alias_manager = $this->createStub(SiteAliasManager::class); $alias_manager->method('getSelf')->willReturn($self_alias); $records = ['@self' => $self_alias] + array_map(fn(string $resolved_name): SiteAlias => new SiteAlias([], $resolved_name), $resolved); $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => $records[$name] ?? FALSE); diff --git a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php index d1ebed1223..c80565c29a 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php @@ -1,4 +1,4 @@ -@@ -120,15 +120,15 @@ +@@ -119,15 +119,15 @@ $prod = ['prod']; yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; From f92b8a261e57b0cac57970277ad7552c516cbaf5 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 13:13:31 +1100 Subject: [PATCH 20/25] [#3325] Moved the production guard logic into 'drevops/vortex-tooling' with a PHP lint and test setup, leaving a 'drush/Listeners' stub in the template. --- .github/workflows/vortex-test-common.yml | 12 ++ .vortex/.ahoy.yml | 20 ++ .vortex/tests/lint.scripts.sh | 4 +- .vortex/tooling/.gitattributes | 4 + .vortex/tooling/.gitignore | 3 + .vortex/tooling/composer.json | 37 +++- .vortex/tooling/phpcs.xml | 25 +++ .vortex/tooling/phpstan.neon | 26 +++ .vortex/tooling/phpunit.xml | 45 +++++ .vortex/tooling/rector.php | 86 +++++++++ .../src/Drush/ProductionGuardListener.php | 171 ++++++++++++++++++ .../Drush/ProductionGuardListenerTest.php | 14 +- drush/Listeners/ProductionGuardListener.php | 151 +--------------- 13 files changed, 440 insertions(+), 158 deletions(-) create mode 100644 .vortex/tooling/phpcs.xml create mode 100644 .vortex/tooling/phpstan.neon create mode 100644 .vortex/tooling/phpunit.xml create mode 100644 .vortex/tooling/rector.php create mode 100644 .vortex/tooling/src/Drush/ProductionGuardListener.php rename {tests/phpunit => .vortex/tooling/tests}/Drush/ProductionGuardListenerTest.php (96%) diff --git a/.github/workflows/vortex-test-common.yml b/.github/workflows/vortex-test-common.yml index 89fcdccf64..ea1ab572fb 100644 --- a/.github/workflows/vortex-test-common.yml +++ b/.github/workflows/vortex-test-common.yml @@ -89,6 +89,18 @@ jobs: ./tests/lint.ci.sh working-directory: .vortex + - name: Install tooling PHP dependencies + run: composer --working-dir=tooling install + working-directory: .vortex + + - name: Lint tooling PHP code + run: composer --working-dir=tooling lint + working-directory: .vortex + + - name: Test tooling PHP code + run: composer --working-dir=tooling test + working-directory: .vortex + # Special case to validate Renovate configuration with the same action # as used in the Renovate workflow to ensure that they both use the same # version. diff --git a/.vortex/.ahoy.yml b/.vortex/.ahoy.yml index f6fc4ea370..57d616a255 100644 --- a/.vortex/.ahoy.yml +++ b/.vortex/.ahoy.yml @@ -12,6 +12,8 @@ commands: yarn --cwd=tests install --frozen-lockfile [ -d ./tooling/node_modules ] && rm -rf ./tooling/node_modules yarn --cwd=tooling install --frozen-lockfile + [ -d ./tooling/vendor ] && rm -rf ./tooling/vendor + composer --working-dir tooling install [ -d ./installer/vendor ] && rm -rf ./installer/vendor composer --working-dir installer install [ -d ./docs/node_modules ] && rm -rf ./docs/node_modules @@ -123,6 +125,24 @@ commands: [ ! -d tooling/node_modules ] && yarn --cwd=tooling install --frozen-lockfile tooling/node_modules/.bin/bats --no-tempdir-cleanup --tap "$@" + lint-tooling: + usage: Lint tooling PHP code. + cmd: | + [ ! -d tooling/vendor ] && composer --working-dir tooling install + composer --working-dir tooling lint + + lint-tooling-fix: + usage: Fix tooling PHP code. + cmd: | + [ ! -d tooling/vendor ] && composer --working-dir tooling install + composer --working-dir tooling lint-fix + + test-tooling: + usage: Run tooling PHPUnit tests. + cmd: | + [ ! -d tooling/vendor ] && composer --working-dir tooling install + composer --working-dir tooling test "$@" + test-common: cmd: ./tests/test.common.sh diff --git a/.vortex/tests/lint.scripts.sh b/.vortex/tests/lint.scripts.sh index 08735d3651..1562b9c617 100755 --- a/.vortex/tests/lint.scripts.sh +++ b/.vortex/tests/lint.scripts.sh @@ -58,10 +58,12 @@ done < <( -print0 # Tooling scripts in '.vortex/tooling/src/' are extensionless executables; - # include them unconditionally. + # include them unconditionally. PHP classes there are linted by the tooling + # Composer 'lint' script. find \ "${ROOT_DIR}"/.vortex/tooling/src \ -type f \ + -not -name "*.php" \ -print0 ) diff --git a/.vortex/tooling/.gitattributes b/.vortex/tooling/.gitattributes index 65b9304e2f..6219ddc29e 100644 --- a/.vortex/tooling/.gitattributes +++ b/.vortex/tooling/.gitattributes @@ -1,4 +1,8 @@ /.gitattributes export-ignore /.gitignore export-ignore +/phpcs.xml export-ignore +/phpstan.neon export-ignore +/phpunit.xml export-ignore /playground export-ignore +/rector.php export-ignore /tests export-ignore diff --git a/.vortex/tooling/.gitignore b/.vortex/tooling/.gitignore index 58875fd783..4a46913e4a 100644 --- a/.vortex/tooling/.gitignore +++ b/.vortex/tooling/.gitignore @@ -1,2 +1,5 @@ +/.logs +/.phpunit.cache +/composer.lock /node_modules /vendor diff --git a/.vortex/tooling/composer.json b/.vortex/tooling/composer.json index 492b6c507a..5fc44b2182 100644 --- a/.vortex/tooling/composer.json +++ b/.vortex/tooling/composer.json @@ -1,6 +1,6 @@ { "name": "drevops/vortex-tooling", - "description": "Shell scripts for Vortex-based Drupal projects (deploy, notify, provision, fetch/export DB, fetch files, etc.).", + "description": "Shell scripts and Drush listeners for Vortex-based Drupal projects (deploy, notify, provision, fetch/export DB, fetch files, production guard, etc.).", "license": "GPL-3.0-or-later", "type": "library", "authors": [ @@ -19,6 +19,25 @@ "require": { "php": ">=8.2" }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "^1.2.1", + "drevops/phpcs-standard": "^1.1.0", + "drupal/coder": "^9.0.1", + "drush/drush": "^13.8.0", + "phpstan/phpstan": "^2.3.2", + "phpunit/phpunit": "^12.5.40", + "rector/rector": "^2.7.0" + }, + "autoload": { + "psr-4": { + "DrevOps\\VortexTooling\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { + "DrevOps\\VortexTooling\\Tests\\": "tests/" + } + }, "bin": [ "src/vortex-check-worktree-changes", "src/vortex-convert-audit-sarif", @@ -43,7 +62,23 @@ "src/vortex-update" ], "config": { + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + }, "process-timeout": 0, "sort-packages": true + }, + "scripts": { + "lint": [ + "phpcs", + "phpstan --memory-limit=2G", + "rector --dry-run" + ], + "lint-fix": [ + "rector", + "phpcbf" + ], + "test": "phpunit --no-coverage", + "test-coverage": "php -d pcov.directory=. vendor/bin/phpunit" } } diff --git a/.vortex/tooling/phpcs.xml b/.vortex/tooling/phpcs.xml new file mode 100644 index 0000000000..58a97efd1c --- /dev/null +++ b/.vortex/tooling/phpcs.xml @@ -0,0 +1,25 @@ + + + Custom PHPCS standard. + + + + + + + + + + + + + + src/Drush + tests/Drush + + + + *.Test\.php + *.TestCase\.php + + diff --git a/.vortex/tooling/phpstan.neon b/.vortex/tooling/phpstan.neon new file mode 100644 index 0000000000..0c790e0062 --- /dev/null +++ b/.vortex/tooling/phpstan.neon @@ -0,0 +1,26 @@ +## +# Configuration file for PHPStan static code checking, see https://phpstan.org . +# + +parameters: + + level: 9 + + paths: + - src/Drush + - tests/Drush + + excludePaths: + - vendor/* + - node_modules/* + + treatPhpDocTypesAsCertain: false + + ignoreErrors: + - + # Since tests and data providers do not have to have parameter docblocks, + # it is not possible to specify the type of the parameter, so we ignore + # this error. + message: '#.*no value type specified in iterable type array.#' + path: tests/* + reportUnmatched: false diff --git a/.vortex/tooling/phpunit.xml b/.vortex/tooling/phpunit.xml new file mode 100644 index 0000000000..9d0ca33b20 --- /dev/null +++ b/.vortex/tooling/phpunit.xml @@ -0,0 +1,45 @@ + + + + + tests/Drush + + + + + src/Drush + + + + + + + + + + + + + diff --git a/.vortex/tooling/rector.php b/.vortex/tooling/rector.php new file mode 100644 index 0000000000..dca11bd601 --- /dev/null +++ b/.vortex/tooling/rector.php @@ -0,0 +1,86 @@ +withPaths([ + __DIR__ . '/src/Drush', + __DIR__ . '/tests/Drush', + ]) + ->withSkip([ + // Specific rules to skip based on project coding standards. + CatchExceptionNameMatchingTypeRector::class, + ChangeSwitchToMatchRector::class, + CompleteDynamicPropertiesRector::class, + CountArrayToEmptyArrayComparisonRector::class, + DisallowedEmptyRuleFixerRector::class, + InlineArrayReturnAssignRector::class, + NewlineAfterStatementRector::class, + NewlineBeforeNewAssignSetRector::class, + PrivatizeFinalClassMethodRector::class, + PrivatizeFinalClassPropertyRector::class, + PrivatizeLocalGetterToPropertyRector::class, + RemoveAlwaysTrueIfConditionRector::class, + RemoveDeadZeroAndOneOperationRector::class, + RenameParamToMatchTypeRector::class, + RenameVariableToMatchMethodCallReturnTypeRector::class, + RenameVariableToMatchNewTypeRector::class, + SimplifyEmptyCheckOnEmptyArrayRector::class, + StringClassNameToClassConstantRector::class, + // Directories to skip. + '*/vendor/*', + '*/node_modules/*', + ]) + // PHP version upgrade sets - modernizes syntax to PHP 8.2. + // Includes all rules from PHP 5.3 through 8.2. + ->withPhpSets(php82: TRUE) + // Code quality improvement sets. + ->withPreparedSets( + codeQuality: TRUE, + codingStyle: TRUE, + deadCode: TRUE, + naming: TRUE, + privatization: TRUE, + typeDeclarations: TRUE, + ) + // Additional rules. + ->withRules([ + DeclareStrictTypesRector::class, + ]) + ->withFileExtensions([ + 'php', + 'inc', + ]) + // Import configuration. + ->withImportNames(importNames: FALSE, importDocBlockNames: FALSE); diff --git a/.vortex/tooling/src/Drush/ProductionGuardListener.php b/.vortex/tooling/src/Drush/ProductionGuardListener.php new file mode 100644 index 0000000000..1884013b02 --- /dev/null +++ b/.vortex/tooling/src/Drush/ProductionGuardListener.php @@ -0,0 +1,171 @@ +getCommand()?->getName(); + + if ($command === NULL) { + return; + } + + $aliases = array_values(array_filter($this->resolveTargetAliases($command, $event->getInput()), $this->isProductionAlias(...))); + + if ($aliases === []) { + return; + } + + $alias = $aliases[0]; + + $context = ['command' => $command, 'alias' => $alias, 'variable' => self::SKIP_VARIABLE]; + + if (getenv(self::SKIP_VARIABLE) === '1') { + $this->logger?->warning('Running {command} against the production site alias {alias} because {variable} is set to 1.', $context); + + return; + } + + $this->logger?->error('Refusing to run {command} against the production site alias {alias}. Set {variable}=1 to bypass this guard.', $context); + $event->disableCommand(); + $event->stopPropagation(); + } + + /** + * Resolves the alias names that a guarded command writes to. + * + * @param string $command + * The command name. + * @param \Symfony\Component\Console\Input\InputInterface $input + * The command input. + * + * @return string[] + * The alias names, or an empty array if the command is not guarded or its + * target is not an alias. + */ + protected function resolveTargetAliases(string $command, InputInterface $input): array { + $target = $input->hasArgument('target') ? $input->getArgument('target') : NULL; + $target = is_string($target) ? $target : ''; + + return match ($command) { + 'sql:sync' => $this->resolveAliases($target), + // The target is '@alias:path' or a plain path. + 'core:rsync' => $this->resolveAliases(explode(':', $target, 2)[0]), + 'sql:drop', 'site:install' => [$this->siteAliasManager()->getSelf()->name()], + default => [], + }; + } + + /** + * Resolves an alias argument to the requested and the resolved alias names. + * + * '@self' resolves to the alias selected on the command line, and a site + * name resolves to its default environment, such as '@prod' to '@prod.live'. + * + * @param string $name + * The argument value. + * + * @return string[] + * The requested name, followed by the resolved name if it differs, or an + * empty array if the value is not an alias. + */ + protected function resolveAliases(string $name): array { + if (!str_starts_with($name, '@')) { + return []; + } + + $record = $this->siteAliasManager()->get($name); + + return $record instanceof SiteAlias && $record->name() !== $name ? [$name, $record->name()] : [$name]; + } + + /** + * Checks whether an alias matches a configured production alias. + * + * @param string $alias + * The alias name. + * + * @return bool + * TRUE if the alias is a production alias, FALSE otherwise. + */ + protected function isProductionAlias(string $alias): bool { + $name = ltrim($alias, '@'); + + return array_filter($this->productionAliases(), fn(string $production_alias): bool => $name === $production_alias || str_ends_with($name, '.' . $production_alias)) !== []; + } + + /** + * Gets the configured production aliases. + * + * @return string[] + * The production alias names without the leading '@'. + */ + protected function productionAliases(): array { + $value = $this->getConfig()->get(self::CONFIG_KEY) ?? []; + $values = is_array($value) ? $value : [$value]; + + $aliases = []; + + foreach ($values as $item) { + $alias = is_string($item) ? ltrim(trim($item), '@') : ''; + + if ($alias !== '') { + $aliases[] = $alias; + } + } + + return $aliases; + } + +} diff --git a/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/tooling/tests/Drush/ProductionGuardListenerTest.php similarity index 96% rename from tests/phpunit/Drush/ProductionGuardListenerTest.php rename to .vortex/tooling/tests/Drush/ProductionGuardListenerTest.php index 959a5ba248..bf593ed495 100644 --- a/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ b/.vortex/tooling/tests/Drush/ProductionGuardListenerTest.php @@ -2,11 +2,13 @@ declare(strict_types=1); -namespace Drush\Listeners; +namespace DrevOps\VortexTooling\Tests\Drush; use Consolidation\Config\Config; use Consolidation\SiteAlias\SiteAlias; use Consolidation\SiteAlias\SiteAliasManager; +use DrevOps\VortexTooling\Drush\ProductionGuardListener; +use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -18,14 +20,10 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Output\NullOutput; -// Drush loads site-wide listeners itself, so they are not in the autoloader. -require_once __DIR__ . '/../../../drush/Listeners/ProductionGuardListener.php'; - /** - * Class ProductionGuardListenerTest. - * * Tests for the Drush production guard listener. */ +#[CoversClass(ProductionGuardListener::class)] #[Group('drush')] class ProductionGuardListenerTest extends TestCase { @@ -227,10 +225,10 @@ public function log($level, string|\Stringable $message, array $context = []): v $replacements = []; foreach ($context as $key => $value) { - $replacements['{' . $key . '}'] = (string) $value; + $replacements['{' . $key . '}'] = is_scalar($value) ? (string) $value : ''; } - $this->messages[] = $level . ': ' . strtr((string) $message, $replacements); + $this->messages[] = (is_string($level) ? $level : '') . ': ' . strtr((string) $message, $replacements); } }; diff --git a/drush/Listeners/ProductionGuardListener.php b/drush/Listeners/ProductionGuardListener.php index 7ae9922b99..9e4bc78023 100644 --- a/drush/Listeners/ProductionGuardListener.php +++ b/drush/Listeners/ProductionGuardListener.php @@ -4,163 +4,18 @@ namespace Drush\Listeners; -use Consolidation\Config\ConfigAwareTrait; -use Consolidation\SiteAlias\SiteAlias; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; -use Psr\Log\LoggerAwareInterface; -use Psr\Log\LoggerAwareTrait; -use Robo\Contract\ConfigAwareInterface; -use Symfony\Component\Console\Event\ConsoleCommandEvent; -use Symfony\Component\Console\Input\InputInterface; +use DrevOps\VortexTooling\Drush\ProductionGuardListener as VortexProductionGuardListener; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; /** * Refuses destructive Drush commands that target a production site alias. * - * The production aliases are read from the 'vortex.production_aliases' Drush - * configuration. An entry matches the alias with that name and any alias - * whose environment has that name: 'prod' matches '@prod' and '@site.prod'. - * - * Setting VORTEX_DRUSH_PRODUCTION_GUARD_SKIP to 1 bypasses the guard. - * - * Services are set through the Drush '*AwareInterface' inflection. A listener - * with a static 'create()' is registered only after a full Drupal bootstrap, - * and a command against a remote alias never bootstraps Drupal locally. + * Override a method of the parent class to change the guard for this project. * * The priority runs the guard before the 'core:rsync' command event hook, * which connects to the target to evaluate its path. */ #[AsEventListener(priority: 100)] -class ProductionGuardListener implements ConfigAwareInterface, LoggerAwareInterface, SiteAliasManagerAwareInterface { - - use ConfigAwareTrait; - use LoggerAwareTrait; - use SiteAliasManagerAwareTrait; - - /** - * Drush configuration key with the production aliases. - */ - const CONFIG_KEY = 'vortex.production_aliases'; - - /** - * Environment variable that bypasses the guard when set to 1. - */ - const SKIP_VARIABLE = 'VORTEX_DRUSH_PRODUCTION_GUARD_SKIP'; - - /** - * Disables a guarded command that targets a production alias. - * - * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event - * The event fired before the command runs. - */ - public function __invoke(ConsoleCommandEvent $event): void { - $command = $event->getCommand()?->getName(); - - if ($command === NULL) { - return; - } - - $alias = array_find($this->resolveTargetAliases($command, $event->getInput()), $this->isProductionAlias(...)); - - if ($alias === NULL) { - return; - } - - $context = ['command' => $command, 'alias' => $alias, 'variable' => self::SKIP_VARIABLE]; - - if (getenv(self::SKIP_VARIABLE) === '1') { - $this->logger?->warning('Running {command} against the production site alias {alias} because {variable} is set to 1.', $context); - - return; - } - - $this->logger?->error('Refusing to run {command} against the production site alias {alias}. Set {variable}=1 to bypass this guard.', $context); - $event->disableCommand(); - $event->stopPropagation(); - } - - /** - * Resolves the alias names that a guarded command writes to. - * - * @param string $command - * The command name. - * @param \Symfony\Component\Console\Input\InputInterface $input - * The command input. - * - * @return string[] - * The alias names, or an empty array if the command is not guarded or its - * target is not an alias. - */ - protected function resolveTargetAliases(string $command, InputInterface $input): array { - return match ($command) { - 'sql:sync' => $this->resolveAliases((string) $input->getArgument('target')), - // The target is '@alias:path' or a plain path. - 'core:rsync' => $this->resolveAliases(explode(':', (string) $input->getArgument('target'), 2)[0]), - 'sql:drop', 'site:install' => [$this->siteAliasManager()->getSelf()->name()], - default => [], - }; - } - - /** - * Resolves an alias argument to the requested and the resolved alias names. - * - * '@self' resolves to the alias selected on the command line, and a site - * name resolves to its default environment, such as '@prod' to '@prod.live'. - * - * @param string $name - * The argument value. - * - * @return string[] - * The requested name, followed by the resolved name if it differs, or an - * empty array if the value is not an alias. - */ - protected function resolveAliases(string $name): array { - if (!str_starts_with($name, '@')) { - return []; - } - - $record = $this->siteAliasManager()->get($name); - - return $record instanceof SiteAlias && $record->name() !== $name ? [$name, $record->name()] : [$name]; - } - - /** - * Checks whether an alias matches a configured production alias. - * - * @param string $alias - * The alias name. - * - * @return bool - * TRUE if the alias is a production alias, FALSE otherwise. - */ - protected function isProductionAlias(string $alias): bool { - $name = ltrim($alias, '@'); - - return array_any($this->productionAliases(), fn(string $production_alias): bool => $name === $production_alias || str_ends_with($name, '.' . $production_alias)); - } - - /** - * Gets the configured production aliases. - * - * @return string[] - * The production alias names without the leading '@'. - */ - protected function productionAliases(): array { - $value = $this->getConfig()->get(self::CONFIG_KEY) ?? []; - $values = is_array($value) ? $value : [$value]; - - $aliases = []; - - foreach ($values as $item) { - $alias = is_string($item) ? ltrim(trim($item), '@') : ''; - - if ($alias !== '') { - $aliases[] = $alias; - } - } - - return $aliases; - } +class ProductionGuardListener extends VortexProductionGuardListener { } From 4c3caabf68de316e139afa9963549abf98e2aac5 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 13:13:33 +1100 Subject: [PATCH 21/25] [#3325] Documented overriding the production guard stub and the tooling PHP setup for Drush listeners. --- .vortex/CLAUDE.md | 13 ++++- .../contributing/maintenance/tooling.mdx | 53 +++++++++++++++++-- .../content/development/environment/drush.mdx | 36 ++++++++++++- 3 files changed, 95 insertions(+), 7 deletions(-) diff --git a/.vortex/CLAUDE.md b/.vortex/CLAUDE.md index dec5a0e33b..b4f98fd4fe 100644 --- a/.vortex/CLAUDE.md +++ b/.vortex/CLAUDE.md @@ -29,7 +29,7 @@ vortex/ | `docs/` | Docusaurus, Jest | vortextemplate.com | | `installer/` | Symfony Console, PHPUnit | Template customization | | `tests/` | PHPUnit | Template integration testing | -| `tooling/` | Bash, BATS | 'drevops/vortex-tooling' pkg | +| `tooling/` | Bash, BATS, PHP, PHPUnit | 'drevops/vortex-tooling' pkg | Each subsystem has its own CLAUDE.md - read it when working there: @@ -58,6 +58,15 @@ projects install it and run the shipped scripts from `vendor/bin/vortex-*`. - **Manual** - `tooling/playground/` holds scripts that hit live services (Slack, JIRA, New Relic). Not automated; see `tooling/playground/README.md`. +- **PHP unit** - PHPUnit tests in `tooling/tests/Drush/` cover the PHP + classes in `tooling/src/Drush/`. Run with `ahoy test-tooling`, lint with + `ahoy lint-tooling` from `.vortex/`. + +**Drush listeners**: Drush discovers listeners only in a project's +`drush/Listeners/` directory, never in `vendor/`. A listener's logic lives +in `tooling/src/Drush/` (all methods `protected`, no static `create()`), +and the template's `drush/Listeners/` holds a class that extends it and +declares `#[AsEventListener]`, which PHP does not inherit. **Script pattern**: the structure, the variable block and the five output helpers live in the boilerplate at @@ -115,6 +124,8 @@ ahoy install # Install all dependencies ahoy update-snapshots # Update fixtures (see Snapshots below) ahoy lint-scripts # Lint scripts ahoy test-bats # Run BATS unit tests +ahoy test-tooling # Run tooling PHPUnit tests +ahoy lint-tooling # Lint tooling PHP code ahoy update-docs # Regenerate docs variables from scripts ahoy lint-markdown # Lint markdown files ``` diff --git a/.vortex/docs/content/contributing/maintenance/tooling.mdx b/.vortex/docs/content/contributing/maintenance/tooling.mdx index 2f9a4437b0..da39e61fc0 100644 --- a/.vortex/docs/content/contributing/maintenance/tooling.mdx +++ b/.vortex/docs/content/contributing/maintenance/tooling.mdx @@ -7,7 +7,8 @@ sidebar_position: 4 `.vortex/tooling/` holds the shell scripts that consumer projects run for provisioning, database work, deployment, notifications and helpers such as -`doctor` and `info`. This page is for contributors who change the scripts: +`doctor` and `info`, and the PHP classes behind the project's Drush +listeners. This page is for contributors who change the scripts: it covers the package layout, the script conventions, the test levels and how the package gets published. To use the scripts in a project, see [Architecture > Automation @@ -27,16 +28,20 @@ The following table lists the contents of `.vortex/tooling/`: | Path | Purpose | |------|---------| | `src/` | The shipped scripts; the only directory in the published archive | -| `composer.json` | The package definition, with the user-facing scripts under `bin` and no `version` field | +| `src/Drush/` | PHP classes in the `DrevOps\VortexTooling\Drush` namespace, which Composer loads through PSR-4 | +| `composer.json` | The package definition, with the user-facing scripts under `bin`, the PSR-4 autoload, the PHP development dependencies and no `version` field | | `tests/unit/` | BATS unit tests covering the shipped scripts and a few template scripts outside the package | | `tests/_helper.bash`, `tests/fixtures/` | The shared BATS setup and the fixture files the tests read | +| `tests/Drush/` | PHPUnit tests covering the classes in `src/Drush/` | +| `phpcs.xml`, `phpstan.neon`, `rector.php`, `phpunit.xml` | The PHP lint and test configuration, scoped to `src/Drush/` and `tests/Drush/` | | `package.json`, `yarn.lock` | The `bats-helpers` library that the BATS tests load; `ahoy test-bats` installs it on first run | | `playground/` | Manual scripts that hit live services (email, GitHub, Jira, New Relic, Slack, webhooks and S3); not automated | | `README.md`, `LICENSE` | The package readme and license, which ship with the scripts | | `.gitattributes`, `.gitignore` | The Git settings, which stay out of the published archive | -`.gitattributes` marks only `.gitattributes`, `.gitignore`, `playground/` -and `tests/` as `export-ignore`. The published archive holds the rest: +`.gitattributes` marks `.gitattributes`, `.gitignore`, `playground/`, +`tests/` and the PHP lint and test configuration files as `export-ignore`. +The published archive holds the rest: - `src/` - `composer.json` @@ -110,9 +115,28 @@ Composer creates a `vendor/bin` entry only for the scripts in that list. Leave helper scripts off it: `vortex-notify`, for example, runs each `vortex-notify-*` channel script by path from `src/`. +## Add a Drush listener + +Drush discovers listeners only in a project's `drush/Listeners/` directory, +so a listener class in `vendor/` never runs on its own. Each listener +therefore has 2 parts: + +1. The logic, as a class in `src/Drush/` with its PHPUnit tests in + `tests/Drush/`. Make every method `protected`, so a project can override + any of them. +2. A class in the template's `drush/Listeners/` directory that extends the + tooling class and declares `#[AsEventListener]`. PHP doesn't inherit + attributes, so Drush reads the attribute from this class. + +A listener must not have a static `create()` method. Drush registers such a +listener only after a full Drupal bootstrap, which doesn't happen for a +command against a remote alias. Set services through the Drush +`*AwareInterface` inflection instead, as +`src/Drush/ProductionGuardListener.php` does. + ## Test the tooling -Tests cover the scripts at 3 levels: +Tests cover the tooling at 4 levels: - **Unit** - BATS tests in `tooling/tests/unit/` cover the shipped scripts with external commands mocked. The same suite also covers a few template @@ -149,6 +173,25 @@ Tests cover the scripts at 3 levels: `VORTEX_DEV_TEST_COVERAGE_DIR`, so the script runs BATS through `kcov` and writes the scripts' code coverage to that directory. +- **PHP unit** - PHPUnit tests in `tooling/tests/Drush/` cover the classes + in `src/Drush/`. Lint and test them from `.vortex/`: + + ```shell + cd .vortex + + # Run the PHPUnit tests. + ahoy test-tooling + + # Run PHP_CodeSniffer, PHPStan and Rector. + ahoy lint-tooling + + # Fix the issues that Rector and PHP_CodeSniffer can fix. + ahoy lint-tooling-fix + ``` + + Each command installs the tooling's Composer dependencies on first run. + The `vortex-test-common` job runs the same lint and tests. + - **Integration** - the template's PHPUnit functional tests in `.vortex/tests/` run the scripts end-to-end. See [Template > Test the template](template.mdx#test-the-template). diff --git a/.vortex/docs/content/development/environment/drush.mdx b/.vortex/docs/content/development/environment/drush.mdx index 9c2f560210..c4cc796e7e 100644 --- a/.vortex/docs/content/development/environment/drush.mdx +++ b/.vortex/docs/content/development/environment/drush.mdx @@ -65,7 +65,8 @@ The shipped Drush configuration lives in the `drush/` directory: file also adds `--no-tablespaces` to `sql:dump` and lists the production site aliases. See [Production guard](#production-guard). - [`drush/Listeners/ProductionGuardListener.php`](https://github.com/drevops/vortex/blob/main/drush/Listeners/ProductionGuardListener.php) - blocks destructive commands that target a production site alias. + registers the production guard, whose logic ships in + `drevops/vortex-tooling`. See [Production guard](#production-guard). - [`drush/sites/lagoon.site.yml`](https://github.com/drevops/vortex/blob/main/drush/sites/lagoon.site.yml) defines the site aliases for Lagoon-hosted environments. See [Aliases](#aliases). @@ -334,3 +335,36 @@ warning that names the variable, then runs the command: Set the variable on the command line only. An `export` in your shell turns off the guard for every command that follows. + +### Change the guard + +The guard's logic lives in the +`DrevOps\VortexTooling\Drush\ProductionGuardListener` class of +`drevops/vortex-tooling`, so tooling updates bring fixes to your project. +Drush finds the guard through `drush/Listeners/ProductionGuardListener.php`, +which extends that class and adds nothing else. + +To change a rule for your project, override the matching method in +`drush/Listeners/ProductionGuardListener.php`. For example, to also guard +`sql:create` against the selected site: + +```php +use Symfony\Component\Console\Input\InputInterface; + +#[AsEventListener(priority: 100)] +class ProductionGuardListener extends VortexProductionGuardListener { + + protected function resolveTargetAliases(string $command, InputInterface $input): array { + if ($command === 'sql:create') { + return [$this->siteAliasManager()->getSelf()->name()]; + } + + return parent::resolveTargetAliases($command, $input); + } + +} +``` + +Keep the `#[AsEventListener(priority: 100)]` line. Drush registers the guard +only when the class in `drush/Listeners/` declares it, and the priority runs +the guard before `core:rsync` connects to the target. From 4ba0bcda69691b26aca7c5d82edd4b0bba441125 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 13:13:43 +1100 Subject: [PATCH 22/25] Updated baseline. --- .../Listeners/ProductionGuardListener.php | 151 +---------- .../Drush/ProductionGuardListenerTest.php | 239 ------------------ 2 files changed, 3 insertions(+), 387 deletions(-) delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php index 7ae9922b99..9e4bc78023 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php @@ -4,163 +4,18 @@ namespace Drush\Listeners; -use Consolidation\Config\ConfigAwareTrait; -use Consolidation\SiteAlias\SiteAlias; -use Consolidation\SiteAlias\SiteAliasManagerAwareInterface; -use Consolidation\SiteAlias\SiteAliasManagerAwareTrait; -use Psr\Log\LoggerAwareInterface; -use Psr\Log\LoggerAwareTrait; -use Robo\Contract\ConfigAwareInterface; -use Symfony\Component\Console\Event\ConsoleCommandEvent; -use Symfony\Component\Console\Input\InputInterface; +use DrevOps\VortexTooling\Drush\ProductionGuardListener as VortexProductionGuardListener; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; /** * Refuses destructive Drush commands that target a production site alias. * - * The production aliases are read from the 'vortex.production_aliases' Drush - * configuration. An entry matches the alias with that name and any alias - * whose environment has that name: 'prod' matches '@prod' and '@site.prod'. - * - * Setting VORTEX_DRUSH_PRODUCTION_GUARD_SKIP to 1 bypasses the guard. - * - * Services are set through the Drush '*AwareInterface' inflection. A listener - * with a static 'create()' is registered only after a full Drupal bootstrap, - * and a command against a remote alias never bootstraps Drupal locally. + * Override a method of the parent class to change the guard for this project. * * The priority runs the guard before the 'core:rsync' command event hook, * which connects to the target to evaluate its path. */ #[AsEventListener(priority: 100)] -class ProductionGuardListener implements ConfigAwareInterface, LoggerAwareInterface, SiteAliasManagerAwareInterface { - - use ConfigAwareTrait; - use LoggerAwareTrait; - use SiteAliasManagerAwareTrait; - - /** - * Drush configuration key with the production aliases. - */ - const CONFIG_KEY = 'vortex.production_aliases'; - - /** - * Environment variable that bypasses the guard when set to 1. - */ - const SKIP_VARIABLE = 'VORTEX_DRUSH_PRODUCTION_GUARD_SKIP'; - - /** - * Disables a guarded command that targets a production alias. - * - * @param \Symfony\Component\Console\Event\ConsoleCommandEvent $event - * The event fired before the command runs. - */ - public function __invoke(ConsoleCommandEvent $event): void { - $command = $event->getCommand()?->getName(); - - if ($command === NULL) { - return; - } - - $alias = array_find($this->resolveTargetAliases($command, $event->getInput()), $this->isProductionAlias(...)); - - if ($alias === NULL) { - return; - } - - $context = ['command' => $command, 'alias' => $alias, 'variable' => self::SKIP_VARIABLE]; - - if (getenv(self::SKIP_VARIABLE) === '1') { - $this->logger?->warning('Running {command} against the production site alias {alias} because {variable} is set to 1.', $context); - - return; - } - - $this->logger?->error('Refusing to run {command} against the production site alias {alias}. Set {variable}=1 to bypass this guard.', $context); - $event->disableCommand(); - $event->stopPropagation(); - } - - /** - * Resolves the alias names that a guarded command writes to. - * - * @param string $command - * The command name. - * @param \Symfony\Component\Console\Input\InputInterface $input - * The command input. - * - * @return string[] - * The alias names, or an empty array if the command is not guarded or its - * target is not an alias. - */ - protected function resolveTargetAliases(string $command, InputInterface $input): array { - return match ($command) { - 'sql:sync' => $this->resolveAliases((string) $input->getArgument('target')), - // The target is '@alias:path' or a plain path. - 'core:rsync' => $this->resolveAliases(explode(':', (string) $input->getArgument('target'), 2)[0]), - 'sql:drop', 'site:install' => [$this->siteAliasManager()->getSelf()->name()], - default => [], - }; - } - - /** - * Resolves an alias argument to the requested and the resolved alias names. - * - * '@self' resolves to the alias selected on the command line, and a site - * name resolves to its default environment, such as '@prod' to '@prod.live'. - * - * @param string $name - * The argument value. - * - * @return string[] - * The requested name, followed by the resolved name if it differs, or an - * empty array if the value is not an alias. - */ - protected function resolveAliases(string $name): array { - if (!str_starts_with($name, '@')) { - return []; - } - - $record = $this->siteAliasManager()->get($name); - - return $record instanceof SiteAlias && $record->name() !== $name ? [$name, $record->name()] : [$name]; - } - - /** - * Checks whether an alias matches a configured production alias. - * - * @param string $alias - * The alias name. - * - * @return bool - * TRUE if the alias is a production alias, FALSE otherwise. - */ - protected function isProductionAlias(string $alias): bool { - $name = ltrim($alias, '@'); - - return array_any($this->productionAliases(), fn(string $production_alias): bool => $name === $production_alias || str_ends_with($name, '.' . $production_alias)); - } - - /** - * Gets the configured production aliases. - * - * @return string[] - * The production alias names without the leading '@'. - */ - protected function productionAliases(): array { - $value = $this->getConfig()->get(self::CONFIG_KEY) ?? []; - $values = is_array($value) ? $value : [$value]; - - $aliases = []; - - foreach ($values as $item) { - $alias = is_string($item) ? ltrim(trim($item), '@') : ''; - - if ($alias !== '') { - $aliases[] = $alias; - } - } - - return $aliases; - } +class ProductionGuardListener extends VortexProductionGuardListener { } diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php deleted file mode 100644 index a47c5fb2fc..0000000000 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ /dev/null @@ -1,239 +0,0 @@ -skipVariableOriginal = getenv(ProductionGuardListener::SKIP_VARIABLE); - putenv(ProductionGuardListener::SKIP_VARIABLE); - } - - /** - * {@inheritdoc} - */ - protected function tearDown(): void { - putenv($this->skipVariableOriginal === FALSE ? ProductionGuardListener::SKIP_VARIABLE : ProductionGuardListener::SKIP_VARIABLE . '=' . $this->skipVariableOriginal); - - parent::tearDown(); - } - - /** - * Tests the guard against command targets. - * - * @param string $command - * The command name. - * @param array $arguments - * The command arguments. - * @param string $self - * The name of the '@self' alias. - * @param mixed $production_aliases - * The 'vortex.production_aliases' configuration value, or NULL to leave - * it unset. - * @param string|null $skip - * The value of the skip variable, or NULL to leave it unset. - * @param bool $expected_run - * Whether the command is expected to run. - * @param string[] $expected_messages - * The expected log messages. - * @param array $resolved - * Alias records the alias manager returns, keyed by the requested name. - * Any other name besides '@self' has no record. - */ - #[DataProvider('dataProviderInvoke')] - public function testInvoke(string $command, array $arguments, string $self, mixed $production_aliases, ?string $skip, bool $expected_run, array $expected_messages = [], array $resolved = []): void { - if ($skip !== NULL) { - putenv(ProductionGuardListener::SKIP_VARIABLE . '=' . $skip); - } - - $config = $production_aliases === NULL ? [] : ['vortex' => ['production_aliases' => $production_aliases]]; - $messages = []; - $logger = static::createLogger($messages); - - $self_alias = new SiteAlias([], $self); - $alias_manager = $this->createStub(SiteAliasManager::class); - $alias_manager->method('getSelf')->willReturn($self_alias); - $records = ['@self' => $self_alias] + array_map(fn(string $resolved_name): SiteAlias => new SiteAlias([], $resolved_name), $resolved); - $alias_manager->method('get')->willReturnCallback(fn(string $name): SiteAlias|false => $records[$name] ?? FALSE); - - $listener = new ProductionGuardListener(); - $listener->setConfig(new Config($config)); - $listener->setLogger($logger); - $listener->setSiteAliasManager($alias_manager); - - $command_object = new Command($command); - $command_object->addArgument('source', InputArgument::OPTIONAL); - $command_object->addArgument('target', InputArgument::OPTIONAL); - - $event = new ConsoleCommandEvent($command_object, new ArrayInput($arguments, $command_object->getDefinition()), new NullOutput()); - - $listener($event); - - $this->assertSame($expected_run, $event->commandShouldRun()); - $this->assertSame(!$expected_run, $event->isPropagationStopped()); - $this->assertSame($expected_messages, $messages); - } - - /** - * Data provider for testInvoke(). - */ - public static function dataProviderInvoke(): \Iterator { - $prod = ['prod']; - - yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; - yield 'sql:sync, production target with a site' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@star_wars.prod')]]; - yield 'sql:sync, self target resolved to production' => ['sql:sync', ['source' => '@stage', 'target' => '@self'], '@prod', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; - yield 'sql:sync, production source' => ['sql:sync', ['source' => '@prod', 'target' => '@self'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, non-production target' => ['sql:sync', ['source' => '@prod', 'target' => '@stage'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.preprod'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, environment starting with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@production'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, site named as the production environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod.stage'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')], ['@prod' => '@prod.live']]; - yield 'sql:sync, target resolved to a production environment' => ['sql:sync', ['source' => '@self', 'target' => '@live'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@star_wars.prod')], ['@live' => '@star_wars.prod']]; - yield 'sql:sync, non-production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@stage'], '@self', $prod, NULL, TRUE, [], ['@stage' => '@stage.live']]; - - yield 'core:rsync, production target' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; - yield 'core:rsync, production target without a path' => ['core:rsync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; - yield 'core:rsync, production target resolved to a default environment' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')], ['@prod' => '@prod.live']]; - yield 'core:rsync, production source' => ['core:rsync', ['source' => '@prod:%files', 'target' => '@self:%files'], '@self', $prod, NULL, TRUE]; - yield 'core:rsync, path target' => ['core:rsync', ['source' => '@self:%files', 'target' => '/var/backups/files'], '@prod', $prod, NULL, TRUE]; - yield 'core:rsync, remote path target' => ['core:rsync', ['source' => '@self:%files', 'target' => 'user@prod:/var/www'], '@self', $prod, NULL, TRUE]; - - yield 'sql:drop, production self' => ['sql:drop', [], '@prod', $prod, NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; - yield 'sql:drop, non-production self' => ['sql:drop', [], '@self', $prod, NULL, TRUE]; - yield 'site:install, production self with a site' => ['site:install', [], '@lagoon.prod', $prod, NULL, FALSE, [static::blocked('site:install', '@lagoon.prod')]]; - yield 'site:install, non-production self' => ['site:install', [], '@lagoon.develop', $prod, NULL, TRUE]; - - yield 'unguarded command, production self' => ['cache:rebuild', [], '@prod', $prod, NULL, TRUE]; - yield 'unguarded command, production target' => ['sql:query', ['source' => '@self', 'target' => '@prod'], '@prod', $prod, NULL, TRUE]; - - yield 'skip variable set to 1' => ['sql:drop', [], '@prod', $prod, '1', TRUE, [static::bypassed('sql:drop', '@prod')]]; - yield 'skip variable set to 0' => ['sql:drop', [], '@prod', $prod, '0', FALSE, [static::blocked('sql:drop', '@prod')]]; - yield 'skip variable empty' => ['sql:drop', [], '@prod', $prod, '', FALSE, [static::blocked('sql:drop', '@prod')]]; - yield 'skip variable on a non-production target' => ['sql:drop', [], '@stage', $prod, '1', TRUE]; - - yield 'config, single string' => ['sql:drop', [], '@prod', 'prod', NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; - yield 'config, entry with a leading @' => ['sql:drop', [], '@prod', ['@prod'], NULL, FALSE, [static::blocked('sql:drop', '@prod')]]; - yield 'config, entry with a site' => ['sql:drop', [], '@lagoon.main', ['lagoon.main'], NULL, FALSE, [static::blocked('sql:drop', '@lagoon.main')]]; - yield 'config, entry with a site, other site' => ['sql:drop', [], '@other.main', ['lagoon.main'], NULL, TRUE]; - yield 'config, multiple entries' => ['sql:drop', [], '@lagoon.main', ['prod', 'main'], NULL, FALSE, [static::blocked('sql:drop', '@lagoon.main')]]; - yield 'config, invalid entries' => ['sql:drop', [], '@prod', [' ', '@', 123, NULL], NULL, TRUE]; - yield 'config, empty' => ['sql:drop', [], '@prod', [], NULL, TRUE]; - yield 'config, unset' => ['sql:drop', [], '@prod', NULL, NULL, TRUE]; - } - - /** - * Tests that an event without a command is ignored. - */ - public function testInvokeWithoutCommand(): void { - $messages = []; - $logger = static::createLogger($messages); - - $listener = new ProductionGuardListener(); - $listener->setConfig(new Config(['vortex' => ['production_aliases' => ['prod']]])); - $listener->setLogger($logger); - $listener->setSiteAliasManager(new SiteAliasManager()); - - $event = new ConsoleCommandEvent(NULL, new ArrayInput([]), new NullOutput()); - - $listener($event); - - $this->assertTrue($event->commandShouldRun()); - $this->assertSame([], $messages); - } - - /** - * Builds the message logged for a blocked command. - */ - protected static function blocked(string $command, string $alias): string { - return sprintf('error: Refusing to run %s against the production site alias %s. Set VORTEX_DRUSH_PRODUCTION_GUARD_SKIP=1 to bypass this guard.', $command, $alias); - } - - /** - * Builds the message logged for a bypassed command. - */ - protected static function bypassed(string $command, string $alias): string { - return sprintf('warning: Running %s against the production site alias %s because VORTEX_DRUSH_PRODUCTION_GUARD_SKIP is set to 1.', $command, $alias); - } - - /** - * Creates a logger that records interpolated messages. - * - * @param string[] $messages - * The array that receives the messages, prefixed with their level. - */ - protected static function createLogger(array &$messages): LoggerInterface { - return new class($messages) extends AbstractLogger { - - /** - * Recorded messages, prefixed with their level. - * - * @var string[] - */ - protected array $messages; - - /** - * Constructs the logger. - * - * @param string[] $messages - * The array that receives the messages. - */ - public function __construct(array &$messages) { - $this->messages = &$messages; - } - - /** - * {@inheritdoc} - */ - public function log($level, string|\Stringable $message, array $context = []): void { - $replacements = []; - - foreach ($context as $key => $value) { - $replacements['{' . $key . '}'] = (string) $value; - } - - $this->messages[] = $level . ': ' . strtr((string) $message, $replacements); - } - - }; - } - -} From 6cbd0454bf6c661be8a3721460ca5b734a8e7d17 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 13:15:53 +1100 Subject: [PATCH 23/25] Removed the orphaned installer fixture deltas for the moved 'ProductionGuardListenerTest'. --- .../Drush/ProductionGuardListenerTest.php | 19 ------------------- .../Drush/-ProductionGuardListenerTest.php | 0 .../Drush/-ProductionGuardListenerTest.php | 0 .../Drush/-ProductionGuardListenerTest.php | 0 .../Drush/-ProductionGuardListenerTest.php | 0 .../Drush/-ProductionGuardListenerTest.php | 0 6 files changed, 19 deletions(-) delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/tests/phpunit/Drush/-ProductionGuardListenerTest.php delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/tests/phpunit/Drush/-ProductionGuardListenerTest.php delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php delete mode 100644 .vortex/installer/tests/Fixtures/handler_process/tools_none/tests/phpunit/Drush/-ProductionGuardListenerTest.php diff --git a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php deleted file mode 100644 index c80565c29a..0000000000 --- a/.vortex/installer/tests/Fixtures/handler_process/names/tests/phpunit/Drush/ProductionGuardListenerTest.php +++ /dev/null @@ -1,19 +0,0 @@ -@@ -119,15 +119,15 @@ - $prod = ['prod']; - - yield 'sql:sync, production target' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; -- yield 'sql:sync, production target with a site' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@star_wars.prod')]]; -+ yield 'sql:sync, production target with a site' => ['sql:sync', ['source' => '@self', 'target' => '@the_new_hope.prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@the_new_hope.prod')]]; - yield 'sql:sync, self target resolved to production' => ['sql:sync', ['source' => '@stage', 'target' => '@self'], '@prod', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')]]; - yield 'sql:sync, production source' => ['sql:sync', ['source' => '@prod', 'target' => '@self'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, non-production target' => ['sql:sync', ['source' => '@prod', 'target' => '@stage'], '@self', $prod, NULL, TRUE]; -- yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@star_wars.preprod'], '@self', $prod, NULL, TRUE]; -+ yield 'sql:sync, environment ending with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@the_new_hope.preprod'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, environment starting with the production name' => ['sql:sync', ['source' => '@self', 'target' => '@production'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, site named as the production environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod.stage'], '@self', $prod, NULL, TRUE]; - yield 'sql:sync, production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@prod'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@prod')], ['@prod' => '@prod.live']]; -- yield 'sql:sync, target resolved to a production environment' => ['sql:sync', ['source' => '@self', 'target' => '@live'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@star_wars.prod')], ['@live' => '@star_wars.prod']]; -+ yield 'sql:sync, target resolved to a production environment' => ['sql:sync', ['source' => '@self', 'target' => '@live'], '@self', $prod, NULL, FALSE, [static::blocked('sql:sync', '@the_new_hope.prod')], ['@live' => '@the_new_hope.prod']]; - yield 'sql:sync, non-production target resolved to a default environment' => ['sql:sync', ['source' => '@self', 'target' => '@stage'], '@self', $prod, NULL, TRUE, [], ['@stage' => '@stage.live']]; - - yield 'core:rsync, production target' => ['core:rsync', ['source' => '@self:%files', 'target' => '@prod:%files'], '@self', $prod, NULL, FALSE, [static::blocked('core:rsync', '@prod')]]; diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests/tests/phpunit/Drush/-ProductionGuardListenerTest.php deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_groups_no_be_tests_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit/tests/phpunit/Drush/-ProductionGuardListenerTest.php deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_no_phpunit_circleci/tests/phpunit/Drush/-ProductionGuardListenerTest.php deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/.vortex/installer/tests/Fixtures/handler_process/tools_none/tests/phpunit/Drush/-ProductionGuardListenerTest.php b/.vortex/installer/tests/Fixtures/handler_process/tools_none/tests/phpunit/Drush/-ProductionGuardListenerTest.php deleted file mode 100644 index e69de29bb2..0000000000 From 58f2b3ab71931cf98a8b93ee32eae1655e89cda0 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 13:36:32 +1100 Subject: [PATCH 24/25] [#3325] Skipped declaring the production guard stub when the installed 'drevops/vortex-tooling' lacks its parent class. --- .vortex/docs/content/development/environment/drush.mdx | 4 +++- drush/Listeners/ProductionGuardListener.php | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.vortex/docs/content/development/environment/drush.mdx b/.vortex/docs/content/development/environment/drush.mdx index c4cc796e7e..985e861c05 100644 --- a/.vortex/docs/content/development/environment/drush.mdx +++ b/.vortex/docs/content/development/environment/drush.mdx @@ -342,7 +342,9 @@ The guard's logic lives in the `DrevOps\VortexTooling\Drush\ProductionGuardListener` class of `drevops/vortex-tooling`, so tooling updates bring fixes to your project. Drush finds the guard through `drush/Listeners/ProductionGuardListener.php`, -which extends that class and adds nothing else. +which extends that class and adds nothing else. With a tooling version that +doesn't ship the class, the file declares nothing and the guard stays off +until you update `drevops/vortex-tooling`. To change a rule for your project, override the matching method in `drush/Listeners/ProductionGuardListener.php`. For example, to also guard diff --git a/drush/Listeners/ProductionGuardListener.php b/drush/Listeners/ProductionGuardListener.php index 9e4bc78023..110a9d4330 100644 --- a/drush/Listeners/ProductionGuardListener.php +++ b/drush/Listeners/ProductionGuardListener.php @@ -7,6 +7,12 @@ use DrevOps\VortexTooling\Drush\ProductionGuardListener as VortexProductionGuardListener; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; +// Releases of 'drevops/vortex-tooling' before the guard do not ship the +// parent class; declaring the subclass then would break every Drush command. +if (!class_exists(VortexProductionGuardListener::class)) { + return; +} + /** * Refuses destructive Drush commands that target a production site alias. * From 622dd736ed362d502e022abfbb7507b446e53eb8 Mon Sep 17 00:00:00 2001 From: Alex Skrypnyk Date: Tue, 17 Nov 2026 13:36:38 +1100 Subject: [PATCH 25/25] Updated baseline. --- .../_baseline/drush/Listeners/ProductionGuardListener.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php index 9e4bc78023..110a9d4330 100644 --- a/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php +++ b/.vortex/installer/tests/Fixtures/handler_process/_baseline/drush/Listeners/ProductionGuardListener.php @@ -7,6 +7,12 @@ use DrevOps\VortexTooling\Drush\ProductionGuardListener as VortexProductionGuardListener; use Symfony\Component\EventDispatcher\Attribute\AsEventListener; +// Releases of 'drevops/vortex-tooling' before the guard do not ship the +// parent class; declaring the subclass then would break every Drush command. +if (!class_exists(VortexProductionGuardListener::class)) { + return; +} + /** * Refuses destructive Drush commands that target a production site alias. *